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, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/log2.h>
39 #include <linux/ktime.h>
41 #include "../../include/linux/lnet/lib-lnet.h"
42 #include "../../include/linux/lnet/lib-dlc.h"
44 #define D_LNI D_CONSOLE
46 lnet_t the_lnet; /* THE state of the network */
47 EXPORT_SYMBOL(the_lnet);
49 static char *ip2nets = "";
50 module_param(ip2nets, charp, 0444);
51 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
53 static char *networks = "";
54 module_param(networks, charp, 0444);
55 MODULE_PARM_DESC(networks, "local networks");
57 static char *routes = "";
58 module_param(routes, charp, 0444);
59 MODULE_PARM_DESC(routes, "routes to non-local networks");
61 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
62 module_param(rnet_htable_size, int, 0444);
63 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
65 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
66 lnet_process_id_t __user *ids, int n_ids);
75 lnet_get_networks(void)
80 if (*networks && *ip2nets) {
81 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or 'ip2nets' but not both at once\n");
86 rc = lnet_parse_ip2nets(&nets, ip2nets);
87 return !rc ? nets : NULL;
99 spin_lock_init(&the_lnet.ln_eq_wait_lock);
100 init_waitqueue_head(&the_lnet.ln_eq_waitq);
101 init_waitqueue_head(&the_lnet.ln_rc_waitq);
102 mutex_init(&the_lnet.ln_lnd_mutex);
103 mutex_init(&the_lnet.ln_api_mutex);
107 lnet_create_remote_nets_table(void)
110 struct list_head *hash;
112 LASSERT(!the_lnet.ln_remote_nets_hash);
113 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
114 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
116 CERROR("Failed to create remote nets hash table\n");
120 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
121 INIT_LIST_HEAD(&hash[i]);
122 the_lnet.ln_remote_nets_hash = hash;
127 lnet_destroy_remote_nets_table(void)
131 if (!the_lnet.ln_remote_nets_hash)
134 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
135 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
137 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
138 LNET_REMOTE_NETS_HASH_SIZE *
139 sizeof(the_lnet.ln_remote_nets_hash[0]));
140 the_lnet.ln_remote_nets_hash = NULL;
144 lnet_destroy_locks(void)
146 if (the_lnet.ln_res_lock) {
147 cfs_percpt_lock_free(the_lnet.ln_res_lock);
148 the_lnet.ln_res_lock = NULL;
151 if (the_lnet.ln_net_lock) {
152 cfs_percpt_lock_free(the_lnet.ln_net_lock);
153 the_lnet.ln_net_lock = NULL;
158 lnet_create_locks(void)
162 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
163 if (!the_lnet.ln_res_lock)
166 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
167 if (!the_lnet.ln_net_lock)
173 lnet_destroy_locks();
177 static void lnet_assert_wire_constants(void)
180 * Wire protocol assertions generated by 'wirecheck'
181 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
182 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
183 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
187 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
188 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
189 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
190 CLASSERT(LNET_MSG_ACK == 0);
191 CLASSERT(LNET_MSG_PUT == 1);
192 CLASSERT(LNET_MSG_GET == 2);
193 CLASSERT(LNET_MSG_REPLY == 3);
194 CLASSERT(LNET_MSG_HELLO == 4);
196 /* Checks for struct ptl_handle_wire_t */
197 CLASSERT((int)sizeof(lnet_handle_wire_t) == 16);
198 CLASSERT((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
199 CLASSERT((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
200 CLASSERT((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
201 CLASSERT((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
203 /* Checks for struct lnet_magicversion_t */
204 CLASSERT((int)sizeof(lnet_magicversion_t) == 8);
205 CLASSERT((int)offsetof(lnet_magicversion_t, magic) == 0);
206 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
207 CLASSERT((int)offsetof(lnet_magicversion_t, version_major) == 4);
208 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
209 CLASSERT((int)offsetof(lnet_magicversion_t, version_minor) == 6);
210 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
212 /* Checks for struct lnet_hdr_t */
213 CLASSERT((int)sizeof(lnet_hdr_t) == 72);
214 CLASSERT((int)offsetof(lnet_hdr_t, dest_nid) == 0);
215 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
216 CLASSERT((int)offsetof(lnet_hdr_t, src_nid) == 8);
217 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
218 CLASSERT((int)offsetof(lnet_hdr_t, dest_pid) == 16);
219 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
220 CLASSERT((int)offsetof(lnet_hdr_t, src_pid) == 20);
221 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
222 CLASSERT((int)offsetof(lnet_hdr_t, type) == 24);
223 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
224 CLASSERT((int)offsetof(lnet_hdr_t, payload_length) == 28);
225 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
226 CLASSERT((int)offsetof(lnet_hdr_t, msg) == 32);
227 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
230 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
231 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
232 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
233 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
234 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
235 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
238 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
239 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
240 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
241 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
242 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
243 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
244 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
245 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
246 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
247 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
250 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
251 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
252 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
253 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
254 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
255 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
256 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
257 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
258 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
259 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
262 CLASSERT((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
263 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
266 CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
267 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
268 CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
269 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
273 lnet_find_lnd_by_type(__u32 type)
276 struct list_head *tmp;
278 /* holding lnd mutex */
279 list_for_each(tmp, &the_lnet.ln_lnds) {
280 lnd = list_entry(tmp, lnd_t, lnd_list);
282 if (lnd->lnd_type == type)
290 lnet_register_lnd(lnd_t *lnd)
292 mutex_lock(&the_lnet.ln_lnd_mutex);
294 LASSERT(the_lnet.ln_init);
295 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
296 LASSERT(!lnet_find_lnd_by_type(lnd->lnd_type));
298 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
299 lnd->lnd_refcount = 0;
301 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
303 mutex_unlock(&the_lnet.ln_lnd_mutex);
305 EXPORT_SYMBOL(lnet_register_lnd);
308 lnet_unregister_lnd(lnd_t *lnd)
310 mutex_lock(&the_lnet.ln_lnd_mutex);
312 LASSERT(the_lnet.ln_init);
313 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
314 LASSERT(!lnd->lnd_refcount);
316 list_del(&lnd->lnd_list);
317 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
319 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;
346 lnet_net_unlock(LNET_LOCK_EX);
348 EXPORT_SYMBOL(lnet_counters_get);
351 lnet_counters_reset(void)
353 lnet_counters_t *counters;
356 lnet_net_lock(LNET_LOCK_EX);
358 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
359 memset(counters, 0, sizeof(lnet_counters_t));
361 lnet_net_unlock(LNET_LOCK_EX);
365 lnet_res_type2str(int type)
370 case LNET_COOKIE_TYPE_MD:
372 case LNET_COOKIE_TYPE_ME:
374 case LNET_COOKIE_TYPE_EQ:
380 lnet_res_container_cleanup(struct lnet_res_container *rec)
384 if (!rec->rec_type) /* not set yet, it's uninitialized */
387 while (!list_empty(&rec->rec_active)) {
388 struct list_head *e = rec->rec_active.next;
391 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
392 lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
394 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
395 lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
397 } else { /* NB: Active MEs should be attached on portals */
405 * Found alive MD/ME/EQ, user really should unlink/free
406 * all of them before finalize LNet, but if someone didn't,
407 * we have to recycle garbage for him
409 CERROR("%d active elements on exit of %s container\n",
410 count, lnet_res_type2str(rec->rec_type));
413 if (rec->rec_lh_hash) {
414 LIBCFS_FREE(rec->rec_lh_hash,
415 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
416 rec->rec_lh_hash = NULL;
419 rec->rec_type = 0; /* mark it as finalized */
423 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
428 LASSERT(!rec->rec_type);
430 rec->rec_type = type;
431 INIT_LIST_HEAD(&rec->rec_active);
432 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
434 /* Arbitrary choice of hash table size */
435 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
436 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
437 if (!rec->rec_lh_hash) {
442 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
443 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
448 CERROR("Failed to setup %s resource container\n",
449 lnet_res_type2str(type));
450 lnet_res_container_cleanup(rec);
455 lnet_res_containers_destroy(struct lnet_res_container **recs)
457 struct lnet_res_container *rec;
460 cfs_percpt_for_each(rec, i, recs)
461 lnet_res_container_cleanup(rec);
463 cfs_percpt_free(recs);
466 static struct lnet_res_container **
467 lnet_res_containers_create(int type)
469 struct lnet_res_container **recs;
470 struct lnet_res_container *rec;
474 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
476 CERROR("Failed to allocate %s resource containers\n",
477 lnet_res_type2str(type));
481 cfs_percpt_for_each(rec, i, recs) {
482 rc = lnet_res_container_setup(rec, i, type);
484 lnet_res_containers_destroy(recs);
493 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
495 /* ALWAYS called with lnet_res_lock held */
496 struct list_head *head;
497 lnet_libhandle_t *lh;
500 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
503 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
504 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
506 list_for_each_entry(lh, head, lh_hash_chain) {
507 if (lh->lh_cookie == cookie)
515 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
517 /* ALWAYS called with lnet_res_lock held */
518 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
521 lh->lh_cookie = rec->rec_lh_cookie;
522 rec->rec_lh_cookie += 1 << ibits;
524 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
526 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
529 static int lnet_unprepare(void);
532 lnet_prepare(lnet_pid_t requested_pid)
534 /* Prepare to bring up the network */
535 struct lnet_res_container **recs;
538 if (requested_pid == LNET_PID_ANY) {
539 /* Don't instantiate LNET just for me */
543 LASSERT(!the_lnet.ln_refcount);
545 the_lnet.ln_routing = 0;
547 LASSERT(!(requested_pid & LNET_PID_USERFLAG));
548 the_lnet.ln_pid = requested_pid;
550 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
551 INIT_LIST_HEAD(&the_lnet.ln_nis);
552 INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
553 INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
554 INIT_LIST_HEAD(&the_lnet.ln_routers);
556 rc = lnet_create_remote_nets_table();
560 * NB the interface cookie in wire handles guards against delayed
561 * replies and ACKs appearing valid after reboot.
563 the_lnet.ln_interface_cookie = ktime_get_ns();
565 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
566 sizeof(lnet_counters_t));
567 if (!the_lnet.ln_counters) {
568 CERROR("Failed to allocate counters for LNet\n");
573 rc = lnet_peer_tables_create();
577 rc = lnet_msg_containers_create();
581 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
582 LNET_COOKIE_TYPE_EQ);
586 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
592 the_lnet.ln_me_containers = recs;
594 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
600 the_lnet.ln_md_containers = recs;
602 rc = lnet_portals_create();
604 CERROR("Failed to create portals for LNet: %d\n", rc);
619 * NB no LNET_LOCK since this is the last reference. All LND instances
620 * have shut down already, so it is safe to unlink and free all
621 * descriptors, even those that appear committed to a network op (eg MD
622 * with non-zero pending count)
624 lnet_fail_nid(LNET_NID_ANY, 0);
626 LASSERT(!the_lnet.ln_refcount);
627 LASSERT(list_empty(&the_lnet.ln_test_peers));
628 LASSERT(list_empty(&the_lnet.ln_nis));
629 LASSERT(list_empty(&the_lnet.ln_nis_cpt));
630 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
632 lnet_portals_destroy();
634 if (the_lnet.ln_md_containers) {
635 lnet_res_containers_destroy(the_lnet.ln_md_containers);
636 the_lnet.ln_md_containers = NULL;
639 if (the_lnet.ln_me_containers) {
640 lnet_res_containers_destroy(the_lnet.ln_me_containers);
641 the_lnet.ln_me_containers = NULL;
644 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
646 lnet_msg_containers_destroy();
647 lnet_peer_tables_destroy();
648 lnet_rtrpools_free(0);
650 if (the_lnet.ln_counters) {
651 cfs_percpt_free(the_lnet.ln_counters);
652 the_lnet.ln_counters = NULL;
654 lnet_destroy_remote_nets_table();
660 lnet_net2ni_locked(__u32 net, int cpt)
662 struct list_head *tmp;
665 LASSERT(cpt != LNET_LOCK_EX);
667 list_for_each(tmp, &the_lnet.ln_nis) {
668 ni = list_entry(tmp, lnet_ni_t, ni_list);
670 if (LNET_NIDNET(ni->ni_nid) == net) {
671 lnet_ni_addref_locked(ni, cpt);
680 lnet_net2ni(__u32 net)
685 ni = lnet_net2ni_locked(net, 0);
690 EXPORT_SYMBOL(lnet_net2ni);
693 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
698 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
703 val = hash_long(key, LNET_CPT_BITS);
704 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
708 return (unsigned int)(key + val + (val >> 1)) % number;
712 lnet_cpt_of_nid_locked(lnet_nid_t nid)
716 /* must called with hold of lnet_net_lock */
717 if (LNET_CPT_NUMBER == 1)
718 return 0; /* the only one */
720 /* take lnet_net_lock(any) would be OK */
721 if (!list_empty(&the_lnet.ln_nis_cpt)) {
722 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
723 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
726 LASSERT(ni->ni_cpts);
727 return ni->ni_cpts[lnet_nid_cpt_hash
728 (nid, ni->ni_ncpts)];
732 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
736 lnet_cpt_of_nid(lnet_nid_t nid)
741 if (LNET_CPT_NUMBER == 1)
742 return 0; /* the only one */
744 if (list_empty(&the_lnet.ln_nis_cpt))
745 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
747 cpt = lnet_net_lock_current();
748 cpt2 = lnet_cpt_of_nid_locked(nid);
749 lnet_net_unlock(cpt);
753 EXPORT_SYMBOL(lnet_cpt_of_nid);
756 lnet_islocalnet(__u32 net)
761 cpt = lnet_net_lock_current();
763 ni = lnet_net2ni_locked(net, cpt);
765 lnet_ni_decref_locked(ni, cpt);
767 lnet_net_unlock(cpt);
773 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
776 struct list_head *tmp;
778 LASSERT(cpt != LNET_LOCK_EX);
780 list_for_each(tmp, &the_lnet.ln_nis) {
781 ni = list_entry(tmp, lnet_ni_t, ni_list);
783 if (ni->ni_nid == nid) {
784 lnet_ni_addref_locked(ni, cpt);
793 lnet_islocalnid(lnet_nid_t nid)
798 cpt = lnet_net_lock_current();
799 ni = lnet_nid2ni_locked(nid, cpt);
801 lnet_ni_decref_locked(ni, cpt);
802 lnet_net_unlock(cpt);
808 lnet_count_acceptor_nis(void)
810 /* Return the # of NIs that need the acceptor. */
812 struct list_head *tmp;
816 cpt = lnet_net_lock_current();
817 list_for_each(tmp, &the_lnet.ln_nis) {
818 ni = list_entry(tmp, lnet_ni_t, ni_list);
820 if (ni->ni_lnd->lnd_accept)
824 lnet_net_unlock(cpt);
829 static lnet_ping_info_t *
830 lnet_ping_info_create(int num_ni)
832 lnet_ping_info_t *ping_info;
835 infosz = offsetof(lnet_ping_info_t, pi_ni[num_ni]);
836 LIBCFS_ALLOC(ping_info, infosz);
838 CERROR("Can't allocate ping info[%d]\n", num_ni);
842 ping_info->pi_nnis = num_ni;
843 ping_info->pi_pid = the_lnet.ln_pid;
844 ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
845 ping_info->pi_features = LNET_PING_FEAT_NI_STATUS;
851 lnet_get_ni_count(void)
858 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list)
867 lnet_ping_info_free(lnet_ping_info_t *pinfo)
870 offsetof(lnet_ping_info_t,
871 pi_ni[pinfo->pi_nnis]));
875 lnet_ping_info_destroy(void)
879 lnet_net_lock(LNET_LOCK_EX);
881 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
883 ni->ni_status = NULL;
887 lnet_ping_info_free(the_lnet.ln_ping_info);
888 the_lnet.ln_ping_info = NULL;
890 lnet_net_unlock(LNET_LOCK_EX);
894 lnet_ping_event_handler(lnet_event_t *event)
896 lnet_ping_info_t *pinfo = event->md.user_ptr;
899 pinfo->pi_features = LNET_PING_FEAT_INVAL;
903 lnet_ping_info_setup(lnet_ping_info_t **ppinfo, lnet_handle_md_t *md_handle,
904 int ni_count, bool set_eq)
906 lnet_process_id_t id = {LNET_NID_ANY, LNET_PID_ANY};
907 lnet_handle_me_t me_handle;
908 lnet_md_t md = { NULL };
912 rc = LNetEQAlloc(0, lnet_ping_event_handler,
913 &the_lnet.ln_ping_target_eq);
915 CERROR("Can't allocate ping EQ: %d\n", rc);
920 *ppinfo = lnet_ping_info_create(ni_count);
926 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
927 LNET_PROTO_PING_MATCHBITS, 0,
928 LNET_UNLINK, LNET_INS_AFTER,
931 CERROR("Can't create ping ME: %d\n", rc);
935 /* initialize md content */
937 md.length = offsetof(lnet_ping_info_t,
938 pi_ni[(*ppinfo)->pi_nnis]);
939 md.threshold = LNET_MD_THRESH_INF;
941 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
942 LNET_MD_MANAGE_REMOTE;
944 md.eq_handle = the_lnet.ln_ping_target_eq;
945 md.user_ptr = *ppinfo;
947 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, md_handle);
949 CERROR("Can't attach ping MD: %d\n", rc);
956 rc2 = LNetMEUnlink(me_handle);
959 lnet_ping_info_free(*ppinfo);
963 LNetEQFree(the_lnet.ln_ping_target_eq);
968 lnet_ping_md_unlink(lnet_ping_info_t *pinfo, lnet_handle_md_t *md_handle)
970 sigset_t blocked = cfs_block_allsigs();
972 LNetMDUnlink(*md_handle);
973 LNetInvalidateHandle(md_handle);
975 /* NB md could be busy; this just starts the unlink */
976 while (pinfo->pi_features != LNET_PING_FEAT_INVAL) {
977 CDEBUG(D_NET, "Still waiting for ping MD to unlink\n");
978 set_current_state(TASK_UNINTERRUPTIBLE);
979 schedule_timeout(cfs_time_seconds(1));
982 cfs_restore_sigs(blocked);
986 lnet_ping_info_install_locked(lnet_ping_info_t *ping_info)
988 lnet_ni_status_t *ns;
992 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
993 LASSERT(i < ping_info->pi_nnis);
995 ns = &ping_info->pi_ni[i];
997 ns->ns_nid = ni->ni_nid;
1000 ns->ns_status = (ni->ni_status) ?
1001 ni->ni_status->ns_status : LNET_NI_STATUS_UP;
1010 lnet_ping_target_update(lnet_ping_info_t *pinfo, lnet_handle_md_t md_handle)
1012 lnet_ping_info_t *old_pinfo = NULL;
1013 lnet_handle_md_t old_md;
1015 /* switch the NIs to point to the new ping info created */
1016 lnet_net_lock(LNET_LOCK_EX);
1018 if (!the_lnet.ln_routing)
1019 pinfo->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1020 lnet_ping_info_install_locked(pinfo);
1022 if (the_lnet.ln_ping_info) {
1023 old_pinfo = the_lnet.ln_ping_info;
1024 old_md = the_lnet.ln_ping_target_md;
1026 the_lnet.ln_ping_target_md = md_handle;
1027 the_lnet.ln_ping_info = pinfo;
1029 lnet_net_unlock(LNET_LOCK_EX);
1032 /* unlink the old ping info */
1033 lnet_ping_md_unlink(old_pinfo, &old_md);
1034 lnet_ping_info_free(old_pinfo);
1039 lnet_ping_target_fini(void)
1043 lnet_ping_md_unlink(the_lnet.ln_ping_info,
1044 &the_lnet.ln_ping_target_md);
1046 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1049 lnet_ping_info_destroy();
1053 lnet_ni_tq_credits(lnet_ni_t *ni)
1057 LASSERT(ni->ni_ncpts >= 1);
1059 if (ni->ni_ncpts == 1)
1060 return ni->ni_maxtxcredits;
1062 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
1063 credits = max(credits, 8 * ni->ni_peertxcredits);
1064 credits = min(credits, ni->ni_maxtxcredits);
1070 lnet_ni_unlink_locked(lnet_ni_t *ni)
1072 if (!list_empty(&ni->ni_cptlist)) {
1073 list_del_init(&ni->ni_cptlist);
1074 lnet_ni_decref_locked(ni, 0);
1077 /* move it to zombie list and nobody can find it anymore */
1078 LASSERT(!list_empty(&ni->ni_list));
1079 list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1080 lnet_ni_decref_locked(ni, 0); /* drop ln_nis' ref */
1084 lnet_clear_zombies_nis_locked(void)
1091 * Now wait for the NI's I just nuked to show up on ln_zombie_nis
1092 * and shut them down in guaranteed thread context
1095 while (!list_empty(&the_lnet.ln_nis_zombie)) {
1099 ni = list_entry(the_lnet.ln_nis_zombie.next,
1100 lnet_ni_t, ni_list);
1101 list_del_init(&ni->ni_list);
1102 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1105 /* still busy, add it back to zombie list */
1106 list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1110 if (!list_empty(&ni->ni_list)) {
1111 lnet_net_unlock(LNET_LOCK_EX);
1113 if ((i & (-i)) == i) {
1114 CDEBUG(D_WARNING, "Waiting for zombie LNI %s\n",
1115 libcfs_nid2str(ni->ni_nid));
1117 set_current_state(TASK_UNINTERRUPTIBLE);
1118 schedule_timeout(cfs_time_seconds(1));
1119 lnet_net_lock(LNET_LOCK_EX);
1123 ni->ni_lnd->lnd_refcount--;
1124 lnet_net_unlock(LNET_LOCK_EX);
1126 islo = ni->ni_lnd->lnd_type == LOLND;
1128 LASSERT(!in_interrupt());
1129 ni->ni_lnd->lnd_shutdown(ni);
1132 * can't deref lnd anymore now; it might have unregistered
1136 CDEBUG(D_LNI, "Removed LNI %s\n",
1137 libcfs_nid2str(ni->ni_nid));
1142 lnet_net_lock(LNET_LOCK_EX);
1147 lnet_shutdown_lndnis(void)
1152 /* NB called holding the global mutex */
1154 /* All quiet on the API front */
1155 LASSERT(!the_lnet.ln_shutdown);
1156 LASSERT(!the_lnet.ln_refcount);
1157 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
1159 lnet_net_lock(LNET_LOCK_EX);
1160 the_lnet.ln_shutdown = 1; /* flag shutdown */
1162 /* Unlink NIs from the global table */
1163 while (!list_empty(&the_lnet.ln_nis)) {
1164 ni = list_entry(the_lnet.ln_nis.next,
1165 lnet_ni_t, ni_list);
1166 lnet_ni_unlink_locked(ni);
1169 /* Drop the cached eqwait NI. */
1170 if (the_lnet.ln_eq_waitni) {
1171 lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
1172 the_lnet.ln_eq_waitni = NULL;
1175 /* Drop the cached loopback NI. */
1176 if (the_lnet.ln_loni) {
1177 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1178 the_lnet.ln_loni = NULL;
1181 lnet_net_unlock(LNET_LOCK_EX);
1184 * Clear lazy portals and drop delayed messages which hold refs
1185 * on their lnet_msg_t::msg_rxpeer
1187 for (i = 0; i < the_lnet.ln_nportals; i++)
1188 LNetClearLazyPortal(i);
1191 * 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);
1244 if (lnd_type == LOLND) {
1249 CERROR("Net %s is not unique\n",
1250 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
1255 mutex_lock(&the_lnet.ln_lnd_mutex);
1256 lnd = lnet_find_lnd_by_type(lnd_type);
1259 mutex_unlock(&the_lnet.ln_lnd_mutex);
1260 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1261 mutex_lock(&the_lnet.ln_lnd_mutex);
1263 lnd = lnet_find_lnd_by_type(lnd_type);
1265 mutex_unlock(&the_lnet.ln_lnd_mutex);
1266 CERROR("Can't load LND %s, module %s, rc=%d\n",
1267 libcfs_lnd2str(lnd_type),
1268 libcfs_lnd2modname(lnd_type), rc);
1274 lnet_net_lock(LNET_LOCK_EX);
1275 lnd->lnd_refcount++;
1276 lnet_net_unlock(LNET_LOCK_EX);
1280 rc = lnd->lnd_startup(ni);
1282 mutex_unlock(&the_lnet.ln_lnd_mutex);
1285 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1286 rc, libcfs_lnd2str(lnd->lnd_type));
1287 lnet_net_lock(LNET_LOCK_EX);
1288 lnd->lnd_refcount--;
1289 lnet_net_unlock(LNET_LOCK_EX);
1294 * If given some LND tunable parameters, parse those now to
1295 * override the values in the NI structure.
1297 if (peer_buf_cr >= 0)
1298 ni->ni_peerrtrcredits = peer_buf_cr;
1299 if (peer_timeout >= 0)
1300 ni->ni_peertimeout = peer_timeout;
1303 * Note: For now, don't allow the user to change
1304 * peertxcredits as this number is used in the
1305 * IB LND to control queue depth.
1306 * if (peer_cr != -1)
1307 * ni->ni_peertxcredits = peer_cr;
1310 ni->ni_maxtxcredits = credits;
1312 LASSERT(ni->ni_peertimeout <= 0 || lnd->lnd_query);
1314 lnet_net_lock(LNET_LOCK_EX);
1315 /* refcount for ln_nis */
1316 lnet_ni_addref_locked(ni, 0);
1317 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1319 lnet_ni_addref_locked(ni, 0);
1320 list_add_tail(&ni->ni_cptlist, &the_lnet.ln_nis_cpt);
1323 lnet_net_unlock(LNET_LOCK_EX);
1325 if (lnd->lnd_type == LOLND) {
1327 LASSERT(!the_lnet.ln_loni);
1328 the_lnet.ln_loni = ni;
1332 if (!ni->ni_peertxcredits || !ni->ni_maxtxcredits) {
1333 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1334 libcfs_lnd2str(lnd->lnd_type),
1335 !ni->ni_peertxcredits ?
1338 * shutdown the NI since if we get here then it must've already
1341 lnet_shutdown_lndni(ni);
1345 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1346 tq->tq_credits_min =
1347 tq->tq_credits_max =
1348 tq->tq_credits = lnet_ni_tq_credits(ni);
1351 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1352 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1353 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1354 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1363 lnet_startup_lndnis(struct list_head *nilist)
1370 while (!list_empty(nilist)) {
1371 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
1372 list_del(&ni->ni_list);
1373 rc = lnet_startup_lndni(ni, -1, -1, -1, -1);
1381 if (the_lnet.ln_eq_waitni && ni_count > 1) {
1382 lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1383 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network\n",
1384 libcfs_lnd2str(lnd_type));
1391 lnet_shutdown_lndnis();
1397 * Initialize LNet library.
1399 * Only userspace program needs to call this function - it's automatically
1400 * called in the kernel at module loading time. Caller has to call lnet_fini()
1401 * after a call to lnet_init(), if and only if the latter returned 0. It must
1402 * be called exactly once.
1404 * \return 0 on success, and -ve on failures.
1411 lnet_assert_wire_constants();
1412 LASSERT(!the_lnet.ln_init);
1414 memset(&the_lnet, 0, sizeof(the_lnet));
1416 /* refer to global cfs_cpt_table for now */
1417 the_lnet.ln_cpt_table = cfs_cpt_table;
1418 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1420 LASSERT(the_lnet.ln_cpt_number > 0);
1421 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1422 /* we are under risk of consuming all lh_cookie */
1423 CERROR("Can't have %d CPTs for LNet (max allowed is %d), please change setting of CPT-table and retry\n",
1424 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1428 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1429 the_lnet.ln_cpt_bits++;
1431 rc = lnet_create_locks();
1433 CERROR("Can't create LNet global locks: %d\n", rc);
1437 the_lnet.ln_refcount = 0;
1438 the_lnet.ln_init = 1;
1439 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1440 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1441 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1442 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1445 * The hash table size is the number of bits it takes to express the set
1446 * ln_num_routes, minus 1 (better to under estimate than over so we
1447 * don't waste memory).
1449 if (rnet_htable_size <= 0)
1450 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1451 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1452 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1453 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1454 order_base_2(rnet_htable_size) - 1);
1457 * All LNDs apart from the LOLND are in separate modules. They
1458 * register themselves when their module loads, and unregister
1459 * themselves when their module is unloaded.
1461 lnet_register_lnd(&the_lolnd);
1466 * Finalize LNet library.
1468 * Only userspace program needs to call this function. It can be called
1471 * \pre lnet_init() called with success.
1472 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1477 LASSERT(the_lnet.ln_init);
1478 LASSERT(!the_lnet.ln_refcount);
1480 while (!list_empty(&the_lnet.ln_lnds))
1481 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1483 lnet_destroy_locks();
1485 the_lnet.ln_init = 0;
1489 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1491 * Userspace program should call this after a successful call to lnet_init().
1492 * Users must call this function at least once before any other functions.
1493 * For each successful call there must be a corresponding call to
1494 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1497 * The PID used by LNet may be different from the one requested.
1500 * \param requested_pid PID requested by the caller.
1502 * \return >= 0 on success, and < 0 error code on failures.
1505 LNetNIInit(lnet_pid_t requested_pid)
1507 int im_a_router = 0;
1510 lnet_ping_info_t *pinfo;
1511 lnet_handle_md_t md_handle;
1512 struct list_head net_head;
1514 INIT_LIST_HEAD(&net_head);
1516 mutex_lock(&the_lnet.ln_api_mutex);
1518 LASSERT(the_lnet.ln_init);
1519 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1521 if (the_lnet.ln_refcount > 0) {
1522 rc = the_lnet.ln_refcount++;
1523 mutex_unlock(&the_lnet.ln_api_mutex);
1527 rc = lnet_prepare(requested_pid);
1529 mutex_unlock(&the_lnet.ln_api_mutex);
1533 /* Add in the loopback network */
1534 if (!lnet_ni_alloc(LNET_MKNET(LOLND, 0), NULL, &net_head)) {
1536 goto err_empty_list;
1540 * If LNet is being initialized via DLC it is possible
1541 * that the user requests not to load module parameters (ones which
1542 * are supported by DLC) on initialization. Therefore, make sure not
1543 * to load networks, routes and forwarding from module parameters
1544 * in this case. On cleanup in case of failure only clean up
1545 * routes if it has been loaded
1547 if (!the_lnet.ln_nis_from_mod_params) {
1548 rc = lnet_parse_networks(&net_head, lnet_get_networks());
1550 goto err_empty_list;
1553 ni_count = lnet_startup_lndnis(&net_head);
1556 goto err_empty_list;
1559 if (!the_lnet.ln_nis_from_mod_params) {
1560 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1562 goto err_shutdown_lndnis;
1564 rc = lnet_check_routes();
1566 goto err_destory_routes;
1568 rc = lnet_rtrpools_alloc(im_a_router);
1570 goto err_destory_routes;
1573 rc = lnet_acceptor_start();
1575 goto err_destory_routes;
1577 the_lnet.ln_refcount = 1;
1578 /* Now I may use my own API functions... */
1580 rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
1582 goto err_acceptor_stop;
1584 lnet_ping_target_update(pinfo, md_handle);
1586 rc = lnet_router_checker_start();
1590 lnet_router_debugfs_init();
1592 mutex_unlock(&the_lnet.ln_api_mutex);
1597 lnet_ping_target_fini();
1599 the_lnet.ln_refcount = 0;
1600 lnet_acceptor_stop();
1602 if (!the_lnet.ln_nis_from_mod_params)
1603 lnet_destroy_routes();
1604 err_shutdown_lndnis:
1605 lnet_shutdown_lndnis();
1609 mutex_unlock(&the_lnet.ln_api_mutex);
1610 while (!list_empty(&net_head)) {
1613 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1614 list_del_init(&ni->ni_list);
1619 EXPORT_SYMBOL(LNetNIInit);
1622 * Stop LNet interfaces, routing, and forwarding.
1624 * Users must call this function once for each successful call to LNetNIInit().
1625 * Once the LNetNIFini() operation has been started, the results of pending
1626 * API operations are undefined.
1628 * \return always 0 for current implementation.
1633 mutex_lock(&the_lnet.ln_api_mutex);
1635 LASSERT(the_lnet.ln_init);
1636 LASSERT(the_lnet.ln_refcount > 0);
1638 if (the_lnet.ln_refcount != 1) {
1639 the_lnet.ln_refcount--;
1641 LASSERT(!the_lnet.ln_niinit_self);
1643 lnet_router_debugfs_fini();
1644 lnet_router_checker_stop();
1645 lnet_ping_target_fini();
1647 /* Teardown fns that use my own API functions BEFORE here */
1648 the_lnet.ln_refcount = 0;
1650 lnet_acceptor_stop();
1651 lnet_destroy_routes();
1652 lnet_shutdown_lndnis();
1656 mutex_unlock(&the_lnet.ln_api_mutex);
1659 EXPORT_SYMBOL(LNetNIFini);
1662 * Grabs the ni data from the ni structure and fills the out
1665 * \param[in] ni network interface structure
1666 * \param[out] cpt_count the number of cpts the ni is on
1667 * \param[out] nid Network Interface ID
1668 * \param[out] peer_timeout NI peer timeout
1669 * \param[out] peer_tx_crdits NI peer transmit credits
1670 * \param[out] peer_rtr_credits NI peer router credits
1671 * \param[out] max_tx_credits NI max transmit credit
1672 * \param[out] net_config Network configuration
1675 lnet_fill_ni_info(struct lnet_ni *ni, __u32 *cpt_count, __u64 *nid,
1676 int *peer_timeout, int *peer_tx_credits,
1677 int *peer_rtr_credits, int *max_tx_credits,
1678 struct lnet_ioctl_net_config *net_config)
1688 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
1689 ARRAY_SIZE(net_config->ni_interfaces));
1691 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
1692 if (!ni->ni_interfaces[i])
1695 strncpy(net_config->ni_interfaces[i],
1696 ni->ni_interfaces[i],
1697 sizeof(net_config->ni_interfaces[i]));
1701 *peer_timeout = ni->ni_peertimeout;
1702 *peer_tx_credits = ni->ni_peertxcredits;
1703 *peer_rtr_credits = ni->ni_peerrtrcredits;
1704 *max_tx_credits = ni->ni_maxtxcredits;
1706 net_config->ni_status = ni->ni_status->ns_status;
1709 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
1711 for (i = 0; i < num_cpts; i++)
1712 net_config->ni_cpts[i] = ni->ni_cpts[i];
1714 *cpt_count = num_cpts;
1719 lnet_get_net_config(int idx, __u32 *cpt_count, __u64 *nid, int *peer_timeout,
1720 int *peer_tx_credits, int *peer_rtr_credits,
1721 int *max_tx_credits,
1722 struct lnet_ioctl_net_config *net_config)
1725 struct list_head *tmp;
1729 cpt = lnet_net_lock_current();
1731 list_for_each(tmp, &the_lnet.ln_nis) {
1735 ni = list_entry(tmp, lnet_ni_t, ni_list);
1737 lnet_fill_ni_info(ni, cpt_count, nid, peer_timeout,
1738 peer_tx_credits, peer_rtr_credits,
1739 max_tx_credits, net_config);
1745 lnet_net_unlock(cpt);
1750 lnet_dyn_add_ni(lnet_pid_t requested_pid, char *nets,
1751 __s32 peer_timeout, __s32 peer_cr, __s32 peer_buf_cr,
1754 lnet_ping_info_t *pinfo;
1755 lnet_handle_md_t md_handle;
1757 struct list_head net_head;
1758 lnet_remotenet_t *rnet;
1761 INIT_LIST_HEAD(&net_head);
1763 /* Create a ni structure for the network string */
1764 rc = lnet_parse_networks(&net_head, nets);
1766 return !rc ? -EINVAL : rc;
1768 mutex_lock(&the_lnet.ln_api_mutex);
1771 rc = -EINVAL; /* only add one interface per call */
1775 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1777 lnet_net_lock(LNET_LOCK_EX);
1778 rnet = lnet_find_net_locked(LNET_NIDNET(ni->ni_nid));
1779 lnet_net_unlock(LNET_LOCK_EX);
1781 * make sure that the net added doesn't invalidate the current
1782 * configuration LNet is keeping
1785 CERROR("Adding net %s will invalidate routing configuration\n",
1791 rc = lnet_ping_info_setup(&pinfo, &md_handle, 1 + lnet_get_ni_count(),
1796 list_del_init(&ni->ni_list);
1798 rc = lnet_startup_lndni(ni, peer_timeout, peer_cr,
1799 peer_buf_cr, credits);
1803 if (ni->ni_lnd->lnd_accept) {
1804 rc = lnet_acceptor_start();
1806 /* shutdown the ni that we just started */
1807 CERROR("Failed to start up acceptor thread\n");
1808 lnet_shutdown_lndni(ni);
1813 lnet_ping_target_update(pinfo, md_handle);
1814 mutex_unlock(&the_lnet.ln_api_mutex);
1819 lnet_ping_md_unlink(pinfo, &md_handle);
1820 lnet_ping_info_free(pinfo);
1822 mutex_unlock(&the_lnet.ln_api_mutex);
1823 while (!list_empty(&net_head)) {
1824 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1825 list_del_init(&ni->ni_list);
1832 lnet_dyn_del_ni(__u32 net)
1835 lnet_ping_info_t *pinfo;
1836 lnet_handle_md_t md_handle;
1839 /* don't allow userspace to shutdown the LOLND */
1840 if (LNET_NETTYP(net) == LOLND)
1843 mutex_lock(&the_lnet.ln_api_mutex);
1844 /* create and link a new ping info, before removing the old one */
1845 rc = lnet_ping_info_setup(&pinfo, &md_handle,
1846 lnet_get_ni_count() - 1, false);
1850 ni = lnet_net2ni(net);
1856 /* decrement the reference counter taken by lnet_net2ni() */
1857 lnet_ni_decref_locked(ni, 0);
1859 lnet_shutdown_lndni(ni);
1861 if (!lnet_count_acceptor_nis())
1862 lnet_acceptor_stop();
1864 lnet_ping_target_update(pinfo, md_handle);
1867 lnet_ping_md_unlink(pinfo, &md_handle);
1868 lnet_ping_info_free(pinfo);
1870 mutex_unlock(&the_lnet.ln_api_mutex);
1876 * LNet ioctl handler.
1880 LNetCtl(unsigned int cmd, void *arg)
1882 struct libcfs_ioctl_data *data = arg;
1883 struct lnet_ioctl_config_data *config;
1884 lnet_process_id_t id = {0};
1887 unsigned long secs_passed;
1889 LASSERT(the_lnet.ln_init);
1892 case IOC_LIBCFS_GET_NI:
1893 rc = LNetGetId(data->ioc_count, &id);
1894 data->ioc_nid = id.nid;
1897 case IOC_LIBCFS_FAIL_NID:
1898 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1900 case IOC_LIBCFS_ADD_ROUTE:
1903 if (config->cfg_hdr.ioc_len < sizeof(*config))
1906 mutex_lock(&the_lnet.ln_api_mutex);
1907 rc = lnet_add_route(config->cfg_net,
1908 config->cfg_config_u.cfg_route.rtr_hop,
1910 config->cfg_config_u.cfg_route.rtr_priority);
1912 rc = lnet_check_routes();
1914 lnet_del_route(config->cfg_net,
1917 mutex_unlock(&the_lnet.ln_api_mutex);
1920 case IOC_LIBCFS_DEL_ROUTE:
1923 if (config->cfg_hdr.ioc_len < sizeof(*config))
1926 mutex_lock(&the_lnet.ln_api_mutex);
1927 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
1928 mutex_unlock(&the_lnet.ln_api_mutex);
1931 case IOC_LIBCFS_GET_ROUTE:
1934 if (config->cfg_hdr.ioc_len < sizeof(*config))
1937 return lnet_get_route(config->cfg_count,
1939 &config->cfg_config_u.cfg_route.rtr_hop,
1941 &config->cfg_config_u.cfg_route.rtr_flags,
1942 &config->cfg_config_u.cfg_route.rtr_priority);
1944 case IOC_LIBCFS_GET_NET: {
1945 struct lnet_ioctl_net_config *net_config;
1946 size_t total = sizeof(*config) + sizeof(*net_config);
1950 if (config->cfg_hdr.ioc_len < total)
1953 net_config = (struct lnet_ioctl_net_config *)
1958 return lnet_get_net_config(config->cfg_count,
1961 &config->cfg_config_u.cfg_net.net_peer_timeout,
1962 &config->cfg_config_u.cfg_net.net_peer_tx_credits,
1963 &config->cfg_config_u.cfg_net.net_peer_rtr_credits,
1964 &config->cfg_config_u.cfg_net.net_max_tx_credits,
1968 case IOC_LIBCFS_GET_LNET_STATS: {
1969 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
1971 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
1974 lnet_counters_get(&lnet_stats->st_cntrs);
1978 case IOC_LIBCFS_CONFIG_RTR:
1981 if (config->cfg_hdr.ioc_len < sizeof(*config))
1984 mutex_lock(&the_lnet.ln_api_mutex);
1985 if (config->cfg_config_u.cfg_buffers.buf_enable) {
1986 rc = lnet_rtrpools_enable();
1987 mutex_unlock(&the_lnet.ln_api_mutex);
1990 lnet_rtrpools_disable();
1991 mutex_unlock(&the_lnet.ln_api_mutex);
1994 case IOC_LIBCFS_ADD_BUF:
1997 if (config->cfg_hdr.ioc_len < sizeof(*config))
2000 mutex_lock(&the_lnet.ln_api_mutex);
2001 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.buf_tiny,
2002 config->cfg_config_u.cfg_buffers.buf_small,
2003 config->cfg_config_u.cfg_buffers.buf_large);
2004 mutex_unlock(&the_lnet.ln_api_mutex);
2007 case IOC_LIBCFS_GET_BUF: {
2008 struct lnet_ioctl_pool_cfg *pool_cfg;
2009 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2013 if (config->cfg_hdr.ioc_len < total)
2016 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2017 return lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2020 case IOC_LIBCFS_GET_PEER_INFO: {
2021 struct lnet_ioctl_peer *peer_info = arg;
2023 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2026 return lnet_get_peer_info(peer_info->pr_count,
2028 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2029 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2030 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2031 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2032 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2033 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2034 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_rtr_credits,
2035 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2038 case IOC_LIBCFS_NOTIFY_ROUTER:
2039 secs_passed = (ktime_get_real_seconds() - data->ioc_u64[0]);
2040 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2041 jiffies - secs_passed * HZ);
2043 case IOC_LIBCFS_LNET_DIST:
2044 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2045 if (rc < 0 && rc != -EHOSTUNREACH)
2048 data->ioc_u32[0] = rc;
2051 case IOC_LIBCFS_TESTPROTOCOMPAT:
2052 lnet_net_lock(LNET_LOCK_EX);
2053 the_lnet.ln_testprotocompat = data->ioc_flags;
2054 lnet_net_unlock(LNET_LOCK_EX);
2057 case IOC_LIBCFS_PING:
2058 id.nid = data->ioc_nid;
2059 id.pid = data->ioc_u32[0];
2060 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
2062 data->ioc_plen1 / sizeof(lnet_process_id_t));
2065 data->ioc_count = rc;
2069 ni = lnet_net2ni(data->ioc_net);
2073 if (!ni->ni_lnd->lnd_ctl)
2076 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2083 EXPORT_SYMBOL(LNetCtl);
2085 void LNetDebugPeer(lnet_process_id_t id)
2087 lnet_debug_peer(id.nid);
2089 EXPORT_SYMBOL(LNetDebugPeer);
2092 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
2093 * all interfaces share a same PID, as requested by LNetNIInit().
2095 * \param index Index of the interface to look up.
2096 * \param id On successful return, this location will hold the
2097 * lnet_process_id_t ID of the interface.
2099 * \retval 0 If an interface exists at \a index.
2100 * \retval -ENOENT If no interface has been found.
2103 LNetGetId(unsigned int index, lnet_process_id_t *id)
2106 struct list_head *tmp;
2110 LASSERT(the_lnet.ln_init);
2112 /* LNetNI initilization failed? */
2113 if (!the_lnet.ln_refcount)
2116 cpt = lnet_net_lock_current();
2118 list_for_each(tmp, &the_lnet.ln_nis) {
2122 ni = list_entry(tmp, lnet_ni_t, ni_list);
2124 id->nid = ni->ni_nid;
2125 id->pid = the_lnet.ln_pid;
2130 lnet_net_unlock(cpt);
2133 EXPORT_SYMBOL(LNetGetId);
2136 * Print a string representation of handle \a h into buffer \a str of
2140 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
2142 snprintf(str, len, "%#llx", h.cookie);
2144 EXPORT_SYMBOL(LNetSnprintHandle);
2146 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
2147 lnet_process_id_t __user *ids, int n_ids)
2149 lnet_handle_eq_t eqh;
2150 lnet_handle_md_t mdh;
2152 lnet_md_t md = { NULL };
2156 const int a_long_time = 60000; /* mS */
2158 lnet_ping_info_t *info;
2159 lnet_process_id_t tmpid;
2166 infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
2169 id.nid == LNET_NID_ANY ||
2170 timeout_ms > 500000 || /* arbitrary limit! */
2171 n_ids > 20) /* arbitrary limit! */
2174 if (id.pid == LNET_PID_ANY)
2175 id.pid = LNET_PID_LUSTRE;
2177 LIBCFS_ALLOC(info, infosz);
2181 /* NB 2 events max (including any unlink event) */
2182 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
2184 CERROR("Can't allocate EQ: %d\n", rc);
2188 /* initialize md content */
2191 md.threshold = 2; /*GET/REPLY*/
2193 md.options = LNET_MD_TRUNCATE;
2197 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
2199 CERROR("Can't bind MD: %d\n", rc);
2203 rc = LNetGet(LNET_NID_ANY, mdh, id,
2204 LNET_RESERVED_PORTAL,
2205 LNET_PROTO_PING_MATCHBITS, 0);
2208 /* Don't CERROR; this could be deliberate! */
2210 rc2 = LNetMDUnlink(mdh);
2213 /* NB must wait for the UNLINK event below... */
2215 timeout_ms = a_long_time;
2219 /* MUST block for unlink to complete */
2221 blocked = cfs_block_allsigs();
2223 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
2226 cfs_restore_sigs(blocked);
2228 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
2229 (rc2 <= 0) ? -1 : event.type,
2230 (rc2 <= 0) ? -1 : event.status,
2231 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
2233 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
2235 if (rc2 <= 0 || event.status) {
2236 /* timeout or error */
2237 if (!replied && !rc)
2238 rc = (rc2 < 0) ? rc2 :
2243 /* Ensure completion in finite time... */
2245 /* No assertion (racing with network) */
2247 timeout_ms = a_long_time;
2249 /* timed out waiting for unlink */
2250 CWARN("ping %s: late network completion\n",
2253 } else if (event.type == LNET_EVENT_REPLY) {
2258 } while (rc2 <= 0 || !event.unlinked);
2262 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2269 LASSERT(nob >= 0 && nob <= infosz);
2271 rc = -EPROTO; /* if I can't parse... */
2274 /* can't check magic/version */
2275 CERROR("%s: ping info too short %d\n",
2276 libcfs_id2str(id), nob);
2280 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2281 lnet_swap_pinginfo(info);
2282 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2283 CERROR("%s: Unexpected magic %08x\n",
2284 libcfs_id2str(id), info->pi_magic);
2288 if (!(info->pi_features & LNET_PING_FEAT_NI_STATUS)) {
2289 CERROR("%s: ping w/o NI status: 0x%x\n",
2290 libcfs_id2str(id), info->pi_features);
2294 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2295 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2296 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2300 if (info->pi_nnis < n_ids)
2301 n_ids = info->pi_nnis;
2303 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2304 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2305 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2309 rc = -EFAULT; /* If I SEGV... */
2311 memset(&tmpid, 0, sizeof(tmpid));
2312 for (i = 0; i < n_ids; i++) {
2313 tmpid.pid = info->pi_pid;
2314 tmpid.nid = info->pi_ni[i].ns_nid;
2315 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2321 rc2 = LNetEQFree(eqh);
2323 CERROR("rc2 %d\n", rc2);
2327 LIBCFS_FREE(info, infosz);