4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LNET
34 #include <linux/log2.h>
35 #include <linux/ktime.h>
37 #include <lnet/lib-lnet.h>
39 #define D_LNI D_CONSOLE
41 lnet_t the_lnet; /* THE state of the network */
42 EXPORT_SYMBOL(the_lnet);
44 static char *ip2nets = "";
45 module_param(ip2nets, charp, 0444);
46 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
48 static char *networks = "";
49 module_param(networks, charp, 0444);
50 MODULE_PARM_DESC(networks, "local networks");
52 static char *routes = "";
53 module_param(routes, charp, 0444);
54 MODULE_PARM_DESC(routes, "routes to non-local networks");
56 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
57 module_param(rnet_htable_size, int, 0444);
58 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
60 static int use_tcp_bonding = false;
61 module_param(use_tcp_bonding, int, 0444);
62 MODULE_PARM_DESC(use_tcp_bonding,
63 "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
66 * This sequence number keeps track of how many times DLC was used to
67 * update the configuration. It is incremented on any DLC update and
68 * checked when sending a message to determine if there is a need to
69 * re-run the selection algorithm to handle configuration change.
70 * Look at lnet_select_pathway() for more details on its usage.
72 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
74 static int lnet_ping(lnet_process_id_t id, signed long timeout,
75 lnet_process_id_t __user *ids, int n_ids);
84 lnet_get_networks(void)
89 if (*networks != 0 && *ip2nets != 0) {
90 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
91 "'ip2nets' but not both at once\n");
96 rc = lnet_parse_ip2nets(&nets, ip2nets);
97 return (rc == 0) ? nets : NULL;
107 lnet_init_locks(void)
109 spin_lock_init(&the_lnet.ln_eq_wait_lock);
110 init_waitqueue_head(&the_lnet.ln_eq_waitq);
111 init_waitqueue_head(&the_lnet.ln_rc_waitq);
112 mutex_init(&the_lnet.ln_lnd_mutex);
113 mutex_init(&the_lnet.ln_api_mutex);
117 lnet_fini_locks(void)
121 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
122 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
126 lnet_descriptor_setup(void)
128 /* create specific kmem_cache for MEs and small MDs (i.e., originally
129 * allocated in <size-xxx> kmem_cache).
131 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(lnet_me_t),
133 if (!lnet_mes_cachep)
136 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
137 LNET_SMALL_MD_SIZE, 0, 0,
139 if (!lnet_small_mds_cachep)
146 lnet_descriptor_cleanup(void)
149 if (lnet_small_mds_cachep) {
150 kmem_cache_destroy(lnet_small_mds_cachep);
151 lnet_small_mds_cachep = NULL;
154 if (lnet_mes_cachep) {
155 kmem_cache_destroy(lnet_mes_cachep);
156 lnet_mes_cachep = NULL;
161 lnet_create_remote_nets_table(void)
164 struct list_head *hash;
166 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
167 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
168 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
170 CERROR("Failed to create remote nets hash table\n");
174 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
175 INIT_LIST_HEAD(&hash[i]);
176 the_lnet.ln_remote_nets_hash = hash;
181 lnet_destroy_remote_nets_table(void)
185 if (the_lnet.ln_remote_nets_hash == NULL)
188 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
189 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
191 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
192 LNET_REMOTE_NETS_HASH_SIZE *
193 sizeof(the_lnet.ln_remote_nets_hash[0]));
194 the_lnet.ln_remote_nets_hash = NULL;
198 lnet_destroy_locks(void)
200 if (the_lnet.ln_res_lock != NULL) {
201 cfs_percpt_lock_free(the_lnet.ln_res_lock);
202 the_lnet.ln_res_lock = NULL;
205 if (the_lnet.ln_net_lock != NULL) {
206 cfs_percpt_lock_free(the_lnet.ln_net_lock);
207 the_lnet.ln_net_lock = NULL;
214 lnet_create_locks(void)
218 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
219 if (the_lnet.ln_res_lock == NULL)
222 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
223 if (the_lnet.ln_net_lock == NULL)
229 lnet_destroy_locks();
233 static void lnet_assert_wire_constants(void)
235 /* Wire protocol assertions generated by 'wirecheck'
236 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
237 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
238 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
241 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
242 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
243 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
244 CLASSERT(LNET_MSG_ACK == 0);
245 CLASSERT(LNET_MSG_PUT == 1);
246 CLASSERT(LNET_MSG_GET == 2);
247 CLASSERT(LNET_MSG_REPLY == 3);
248 CLASSERT(LNET_MSG_HELLO == 4);
250 /* Checks for struct lnet_handle_wire */
251 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
252 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
253 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
254 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
255 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
257 /* Checks for struct lnet_magicversion_t */
258 CLASSERT((int)sizeof(lnet_magicversion_t) == 8);
259 CLASSERT((int)offsetof(lnet_magicversion_t, magic) == 0);
260 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
261 CLASSERT((int)offsetof(lnet_magicversion_t, version_major) == 4);
262 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
263 CLASSERT((int)offsetof(lnet_magicversion_t, version_minor) == 6);
264 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
266 /* Checks for struct lnet_hdr_t */
267 CLASSERT((int)sizeof(lnet_hdr_t) == 72);
268 CLASSERT((int)offsetof(lnet_hdr_t, dest_nid) == 0);
269 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
270 CLASSERT((int)offsetof(lnet_hdr_t, src_nid) == 8);
271 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
272 CLASSERT((int)offsetof(lnet_hdr_t, dest_pid) == 16);
273 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
274 CLASSERT((int)offsetof(lnet_hdr_t, src_pid) == 20);
275 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
276 CLASSERT((int)offsetof(lnet_hdr_t, type) == 24);
277 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
278 CLASSERT((int)offsetof(lnet_hdr_t, payload_length) == 28);
279 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
280 CLASSERT((int)offsetof(lnet_hdr_t, msg) == 32);
281 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
284 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
285 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
286 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
287 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
288 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
289 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
292 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
293 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
294 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
295 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
296 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
297 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
298 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
299 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
300 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
301 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
304 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
305 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
306 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
307 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
308 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
309 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
310 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
311 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
312 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
313 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
316 CLASSERT((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
317 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
320 CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
321 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
322 CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
323 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
326 static lnd_t *lnet_find_lnd_by_type(__u32 type)
329 struct list_head *tmp;
331 /* holding lnd mutex */
332 list_for_each(tmp, &the_lnet.ln_lnds) {
333 lnd = list_entry(tmp, lnd_t, lnd_list);
335 if (lnd->lnd_type == type)
342 lnet_register_lnd (lnd_t *lnd)
344 mutex_lock(&the_lnet.ln_lnd_mutex);
346 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
347 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
349 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
350 lnd->lnd_refcount = 0;
352 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
354 mutex_unlock(&the_lnet.ln_lnd_mutex);
356 EXPORT_SYMBOL(lnet_register_lnd);
359 lnet_unregister_lnd (lnd_t *lnd)
361 mutex_lock(&the_lnet.ln_lnd_mutex);
363 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
364 LASSERT(lnd->lnd_refcount == 0);
366 list_del(&lnd->lnd_list);
367 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
369 mutex_unlock(&the_lnet.ln_lnd_mutex);
371 EXPORT_SYMBOL(lnet_unregister_lnd);
374 lnet_counters_get(lnet_counters_t *counters)
376 lnet_counters_t *ctr;
379 memset(counters, 0, sizeof(*counters));
381 lnet_net_lock(LNET_LOCK_EX);
383 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
384 counters->msgs_max += ctr->msgs_max;
385 counters->msgs_alloc += ctr->msgs_alloc;
386 counters->errors += ctr->errors;
387 counters->send_count += ctr->send_count;
388 counters->recv_count += ctr->recv_count;
389 counters->route_count += ctr->route_count;
390 counters->drop_count += ctr->drop_count;
391 counters->send_length += ctr->send_length;
392 counters->recv_length += ctr->recv_length;
393 counters->route_length += ctr->route_length;
394 counters->drop_length += ctr->drop_length;
397 lnet_net_unlock(LNET_LOCK_EX);
399 EXPORT_SYMBOL(lnet_counters_get);
402 lnet_counters_reset(void)
404 lnet_counters_t *counters;
407 lnet_net_lock(LNET_LOCK_EX);
409 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
410 memset(counters, 0, sizeof(lnet_counters_t));
412 lnet_net_unlock(LNET_LOCK_EX);
416 lnet_res_type2str(int type)
421 case LNET_COOKIE_TYPE_MD:
423 case LNET_COOKIE_TYPE_ME:
425 case LNET_COOKIE_TYPE_EQ:
431 lnet_res_container_cleanup(struct lnet_res_container *rec)
435 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
438 while (!list_empty(&rec->rec_active)) {
439 struct list_head *e = rec->rec_active.next;
442 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
443 lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
445 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
446 lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
448 } else { /* NB: Active MEs should be attached on portals */
455 /* Found alive MD/ME/EQ, user really should unlink/free
456 * all of them before finalize LNet, but if someone didn't,
457 * we have to recycle garbage for him */
458 CERROR("%d active elements on exit of %s container\n",
459 count, lnet_res_type2str(rec->rec_type));
462 if (rec->rec_lh_hash != NULL) {
463 LIBCFS_FREE(rec->rec_lh_hash,
464 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
465 rec->rec_lh_hash = NULL;
468 rec->rec_type = 0; /* mark it as finalized */
472 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
477 LASSERT(rec->rec_type == 0);
479 rec->rec_type = type;
480 INIT_LIST_HEAD(&rec->rec_active);
482 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
484 /* Arbitrary choice of hash table size */
485 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
486 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
487 if (rec->rec_lh_hash == NULL) {
492 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
493 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
498 CERROR("Failed to setup %s resource container\n",
499 lnet_res_type2str(type));
500 lnet_res_container_cleanup(rec);
505 lnet_res_containers_destroy(struct lnet_res_container **recs)
507 struct lnet_res_container *rec;
510 cfs_percpt_for_each(rec, i, recs)
511 lnet_res_container_cleanup(rec);
513 cfs_percpt_free(recs);
516 static struct lnet_res_container **
517 lnet_res_containers_create(int type)
519 struct lnet_res_container **recs;
520 struct lnet_res_container *rec;
524 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
526 CERROR("Failed to allocate %s resource containers\n",
527 lnet_res_type2str(type));
531 cfs_percpt_for_each(rec, i, recs) {
532 rc = lnet_res_container_setup(rec, i, type);
534 lnet_res_containers_destroy(recs);
543 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
545 /* ALWAYS called with lnet_res_lock held */
546 struct list_head *head;
547 lnet_libhandle_t *lh;
550 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
553 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
554 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
556 list_for_each_entry(lh, head, lh_hash_chain) {
557 if (lh->lh_cookie == cookie)
565 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
567 /* ALWAYS called with lnet_res_lock held */
568 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
571 lh->lh_cookie = rec->rec_lh_cookie;
572 rec->rec_lh_cookie += 1 << ibits;
574 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
576 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
579 static int lnet_unprepare(void);
582 lnet_prepare(lnet_pid_t requested_pid)
584 /* Prepare to bring up the network */
585 struct lnet_res_container **recs;
588 if (requested_pid == LNET_PID_ANY) {
589 /* Don't instantiate LNET just for me */
593 LASSERT(the_lnet.ln_refcount == 0);
595 the_lnet.ln_routing = 0;
597 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
598 the_lnet.ln_pid = requested_pid;
600 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
601 INIT_LIST_HEAD(&the_lnet.ln_peers);
602 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
603 INIT_LIST_HEAD(&the_lnet.ln_nets);
604 INIT_LIST_HEAD(&the_lnet.ln_routers);
605 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
606 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
608 rc = lnet_descriptor_setup();
612 rc = lnet_create_remote_nets_table();
617 * NB the interface cookie in wire handles guards against delayed
618 * replies and ACKs appearing valid after reboot.
620 the_lnet.ln_interface_cookie = ktime_get_real_ns();
622 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
623 sizeof(lnet_counters_t));
624 if (the_lnet.ln_counters == NULL) {
625 CERROR("Failed to allocate counters for LNet\n");
630 rc = lnet_peer_tables_create();
634 rc = lnet_msg_containers_create();
638 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
639 LNET_COOKIE_TYPE_EQ);
643 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
649 the_lnet.ln_me_containers = recs;
651 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
657 the_lnet.ln_md_containers = recs;
659 rc = lnet_portals_create();
661 CERROR("Failed to create portals for LNet: %d\n", rc);
673 lnet_unprepare (void)
675 /* NB no LNET_LOCK since this is the last reference. All LND instances
676 * have shut down already, so it is safe to unlink and free all
677 * descriptors, even those that appear committed to a network op (eg MD
678 * with non-zero pending count) */
680 lnet_fail_nid(LNET_NID_ANY, 0);
682 LASSERT(the_lnet.ln_refcount == 0);
683 LASSERT(list_empty(&the_lnet.ln_test_peers));
684 LASSERT(list_empty(&the_lnet.ln_nets));
686 lnet_portals_destroy();
688 if (the_lnet.ln_md_containers != NULL) {
689 lnet_res_containers_destroy(the_lnet.ln_md_containers);
690 the_lnet.ln_md_containers = NULL;
693 if (the_lnet.ln_me_containers != NULL) {
694 lnet_res_containers_destroy(the_lnet.ln_me_containers);
695 the_lnet.ln_me_containers = NULL;
698 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
700 lnet_msg_containers_destroy();
702 lnet_rtrpools_free(0);
704 if (the_lnet.ln_counters != NULL) {
705 cfs_percpt_free(the_lnet.ln_counters);
706 the_lnet.ln_counters = NULL;
708 lnet_destroy_remote_nets_table();
709 lnet_descriptor_cleanup();
715 lnet_net2ni_locked(__u32 net_id, int cpt)
718 struct lnet_net *net;
720 LASSERT(cpt != LNET_LOCK_EX);
722 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
723 if (net->net_id == net_id) {
724 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
734 lnet_net2ni_addref(__u32 net)
739 ni = lnet_net2ni_locked(net, 0);
741 lnet_ni_addref_locked(ni, 0);
746 EXPORT_SYMBOL(lnet_net2ni_addref);
749 lnet_get_net_locked(__u32 net_id)
751 struct lnet_net *net;
753 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
754 if (net->net_id == net_id)
762 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
767 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
772 val = hash_long(key, LNET_CPT_BITS);
773 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
777 return (unsigned int)(key + val + (val >> 1)) % number;
781 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
783 struct lnet_net *net;
785 /* must called with hold of lnet_net_lock */
786 if (LNET_CPT_NUMBER == 1)
787 return 0; /* the only one */
790 * If NI is provided then use the CPT identified in the NI cpt
791 * list if one exists. If one doesn't exist, then that NI is
792 * associated with all CPTs and it follows that the net it belongs
793 * to is implicitly associated with all CPTs, so just hash the nid
797 if (ni->ni_cpts != NULL)
798 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
801 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
804 /* no NI provided so look at the net */
805 net = lnet_get_net_locked(LNET_NIDNET(nid));
807 if (net != NULL && net->net_cpts != NULL) {
808 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
811 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
815 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
820 if (LNET_CPT_NUMBER == 1)
821 return 0; /* the only one */
823 cpt = lnet_net_lock_current();
825 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
827 lnet_net_unlock(cpt);
831 EXPORT_SYMBOL(lnet_cpt_of_nid);
834 lnet_islocalnet(__u32 net_id)
836 struct lnet_net *net;
840 cpt = lnet_net_lock_current();
842 net = lnet_get_net_locked(net_id);
846 lnet_net_unlock(cpt);
852 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
854 if (ni->ni_state == LNET_NI_STATE_ACTIVE ||
855 ni->ni_state == LNET_NI_STATE_DEGRADED)
862 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
864 struct lnet_net *net;
867 LASSERT(cpt != LNET_LOCK_EX);
869 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
870 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
871 if (ni->ni_nid == nid)
880 lnet_nid2ni_addref(lnet_nid_t nid)
885 ni = lnet_nid2ni_locked(nid, 0);
887 lnet_ni_addref_locked(ni, 0);
892 EXPORT_SYMBOL(lnet_nid2ni_addref);
895 lnet_islocalnid(lnet_nid_t nid)
900 cpt = lnet_net_lock_current();
901 ni = lnet_nid2ni_locked(nid, cpt);
902 lnet_net_unlock(cpt);
908 lnet_count_acceptor_nets(void)
910 /* Return the # of NIs that need the acceptor. */
912 struct lnet_net *net;
915 cpt = lnet_net_lock_current();
916 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
917 /* all socklnd type networks should have the acceptor
919 if (net->net_lnd->lnd_accept != NULL)
923 lnet_net_unlock(cpt);
928 static struct lnet_ping_info *
929 lnet_ping_info_create(int num_ni)
931 struct lnet_ping_info *ping_info;
934 infosz = offsetof(struct lnet_ping_info, pi_ni[num_ni]);
935 LIBCFS_ALLOC(ping_info, infosz);
936 if (ping_info == NULL) {
937 CERROR("Can't allocate ping info[%d]\n", num_ni);
941 ping_info->pi_nnis = num_ni;
942 ping_info->pi_pid = the_lnet.ln_pid;
943 ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
944 ping_info->pi_features = LNET_PING_FEAT_NI_STATUS;
950 lnet_get_net_ni_count_locked(struct lnet_net *net)
955 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
962 lnet_get_net_ni_count_pre(struct lnet_net *net)
967 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
974 lnet_get_ni_count(void)
977 struct lnet_net *net;
982 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
983 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
993 lnet_ping_info_free(struct lnet_ping_info *pinfo)
996 offsetof(struct lnet_ping_info,
997 pi_ni[pinfo->pi_nnis]));
1001 lnet_ping_info_destroy(void)
1003 struct lnet_net *net;
1006 lnet_net_lock(LNET_LOCK_EX);
1008 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1009 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1011 ni->ni_status = NULL;
1016 lnet_ping_info_free(the_lnet.ln_ping_info);
1017 the_lnet.ln_ping_info = NULL;
1019 lnet_net_unlock(LNET_LOCK_EX);
1023 lnet_ping_event_handler(lnet_event_t *event)
1025 struct lnet_ping_info *pinfo = event->md.user_ptr;
1027 if (event->unlinked)
1028 pinfo->pi_features = LNET_PING_FEAT_INVAL;
1032 lnet_ping_info_setup(struct lnet_ping_info **ppinfo, lnet_handle_md_t *md_handle,
1033 int ni_count, bool set_eq)
1035 lnet_handle_me_t me_handle;
1036 lnet_process_id_t id = {LNET_NID_ANY, LNET_PID_ANY};
1037 lnet_md_t md = {NULL};
1041 rc = LNetEQAlloc(0, lnet_ping_event_handler,
1042 &the_lnet.ln_ping_target_eq);
1044 CERROR("Can't allocate ping EQ: %d\n", rc);
1049 *ppinfo = lnet_ping_info_create(ni_count);
1050 if (*ppinfo == NULL) {
1055 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1056 LNET_PROTO_PING_MATCHBITS, 0,
1057 LNET_UNLINK, LNET_INS_AFTER,
1060 CERROR("Can't create ping ME: %d\n", rc);
1064 /* initialize md content */
1066 md.length = offsetof(struct lnet_ping_info,
1067 pi_ni[(*ppinfo)->pi_nnis]);
1068 md.threshold = LNET_MD_THRESH_INF;
1070 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1071 LNET_MD_MANAGE_REMOTE;
1073 md.eq_handle = the_lnet.ln_ping_target_eq;
1074 md.user_ptr = *ppinfo;
1076 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, md_handle);
1078 CERROR("Can't attach ping MD: %d\n", rc);
1085 rc2 = LNetMEUnlink(me_handle);
1088 lnet_ping_info_free(*ppinfo);
1092 LNetEQFree(the_lnet.ln_ping_target_eq);
1097 lnet_ping_md_unlink(struct lnet_ping_info *pinfo, lnet_handle_md_t *md_handle)
1099 sigset_t blocked = cfs_block_allsigs();
1101 LNetMDUnlink(*md_handle);
1102 LNetInvalidateHandle(md_handle);
1104 /* NB md could be busy; this just starts the unlink */
1105 while (pinfo->pi_features != LNET_PING_FEAT_INVAL) {
1106 CDEBUG(D_NET, "Still waiting for ping MD to unlink\n");
1107 set_current_state(TASK_UNINTERRUPTIBLE);
1108 schedule_timeout(cfs_time_seconds(1));
1111 cfs_restore_sigs(blocked);
1115 lnet_ping_info_install_locked(struct lnet_ping_info *ping_info)
1119 struct lnet_net *net;
1120 struct lnet_ni_status *ns;
1123 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1124 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1125 LASSERT(i < ping_info->pi_nnis);
1127 ns = &ping_info->pi_ni[i];
1129 ns->ns_nid = ni->ni_nid;
1132 ns->ns_status = (ni->ni_status != NULL) ?
1133 ni->ni_status->ns_status :
1145 lnet_ping_target_update(struct lnet_ping_info *pinfo, lnet_handle_md_t md_handle)
1147 struct lnet_ping_info *old_pinfo = NULL;
1148 lnet_handle_md_t old_md;
1150 /* switch the NIs to point to the new ping info created */
1151 lnet_net_lock(LNET_LOCK_EX);
1153 if (!the_lnet.ln_routing)
1154 pinfo->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1155 lnet_ping_info_install_locked(pinfo);
1157 if (the_lnet.ln_ping_info != NULL) {
1158 old_pinfo = the_lnet.ln_ping_info;
1159 old_md = the_lnet.ln_ping_target_md;
1161 the_lnet.ln_ping_target_md = md_handle;
1162 the_lnet.ln_ping_info = pinfo;
1164 lnet_net_unlock(LNET_LOCK_EX);
1166 if (old_pinfo != NULL) {
1167 /* unlink the old ping info */
1168 lnet_ping_md_unlink(old_pinfo, &old_md);
1169 lnet_ping_info_free(old_pinfo);
1174 lnet_ping_target_fini(void)
1178 lnet_ping_md_unlink(the_lnet.ln_ping_info,
1179 &the_lnet.ln_ping_target_md);
1181 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1184 lnet_ping_info_destroy();
1188 lnet_ni_tq_credits(lnet_ni_t *ni)
1192 LASSERT(ni->ni_ncpts >= 1);
1194 if (ni->ni_ncpts == 1)
1195 return ni->ni_net->net_tunables.lct_max_tx_credits;
1197 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1198 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1199 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1205 lnet_ni_unlink_locked(lnet_ni_t *ni)
1207 if (!list_empty(&ni->ni_cptlist)) {
1208 list_del_init(&ni->ni_cptlist);
1209 lnet_ni_decref_locked(ni, 0);
1212 /* move it to zombie list and nobody can find it anymore */
1213 LASSERT(!list_empty(&ni->ni_netlist));
1214 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1215 lnet_ni_decref_locked(ni, 0);
1219 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1224 struct list_head *zombie_list = &net->net_ni_zombie;
1227 * Now wait for the NIs I just nuked to show up on the zombie
1228 * list and shut them down in guaranteed thread context
1231 while (!list_empty(zombie_list)) {
1235 ni = list_entry(zombie_list->next,
1236 lnet_ni_t, ni_netlist);
1237 list_del_init(&ni->ni_netlist);
1238 /* the ni should be in deleting state. If it's not it's
1240 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
1241 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1244 /* still busy, add it back to zombie list */
1245 list_add(&ni->ni_netlist, zombie_list);
1249 if (!list_empty(&ni->ni_netlist)) {
1250 lnet_net_unlock(LNET_LOCK_EX);
1252 if ((i & (-i)) == i) {
1254 "Waiting for zombie LNI %s\n",
1255 libcfs_nid2str(ni->ni_nid));
1257 set_current_state(TASK_UNINTERRUPTIBLE);
1258 schedule_timeout(cfs_time_seconds(1));
1259 lnet_net_lock(LNET_LOCK_EX);
1263 lnet_net_unlock(LNET_LOCK_EX);
1265 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1267 LASSERT(!in_interrupt());
1268 (net->net_lnd->lnd_shutdown)(ni);
1271 CDEBUG(D_LNI, "Removed LNI %s\n",
1272 libcfs_nid2str(ni->ni_nid));
1276 lnet_net_lock(LNET_LOCK_EX);
1280 /* shutdown down the NI and release refcount */
1282 lnet_shutdown_lndni(struct lnet_ni *ni)
1285 struct lnet_net *net = ni->ni_net;
1287 lnet_net_lock(LNET_LOCK_EX);
1288 ni->ni_state = LNET_NI_STATE_DELETING;
1289 lnet_ni_unlink_locked(ni);
1290 lnet_net_unlock(LNET_LOCK_EX);
1292 /* clear messages for this NI on the lazy portal */
1293 for (i = 0; i < the_lnet.ln_nportals; i++)
1294 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1296 /* Do peer table cleanup for this ni */
1297 lnet_peer_tables_cleanup(ni);
1299 lnet_net_lock(LNET_LOCK_EX);
1300 lnet_clear_zombies_nis_locked(net);
1301 lnet_net_unlock(LNET_LOCK_EX);
1305 lnet_shutdown_lndnet(struct lnet_net *net)
1309 lnet_net_lock(LNET_LOCK_EX);
1311 net->net_state = LNET_NET_STATE_DELETING;
1313 list_del_init(&net->net_list);
1315 while (!list_empty(&net->net_ni_list)) {
1316 ni = list_entry(net->net_ni_list.next,
1317 lnet_ni_t, ni_netlist);
1318 lnet_net_unlock(LNET_LOCK_EX);
1319 lnet_shutdown_lndni(ni);
1320 lnet_net_lock(LNET_LOCK_EX);
1324 * decrement ref count on lnd only when the entire network goes
1327 net->net_lnd->lnd_refcount--;
1329 lnet_net_unlock(LNET_LOCK_EX);
1335 lnet_shutdown_lndnets(void)
1337 struct lnet_net *net;
1339 /* NB called holding the global mutex */
1341 /* All quiet on the API front */
1342 LASSERT(!the_lnet.ln_shutdown);
1343 LASSERT(the_lnet.ln_refcount == 0);
1345 lnet_net_lock(LNET_LOCK_EX);
1346 the_lnet.ln_shutdown = 1; /* flag shutdown */
1348 while (!list_empty(&the_lnet.ln_nets)) {
1350 * move the nets to the zombie list to avoid them being
1351 * picked up for new work. LONET is also included in the
1352 * Nets that will be moved to the zombie list
1354 net = list_entry(the_lnet.ln_nets.next,
1355 struct lnet_net, net_list);
1356 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1359 /* Drop the cached loopback Net. */
1360 if (the_lnet.ln_loni != NULL) {
1361 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1362 the_lnet.ln_loni = NULL;
1364 lnet_net_unlock(LNET_LOCK_EX);
1366 /* iterate through the net zombie list and delete each net */
1367 while (!list_empty(&the_lnet.ln_net_zombie)) {
1368 net = list_entry(the_lnet.ln_net_zombie.next,
1369 struct lnet_net, net_list);
1370 lnet_shutdown_lndnet(net);
1373 lnet_net_lock(LNET_LOCK_EX);
1374 the_lnet.ln_shutdown = 0;
1375 lnet_net_unlock(LNET_LOCK_EX);
1379 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1382 struct lnet_tx_queue *tq;
1384 struct lnet_net *net = ni->ni_net;
1386 mutex_lock(&the_lnet.ln_lnd_mutex);
1389 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1390 ni->ni_lnd_tunables_set = true;
1393 rc = (net->net_lnd->lnd_startup)(ni);
1395 mutex_unlock(&the_lnet.ln_lnd_mutex);
1398 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1399 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1400 lnet_net_lock(LNET_LOCK_EX);
1401 net->net_lnd->lnd_refcount--;
1402 lnet_net_unlock(LNET_LOCK_EX);
1406 ni->ni_state = LNET_NI_STATE_ACTIVE;
1408 /* We keep a reference on the loopback net through the loopback NI */
1409 if (net->net_lnd->lnd_type == LOLND) {
1411 LASSERT(the_lnet.ln_loni == NULL);
1412 the_lnet.ln_loni = ni;
1413 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
1414 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
1415 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
1416 ni->ni_net->net_tunables.lct_peer_timeout = 0;
1420 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
1421 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
1422 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1423 libcfs_lnd2str(net->net_lnd->lnd_type),
1424 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
1426 /* shutdown the NI since if we get here then it must've already
1429 lnet_shutdown_lndni(ni);
1433 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1434 tq->tq_credits_min =
1435 tq->tq_credits_max =
1436 tq->tq_credits = lnet_ni_tq_credits(ni);
1439 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1440 libcfs_nid2str(ni->ni_nid),
1441 ni->ni_net->net_tunables.lct_peer_tx_credits,
1442 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1443 ni->ni_net->net_tunables.lct_peer_rtr_credits,
1444 ni->ni_net->net_tunables.lct_peer_timeout);
1453 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
1456 struct lnet_net *net_l = NULL;
1457 struct list_head local_ni_list;
1463 net->net_tunables.lct_peer_timeout;
1465 net->net_tunables.lct_max_tx_credits;
1466 int peerrtrcredits =
1467 net->net_tunables.lct_peer_rtr_credits;
1469 INIT_LIST_HEAD(&local_ni_list);
1472 * make sure that this net is unique. If it isn't then
1473 * we are adding interfaces to an already existing network, and
1474 * 'net' is just a convenient way to pass in the list.
1475 * if it is unique we need to find the LND and load it if
1478 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
1479 lnd_type = LNET_NETTYP(net->net_id);
1481 LASSERT(libcfs_isknown_lnd(lnd_type));
1483 if (lnd_type == CIBLND || lnd_type == OPENIBLND ||
1484 lnd_type == IIBLND || lnd_type == VIBLND) {
1485 CERROR("LND %s obsoleted\n", libcfs_lnd2str(lnd_type));
1490 mutex_lock(&the_lnet.ln_lnd_mutex);
1491 lnd = lnet_find_lnd_by_type(lnd_type);
1494 mutex_unlock(&the_lnet.ln_lnd_mutex);
1495 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1496 mutex_lock(&the_lnet.ln_lnd_mutex);
1498 lnd = lnet_find_lnd_by_type(lnd_type);
1500 mutex_unlock(&the_lnet.ln_lnd_mutex);
1501 CERROR("Can't load LND %s, module %s, rc=%d\n",
1502 libcfs_lnd2str(lnd_type),
1503 libcfs_lnd2modname(lnd_type), rc);
1504 #ifndef HAVE_MODULE_LOADING_SUPPORT
1505 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1506 "compiled with kernel module "
1507 "loading support.");
1514 lnet_net_lock(LNET_LOCK_EX);
1515 lnd->lnd_refcount++;
1516 lnet_net_unlock(LNET_LOCK_EX);
1520 mutex_unlock(&the_lnet.ln_lnd_mutex);
1526 * net_l: if the network being added is unique then net_l
1527 * will point to that network
1528 * if the network being added is not unique then
1529 * net_l points to the existing network.
1531 * When we enter the loop below, we'll pick NIs off he
1532 * network beign added and start them up, then add them to
1533 * a local ni list. Once we've successfully started all
1534 * the NIs then we join the local NI list (of started up
1535 * networks) with the net_l->net_ni_list, which should
1536 * point to the correct network to add the new ni list to
1538 * If any of the new NIs fail to start up, then we want to
1539 * iterate through the local ni list, which should include
1540 * any NIs which were successfully started up, and shut
1543 * After than we want to delete the network being added,
1544 * to avoid a memory leak.
1548 * When a network uses TCP bonding then all its interfaces
1549 * must be specified when the network is first defined: the
1550 * TCP bonding code doesn't allow for interfaces to be added
1553 if (net_l != net && net_l != NULL && use_tcp_bonding &&
1554 LNET_NETTYP(net_l->net_id) == SOCKLND) {
1559 while (!list_empty(&net->net_ni_added)) {
1560 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
1562 list_del_init(&ni->ni_netlist);
1564 /* make sure that the the NI we're about to start
1565 * up is actually unique. if it's not fail. */
1566 if (!lnet_ni_unique_net(&net_l->net_ni_list,
1567 ni->ni_interfaces[0])) {
1572 /* adjust the pointer the parent network, just in case it
1573 * the net is a duplicate */
1576 rc = lnet_startup_lndni(ni, tun);
1578 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
1579 ni->ni_net->net_lnd->lnd_query != NULL);
1585 list_add_tail(&ni->ni_netlist, &local_ni_list);
1590 lnet_net_lock(LNET_LOCK_EX);
1591 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
1592 lnet_incr_dlc_seq();
1593 lnet_net_unlock(LNET_LOCK_EX);
1595 /* if the network is not unique then we don't want to keep
1596 * it around after we're done. Free it. Otherwise add that
1597 * net to the global the_lnet.ln_nets */
1598 if (net_l != net && net_l != NULL) {
1600 * TODO - note. currently the tunables can not be updated
1605 net->net_state = LNET_NET_STATE_ACTIVE;
1607 * restore tunables after it has been overwitten by the
1610 if (peer_timeout != -1)
1611 net->net_tunables.lct_peer_timeout = peer_timeout;
1612 if (maxtxcredits != -1)
1613 net->net_tunables.lct_max_tx_credits = maxtxcredits;
1614 if (peerrtrcredits != -1)
1615 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
1617 lnet_net_lock(LNET_LOCK_EX);
1618 list_add_tail(&net->net_list, &the_lnet.ln_nets);
1619 lnet_net_unlock(LNET_LOCK_EX);
1626 * shutdown the new NIs that are being started up
1627 * free the NET being started
1629 while (!list_empty(&local_ni_list)) {
1630 ni = list_entry(local_ni_list.next, struct lnet_ni,
1633 lnet_shutdown_lndni(ni);
1643 lnet_startup_lndnets(struct list_head *netlist)
1645 struct lnet_net *net;
1649 while (!list_empty(netlist)) {
1650 net = list_entry(netlist->next, struct lnet_net, net_list);
1651 list_del_init(&net->net_list);
1653 rc = lnet_startup_lndnet(net, NULL);
1663 lnet_shutdown_lndnets();
1669 * Initialize LNet library.
1671 * Automatically called at module loading time. Caller has to call
1672 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
1673 * latter returned 0. It must be called exactly once.
1675 * \retval 0 on success
1676 * \retval -ve on failures.
1678 int lnet_lib_init(void)
1682 lnet_assert_wire_constants();
1684 memset(&the_lnet, 0, sizeof(the_lnet));
1686 /* refer to global cfs_cpt_table for now */
1687 the_lnet.ln_cpt_table = cfs_cpt_table;
1688 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1690 LASSERT(the_lnet.ln_cpt_number > 0);
1691 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1692 /* we are under risk of consuming all lh_cookie */
1693 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1694 "please change setting of CPT-table and retry\n",
1695 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1699 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1700 the_lnet.ln_cpt_bits++;
1702 rc = lnet_create_locks();
1704 CERROR("Can't create LNet global locks: %d\n", rc);
1708 the_lnet.ln_refcount = 0;
1709 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1710 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1711 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
1712 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1713 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1715 /* The hash table size is the number of bits it takes to express the set
1716 * ln_num_routes, minus 1 (better to under estimate than over so we
1717 * don't waste memory). */
1718 if (rnet_htable_size <= 0)
1719 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1720 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1721 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1722 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1723 order_base_2(rnet_htable_size) - 1);
1725 /* All LNDs apart from the LOLND are in separate modules. They
1726 * register themselves when their module loads, and unregister
1727 * themselves when their module is unloaded. */
1728 lnet_register_lnd(&the_lolnd);
1733 * Finalize LNet library.
1735 * \pre lnet_lib_init() called with success.
1736 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1738 void lnet_lib_exit(void)
1740 LASSERT(the_lnet.ln_refcount == 0);
1742 while (!list_empty(&the_lnet.ln_lnds))
1743 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1745 lnet_destroy_locks();
1749 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1751 * Users must call this function at least once before any other functions.
1752 * For each successful call there must be a corresponding call to
1753 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1756 * The PID used by LNet may be different from the one requested.
1759 * \param requested_pid PID requested by the caller.
1761 * \return >= 0 on success, and < 0 error code on failures.
1764 LNetNIInit(lnet_pid_t requested_pid)
1766 int im_a_router = 0;
1769 struct lnet_ping_info *pinfo;
1770 lnet_handle_md_t md_handle;
1771 struct list_head net_head;
1772 struct lnet_net *net;
1774 INIT_LIST_HEAD(&net_head);
1776 mutex_lock(&the_lnet.ln_api_mutex);
1778 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1780 if (the_lnet.ln_refcount > 0) {
1781 rc = the_lnet.ln_refcount++;
1782 mutex_unlock(&the_lnet.ln_api_mutex);
1786 rc = lnet_prepare(requested_pid);
1788 mutex_unlock(&the_lnet.ln_api_mutex);
1792 /* create a network for Loopback network */
1793 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
1796 goto err_empty_list;
1799 /* Add in the loopback NI */
1800 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
1802 goto err_empty_list;
1805 /* If LNet is being initialized via DLC it is possible
1806 * that the user requests not to load module parameters (ones which
1807 * are supported by DLC) on initialization. Therefore, make sure not
1808 * to load networks, routes and forwarding from module parameters
1809 * in this case. On cleanup in case of failure only clean up
1810 * routes if it has been loaded */
1811 if (!the_lnet.ln_nis_from_mod_params) {
1812 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
1815 goto err_empty_list;
1818 ni_count = lnet_startup_lndnets(&net_head);
1821 goto err_empty_list;
1824 if (!the_lnet.ln_nis_from_mod_params) {
1825 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1827 goto err_shutdown_lndnis;
1829 rc = lnet_check_routes();
1831 goto err_destroy_routes;
1833 rc = lnet_rtrpools_alloc(im_a_router);
1835 goto err_destroy_routes;
1838 rc = lnet_acceptor_start();
1840 goto err_destroy_routes;
1842 the_lnet.ln_refcount = 1;
1843 /* Now I may use my own API functions... */
1845 rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
1847 goto err_acceptor_stop;
1849 lnet_ping_target_update(pinfo, md_handle);
1851 rc = lnet_router_checker_start();
1858 mutex_unlock(&the_lnet.ln_api_mutex);
1863 lnet_ping_target_fini();
1865 the_lnet.ln_refcount = 0;
1866 lnet_acceptor_stop();
1868 if (!the_lnet.ln_nis_from_mod_params)
1869 lnet_destroy_routes();
1870 err_shutdown_lndnis:
1871 lnet_shutdown_lndnets();
1875 mutex_unlock(&the_lnet.ln_api_mutex);
1876 while (!list_empty(&net_head)) {
1877 struct lnet_net *net;
1879 net = list_entry(net_head.next, struct lnet_net, net_list);
1880 list_del_init(&net->net_list);
1885 EXPORT_SYMBOL(LNetNIInit);
1888 * Stop LNet interfaces, routing, and forwarding.
1890 * Users must call this function once for each successful call to LNetNIInit().
1891 * Once the LNetNIFini() operation has been started, the results of pending
1892 * API operations are undefined.
1894 * \return always 0 for current implementation.
1899 mutex_lock(&the_lnet.ln_api_mutex);
1901 LASSERT(the_lnet.ln_refcount > 0);
1903 if (the_lnet.ln_refcount != 1) {
1904 the_lnet.ln_refcount--;
1906 LASSERT(!the_lnet.ln_niinit_self);
1911 lnet_router_checker_stop();
1912 lnet_ping_target_fini();
1914 /* Teardown fns that use my own API functions BEFORE here */
1915 the_lnet.ln_refcount = 0;
1917 lnet_acceptor_stop();
1918 lnet_destroy_routes();
1919 lnet_shutdown_lndnets();
1923 mutex_unlock(&the_lnet.ln_api_mutex);
1926 EXPORT_SYMBOL(LNetNIFini);
1929 static int lnet_handle_dbg_task(struct lnet_ioctl_dbg *dbg,
1930 struct lnet_dbg_task_info *dbg_info)
1932 switch (dbg->dbg_task) {
1933 case LNET_DBG_INCR_DLC_SEQ:
1934 lnet_incr_dlc_seq();
1940 * Grabs the ni data from the ni structure and fills the out
1943 * \param[in] ni network interface structure
1944 * \param[out] cfg_ni NI config information
1945 * \param[out] tun network and LND tunables
1948 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
1949 struct lnet_ioctl_config_lnd_tunables *tun,
1952 size_t min_size = 0;
1955 if (!ni || !cfg_ni || !tun)
1958 if (ni->ni_interfaces[0] != NULL) {
1959 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
1960 if (ni->ni_interfaces[i] != NULL) {
1961 strncpy(cfg_ni->lic_ni_intf[i],
1962 ni->ni_interfaces[i],
1963 sizeof(cfg_ni->lic_ni_intf[i]));
1968 cfg_ni->lic_nid = ni->ni_nid;
1969 cfg_ni->lic_status = ni->ni_status->ns_status;
1970 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
1972 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
1975 * tun->lt_tun will always be present, but in order to be
1976 * backwards compatible, we need to deal with the cases when
1977 * tun->lt_tun is smaller than what the kernel has, because it
1978 * comes from an older version of a userspace program, then we'll
1979 * need to copy as much information as we have available space.
1981 min_size = tun_size - sizeof(tun->lt_cmn);
1982 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
1984 /* copy over the cpts */
1985 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
1986 ni->ni_cpts == NULL) {
1987 for (i = 0; i < ni->ni_ncpts; i++)
1988 cfg_ni->lic_cpts[i] = i;
1991 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
1992 i < LNET_MAX_SHOW_NUM_CPT;
1994 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
1996 cfg_ni->lic_ncpts = ni->ni_ncpts;
2000 * NOTE: This is a legacy function left in the code to be backwards
2001 * compatible with older userspace programs. It should eventually be
2004 * Grabs the ni data from the ni structure and fills the out
2007 * \param[in] ni network interface structure
2008 * \param[out] config config information
2011 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2012 struct lnet_ioctl_config_data *config)
2014 struct lnet_ioctl_net_config *net_config;
2015 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2016 size_t min_size, tunable_size = 0;
2022 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2026 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2027 ARRAY_SIZE(net_config->ni_interfaces));
2029 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2030 if (!ni->ni_interfaces[i])
2033 strncpy(net_config->ni_interfaces[i],
2034 ni->ni_interfaces[i],
2035 sizeof(net_config->ni_interfaces[i]));
2038 config->cfg_nid = ni->ni_nid;
2039 config->cfg_config_u.cfg_net.net_peer_timeout =
2040 ni->ni_net->net_tunables.lct_peer_timeout;
2041 config->cfg_config_u.cfg_net.net_max_tx_credits =
2042 ni->ni_net->net_tunables.lct_max_tx_credits;
2043 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2044 ni->ni_net->net_tunables.lct_peer_tx_credits;
2045 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2046 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2048 net_config->ni_status = ni->ni_status->ns_status;
2051 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2053 for (i = 0; i < num_cpts; i++)
2054 net_config->ni_cpts[i] = ni->ni_cpts[i];
2056 config->cfg_ncpts = num_cpts;
2060 * See if user land tools sent in a newer and larger version
2061 * of struct lnet_tunables than what the kernel uses.
2063 min_size = sizeof(*config) + sizeof(*net_config);
2065 if (config->cfg_hdr.ioc_len > min_size)
2066 tunable_size = config->cfg_hdr.ioc_len - min_size;
2068 /* Don't copy too much data to user space */
2069 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2070 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2072 if (lnd_cfg && min_size) {
2073 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2074 config->cfg_config_u.cfg_net.net_interface_count = 1;
2076 /* Tell user land that kernel side has less data */
2077 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2078 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2079 config->cfg_hdr.ioc_len -= min_size;
2085 lnet_get_ni_idx_locked(int idx)
2088 struct lnet_net *net;
2090 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2091 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2101 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2104 struct lnet_net *net = mynet;
2108 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2110 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2116 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2117 /* if you reached the end of the ni list and the net is
2118 * specified, then there are no more nis in that net */
2122 /* we reached the end of this net ni list. move to the
2124 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2125 /* no more nets and no more NIs. */
2128 /* get the next net */
2129 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2131 /* get the ni on it */
2132 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2138 /* there are more nis left */
2139 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2145 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2150 int idx = config->cfg_count;
2152 cpt = lnet_net_lock_current();
2154 ni = lnet_get_ni_idx_locked(idx);
2159 lnet_fill_ni_info_legacy(ni, config);
2163 lnet_net_unlock(cpt);
2168 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2169 struct lnet_ioctl_config_lnd_tunables *tun,
2176 if (!cfg_ni || !tun)
2179 cpt = lnet_net_lock_current();
2181 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2186 lnet_fill_ni_info(ni, cfg_ni, tun, tun_size);
2190 lnet_net_unlock(cpt);
2194 static int lnet_add_net_common(struct lnet_net *net,
2195 struct lnet_ioctl_config_lnd_tunables *tun)
2197 struct lnet_net *netl = NULL;
2199 lnet_ping_info_t *pinfo;
2200 lnet_handle_md_t md_handle;
2202 lnet_remotenet_t *rnet;
2204 int num_acceptor_nets;
2206 lnet_net_lock(LNET_LOCK_EX);
2207 rnet = lnet_find_rnet_locked(net->net_id);
2208 lnet_net_unlock(LNET_LOCK_EX);
2210 * make sure that the net added doesn't invalidate the current
2211 * configuration LNet is keeping
2214 CERROR("Adding net %s will invalidate routing configuration\n",
2215 libcfs_net2str(net->net_id));
2221 * make sure you calculate the correct number of slots in the ping
2222 * info. Since the ping info is a flattened list of all the NIs,
2223 * we should allocate enough slots to accomodate the number of NIs
2224 * which will be added.
2226 * since ni hasn't been configured yet, use
2227 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
2229 net_ni_count = lnet_get_net_ni_count_pre(net);
2231 rc = lnet_ping_info_setup(&pinfo, &md_handle,
2232 net_ni_count + lnet_get_ni_count(),
2238 memcpy(&net->net_tunables,
2239 &tun->lt_cmn, sizeof(net->net_tunables));
2241 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
2244 * before starting this network get a count of the current TCP
2245 * networks which require the acceptor thread running. If that
2246 * count is == 0 before we start up this network, then we'd want to
2247 * start up the acceptor thread after starting up this network
2249 num_acceptor_nets = lnet_count_acceptor_nets();
2251 net_id = net->net_id;
2253 rc = lnet_startup_lndnet(net,
2254 (tun) ? &tun->lt_tun : NULL);
2258 lnet_net_lock(LNET_LOCK_EX);
2259 netl = lnet_get_net_locked(net_id);
2260 lnet_net_unlock(LNET_LOCK_EX);
2265 * Start the acceptor thread if this is the first network
2266 * being added that requires the thread.
2268 if (netl->net_lnd->lnd_accept &&
2269 num_acceptor_nets == 0)
2271 rc = lnet_acceptor_start();
2273 /* shutdown the net that we just started */
2274 CERROR("Failed to start up acceptor thread\n");
2275 lnet_shutdown_lndnet(net);
2280 lnet_net_lock(LNET_LOCK_EX);
2281 lnet_peer_net_added(netl);
2282 lnet_net_unlock(LNET_LOCK_EX);
2284 lnet_ping_target_update(pinfo, md_handle);
2289 lnet_ping_md_unlink(pinfo, &md_handle);
2290 lnet_ping_info_free(pinfo);
2296 static int lnet_handle_legacy_ip2nets(char *ip2nets,
2297 struct lnet_ioctl_config_lnd_tunables *tun)
2299 struct lnet_net *net;
2302 struct list_head net_head;
2304 INIT_LIST_HEAD(&net_head);
2306 rc = lnet_parse_ip2nets(&nets, ip2nets);
2310 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2314 mutex_lock(&the_lnet.ln_api_mutex);
2315 while (!list_empty(&net_head)) {
2316 net = list_entry(net_head.next, struct lnet_net, net_list);
2317 list_del_init(&net->net_list);
2318 rc = lnet_add_net_common(net, tun);
2324 mutex_unlock(&the_lnet.ln_api_mutex);
2326 while (!list_empty(&net_head)) {
2327 net = list_entry(net_head.next, struct lnet_net, net_list);
2328 list_del_init(&net->net_list);
2334 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
2336 struct lnet_net *net;
2338 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2342 /* get the tunables if they are available */
2343 if (conf->lic_cfg_hdr.ioc_len >=
2344 sizeof(*conf) + sizeof(*tun))
2345 tun = (struct lnet_ioctl_config_lnd_tunables *)
2348 /* handle legacy ip2nets from DLC */
2349 if (conf->lic_legacy_ip2nets[0] != '\0')
2350 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
2353 net_id = LNET_NIDNET(conf->lic_nid);
2355 net = lnet_net_alloc(net_id, NULL);
2359 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
2360 conf->lic_ni_intf[0]);
2364 mutex_lock(&the_lnet.ln_api_mutex);
2366 rc = lnet_add_net_common(net, tun);
2368 mutex_unlock(&the_lnet.ln_api_mutex);
2373 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
2375 struct lnet_net *net;
2377 __u32 net_id = LNET_NIDNET(conf->lic_nid);
2378 lnet_ping_info_t *pinfo;
2379 lnet_handle_md_t md_handle;
2384 /* don't allow userspace to shutdown the LOLND */
2385 if (LNET_NETTYP(net_id) == LOLND)
2388 mutex_lock(&the_lnet.ln_api_mutex);
2392 net = lnet_get_net_locked(net_id);
2394 CERROR("net %s not found\n",
2395 libcfs_net2str(net_id));
2400 addr = LNET_NIDADDR(conf->lic_nid);
2402 /* remove the entire net */
2403 net_count = lnet_get_net_ni_count_locked(net);
2407 /* create and link a new ping info, before removing the old one */
2408 rc = lnet_ping_info_setup(&pinfo, &md_handle,
2409 lnet_get_ni_count() - net_count,
2414 lnet_shutdown_lndnet(net);
2416 if (lnet_count_acceptor_nets() == 0)
2417 lnet_acceptor_stop();
2419 lnet_ping_target_update(pinfo, md_handle);
2424 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
2426 CERROR("nid %s not found \n",
2427 libcfs_nid2str(conf->lic_nid));
2432 net_count = lnet_get_net_ni_count_locked(net);
2436 /* create and link a new ping info, before removing the old one */
2437 rc = lnet_ping_info_setup(&pinfo, &md_handle,
2438 lnet_get_ni_count() - 1, false);
2442 lnet_shutdown_lndni(ni);
2444 if (lnet_count_acceptor_nets() == 0)
2445 lnet_acceptor_stop();
2447 lnet_ping_target_update(pinfo, md_handle);
2449 /* check if the net is empty and remove it if it is */
2451 lnet_shutdown_lndnet(net);
2458 mutex_unlock(&the_lnet.ln_api_mutex);
2464 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
2465 * They are only expected to be called for unique networks.
2466 * That can be as a result of older DLC library
2467 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
2470 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
2472 struct lnet_net *net;
2473 struct list_head net_head;
2475 struct lnet_ioctl_config_lnd_tunables tun;
2476 char *nets = conf->cfg_config_u.cfg_net.net_intf;
2478 INIT_LIST_HEAD(&net_head);
2480 /* Create a net/ni structures for the network string */
2481 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2483 return rc == 0 ? -EINVAL : rc;
2485 mutex_lock(&the_lnet.ln_api_mutex);
2488 rc = -EINVAL; /* only add one network per call */
2492 net = list_entry(net_head.next, struct lnet_net, net_list);
2493 list_del_init(&net->net_list);
2495 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
2497 memset(&tun, sizeof(tun), 0);
2499 tun.lt_cmn.lct_peer_timeout =
2500 conf->cfg_config_u.cfg_net.net_peer_timeout;
2501 tun.lt_cmn.lct_peer_tx_credits =
2502 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
2503 tun.lt_cmn.lct_peer_rtr_credits =
2504 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
2505 tun.lt_cmn.lct_max_tx_credits =
2506 conf->cfg_config_u.cfg_net.net_max_tx_credits;
2508 rc = lnet_add_net_common(net, &tun);
2515 mutex_unlock(&the_lnet.ln_api_mutex);
2516 while (!list_empty(&net_head)) {
2517 net = list_entry(net_head.next, struct lnet_net, net_list);
2518 list_del_init(&net->net_list);
2525 lnet_dyn_del_net(__u32 net_id)
2527 struct lnet_net *net;
2528 struct lnet_ping_info *pinfo;
2529 lnet_handle_md_t md_handle;
2533 /* don't allow userspace to shutdown the LOLND */
2534 if (LNET_NETTYP(net_id) == LOLND)
2537 mutex_lock(&the_lnet.ln_api_mutex);
2541 net = lnet_get_net_locked(net_id);
2547 net_ni_count = lnet_get_net_ni_count_locked(net);
2551 /* create and link a new ping info, before removing the old one */
2552 rc = lnet_ping_info_setup(&pinfo, &md_handle,
2553 lnet_get_ni_count() - net_ni_count, false);
2557 lnet_shutdown_lndnet(net);
2559 if (lnet_count_acceptor_nets() == 0)
2560 lnet_acceptor_stop();
2562 lnet_ping_target_update(pinfo, md_handle);
2565 mutex_unlock(&the_lnet.ln_api_mutex);
2570 void lnet_incr_dlc_seq(void)
2572 atomic_inc(&lnet_dlc_seq_no);
2575 __u32 lnet_get_dlc_seq_locked(void)
2577 return atomic_read(&lnet_dlc_seq_no);
2581 * LNet ioctl handler.
2585 LNetCtl(unsigned int cmd, void *arg)
2587 struct libcfs_ioctl_data *data = arg;
2588 struct lnet_ioctl_config_data *config;
2589 lnet_process_id_t id = {0};
2593 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
2594 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
2597 case IOC_LIBCFS_GET_NI:
2598 rc = LNetGetId(data->ioc_count, &id);
2599 data->ioc_nid = id.nid;
2602 case IOC_LIBCFS_FAIL_NID:
2603 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
2605 case IOC_LIBCFS_ADD_ROUTE:
2608 if (config->cfg_hdr.ioc_len < sizeof(*config))
2611 mutex_lock(&the_lnet.ln_api_mutex);
2612 rc = lnet_add_route(config->cfg_net,
2613 config->cfg_config_u.cfg_route.rtr_hop,
2615 config->cfg_config_u.cfg_route.
2618 rc = lnet_check_routes();
2620 lnet_del_route(config->cfg_net,
2623 mutex_unlock(&the_lnet.ln_api_mutex);
2626 case IOC_LIBCFS_DEL_ROUTE:
2629 if (config->cfg_hdr.ioc_len < sizeof(*config))
2632 mutex_lock(&the_lnet.ln_api_mutex);
2633 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
2634 mutex_unlock(&the_lnet.ln_api_mutex);
2637 case IOC_LIBCFS_GET_ROUTE:
2640 if (config->cfg_hdr.ioc_len < sizeof(*config))
2643 return lnet_get_route(config->cfg_count,
2645 &config->cfg_config_u.cfg_route.rtr_hop,
2647 &config->cfg_config_u.cfg_route.rtr_flags,
2648 &config->cfg_config_u.cfg_route.
2651 case IOC_LIBCFS_GET_LOCAL_NI: {
2652 struct lnet_ioctl_config_ni *cfg_ni;
2653 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2657 /* get the tunables if they are available */
2658 if (cfg_ni->lic_cfg_hdr.ioc_len <
2659 sizeof(*cfg_ni) + sizeof(*tun))
2662 tun = (struct lnet_ioctl_config_lnd_tunables *)
2665 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni);
2667 return lnet_get_ni_config(cfg_ni, tun, tun_size);
2670 case IOC_LIBCFS_GET_NET: {
2671 size_t total = sizeof(*config) +
2672 sizeof(struct lnet_ioctl_net_config);
2675 if (config->cfg_hdr.ioc_len < total)
2678 return lnet_get_net_config(config);
2681 case IOC_LIBCFS_GET_LNET_STATS:
2683 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
2685 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
2688 lnet_counters_get(&lnet_stats->st_cntrs);
2692 case IOC_LIBCFS_CONFIG_RTR:
2695 if (config->cfg_hdr.ioc_len < sizeof(*config))
2698 mutex_lock(&the_lnet.ln_api_mutex);
2699 if (config->cfg_config_u.cfg_buffers.buf_enable) {
2700 rc = lnet_rtrpools_enable();
2701 mutex_unlock(&the_lnet.ln_api_mutex);
2704 lnet_rtrpools_disable();
2705 mutex_unlock(&the_lnet.ln_api_mutex);
2708 case IOC_LIBCFS_ADD_BUF:
2711 if (config->cfg_hdr.ioc_len < sizeof(*config))
2714 mutex_lock(&the_lnet.ln_api_mutex);
2715 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
2717 config->cfg_config_u.cfg_buffers.
2719 config->cfg_config_u.cfg_buffers.
2721 mutex_unlock(&the_lnet.ln_api_mutex);
2724 case IOC_LIBCFS_GET_BUF: {
2725 struct lnet_ioctl_pool_cfg *pool_cfg;
2726 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2730 if (config->cfg_hdr.ioc_len < total)
2733 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2734 return lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2737 case IOC_LIBCFS_ADD_PEER_NI: {
2738 struct lnet_ioctl_peer_cfg *cfg = arg;
2740 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
2743 return lnet_add_peer_ni_to_peer(cfg->prcfg_key_nid,
2744 cfg->prcfg_cfg_nid);
2747 case IOC_LIBCFS_DEL_PEER_NI: {
2748 struct lnet_ioctl_peer_cfg *cfg = arg;
2750 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
2753 return lnet_del_peer_ni_from_peer(cfg->prcfg_key_nid,
2754 cfg->prcfg_cfg_nid);
2757 case IOC_LIBCFS_GET_PEER_INFO: {
2758 struct lnet_ioctl_peer *peer_info = arg;
2760 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2763 return lnet_get_peer_ni_info(
2764 peer_info->pr_count,
2766 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2767 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2768 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2769 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2770 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2771 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2772 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_rtr_credits,
2773 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2776 case IOC_LIBCFS_GET_PEER_NI: {
2777 struct lnet_ioctl_peer_cfg *cfg = arg;
2778 struct lnet_peer_ni_credit_info *lpni_cri;
2779 size_t total = sizeof(*cfg) + sizeof(*lpni_cri);
2781 if (cfg->prcfg_hdr.ioc_len < total)
2784 lpni_cri = (struct lnet_peer_ni_credit_info*) cfg->prcfg_bulk;
2786 return lnet_get_peer_info(cfg->prcfg_idx, &cfg->prcfg_key_nid,
2787 &cfg->prcfg_cfg_nid, lpni_cri);
2790 case IOC_LIBCFS_NOTIFY_ROUTER: {
2791 unsigned long jiffies_passed;
2793 jiffies_passed = ktime_get_real_seconds() - data->ioc_u64[0];
2794 jiffies_passed = cfs_time_seconds(jiffies_passed);
2796 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2797 jiffies - jiffies_passed);
2800 case IOC_LIBCFS_LNET_DIST:
2801 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2802 if (rc < 0 && rc != -EHOSTUNREACH)
2805 data->ioc_u32[0] = rc;
2808 case IOC_LIBCFS_TESTPROTOCOMPAT:
2809 lnet_net_lock(LNET_LOCK_EX);
2810 the_lnet.ln_testprotocompat = data->ioc_flags;
2811 lnet_net_unlock(LNET_LOCK_EX);
2814 case IOC_LIBCFS_LNET_FAULT:
2815 return lnet_fault_ctl(data->ioc_flags, data);
2817 case IOC_LIBCFS_PING: {
2818 signed long timeout;
2820 id.nid = data->ioc_nid;
2821 id.pid = data->ioc_u32[0];
2823 /* Don't block longer than 2 minutes */
2824 if (data->ioc_u32[1] > 120 * MSEC_PER_SEC)
2827 /* If timestamp is negative then disable timeout */
2828 if ((s32)data->ioc_u32[1] < 0)
2829 timeout = MAX_SCHEDULE_TIMEOUT;
2831 timeout = msecs_to_jiffies(data->ioc_u32[1]);
2833 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
2834 data->ioc_plen1 / sizeof(lnet_process_id_t));
2837 data->ioc_count = rc;
2841 case IOC_LIBCFS_DBG: {
2842 struct lnet_ioctl_dbg *dbg = arg;
2843 struct lnet_dbg_task_info *dbg_info;
2844 size_t total = sizeof(*dbg) + sizeof(*dbg_info);
2846 if (dbg->dbg_hdr.ioc_len < total)
2849 dbg_info = (struct lnet_dbg_task_info*) dbg->dbg_bulk;
2851 return lnet_handle_dbg_task(dbg, dbg_info);
2855 ni = lnet_net2ni_addref(data->ioc_net);
2859 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
2862 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
2869 EXPORT_SYMBOL(LNetCtl);
2871 void LNetDebugPeer(lnet_process_id_t id)
2873 lnet_debug_peer(id.nid);
2875 EXPORT_SYMBOL(LNetDebugPeer);
2878 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
2879 * all interfaces share a same PID, as requested by LNetNIInit().
2881 * \param index Index of the interface to look up.
2882 * \param id On successful return, this location will hold the
2883 * lnet_process_id_t ID of the interface.
2885 * \retval 0 If an interface exists at \a index.
2886 * \retval -ENOENT If no interface has been found.
2889 LNetGetId(unsigned int index, lnet_process_id_t *id)
2892 struct lnet_net *net;
2896 LASSERT(the_lnet.ln_refcount > 0);
2898 cpt = lnet_net_lock_current();
2900 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2901 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2905 id->nid = ni->ni_nid;
2906 id->pid = the_lnet.ln_pid;
2912 lnet_net_unlock(cpt);
2915 EXPORT_SYMBOL(LNetGetId);
2918 * Print a string representation of handle \a h into buffer \a str of
2922 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
2924 snprintf(str, len, "%#llx", h.cookie);
2926 EXPORT_SYMBOL(LNetSnprintHandle);
2928 static int lnet_ping(lnet_process_id_t id, signed long timeout,
2929 lnet_process_id_t __user *ids, int n_ids)
2931 lnet_handle_eq_t eqh;
2932 lnet_handle_md_t mdh;
2934 lnet_md_t md = { NULL };
2938 const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
2940 struct lnet_ping_info *info;
2941 lnet_process_id_t tmpid;
2948 infosz = offsetof(struct lnet_ping_info, pi_ni[n_ids]);
2950 /* n_ids limit is arbitrary */
2951 if (n_ids <= 0 || n_ids > 20 || id.nid == LNET_NID_ANY)
2954 if (id.pid == LNET_PID_ANY)
2955 id.pid = LNET_PID_LUSTRE;
2957 LIBCFS_ALLOC(info, infosz);
2961 /* NB 2 events max (including any unlink event) */
2962 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
2964 CERROR("Can't allocate EQ: %d\n", rc);
2968 /* initialize md content */
2971 md.threshold = 2; /*GET/REPLY*/
2973 md.options = LNET_MD_TRUNCATE;
2977 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
2979 CERROR("Can't bind MD: %d\n", rc);
2983 rc = LNetGet(LNET_NID_ANY, mdh, id,
2984 LNET_RESERVED_PORTAL,
2985 LNET_PROTO_PING_MATCHBITS, 0);
2988 /* Don't CERROR; this could be deliberate! */
2990 rc2 = LNetMDUnlink(mdh);
2993 /* NB must wait for the UNLINK event below... */
2995 timeout = a_long_time;
2999 /* MUST block for unlink to complete */
3001 blocked = cfs_block_allsigs();
3003 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
3006 cfs_restore_sigs(blocked);
3008 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
3009 (rc2 <= 0) ? -1 : event.type,
3010 (rc2 <= 0) ? -1 : event.status,
3011 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
3013 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
3015 if (rc2 <= 0 || event.status != 0) {
3016 /* timeout or error */
3017 if (!replied && rc == 0)
3018 rc = (rc2 < 0) ? rc2 :
3019 (rc2 == 0) ? -ETIMEDOUT :
3023 /* Ensure completion in finite time... */
3025 /* No assertion (racing with network) */
3027 timeout = a_long_time;
3028 } else if (rc2 == 0) {
3029 /* timed out waiting for unlink */
3030 CWARN("ping %s: late network completion\n",
3033 } else if (event.type == LNET_EVENT_REPLY) {
3038 } while (rc2 <= 0 || !event.unlinked);
3042 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
3049 LASSERT(nob >= 0 && nob <= infosz);
3051 rc = -EPROTO; /* if I can't parse... */
3054 /* can't check magic/version */
3055 CERROR("%s: ping info too short %d\n",
3056 libcfs_id2str(id), nob);
3060 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
3061 lnet_swap_pinginfo(info);
3062 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
3063 CERROR("%s: Unexpected magic %08x\n",
3064 libcfs_id2str(id), info->pi_magic);
3068 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
3069 CERROR("%s: ping w/o NI status: 0x%x\n",
3070 libcfs_id2str(id), info->pi_features);
3074 if (nob < offsetof(struct lnet_ping_info, pi_ni[0])) {
3075 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
3076 nob, (int)offsetof(struct lnet_ping_info, pi_ni[0]));
3080 if (info->pi_nnis < n_ids)
3081 n_ids = info->pi_nnis;
3083 if (nob < offsetof(struct lnet_ping_info, pi_ni[n_ids])) {
3084 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
3085 nob, (int)offsetof(struct lnet_ping_info, pi_ni[n_ids]));
3089 rc = -EFAULT; /* If I SEGV... */
3091 memset(&tmpid, 0, sizeof(tmpid));
3092 for (i = 0; i < n_ids; i++) {
3093 tmpid.pid = info->pi_pid;
3094 tmpid.nid = info->pi_ni[i].ns_nid;
3095 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
3101 rc2 = LNetEQFree(eqh);
3103 CERROR("rc2 %d\n", rc2);
3107 LIBCFS_FREE(info, infosz);