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, 2017, 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
35 #include <linux/ctype.h>
36 #include <linux/log2.h>
37 #include <linux/ktime.h>
38 #include <linux/moduleparam.h>
39 #include <linux/uaccess.h>
41 #include <lnet/lib-lnet.h>
43 #define D_LNI D_CONSOLE
46 * initialize ln_api_mutex statically, since it needs to be used in
47 * discovery_set callback. That module parameter callback can be called
48 * before module init completes. The mutex needs to be ready for use then.
50 struct lnet the_lnet = {
51 .ln_api_mutex = __MUTEX_INITIALIZER(the_lnet.ln_api_mutex),
52 }; /* THE state of the network */
53 EXPORT_SYMBOL(the_lnet);
55 static char *ip2nets = "";
56 module_param(ip2nets, charp, 0444);
57 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
59 static char *networks = "";
60 module_param(networks, charp, 0444);
61 MODULE_PARM_DESC(networks, "local networks");
63 static char *routes = "";
64 module_param(routes, charp, 0444);
65 MODULE_PARM_DESC(routes, "routes to non-local networks");
67 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
68 module_param(rnet_htable_size, int, 0444);
69 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
71 static int use_tcp_bonding = false;
72 module_param(use_tcp_bonding, int, 0444);
73 MODULE_PARM_DESC(use_tcp_bonding,
74 "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
76 unsigned int lnet_numa_range = 0;
77 module_param(lnet_numa_range, uint, 0444);
78 MODULE_PARM_DESC(lnet_numa_range,
79 "NUMA range to consider during Multi-Rail selection");
82 * lnet_health_sensitivity determines by how much we decrement the health
83 * value on sending error. The value defaults to 0, which means health
84 * checking is turned off by default.
86 unsigned int lnet_health_sensitivity = 0;
87 static int sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
88 static struct kernel_param_ops param_ops_health_sensitivity = {
89 .set = sensitivity_set,
92 #define param_check_health_sensitivity(name, p) \
93 __param_check(name, p, int)
94 #ifdef HAVE_KERNEL_PARAM_OPS
95 module_param(lnet_health_sensitivity, health_sensitivity, S_IRUGO|S_IWUSR);
97 module_param_call(lnet_health_sensitivity, sensitivity_set, param_get_int,
98 &lnet_health_sensitivity, S_IRUGO|S_IWUSR);
100 MODULE_PARM_DESC(lnet_health_sensitivity,
101 "Value to decrement the health value by on error");
103 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
104 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
106 static struct kernel_param_ops param_ops_interfaces_max = {
108 .get = param_get_int,
111 #define param_check_interfaces_max(name, p) \
112 __param_check(name, p, int)
114 #ifdef HAVE_KERNEL_PARAM_OPS
115 module_param(lnet_interfaces_max, interfaces_max, 0644);
117 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
118 ¶m_ops_interfaces_max, 0644);
120 MODULE_PARM_DESC(lnet_interfaces_max,
121 "Maximum number of interfaces in a node.");
123 unsigned lnet_peer_discovery_disabled = 0;
124 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
126 static struct kernel_param_ops param_ops_discovery_disabled = {
127 .set = discovery_set,
128 .get = param_get_int,
131 #define param_check_discovery_disabled(name, p) \
132 __param_check(name, p, int)
133 #ifdef HAVE_KERNEL_PARAM_OPS
134 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
136 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
137 ¶m_ops_discovery_disabled, 0644);
139 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
140 "Set to 1 to disable peer discovery on this node.");
142 unsigned lnet_transaction_timeout = 5;
143 static int transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp);
144 static struct kernel_param_ops param_ops_transaction_timeout = {
145 .set = transaction_to_set,
146 .get = param_get_int,
149 #define param_check_transaction_timeout(name, p) \
150 __param_check(name, p, int)
151 #ifdef HAVE_KERNEL_PARAM_OPS
152 module_param(lnet_transaction_timeout, transaction_timeout, S_IRUGO|S_IWUSR);
154 module_param_call(lnet_transaction_timeout, transaction_to_set, param_get_int,
155 &lnet_transaction_timeout, S_IRUGO|S_IWUSR);
157 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
158 "Set to 1 to disable peer discovery on this node.");
160 unsigned lnet_retry_count = 0;
161 static int retry_count_set(const char *val, cfs_kernel_param_arg_t *kp);
162 static struct kernel_param_ops param_ops_retry_count = {
163 .set = retry_count_set,
164 .get = param_get_int,
167 #define param_check_retry_count(name, p) \
168 __param_check(name, p, int)
169 #ifdef HAVE_KERNEL_PARAM_OPS
170 module_param(lnet_retry_count, retry_count, S_IRUGO|S_IWUSR);
172 module_param_call(lnet_retry_count, retry_count_set, param_get_int,
173 &lnet_retry_count, S_IRUGO|S_IWUSR);
175 MODULE_PARM_DESC(lnet_retry_count,
176 "Maximum number of times to retry transmitting a message");
179 unsigned lnet_lnd_timeout = LNET_LND_DEFAULT_TIMEOUT;
182 * This sequence number keeps track of how many times DLC was used to
183 * update the local NIs. It is incremented when a NI is added or
184 * removed and checked when sending a message to determine if there is
185 * a need to re-run the selection algorithm. See lnet_select_pathway()
186 * for more details on its usage.
188 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
190 static int lnet_ping(struct lnet_process_id id, signed long timeout,
191 struct lnet_process_id __user *ids, int n_ids);
193 static int lnet_discover(struct lnet_process_id id, __u32 force,
194 struct lnet_process_id __user *ids, int n_ids);
197 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
200 unsigned *sensitivity = (unsigned *)kp->arg;
203 rc = kstrtoul(val, 0, &value);
205 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
210 * The purpose of locking the api_mutex here is to ensure that
211 * the correct value ends up stored properly.
213 mutex_lock(&the_lnet.ln_api_mutex);
215 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
216 mutex_unlock(&the_lnet.ln_api_mutex);
220 if (value == *sensitivity) {
221 mutex_unlock(&the_lnet.ln_api_mutex);
225 *sensitivity = value;
227 mutex_unlock(&the_lnet.ln_api_mutex);
233 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
236 unsigned *discovery = (unsigned *)kp->arg;
238 struct lnet_ping_buffer *pbuf;
240 rc = kstrtoul(val, 0, &value);
242 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
246 value = (value) ? 1 : 0;
249 * The purpose of locking the api_mutex here is to ensure that
250 * the correct value ends up stored properly.
252 mutex_lock(&the_lnet.ln_api_mutex);
254 if (value == *discovery) {
255 mutex_unlock(&the_lnet.ln_api_mutex);
261 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
262 mutex_unlock(&the_lnet.ln_api_mutex);
266 /* tell peers that discovery setting has changed */
267 lnet_net_lock(LNET_LOCK_EX);
268 pbuf = the_lnet.ln_ping_target;
270 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
272 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
273 lnet_net_unlock(LNET_LOCK_EX);
275 lnet_push_update_to_peers(1);
277 mutex_unlock(&the_lnet.ln_api_mutex);
283 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
286 unsigned *transaction_to = (unsigned *)kp->arg;
289 rc = kstrtoul(val, 0, &value);
291 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
296 * The purpose of locking the api_mutex here is to ensure that
297 * the correct value ends up stored properly.
299 mutex_lock(&the_lnet.ln_api_mutex);
301 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
302 mutex_unlock(&the_lnet.ln_api_mutex);
306 if (value < lnet_retry_count || value == 0) {
307 mutex_unlock(&the_lnet.ln_api_mutex);
308 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
309 "Has to be greater than lnet_retry_count (%u)\n",
310 value, lnet_retry_count);
314 if (value == *transaction_to) {
315 mutex_unlock(&the_lnet.ln_api_mutex);
319 *transaction_to = value;
320 if (lnet_retry_count == 0)
321 lnet_lnd_timeout = value;
323 lnet_lnd_timeout = value / lnet_retry_count;
325 mutex_unlock(&the_lnet.ln_api_mutex);
331 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
334 unsigned *retry_count = (unsigned *)kp->arg;
337 rc = kstrtoul(val, 0, &value);
339 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
344 * The purpose of locking the api_mutex here is to ensure that
345 * the correct value ends up stored properly.
347 mutex_lock(&the_lnet.ln_api_mutex);
349 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
350 mutex_unlock(&the_lnet.ln_api_mutex);
354 if (value > lnet_transaction_timeout) {
355 mutex_unlock(&the_lnet.ln_api_mutex);
356 CERROR("Invalid value for lnet_retry_count (%lu). "
357 "Has to be smaller than lnet_transaction_timeout (%u)\n",
358 value, lnet_transaction_timeout);
362 if (value == *retry_count) {
363 mutex_unlock(&the_lnet.ln_api_mutex);
367 *retry_count = value;
370 lnet_lnd_timeout = lnet_transaction_timeout;
372 lnet_lnd_timeout = lnet_transaction_timeout / value;
374 mutex_unlock(&the_lnet.ln_api_mutex);
380 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
384 rc = kstrtoint(val, 0, &value);
386 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
390 if (value < LNET_INTERFACES_MIN) {
391 CWARN("max interfaces provided are too small, setting to %d\n",
392 LNET_INTERFACES_MAX_DEFAULT);
393 value = LNET_INTERFACES_MAX_DEFAULT;
396 *(int *)kp->arg = value;
402 lnet_get_routes(void)
408 lnet_get_networks(void)
413 if (*networks != 0 && *ip2nets != 0) {
414 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
415 "'ip2nets' but not both at once\n");
420 rc = lnet_parse_ip2nets(&nets, ip2nets);
421 return (rc == 0) ? nets : NULL;
431 lnet_init_locks(void)
433 spin_lock_init(&the_lnet.ln_eq_wait_lock);
434 spin_lock_init(&the_lnet.ln_msg_resend_lock);
435 init_waitqueue_head(&the_lnet.ln_eq_waitq);
436 init_waitqueue_head(&the_lnet.ln_mt_waitq);
437 mutex_init(&the_lnet.ln_lnd_mutex);
441 lnet_fini_locks(void)
445 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
446 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
450 lnet_descriptor_setup(void)
452 /* create specific kmem_cache for MEs and small MDs (i.e., originally
453 * allocated in <size-xxx> kmem_cache).
455 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
457 if (!lnet_mes_cachep)
460 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
461 LNET_SMALL_MD_SIZE, 0, 0,
463 if (!lnet_small_mds_cachep)
470 lnet_descriptor_cleanup(void)
473 if (lnet_small_mds_cachep) {
474 kmem_cache_destroy(lnet_small_mds_cachep);
475 lnet_small_mds_cachep = NULL;
478 if (lnet_mes_cachep) {
479 kmem_cache_destroy(lnet_mes_cachep);
480 lnet_mes_cachep = NULL;
485 lnet_create_remote_nets_table(void)
488 struct list_head *hash;
490 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
491 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
492 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
494 CERROR("Failed to create remote nets hash table\n");
498 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
499 INIT_LIST_HEAD(&hash[i]);
500 the_lnet.ln_remote_nets_hash = hash;
505 lnet_destroy_remote_nets_table(void)
509 if (the_lnet.ln_remote_nets_hash == NULL)
512 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
513 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
515 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
516 LNET_REMOTE_NETS_HASH_SIZE *
517 sizeof(the_lnet.ln_remote_nets_hash[0]));
518 the_lnet.ln_remote_nets_hash = NULL;
522 lnet_destroy_locks(void)
524 if (the_lnet.ln_res_lock != NULL) {
525 cfs_percpt_lock_free(the_lnet.ln_res_lock);
526 the_lnet.ln_res_lock = NULL;
529 if (the_lnet.ln_net_lock != NULL) {
530 cfs_percpt_lock_free(the_lnet.ln_net_lock);
531 the_lnet.ln_net_lock = NULL;
538 lnet_create_locks(void)
542 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
543 if (the_lnet.ln_res_lock == NULL)
546 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
547 if (the_lnet.ln_net_lock == NULL)
553 lnet_destroy_locks();
557 static void lnet_assert_wire_constants(void)
559 /* Wire protocol assertions generated by 'wirecheck'
560 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
561 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
562 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
565 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
566 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
567 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
568 CLASSERT(LNET_MSG_ACK == 0);
569 CLASSERT(LNET_MSG_PUT == 1);
570 CLASSERT(LNET_MSG_GET == 2);
571 CLASSERT(LNET_MSG_REPLY == 3);
572 CLASSERT(LNET_MSG_HELLO == 4);
574 /* Checks for struct lnet_handle_wire */
575 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
576 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
577 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
578 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
579 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
581 /* Checks for struct struct lnet_magicversion */
582 CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
583 CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
584 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
585 CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
586 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
587 CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
588 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
590 /* Checks for struct struct lnet_hdr */
591 CLASSERT((int)sizeof(struct lnet_hdr) == 72);
592 CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
593 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
594 CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
595 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
596 CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
597 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
598 CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
599 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
600 CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
601 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
602 CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
603 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
604 CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
605 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
608 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
609 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
610 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
611 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
612 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
613 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
616 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
617 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
618 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
619 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
620 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
621 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
622 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
623 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
624 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
625 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
628 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
629 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
630 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
631 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
632 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
633 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
634 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
635 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
636 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
637 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
640 CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
641 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
644 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
645 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
646 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
647 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
649 /* Checks for struct lnet_ni_status and related constants */
650 CLASSERT(LNET_NI_STATUS_INVALID == 0x00000000);
651 CLASSERT(LNET_NI_STATUS_UP == 0x15aac0de);
652 CLASSERT(LNET_NI_STATUS_DOWN == 0xdeadface);
654 /* Checks for struct lnet_ni_status */
655 CLASSERT((int)sizeof(struct lnet_ni_status) == 16);
656 CLASSERT((int)offsetof(struct lnet_ni_status, ns_nid) == 0);
657 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) == 8);
658 CLASSERT((int)offsetof(struct lnet_ni_status, ns_status) == 8);
659 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_status) == 4);
660 CLASSERT((int)offsetof(struct lnet_ni_status, ns_unused) == 12);
661 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) == 4);
663 /* Checks for struct lnet_ping_info and related constants */
664 CLASSERT(LNET_PROTO_PING_MAGIC == 0x70696E67);
665 CLASSERT(LNET_PING_FEAT_INVAL == 0);
666 CLASSERT(LNET_PING_FEAT_BASE == 1);
667 CLASSERT(LNET_PING_FEAT_NI_STATUS == 2);
668 CLASSERT(LNET_PING_FEAT_RTE_DISABLED == 4);
669 CLASSERT(LNET_PING_FEAT_MULTI_RAIL == 8);
670 CLASSERT(LNET_PING_FEAT_DISCOVERY == 16);
671 CLASSERT(LNET_PING_FEAT_BITS == 31);
673 /* Checks for struct lnet_ping_info */
674 CLASSERT((int)sizeof(struct lnet_ping_info) == 16);
675 CLASSERT((int)offsetof(struct lnet_ping_info, pi_magic) == 0);
676 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) == 4);
677 CLASSERT((int)offsetof(struct lnet_ping_info, pi_features) == 4);
678 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_features) == 4);
679 CLASSERT((int)offsetof(struct lnet_ping_info, pi_pid) == 8);
680 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) == 4);
681 CLASSERT((int)offsetof(struct lnet_ping_info, pi_nnis) == 12);
682 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) == 4);
683 CLASSERT((int)offsetof(struct lnet_ping_info, pi_ni) == 16);
684 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) == 0);
687 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
689 struct lnet_lnd *lnd;
690 struct list_head *tmp;
692 /* holding lnd mutex */
693 list_for_each(tmp, &the_lnet.ln_lnds) {
694 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
696 if (lnd->lnd_type == type)
703 lnet_get_lnd_timeout(void)
705 return lnet_lnd_timeout;
707 EXPORT_SYMBOL(lnet_get_lnd_timeout);
710 lnet_register_lnd(struct lnet_lnd *lnd)
712 mutex_lock(&the_lnet.ln_lnd_mutex);
714 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
715 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
717 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
718 lnd->lnd_refcount = 0;
720 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
722 mutex_unlock(&the_lnet.ln_lnd_mutex);
724 EXPORT_SYMBOL(lnet_register_lnd);
727 lnet_unregister_lnd(struct lnet_lnd *lnd)
729 mutex_lock(&the_lnet.ln_lnd_mutex);
731 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
732 LASSERT(lnd->lnd_refcount == 0);
734 list_del(&lnd->lnd_list);
735 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
737 mutex_unlock(&the_lnet.ln_lnd_mutex);
739 EXPORT_SYMBOL(lnet_unregister_lnd);
742 lnet_counters_get(struct lnet_counters *counters)
744 struct lnet_counters *ctr;
747 memset(counters, 0, sizeof(*counters));
749 lnet_net_lock(LNET_LOCK_EX);
751 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
752 counters->msgs_max += ctr->msgs_max;
753 counters->msgs_alloc += ctr->msgs_alloc;
754 counters->errors += ctr->errors;
755 counters->send_count += ctr->send_count;
756 counters->recv_count += ctr->recv_count;
757 counters->route_count += ctr->route_count;
758 counters->drop_count += ctr->drop_count;
759 counters->send_length += ctr->send_length;
760 counters->recv_length += ctr->recv_length;
761 counters->route_length += ctr->route_length;
762 counters->drop_length += ctr->drop_length;
765 lnet_net_unlock(LNET_LOCK_EX);
767 EXPORT_SYMBOL(lnet_counters_get);
770 lnet_counters_reset(void)
772 struct lnet_counters *counters;
775 lnet_net_lock(LNET_LOCK_EX);
777 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
778 memset(counters, 0, sizeof(struct lnet_counters));
780 lnet_net_unlock(LNET_LOCK_EX);
784 lnet_res_type2str(int type)
789 case LNET_COOKIE_TYPE_MD:
791 case LNET_COOKIE_TYPE_ME:
793 case LNET_COOKIE_TYPE_EQ:
799 lnet_res_container_cleanup(struct lnet_res_container *rec)
803 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
806 while (!list_empty(&rec->rec_active)) {
807 struct list_head *e = rec->rec_active.next;
810 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
811 lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
813 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
814 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
816 } else { /* NB: Active MEs should be attached on portals */
823 /* Found alive MD/ME/EQ, user really should unlink/free
824 * all of them before finalize LNet, but if someone didn't,
825 * we have to recycle garbage for him */
826 CERROR("%d active elements on exit of %s container\n",
827 count, lnet_res_type2str(rec->rec_type));
830 if (rec->rec_lh_hash != NULL) {
831 LIBCFS_FREE(rec->rec_lh_hash,
832 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
833 rec->rec_lh_hash = NULL;
836 rec->rec_type = 0; /* mark it as finalized */
840 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
845 LASSERT(rec->rec_type == 0);
847 rec->rec_type = type;
848 INIT_LIST_HEAD(&rec->rec_active);
850 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
852 /* Arbitrary choice of hash table size */
853 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
854 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
855 if (rec->rec_lh_hash == NULL) {
860 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
861 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
866 CERROR("Failed to setup %s resource container\n",
867 lnet_res_type2str(type));
868 lnet_res_container_cleanup(rec);
873 lnet_res_containers_destroy(struct lnet_res_container **recs)
875 struct lnet_res_container *rec;
878 cfs_percpt_for_each(rec, i, recs)
879 lnet_res_container_cleanup(rec);
881 cfs_percpt_free(recs);
884 static struct lnet_res_container **
885 lnet_res_containers_create(int type)
887 struct lnet_res_container **recs;
888 struct lnet_res_container *rec;
892 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
894 CERROR("Failed to allocate %s resource containers\n",
895 lnet_res_type2str(type));
899 cfs_percpt_for_each(rec, i, recs) {
900 rc = lnet_res_container_setup(rec, i, type);
902 lnet_res_containers_destroy(recs);
910 struct lnet_libhandle *
911 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
913 /* ALWAYS called with lnet_res_lock held */
914 struct list_head *head;
915 struct lnet_libhandle *lh;
918 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
921 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
922 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
924 list_for_each_entry(lh, head, lh_hash_chain) {
925 if (lh->lh_cookie == cookie)
933 lnet_res_lh_initialize(struct lnet_res_container *rec,
934 struct lnet_libhandle *lh)
936 /* ALWAYS called with lnet_res_lock held */
937 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
940 lh->lh_cookie = rec->rec_lh_cookie;
941 rec->rec_lh_cookie += 1 << ibits;
943 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
945 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
948 static int lnet_unprepare(void);
951 lnet_prepare(lnet_pid_t requested_pid)
953 /* Prepare to bring up the network */
954 struct lnet_res_container **recs;
957 if (requested_pid == LNET_PID_ANY) {
958 /* Don't instantiate LNET just for me */
962 LASSERT(the_lnet.ln_refcount == 0);
964 the_lnet.ln_routing = 0;
966 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
967 the_lnet.ln_pid = requested_pid;
969 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
970 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
971 INIT_LIST_HEAD(&the_lnet.ln_nets);
972 INIT_LIST_HEAD(&the_lnet.ln_routers);
973 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
974 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
975 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
976 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
977 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
978 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
979 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
980 init_waitqueue_head(&the_lnet.ln_dc_waitq);
982 rc = lnet_descriptor_setup();
986 rc = lnet_create_remote_nets_table();
991 * NB the interface cookie in wire handles guards against delayed
992 * replies and ACKs appearing valid after reboot.
994 the_lnet.ln_interface_cookie = ktime_get_real_ns();
996 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
997 sizeof(struct lnet_counters));
998 if (the_lnet.ln_counters == NULL) {
999 CERROR("Failed to allocate counters for LNet\n");
1004 rc = lnet_peer_tables_create();
1008 rc = lnet_msg_containers_create();
1012 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1013 LNET_COOKIE_TYPE_EQ);
1017 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
1023 the_lnet.ln_me_containers = recs;
1025 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1031 the_lnet.ln_md_containers = recs;
1033 rc = lnet_portals_create();
1035 CERROR("Failed to create portals for LNet: %d\n", rc);
1047 lnet_unprepare (void)
1049 /* NB no LNET_LOCK since this is the last reference. All LND instances
1050 * have shut down already, so it is safe to unlink and free all
1051 * descriptors, even those that appear committed to a network op (eg MD
1052 * with non-zero pending count) */
1054 lnet_fail_nid(LNET_NID_ANY, 0);
1056 LASSERT(the_lnet.ln_refcount == 0);
1057 LASSERT(list_empty(&the_lnet.ln_test_peers));
1058 LASSERT(list_empty(&the_lnet.ln_nets));
1060 lnet_portals_destroy();
1062 if (the_lnet.ln_md_containers != NULL) {
1063 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1064 the_lnet.ln_md_containers = NULL;
1067 if (the_lnet.ln_me_containers != NULL) {
1068 lnet_res_containers_destroy(the_lnet.ln_me_containers);
1069 the_lnet.ln_me_containers = NULL;
1072 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1074 lnet_msg_containers_destroy();
1076 lnet_rtrpools_free(0);
1078 if (the_lnet.ln_counters != NULL) {
1079 cfs_percpt_free(the_lnet.ln_counters);
1080 the_lnet.ln_counters = NULL;
1082 lnet_destroy_remote_nets_table();
1083 lnet_descriptor_cleanup();
1089 lnet_net2ni_locked(__u32 net_id, int cpt)
1092 struct lnet_net *net;
1094 LASSERT(cpt != LNET_LOCK_EX);
1096 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1097 if (net->net_id == net_id) {
1098 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1108 lnet_net2ni_addref(__u32 net)
1113 ni = lnet_net2ni_locked(net, 0);
1115 lnet_ni_addref_locked(ni, 0);
1120 EXPORT_SYMBOL(lnet_net2ni_addref);
1123 lnet_get_net_locked(__u32 net_id)
1125 struct lnet_net *net;
1127 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1128 if (net->net_id == net_id)
1136 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1141 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1146 val = hash_long(key, LNET_CPT_BITS);
1147 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1151 return (unsigned int)(key + val + (val >> 1)) % number;
1155 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1157 struct lnet_net *net;
1159 /* must called with hold of lnet_net_lock */
1160 if (LNET_CPT_NUMBER == 1)
1161 return 0; /* the only one */
1164 * If NI is provided then use the CPT identified in the NI cpt
1165 * list if one exists. If one doesn't exist, then that NI is
1166 * associated with all CPTs and it follows that the net it belongs
1167 * to is implicitly associated with all CPTs, so just hash the nid
1171 if (ni->ni_cpts != NULL)
1172 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1175 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1178 /* no NI provided so look at the net */
1179 net = lnet_get_net_locked(LNET_NIDNET(nid));
1181 if (net != NULL && net->net_cpts != NULL) {
1182 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1185 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1189 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1194 if (LNET_CPT_NUMBER == 1)
1195 return 0; /* the only one */
1197 cpt = lnet_net_lock_current();
1199 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1201 lnet_net_unlock(cpt);
1205 EXPORT_SYMBOL(lnet_cpt_of_nid);
1208 lnet_islocalnet(__u32 net_id)
1210 struct lnet_net *net;
1214 cpt = lnet_net_lock_current();
1216 net = lnet_get_net_locked(net_id);
1218 local = net != NULL;
1220 lnet_net_unlock(cpt);
1226 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
1228 if (ni->ni_state & LNET_NI_STATE_ACTIVE)
1235 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1237 struct lnet_net *net;
1240 LASSERT(cpt != LNET_LOCK_EX);
1242 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1243 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1244 if (ni->ni_nid == nid)
1253 lnet_nid2ni_addref(lnet_nid_t nid)
1258 ni = lnet_nid2ni_locked(nid, 0);
1260 lnet_ni_addref_locked(ni, 0);
1265 EXPORT_SYMBOL(lnet_nid2ni_addref);
1268 lnet_islocalnid(lnet_nid_t nid)
1273 cpt = lnet_net_lock_current();
1274 ni = lnet_nid2ni_locked(nid, cpt);
1275 lnet_net_unlock(cpt);
1281 lnet_count_acceptor_nets(void)
1283 /* Return the # of NIs that need the acceptor. */
1285 struct lnet_net *net;
1288 cpt = lnet_net_lock_current();
1289 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1290 /* all socklnd type networks should have the acceptor
1292 if (net->net_lnd->lnd_accept != NULL)
1296 lnet_net_unlock(cpt);
1301 struct lnet_ping_buffer *
1302 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1304 struct lnet_ping_buffer *pbuf;
1306 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1308 pbuf->pb_nnis = nnis;
1309 atomic_set(&pbuf->pb_refcnt, 1);
1316 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1318 LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1319 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1322 static struct lnet_ping_buffer *
1323 lnet_ping_target_create(int nnis)
1325 struct lnet_ping_buffer *pbuf;
1327 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1329 CERROR("Can't allocate ping source [%d]\n", nnis);
1333 pbuf->pb_info.pi_nnis = nnis;
1334 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1335 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1336 pbuf->pb_info.pi_features =
1337 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1343 lnet_get_net_ni_count_locked(struct lnet_net *net)
1348 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1355 lnet_get_net_ni_count_pre(struct lnet_net *net)
1360 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1367 lnet_get_ni_count(void)
1370 struct lnet_net *net;
1375 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1376 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1386 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1390 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1392 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1394 /* Loopback is guaranteed to be present */
1395 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1397 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1403 lnet_ping_target_destroy(void)
1405 struct lnet_net *net;
1408 lnet_net_lock(LNET_LOCK_EX);
1410 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1411 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1413 ni->ni_status = NULL;
1418 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1419 the_lnet.ln_ping_target = NULL;
1421 lnet_net_unlock(LNET_LOCK_EX);
1425 lnet_ping_target_event_handler(struct lnet_event *event)
1427 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1429 if (event->unlinked)
1430 lnet_ping_buffer_decref(pbuf);
1434 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1435 struct lnet_handle_md *ping_mdh,
1436 int ni_count, bool set_eq)
1438 struct lnet_process_id id = {
1439 .nid = LNET_NID_ANY,
1442 struct lnet_handle_me me_handle;
1443 struct lnet_md md = { NULL };
1447 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1448 &the_lnet.ln_ping_target_eq);
1450 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1455 *ppbuf = lnet_ping_target_create(ni_count);
1456 if (*ppbuf == NULL) {
1461 /* Ping target ME/MD */
1462 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1463 LNET_PROTO_PING_MATCHBITS, 0,
1464 LNET_UNLINK, LNET_INS_AFTER,
1467 CERROR("Can't create ping target ME: %d\n", rc);
1468 goto fail_decref_ping_buffer;
1471 /* initialize md content */
1472 md.start = &(*ppbuf)->pb_info;
1473 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1474 md.threshold = LNET_MD_THRESH_INF;
1476 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1477 LNET_MD_MANAGE_REMOTE;
1478 md.eq_handle = the_lnet.ln_ping_target_eq;
1479 md.user_ptr = *ppbuf;
1481 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, ping_mdh);
1483 CERROR("Can't attach ping target MD: %d\n", rc);
1484 goto fail_unlink_ping_me;
1486 lnet_ping_buffer_addref(*ppbuf);
1490 fail_unlink_ping_me:
1491 rc2 = LNetMEUnlink(me_handle);
1493 fail_decref_ping_buffer:
1494 LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1495 lnet_ping_buffer_decref(*ppbuf);
1499 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1506 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1507 struct lnet_handle_md *ping_mdh)
1509 sigset_t blocked = cfs_block_allsigs();
1511 LNetMDUnlink(*ping_mdh);
1512 LNetInvalidateMDHandle(ping_mdh);
1514 /* NB the MD could be busy; this just starts the unlink */
1515 while (lnet_ping_buffer_numref(pbuf) > 1) {
1516 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1517 set_current_state(TASK_UNINTERRUPTIBLE);
1518 schedule_timeout(cfs_time_seconds(1));
1521 cfs_restore_sigs(blocked);
1525 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1528 struct lnet_net *net;
1529 struct lnet_ni_status *ns;
1534 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1535 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1536 LASSERT(i < pbuf->pb_nnis);
1538 ns = &pbuf->pb_info.pi_ni[i];
1540 ns->ns_nid = ni->ni_nid;
1543 ns->ns_status = (ni->ni_status != NULL) ?
1544 ni->ni_status->ns_status :
1553 * We (ab)use the ns_status of the loopback interface to
1554 * transmit the sequence number. The first interface listed
1555 * must be the loopback interface.
1557 rc = lnet_ping_info_validate(&pbuf->pb_info);
1559 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1562 LNET_PING_BUFFER_SEQNO(pbuf) =
1563 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1567 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1568 struct lnet_handle_md ping_mdh)
1570 struct lnet_ping_buffer *old_pbuf = NULL;
1571 struct lnet_handle_md old_ping_md;
1573 /* switch the NIs to point to the new ping info created */
1574 lnet_net_lock(LNET_LOCK_EX);
1576 if (!the_lnet.ln_routing)
1577 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1578 if (!lnet_peer_discovery_disabled)
1579 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1581 /* Ensure only known feature bits have been set. */
1582 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1583 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1585 lnet_ping_target_install_locked(pbuf);
1587 if (the_lnet.ln_ping_target) {
1588 old_pbuf = the_lnet.ln_ping_target;
1589 old_ping_md = the_lnet.ln_ping_target_md;
1591 the_lnet.ln_ping_target_md = ping_mdh;
1592 the_lnet.ln_ping_target = pbuf;
1594 lnet_net_unlock(LNET_LOCK_EX);
1597 /* unlink and free the old ping info */
1598 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1599 lnet_ping_buffer_decref(old_pbuf);
1602 lnet_push_update_to_peers(0);
1606 lnet_ping_target_fini(void)
1610 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1611 &the_lnet.ln_ping_target_md);
1613 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1616 lnet_ping_target_destroy();
1619 /* Resize the push target. */
1620 int lnet_push_target_resize(void)
1622 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1623 struct lnet_md md = { NULL };
1624 struct lnet_handle_me meh;
1625 struct lnet_handle_md mdh;
1626 struct lnet_handle_md old_mdh;
1627 struct lnet_ping_buffer *pbuf;
1628 struct lnet_ping_buffer *old_pbuf;
1629 int nnis = the_lnet.ln_push_target_nnis;
1637 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1643 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1644 LNET_PROTO_PING_MATCHBITS, 0,
1645 LNET_UNLINK, LNET_INS_AFTER,
1648 CERROR("Can't create push target ME: %d\n", rc);
1649 goto fail_decref_pbuf;
1652 /* initialize md content */
1653 md.start = &pbuf->pb_info;
1654 md.length = LNET_PING_INFO_SIZE(nnis);
1655 md.threshold = LNET_MD_THRESH_INF;
1657 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1658 LNET_MD_MANAGE_REMOTE;
1660 md.eq_handle = the_lnet.ln_push_target_eq;
1662 rc = LNetMDAttach(meh, md, LNET_RETAIN, &mdh);
1664 CERROR("Can't attach push MD: %d\n", rc);
1665 goto fail_unlink_meh;
1667 lnet_ping_buffer_addref(pbuf);
1669 lnet_net_lock(LNET_LOCK_EX);
1670 old_pbuf = the_lnet.ln_push_target;
1671 old_mdh = the_lnet.ln_push_target_md;
1672 the_lnet.ln_push_target = pbuf;
1673 the_lnet.ln_push_target_md = mdh;
1674 lnet_net_unlock(LNET_LOCK_EX);
1677 LNetMDUnlink(old_mdh);
1678 lnet_ping_buffer_decref(old_pbuf);
1681 if (nnis < the_lnet.ln_push_target_nnis)
1684 CDEBUG(D_NET, "nnis %d success\n", nnis);
1691 lnet_ping_buffer_decref(pbuf);
1693 CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1697 static void lnet_push_target_event_handler(struct lnet_event *ev)
1699 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1701 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1702 lnet_swap_pinginfo(pbuf);
1704 lnet_peer_push_event(ev);
1706 lnet_ping_buffer_decref(pbuf);
1709 /* Initialize the push target. */
1710 static int lnet_push_target_init(void)
1714 if (the_lnet.ln_push_target)
1717 rc = LNetEQAlloc(0, lnet_push_target_event_handler,
1718 &the_lnet.ln_push_target_eq);
1720 CERROR("Can't allocated push target EQ: %d\n", rc);
1724 /* Start at the required minimum, we'll enlarge if required. */
1725 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1727 rc = lnet_push_target_resize();
1730 LNetEQFree(the_lnet.ln_push_target_eq);
1731 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1737 /* Clean up the push target. */
1738 static void lnet_push_target_fini(void)
1740 if (!the_lnet.ln_push_target)
1743 /* Unlink and invalidate to prevent new references. */
1744 LNetMDUnlink(the_lnet.ln_push_target_md);
1745 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1747 /* Wait for the unlink to complete. */
1748 while (lnet_ping_buffer_numref(the_lnet.ln_push_target) > 1) {
1749 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1750 set_current_state(TASK_UNINTERRUPTIBLE);
1751 schedule_timeout(cfs_time_seconds(1));
1754 lnet_ping_buffer_decref(the_lnet.ln_push_target);
1755 the_lnet.ln_push_target = NULL;
1756 the_lnet.ln_push_target_nnis = 0;
1758 LNetEQFree(the_lnet.ln_push_target_eq);
1759 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1763 lnet_ni_tq_credits(struct lnet_ni *ni)
1767 LASSERT(ni->ni_ncpts >= 1);
1769 if (ni->ni_ncpts == 1)
1770 return ni->ni_net->net_tunables.lct_max_tx_credits;
1772 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1773 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1774 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1780 lnet_ni_unlink_locked(struct lnet_ni *ni)
1782 if (!list_empty(&ni->ni_cptlist)) {
1783 list_del_init(&ni->ni_cptlist);
1784 lnet_ni_decref_locked(ni, 0);
1787 /* move it to zombie list and nobody can find it anymore */
1788 LASSERT(!list_empty(&ni->ni_netlist));
1789 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1790 lnet_ni_decref_locked(ni, 0);
1794 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1799 struct list_head *zombie_list = &net->net_ni_zombie;
1802 * Now wait for the NIs I just nuked to show up on the zombie
1803 * list and shut them down in guaranteed thread context
1806 while (!list_empty(zombie_list)) {
1810 ni = list_entry(zombie_list->next,
1811 struct lnet_ni, ni_netlist);
1812 list_del_init(&ni->ni_netlist);
1813 /* the ni should be in deleting state. If it's not it's
1815 LASSERT(ni->ni_state & LNET_NI_STATE_DELETING);
1816 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1819 /* still busy, add it back to zombie list */
1820 list_add(&ni->ni_netlist, zombie_list);
1824 if (!list_empty(&ni->ni_netlist)) {
1825 lnet_net_unlock(LNET_LOCK_EX);
1827 if ((i & (-i)) == i) {
1829 "Waiting for zombie LNI %s\n",
1830 libcfs_nid2str(ni->ni_nid));
1832 set_current_state(TASK_UNINTERRUPTIBLE);
1833 schedule_timeout(cfs_time_seconds(1));
1834 lnet_net_lock(LNET_LOCK_EX);
1838 lnet_net_unlock(LNET_LOCK_EX);
1840 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1842 LASSERT(!in_interrupt());
1843 (net->net_lnd->lnd_shutdown)(ni);
1846 CDEBUG(D_LNI, "Removed LNI %s\n",
1847 libcfs_nid2str(ni->ni_nid));
1851 lnet_net_lock(LNET_LOCK_EX);
1855 /* shutdown down the NI and release refcount */
1857 lnet_shutdown_lndni(struct lnet_ni *ni)
1860 struct lnet_net *net = ni->ni_net;
1862 lnet_net_lock(LNET_LOCK_EX);
1864 ni->ni_state |= LNET_NI_STATE_DELETING;
1865 ni->ni_state &= ~LNET_NI_STATE_ACTIVE;
1867 lnet_ni_unlink_locked(ni);
1868 lnet_incr_dlc_seq();
1869 lnet_net_unlock(LNET_LOCK_EX);
1871 /* clear messages for this NI on the lazy portal */
1872 for (i = 0; i < the_lnet.ln_nportals; i++)
1873 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1875 lnet_net_lock(LNET_LOCK_EX);
1876 lnet_clear_zombies_nis_locked(net);
1877 lnet_net_unlock(LNET_LOCK_EX);
1881 lnet_shutdown_lndnet(struct lnet_net *net)
1885 lnet_net_lock(LNET_LOCK_EX);
1887 net->net_state = LNET_NET_STATE_DELETING;
1889 list_del_init(&net->net_list);
1891 while (!list_empty(&net->net_ni_list)) {
1892 ni = list_entry(net->net_ni_list.next,
1893 struct lnet_ni, ni_netlist);
1894 lnet_net_unlock(LNET_LOCK_EX);
1895 lnet_shutdown_lndni(ni);
1896 lnet_net_lock(LNET_LOCK_EX);
1899 lnet_net_unlock(LNET_LOCK_EX);
1901 /* Do peer table cleanup for this net */
1902 lnet_peer_tables_cleanup(net);
1904 lnet_net_lock(LNET_LOCK_EX);
1906 * decrement ref count on lnd only when the entire network goes
1909 net->net_lnd->lnd_refcount--;
1911 lnet_net_unlock(LNET_LOCK_EX);
1917 lnet_shutdown_lndnets(void)
1919 struct lnet_net *net;
1920 struct list_head resend;
1921 struct lnet_msg *msg, *tmp;
1923 INIT_LIST_HEAD(&resend);
1925 /* NB called holding the global mutex */
1927 /* All quiet on the API front */
1928 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
1929 LASSERT(the_lnet.ln_refcount == 0);
1931 lnet_net_lock(LNET_LOCK_EX);
1932 the_lnet.ln_state = LNET_STATE_STOPPING;
1934 while (!list_empty(&the_lnet.ln_nets)) {
1936 * move the nets to the zombie list to avoid them being
1937 * picked up for new work. LONET is also included in the
1938 * Nets that will be moved to the zombie list
1940 net = list_entry(the_lnet.ln_nets.next,
1941 struct lnet_net, net_list);
1942 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1945 /* Drop the cached loopback Net. */
1946 if (the_lnet.ln_loni != NULL) {
1947 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1948 the_lnet.ln_loni = NULL;
1950 lnet_net_unlock(LNET_LOCK_EX);
1952 /* iterate through the net zombie list and delete each net */
1953 while (!list_empty(&the_lnet.ln_net_zombie)) {
1954 net = list_entry(the_lnet.ln_net_zombie.next,
1955 struct lnet_net, net_list);
1956 lnet_shutdown_lndnet(net);
1959 spin_lock(&the_lnet.ln_msg_resend_lock);
1960 list_splice(&the_lnet.ln_msg_resend, &resend);
1961 spin_unlock(&the_lnet.ln_msg_resend_lock);
1963 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
1964 list_del_init(&msg->msg_list);
1965 msg->msg_no_resend = true;
1966 lnet_finalize(msg, -ECANCELED);
1969 lnet_net_lock(LNET_LOCK_EX);
1970 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
1971 lnet_net_unlock(LNET_LOCK_EX);
1975 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1978 struct lnet_tx_queue *tq;
1980 struct lnet_net *net = ni->ni_net;
1982 mutex_lock(&the_lnet.ln_lnd_mutex);
1985 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1986 ni->ni_lnd_tunables_set = true;
1989 rc = (net->net_lnd->lnd_startup)(ni);
1991 mutex_unlock(&the_lnet.ln_lnd_mutex);
1994 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1995 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1996 lnet_net_lock(LNET_LOCK_EX);
1997 net->net_lnd->lnd_refcount--;
1998 lnet_net_unlock(LNET_LOCK_EX);
2003 ni->ni_state |= LNET_NI_STATE_ACTIVE;
2004 ni->ni_state &= ~LNET_NI_STATE_INIT;
2007 /* We keep a reference on the loopback net through the loopback NI */
2008 if (net->net_lnd->lnd_type == LOLND) {
2010 LASSERT(the_lnet.ln_loni == NULL);
2011 the_lnet.ln_loni = ni;
2012 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2013 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2014 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2015 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2019 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2020 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2021 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2022 libcfs_lnd2str(net->net_lnd->lnd_type),
2023 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2025 /* shutdown the NI since if we get here then it must've already
2028 lnet_shutdown_lndni(ni);
2032 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2033 tq->tq_credits_min =
2034 tq->tq_credits_max =
2035 tq->tq_credits = lnet_ni_tq_credits(ni);
2038 atomic_set(&ni->ni_tx_credits,
2039 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2040 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2042 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2043 libcfs_nid2str(ni->ni_nid),
2044 ni->ni_net->net_tunables.lct_peer_tx_credits,
2045 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2046 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2047 ni->ni_net->net_tunables.lct_peer_timeout);
2056 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2059 struct lnet_net *net_l = NULL;
2060 struct list_head local_ni_list;
2064 struct lnet_lnd *lnd;
2066 net->net_tunables.lct_peer_timeout;
2068 net->net_tunables.lct_max_tx_credits;
2069 int peerrtrcredits =
2070 net->net_tunables.lct_peer_rtr_credits;
2072 INIT_LIST_HEAD(&local_ni_list);
2075 * make sure that this net is unique. If it isn't then
2076 * we are adding interfaces to an already existing network, and
2077 * 'net' is just a convenient way to pass in the list.
2078 * if it is unique we need to find the LND and load it if
2081 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2082 lnd_type = LNET_NETTYP(net->net_id);
2084 mutex_lock(&the_lnet.ln_lnd_mutex);
2085 lnd = lnet_find_lnd_by_type(lnd_type);
2088 mutex_unlock(&the_lnet.ln_lnd_mutex);
2089 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2090 mutex_lock(&the_lnet.ln_lnd_mutex);
2092 lnd = lnet_find_lnd_by_type(lnd_type);
2094 mutex_unlock(&the_lnet.ln_lnd_mutex);
2095 CERROR("Can't load LND %s, module %s, rc=%d\n",
2096 libcfs_lnd2str(lnd_type),
2097 libcfs_lnd2modname(lnd_type), rc);
2098 #ifndef HAVE_MODULE_LOADING_SUPPORT
2099 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2100 "compiled with kernel module "
2101 "loading support.");
2108 lnet_net_lock(LNET_LOCK_EX);
2109 lnd->lnd_refcount++;
2110 lnet_net_unlock(LNET_LOCK_EX);
2114 mutex_unlock(&the_lnet.ln_lnd_mutex);
2120 * net_l: if the network being added is unique then net_l
2121 * will point to that network
2122 * if the network being added is not unique then
2123 * net_l points to the existing network.
2125 * When we enter the loop below, we'll pick NIs off he
2126 * network beign added and start them up, then add them to
2127 * a local ni list. Once we've successfully started all
2128 * the NIs then we join the local NI list (of started up
2129 * networks) with the net_l->net_ni_list, which should
2130 * point to the correct network to add the new ni list to
2132 * If any of the new NIs fail to start up, then we want to
2133 * iterate through the local ni list, which should include
2134 * any NIs which were successfully started up, and shut
2137 * After than we want to delete the network being added,
2138 * to avoid a memory leak.
2142 * When a network uses TCP bonding then all its interfaces
2143 * must be specified when the network is first defined: the
2144 * TCP bonding code doesn't allow for interfaces to be added
2147 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2148 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2153 while (!list_empty(&net->net_ni_added)) {
2154 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2156 list_del_init(&ni->ni_netlist);
2158 /* make sure that the the NI we're about to start
2159 * up is actually unique. if it's not fail. */
2160 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2161 ni->ni_interfaces[0])) {
2166 /* adjust the pointer the parent network, just in case it
2167 * the net is a duplicate */
2170 rc = lnet_startup_lndni(ni, tun);
2172 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
2173 ni->ni_net->net_lnd->lnd_query != NULL);
2179 list_add_tail(&ni->ni_netlist, &local_ni_list);
2184 lnet_net_lock(LNET_LOCK_EX);
2185 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2186 lnet_incr_dlc_seq();
2187 lnet_net_unlock(LNET_LOCK_EX);
2189 /* if the network is not unique then we don't want to keep
2190 * it around after we're done. Free it. Otherwise add that
2191 * net to the global the_lnet.ln_nets */
2192 if (net_l != net && net_l != NULL) {
2194 * TODO - note. currently the tunables can not be updated
2199 net->net_state = LNET_NET_STATE_ACTIVE;
2201 * restore tunables after it has been overwitten by the
2204 if (peer_timeout != -1)
2205 net->net_tunables.lct_peer_timeout = peer_timeout;
2206 if (maxtxcredits != -1)
2207 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2208 if (peerrtrcredits != -1)
2209 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2211 lnet_net_lock(LNET_LOCK_EX);
2212 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2213 lnet_net_unlock(LNET_LOCK_EX);
2220 * shutdown the new NIs that are being started up
2221 * free the NET being started
2223 while (!list_empty(&local_ni_list)) {
2224 ni = list_entry(local_ni_list.next, struct lnet_ni,
2227 lnet_shutdown_lndni(ni);
2237 lnet_startup_lndnets(struct list_head *netlist)
2239 struct lnet_net *net;
2244 * Change to running state before bringing up the LNDs. This
2245 * allows lnet_shutdown_lndnets() to assert that we've passed
2248 lnet_net_lock(LNET_LOCK_EX);
2249 the_lnet.ln_state = LNET_STATE_RUNNING;
2250 lnet_net_unlock(LNET_LOCK_EX);
2252 while (!list_empty(netlist)) {
2253 net = list_entry(netlist->next, struct lnet_net, net_list);
2254 list_del_init(&net->net_list);
2256 rc = lnet_startup_lndnet(net, NULL);
2266 lnet_shutdown_lndnets();
2272 * Initialize LNet library.
2274 * Automatically called at module loading time. Caller has to call
2275 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2276 * latter returned 0. It must be called exactly once.
2278 * \retval 0 on success
2279 * \retval -ve on failures.
2281 int lnet_lib_init(void)
2285 lnet_assert_wire_constants();
2287 /* refer to global cfs_cpt_table for now */
2288 the_lnet.ln_cpt_table = cfs_cpt_table;
2289 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
2291 LASSERT(the_lnet.ln_cpt_number > 0);
2292 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2293 /* we are under risk of consuming all lh_cookie */
2294 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2295 "please change setting of CPT-table and retry\n",
2296 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2300 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2301 the_lnet.ln_cpt_bits++;
2303 rc = lnet_create_locks();
2305 CERROR("Can't create LNet global locks: %d\n", rc);
2309 the_lnet.ln_refcount = 0;
2310 LNetInvalidateEQHandle(&the_lnet.ln_rc_eqh);
2311 INIT_LIST_HEAD(&the_lnet.ln_lnds);
2312 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2313 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
2314 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2315 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
2317 /* The hash table size is the number of bits it takes to express the set
2318 * ln_num_routes, minus 1 (better to under estimate than over so we
2319 * don't waste memory). */
2320 if (rnet_htable_size <= 0)
2321 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2322 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2323 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2324 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2325 order_base_2(rnet_htable_size) - 1);
2327 /* All LNDs apart from the LOLND are in separate modules. They
2328 * register themselves when their module loads, and unregister
2329 * themselves when their module is unloaded. */
2330 lnet_register_lnd(&the_lolnd);
2335 * Finalize LNet library.
2337 * \pre lnet_lib_init() called with success.
2338 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2340 void lnet_lib_exit(void)
2342 LASSERT(the_lnet.ln_refcount == 0);
2344 while (!list_empty(&the_lnet.ln_lnds))
2345 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
2346 struct lnet_lnd, lnd_list));
2347 lnet_destroy_locks();
2351 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2353 * Users must call this function at least once before any other functions.
2354 * For each successful call there must be a corresponding call to
2355 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2358 * The PID used by LNet may be different from the one requested.
2361 * \param requested_pid PID requested by the caller.
2363 * \return >= 0 on success, and < 0 error code on failures.
2366 LNetNIInit(lnet_pid_t requested_pid)
2368 int im_a_router = 0;
2371 struct lnet_ping_buffer *pbuf;
2372 struct lnet_handle_md ping_mdh;
2373 struct list_head net_head;
2374 struct lnet_net *net;
2376 INIT_LIST_HEAD(&net_head);
2378 mutex_lock(&the_lnet.ln_api_mutex);
2380 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2382 if (the_lnet.ln_refcount > 0) {
2383 rc = the_lnet.ln_refcount++;
2384 mutex_unlock(&the_lnet.ln_api_mutex);
2388 rc = lnet_prepare(requested_pid);
2390 mutex_unlock(&the_lnet.ln_api_mutex);
2394 /* create a network for Loopback network */
2395 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2398 goto err_empty_list;
2401 /* Add in the loopback NI */
2402 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2404 goto err_empty_list;
2407 /* If LNet is being initialized via DLC it is possible
2408 * that the user requests not to load module parameters (ones which
2409 * are supported by DLC) on initialization. Therefore, make sure not
2410 * to load networks, routes and forwarding from module parameters
2411 * in this case. On cleanup in case of failure only clean up
2412 * routes if it has been loaded */
2413 if (!the_lnet.ln_nis_from_mod_params) {
2414 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2417 goto err_empty_list;
2420 ni_count = lnet_startup_lndnets(&net_head);
2423 goto err_empty_list;
2426 if (!the_lnet.ln_nis_from_mod_params) {
2427 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2429 goto err_shutdown_lndnis;
2431 rc = lnet_check_routes();
2433 goto err_destroy_routes;
2435 rc = lnet_rtrpools_alloc(im_a_router);
2437 goto err_destroy_routes;
2440 rc = lnet_acceptor_start();
2442 goto err_destroy_routes;
2444 the_lnet.ln_refcount = 1;
2445 /* Now I may use my own API functions... */
2447 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2449 goto err_acceptor_stop;
2451 lnet_ping_target_update(pbuf, ping_mdh);
2453 rc = lnet_monitor_thr_start();
2457 rc = lnet_push_target_init();
2459 goto err_stop_monitor_thr;
2461 rc = lnet_peer_discovery_start();
2463 goto err_destroy_push_target;
2466 lnet_router_debugfs_init();
2468 mutex_unlock(&the_lnet.ln_api_mutex);
2472 err_destroy_push_target:
2473 lnet_push_target_fini();
2474 err_stop_monitor_thr:
2475 lnet_monitor_thr_stop();
2477 lnet_ping_target_fini();
2479 the_lnet.ln_refcount = 0;
2480 lnet_acceptor_stop();
2482 if (!the_lnet.ln_nis_from_mod_params)
2483 lnet_destroy_routes();
2484 err_shutdown_lndnis:
2485 lnet_shutdown_lndnets();
2489 mutex_unlock(&the_lnet.ln_api_mutex);
2490 while (!list_empty(&net_head)) {
2491 struct lnet_net *net;
2493 net = list_entry(net_head.next, struct lnet_net, net_list);
2494 list_del_init(&net->net_list);
2499 EXPORT_SYMBOL(LNetNIInit);
2502 * Stop LNet interfaces, routing, and forwarding.
2504 * Users must call this function once for each successful call to LNetNIInit().
2505 * Once the LNetNIFini() operation has been started, the results of pending
2506 * API operations are undefined.
2508 * \return always 0 for current implementation.
2513 mutex_lock(&the_lnet.ln_api_mutex);
2515 LASSERT(the_lnet.ln_refcount > 0);
2517 if (the_lnet.ln_refcount != 1) {
2518 the_lnet.ln_refcount--;
2520 LASSERT(!the_lnet.ln_niinit_self);
2524 lnet_router_debugfs_init();
2525 lnet_peer_discovery_stop();
2526 lnet_push_target_fini();
2527 lnet_monitor_thr_stop();
2528 lnet_ping_target_fini();
2530 /* Teardown fns that use my own API functions BEFORE here */
2531 the_lnet.ln_refcount = 0;
2533 lnet_acceptor_stop();
2534 lnet_destroy_routes();
2535 lnet_shutdown_lndnets();
2539 mutex_unlock(&the_lnet.ln_api_mutex);
2542 EXPORT_SYMBOL(LNetNIFini);
2545 * Grabs the ni data from the ni structure and fills the out
2548 * \param[in] ni network interface structure
2549 * \param[out] cfg_ni NI config information
2550 * \param[out] tun network and LND tunables
2553 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2554 struct lnet_ioctl_config_lnd_tunables *tun,
2555 struct lnet_ioctl_element_stats *stats,
2558 size_t min_size = 0;
2561 if (!ni || !cfg_ni || !tun)
2564 if (ni->ni_interfaces[0] != NULL) {
2565 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2566 if (ni->ni_interfaces[i] != NULL) {
2567 strncpy(cfg_ni->lic_ni_intf[i],
2568 ni->ni_interfaces[i],
2569 sizeof(cfg_ni->lic_ni_intf[i]));
2574 cfg_ni->lic_nid = ni->ni_nid;
2575 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2576 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2578 cfg_ni->lic_status = ni->ni_status->ns_status;
2579 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2580 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2582 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2585 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2586 LNET_STATS_TYPE_SEND);
2587 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2588 LNET_STATS_TYPE_RECV);
2589 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2590 LNET_STATS_TYPE_DROP);
2594 * tun->lt_tun will always be present, but in order to be
2595 * backwards compatible, we need to deal with the cases when
2596 * tun->lt_tun is smaller than what the kernel has, because it
2597 * comes from an older version of a userspace program, then we'll
2598 * need to copy as much information as we have available space.
2600 min_size = tun_size - sizeof(tun->lt_cmn);
2601 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2603 /* copy over the cpts */
2604 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2605 ni->ni_cpts == NULL) {
2606 for (i = 0; i < ni->ni_ncpts; i++)
2607 cfg_ni->lic_cpts[i] = i;
2610 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2611 i < LNET_MAX_SHOW_NUM_CPT;
2613 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2615 cfg_ni->lic_ncpts = ni->ni_ncpts;
2619 * NOTE: This is a legacy function left in the code to be backwards
2620 * compatible with older userspace programs. It should eventually be
2623 * Grabs the ni data from the ni structure and fills the out
2626 * \param[in] ni network interface structure
2627 * \param[out] config config information
2630 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2631 struct lnet_ioctl_config_data *config)
2633 struct lnet_ioctl_net_config *net_config;
2634 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2635 size_t min_size, tunable_size = 0;
2641 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2645 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2646 ARRAY_SIZE(net_config->ni_interfaces));
2648 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2649 if (!ni->ni_interfaces[i])
2652 strncpy(net_config->ni_interfaces[i],
2653 ni->ni_interfaces[i],
2654 sizeof(net_config->ni_interfaces[i]));
2657 config->cfg_nid = ni->ni_nid;
2658 config->cfg_config_u.cfg_net.net_peer_timeout =
2659 ni->ni_net->net_tunables.lct_peer_timeout;
2660 config->cfg_config_u.cfg_net.net_max_tx_credits =
2661 ni->ni_net->net_tunables.lct_max_tx_credits;
2662 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2663 ni->ni_net->net_tunables.lct_peer_tx_credits;
2664 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2665 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2667 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2668 net_config->ni_status = LNET_NI_STATUS_UP;
2670 net_config->ni_status = ni->ni_status->ns_status;
2673 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2675 for (i = 0; i < num_cpts; i++)
2676 net_config->ni_cpts[i] = ni->ni_cpts[i];
2678 config->cfg_ncpts = num_cpts;
2682 * See if user land tools sent in a newer and larger version
2683 * of struct lnet_tunables than what the kernel uses.
2685 min_size = sizeof(*config) + sizeof(*net_config);
2687 if (config->cfg_hdr.ioc_len > min_size)
2688 tunable_size = config->cfg_hdr.ioc_len - min_size;
2690 /* Don't copy too much data to user space */
2691 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2692 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2694 if (lnd_cfg && min_size) {
2695 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2696 config->cfg_config_u.cfg_net.net_interface_count = 1;
2698 /* Tell user land that kernel side has less data */
2699 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2700 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2701 config->cfg_hdr.ioc_len -= min_size;
2707 lnet_get_ni_idx_locked(int idx)
2710 struct lnet_net *net;
2712 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2713 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2723 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2726 struct lnet_net *net = mynet;
2729 * It is possible that the net has been cleaned out while there is
2730 * a message being sent. This function accessed the net without
2731 * checking if the list is empty
2735 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2737 if (list_empty(&net->net_ni_list))
2739 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2745 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2746 /* if you reached the end of the ni list and the net is
2747 * specified, then there are no more nis in that net */
2751 /* we reached the end of this net ni list. move to the
2753 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2754 /* no more nets and no more NIs. */
2757 /* get the next net */
2758 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2760 if (list_empty(&net->net_ni_list))
2762 /* get the ni on it */
2763 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2769 if (list_empty(&prev->ni_netlist))
2772 /* there are more nis left */
2773 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2779 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2784 int idx = config->cfg_count;
2786 cpt = lnet_net_lock_current();
2788 ni = lnet_get_ni_idx_locked(idx);
2793 lnet_fill_ni_info_legacy(ni, config);
2797 lnet_net_unlock(cpt);
2802 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2803 struct lnet_ioctl_config_lnd_tunables *tun,
2804 struct lnet_ioctl_element_stats *stats,
2811 if (!cfg_ni || !tun || !stats)
2814 cpt = lnet_net_lock_current();
2816 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2821 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
2825 lnet_net_unlock(cpt);
2829 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
2838 cpt = lnet_net_lock_current();
2840 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
2843 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
2847 lnet_net_unlock(cpt);
2852 static int lnet_add_net_common(struct lnet_net *net,
2853 struct lnet_ioctl_config_lnd_tunables *tun)
2856 struct lnet_ping_buffer *pbuf;
2857 struct lnet_handle_md ping_mdh;
2859 struct lnet_remotenet *rnet;
2861 int num_acceptor_nets;
2863 lnet_net_lock(LNET_LOCK_EX);
2864 rnet = lnet_find_rnet_locked(net->net_id);
2865 lnet_net_unlock(LNET_LOCK_EX);
2867 * make sure that the net added doesn't invalidate the current
2868 * configuration LNet is keeping
2871 CERROR("Adding net %s will invalidate routing configuration\n",
2872 libcfs_net2str(net->net_id));
2878 * make sure you calculate the correct number of slots in the ping
2879 * buffer. Since the ping info is a flattened list of all the NIs,
2880 * we should allocate enough slots to accomodate the number of NIs
2881 * which will be added.
2883 * since ni hasn't been configured yet, use
2884 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
2886 net_ni_count = lnet_get_net_ni_count_pre(net);
2888 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2889 net_ni_count + lnet_get_ni_count(),
2897 memcpy(&net->net_tunables,
2898 &tun->lt_cmn, sizeof(net->net_tunables));
2900 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
2903 * before starting this network get a count of the current TCP
2904 * networks which require the acceptor thread running. If that
2905 * count is == 0 before we start up this network, then we'd want to
2906 * start up the acceptor thread after starting up this network
2908 num_acceptor_nets = lnet_count_acceptor_nets();
2910 net_id = net->net_id;
2912 rc = lnet_startup_lndnet(net,
2913 (tun) ? &tun->lt_tun : NULL);
2917 lnet_net_lock(LNET_LOCK_EX);
2918 net = lnet_get_net_locked(net_id);
2919 lnet_net_unlock(LNET_LOCK_EX);
2924 * Start the acceptor thread if this is the first network
2925 * being added that requires the thread.
2927 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
2928 rc = lnet_acceptor_start();
2930 /* shutdown the net that we just started */
2931 CERROR("Failed to start up acceptor thread\n");
2932 lnet_shutdown_lndnet(net);
2937 lnet_net_lock(LNET_LOCK_EX);
2938 lnet_peer_net_added(net);
2939 lnet_net_unlock(LNET_LOCK_EX);
2941 lnet_ping_target_update(pbuf, ping_mdh);
2946 lnet_ping_md_unlink(pbuf, &ping_mdh);
2947 lnet_ping_buffer_decref(pbuf);
2951 static int lnet_handle_legacy_ip2nets(char *ip2nets,
2952 struct lnet_ioctl_config_lnd_tunables *tun)
2954 struct lnet_net *net;
2957 struct list_head net_head;
2959 INIT_LIST_HEAD(&net_head);
2961 rc = lnet_parse_ip2nets(&nets, ip2nets);
2965 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2969 mutex_lock(&the_lnet.ln_api_mutex);
2970 while (!list_empty(&net_head)) {
2971 net = list_entry(net_head.next, struct lnet_net, net_list);
2972 list_del_init(&net->net_list);
2973 rc = lnet_add_net_common(net, tun);
2979 mutex_unlock(&the_lnet.ln_api_mutex);
2981 while (!list_empty(&net_head)) {
2982 net = list_entry(net_head.next, struct lnet_net, net_list);
2983 list_del_init(&net->net_list);
2989 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
2991 struct lnet_net *net;
2993 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2995 __u32 net_id, lnd_type;
2997 /* get the tunables if they are available */
2998 if (conf->lic_cfg_hdr.ioc_len >=
2999 sizeof(*conf) + sizeof(*tun))
3000 tun = (struct lnet_ioctl_config_lnd_tunables *)
3003 /* handle legacy ip2nets from DLC */
3004 if (conf->lic_legacy_ip2nets[0] != '\0')
3005 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3008 net_id = LNET_NIDNET(conf->lic_nid);
3009 lnd_type = LNET_NETTYP(net_id);
3011 if (!libcfs_isknown_lnd(lnd_type)) {
3012 CERROR("No valid net and lnd information provided\n");
3016 net = lnet_net_alloc(net_id, NULL);
3020 for (i = 0; i < conf->lic_ncpts; i++) {
3021 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3025 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3026 conf->lic_ni_intf[0]);
3030 mutex_lock(&the_lnet.ln_api_mutex);
3032 rc = lnet_add_net_common(net, tun);
3034 mutex_unlock(&the_lnet.ln_api_mutex);
3039 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3041 struct lnet_net *net;
3043 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3044 struct lnet_ping_buffer *pbuf;
3045 struct lnet_handle_md ping_mdh;
3050 /* don't allow userspace to shutdown the LOLND */
3051 if (LNET_NETTYP(net_id) == LOLND)
3054 mutex_lock(&the_lnet.ln_api_mutex);
3058 net = lnet_get_net_locked(net_id);
3060 CERROR("net %s not found\n",
3061 libcfs_net2str(net_id));
3066 addr = LNET_NIDADDR(conf->lic_nid);
3068 /* remove the entire net */
3069 net_count = lnet_get_net_ni_count_locked(net);
3073 /* create and link a new ping info, before removing the old one */
3074 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3075 lnet_get_ni_count() - net_count,
3078 goto unlock_api_mutex;
3080 lnet_shutdown_lndnet(net);
3082 if (lnet_count_acceptor_nets() == 0)
3083 lnet_acceptor_stop();
3085 lnet_ping_target_update(pbuf, ping_mdh);
3087 goto unlock_api_mutex;
3090 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3092 CERROR("nid %s not found\n",
3093 libcfs_nid2str(conf->lic_nid));
3098 net_count = lnet_get_net_ni_count_locked(net);
3102 /* create and link a new ping info, before removing the old one */
3103 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3104 lnet_get_ni_count() - 1, false);
3106 goto unlock_api_mutex;
3108 lnet_shutdown_lndni(ni);
3110 if (lnet_count_acceptor_nets() == 0)
3111 lnet_acceptor_stop();
3113 lnet_ping_target_update(pbuf, ping_mdh);
3115 /* check if the net is empty and remove it if it is */
3117 lnet_shutdown_lndnet(net);
3119 goto unlock_api_mutex;
3124 mutex_unlock(&the_lnet.ln_api_mutex);
3130 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3131 * They are only expected to be called for unique networks.
3132 * That can be as a result of older DLC library
3133 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3136 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3138 struct lnet_net *net;
3139 struct list_head net_head;
3141 struct lnet_ioctl_config_lnd_tunables tun;
3142 char *nets = conf->cfg_config_u.cfg_net.net_intf;
3144 INIT_LIST_HEAD(&net_head);
3146 /* Create a net/ni structures for the network string */
3147 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3149 return rc == 0 ? -EINVAL : rc;
3151 mutex_lock(&the_lnet.ln_api_mutex);
3154 rc = -EINVAL; /* only add one network per call */
3155 goto out_unlock_clean;
3158 net = list_entry(net_head.next, struct lnet_net, net_list);
3159 list_del_init(&net->net_list);
3161 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3163 memset(&tun, 0, sizeof(tun));
3165 tun.lt_cmn.lct_peer_timeout =
3166 conf->cfg_config_u.cfg_net.net_peer_timeout;
3167 tun.lt_cmn.lct_peer_tx_credits =
3168 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3169 tun.lt_cmn.lct_peer_rtr_credits =
3170 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3171 tun.lt_cmn.lct_max_tx_credits =
3172 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3174 rc = lnet_add_net_common(net, &tun);
3177 mutex_unlock(&the_lnet.ln_api_mutex);
3178 while (!list_empty(&net_head)) {
3179 /* net_head list is empty in success case */
3180 net = list_entry(net_head.next, struct lnet_net, net_list);
3181 list_del_init(&net->net_list);
3188 lnet_dyn_del_net(__u32 net_id)
3190 struct lnet_net *net;
3191 struct lnet_ping_buffer *pbuf;
3192 struct lnet_handle_md ping_mdh;
3196 /* don't allow userspace to shutdown the LOLND */
3197 if (LNET_NETTYP(net_id) == LOLND)
3200 mutex_lock(&the_lnet.ln_api_mutex);
3204 net = lnet_get_net_locked(net_id);
3211 net_ni_count = lnet_get_net_ni_count_locked(net);
3215 /* create and link a new ping info, before removing the old one */
3216 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3217 lnet_get_ni_count() - net_ni_count, false);
3221 lnet_shutdown_lndnet(net);
3223 if (lnet_count_acceptor_nets() == 0)
3224 lnet_acceptor_stop();
3226 lnet_ping_target_update(pbuf, ping_mdh);
3229 mutex_unlock(&the_lnet.ln_api_mutex);
3234 void lnet_incr_dlc_seq(void)
3236 atomic_inc(&lnet_dlc_seq_no);
3239 __u32 lnet_get_dlc_seq_locked(void)
3241 return atomic_read(&lnet_dlc_seq_no);
3245 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3247 struct lnet_net *net;
3250 lnet_net_lock(LNET_LOCK_EX);
3251 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3252 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3253 if (ni->ni_nid == nid || all) {
3254 atomic_set(&ni->ni_healthv, value);
3255 if (list_empty(&ni->ni_recovery) &&
3256 value < LNET_MAX_HEALTH_VALUE) {
3257 CERROR("manually adding local NI %s to recovery\n",
3258 libcfs_nid2str(ni->ni_nid));
3259 list_add_tail(&ni->ni_recovery,
3260 &the_lnet.ln_mt_localNIRecovq);
3261 lnet_ni_addref_locked(ni, 0);
3264 lnet_net_unlock(LNET_LOCK_EX);
3270 lnet_net_unlock(LNET_LOCK_EX);
3274 * LNet ioctl handler.
3278 LNetCtl(unsigned int cmd, void *arg)
3280 struct libcfs_ioctl_data *data = arg;
3281 struct lnet_ioctl_config_data *config;
3282 struct lnet_process_id id = {0};
3286 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3287 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3290 case IOC_LIBCFS_GET_NI:
3291 rc = LNetGetId(data->ioc_count, &id);
3292 data->ioc_nid = id.nid;
3295 case IOC_LIBCFS_FAIL_NID:
3296 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3298 case IOC_LIBCFS_ADD_ROUTE:
3301 if (config->cfg_hdr.ioc_len < sizeof(*config))
3304 mutex_lock(&the_lnet.ln_api_mutex);
3305 rc = lnet_add_route(config->cfg_net,
3306 config->cfg_config_u.cfg_route.rtr_hop,
3308 config->cfg_config_u.cfg_route.
3311 rc = lnet_check_routes();
3313 lnet_del_route(config->cfg_net,
3316 mutex_unlock(&the_lnet.ln_api_mutex);
3319 case IOC_LIBCFS_DEL_ROUTE:
3322 if (config->cfg_hdr.ioc_len < sizeof(*config))
3325 mutex_lock(&the_lnet.ln_api_mutex);
3326 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3327 mutex_unlock(&the_lnet.ln_api_mutex);
3330 case IOC_LIBCFS_GET_ROUTE:
3333 if (config->cfg_hdr.ioc_len < sizeof(*config))
3336 mutex_lock(&the_lnet.ln_api_mutex);
3337 rc = lnet_get_route(config->cfg_count,
3339 &config->cfg_config_u.cfg_route.rtr_hop,
3341 &config->cfg_config_u.cfg_route.rtr_flags,
3342 &config->cfg_config_u.cfg_route.
3344 mutex_unlock(&the_lnet.ln_api_mutex);
3347 case IOC_LIBCFS_GET_LOCAL_NI: {
3348 struct lnet_ioctl_config_ni *cfg_ni;
3349 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3350 struct lnet_ioctl_element_stats *stats;
3355 /* get the tunables if they are available */
3356 if (cfg_ni->lic_cfg_hdr.ioc_len <
3357 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3360 stats = (struct lnet_ioctl_element_stats *)
3362 tun = (struct lnet_ioctl_config_lnd_tunables *)
3363 (cfg_ni->lic_bulk + sizeof(*stats));
3365 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3368 mutex_lock(&the_lnet.ln_api_mutex);
3369 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3370 mutex_unlock(&the_lnet.ln_api_mutex);
3374 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3375 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3377 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3380 mutex_lock(&the_lnet.ln_api_mutex);
3381 rc = lnet_get_ni_stats(msg_stats);
3382 mutex_unlock(&the_lnet.ln_api_mutex);
3387 case IOC_LIBCFS_GET_NET: {
3388 size_t total = sizeof(*config) +
3389 sizeof(struct lnet_ioctl_net_config);
3392 if (config->cfg_hdr.ioc_len < total)
3395 mutex_lock(&the_lnet.ln_api_mutex);
3396 rc = lnet_get_net_config(config);
3397 mutex_unlock(&the_lnet.ln_api_mutex);
3401 case IOC_LIBCFS_GET_LNET_STATS:
3403 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3405 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3408 mutex_lock(&the_lnet.ln_api_mutex);
3409 lnet_counters_get(&lnet_stats->st_cntrs);
3410 mutex_unlock(&the_lnet.ln_api_mutex);
3414 case IOC_LIBCFS_CONFIG_RTR:
3417 if (config->cfg_hdr.ioc_len < sizeof(*config))
3420 mutex_lock(&the_lnet.ln_api_mutex);
3421 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3422 rc = lnet_rtrpools_enable();
3423 mutex_unlock(&the_lnet.ln_api_mutex);
3426 lnet_rtrpools_disable();
3427 mutex_unlock(&the_lnet.ln_api_mutex);
3430 case IOC_LIBCFS_ADD_BUF:
3433 if (config->cfg_hdr.ioc_len < sizeof(*config))
3436 mutex_lock(&the_lnet.ln_api_mutex);
3437 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3439 config->cfg_config_u.cfg_buffers.
3441 config->cfg_config_u.cfg_buffers.
3443 mutex_unlock(&the_lnet.ln_api_mutex);
3446 case IOC_LIBCFS_SET_NUMA_RANGE: {
3447 struct lnet_ioctl_set_value *numa;
3449 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3451 lnet_net_lock(LNET_LOCK_EX);
3452 lnet_numa_range = numa->sv_value;
3453 lnet_net_unlock(LNET_LOCK_EX);
3457 case IOC_LIBCFS_GET_NUMA_RANGE: {
3458 struct lnet_ioctl_set_value *numa;
3460 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3462 numa->sv_value = lnet_numa_range;
3466 case IOC_LIBCFS_GET_BUF: {
3467 struct lnet_ioctl_pool_cfg *pool_cfg;
3468 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3472 if (config->cfg_hdr.ioc_len < total)
3475 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3477 mutex_lock(&the_lnet.ln_api_mutex);
3478 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3479 mutex_unlock(&the_lnet.ln_api_mutex);
3483 case IOC_LIBCFS_ADD_PEER_NI: {
3484 struct lnet_ioctl_peer_cfg *cfg = arg;
3486 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3489 mutex_lock(&the_lnet.ln_api_mutex);
3490 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3493 mutex_unlock(&the_lnet.ln_api_mutex);
3497 case IOC_LIBCFS_DEL_PEER_NI: {
3498 struct lnet_ioctl_peer_cfg *cfg = arg;
3500 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3503 mutex_lock(&the_lnet.ln_api_mutex);
3504 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3505 cfg->prcfg_cfg_nid);
3506 mutex_unlock(&the_lnet.ln_api_mutex);
3510 case IOC_LIBCFS_GET_PEER_INFO: {
3511 struct lnet_ioctl_peer *peer_info = arg;
3513 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3516 mutex_lock(&the_lnet.ln_api_mutex);
3517 rc = lnet_get_peer_ni_info(
3518 peer_info->pr_count,
3520 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3521 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3522 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3523 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3524 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3525 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3526 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3527 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3528 mutex_unlock(&the_lnet.ln_api_mutex);
3532 case IOC_LIBCFS_GET_PEER_NI: {
3533 struct lnet_ioctl_peer_cfg *cfg = arg;
3535 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3538 mutex_lock(&the_lnet.ln_api_mutex);
3539 rc = lnet_get_peer_info(cfg,
3540 (void __user *)cfg->prcfg_bulk);
3541 mutex_unlock(&the_lnet.ln_api_mutex);
3545 case IOC_LIBCFS_GET_PEER_LIST: {
3546 struct lnet_ioctl_peer_cfg *cfg = arg;
3548 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3551 mutex_lock(&the_lnet.ln_api_mutex);
3552 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3553 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3554 mutex_unlock(&the_lnet.ln_api_mutex);
3558 case IOC_LIBCFS_SET_HEALHV: {
3559 struct lnet_ioctl_reset_health_cfg *cfg = arg;
3561 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
3563 if (cfg->rh_value < 0 ||
3564 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
3565 value = LNET_MAX_HEALTH_VALUE;
3567 value = cfg->rh_value;
3568 mutex_lock(&the_lnet.ln_api_mutex);
3569 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
3570 lnet_ni_set_healthv(cfg->rh_nid, value,
3573 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
3575 mutex_unlock(&the_lnet.ln_api_mutex);
3578 case IOC_LIBCFS_NOTIFY_ROUTER: {
3579 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3581 /* The deadline passed in by the user should be some time in
3582 * seconds in the future since the UNIX epoch. We have to map
3583 * that deadline to the wall clock.
3585 deadline += ktime_get_seconds();
3586 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
3590 case IOC_LIBCFS_LNET_DIST:
3591 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3592 if (rc < 0 && rc != -EHOSTUNREACH)
3595 data->ioc_u32[0] = rc;
3598 case IOC_LIBCFS_TESTPROTOCOMPAT:
3599 lnet_net_lock(LNET_LOCK_EX);
3600 the_lnet.ln_testprotocompat = data->ioc_flags;
3601 lnet_net_unlock(LNET_LOCK_EX);
3604 case IOC_LIBCFS_LNET_FAULT:
3605 return lnet_fault_ctl(data->ioc_flags, data);
3607 case IOC_LIBCFS_PING: {
3608 signed long timeout;
3610 id.nid = data->ioc_nid;
3611 id.pid = data->ioc_u32[0];
3613 /* If timeout is negative then set default of 3 minutes */
3614 if (((s32)data->ioc_u32[1] <= 0) ||
3615 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3616 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3618 timeout = msecs_to_jiffies(data->ioc_u32[1]);
3620 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3621 data->ioc_plen1 / sizeof(struct lnet_process_id));
3626 data->ioc_count = rc;
3630 case IOC_LIBCFS_PING_PEER: {
3631 struct lnet_ioctl_ping_data *ping = arg;
3632 struct lnet_peer *lp;
3633 signed long timeout;
3635 /* If timeout is negative then set default of 3 minutes */
3636 if (((s32)ping->op_param) <= 0 ||
3637 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3638 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3640 timeout = msecs_to_jiffies(ping->op_param);
3642 rc = lnet_ping(ping->ping_id, timeout,
3648 mutex_lock(&the_lnet.ln_api_mutex);
3649 lp = lnet_find_peer(ping->ping_id.nid);
3651 ping->ping_id.nid = lp->lp_primary_nid;
3652 ping->mr_info = lnet_peer_is_multi_rail(lp);
3653 lnet_peer_decref_locked(lp);
3655 mutex_unlock(&the_lnet.ln_api_mutex);
3657 ping->ping_count = rc;
3661 case IOC_LIBCFS_DISCOVER: {
3662 struct lnet_ioctl_ping_data *discover = arg;
3663 struct lnet_peer *lp;
3665 rc = lnet_discover(discover->ping_id, discover->op_param,
3667 discover->ping_count);
3671 mutex_lock(&the_lnet.ln_api_mutex);
3672 lp = lnet_find_peer(discover->ping_id.nid);
3674 discover->ping_id.nid = lp->lp_primary_nid;
3675 discover->mr_info = lnet_peer_is_multi_rail(lp);
3676 lnet_peer_decref_locked(lp);
3678 mutex_unlock(&the_lnet.ln_api_mutex);
3680 discover->ping_count = rc;
3685 ni = lnet_net2ni_addref(data->ioc_net);
3689 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3692 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
3699 EXPORT_SYMBOL(LNetCtl);
3701 void LNetDebugPeer(struct lnet_process_id id)
3703 lnet_debug_peer(id.nid);
3705 EXPORT_SYMBOL(LNetDebugPeer);
3708 * Determine if the specified peer \a nid is on the local node.
3710 * \param nid peer nid to check
3712 * \retval true If peer NID is on the local node.
3713 * \retval false If peer NID is not on the local node.
3715 bool LNetIsPeerLocal(lnet_nid_t nid)
3717 struct lnet_net *net;
3721 cpt = lnet_net_lock_current();
3722 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3723 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3724 if (ni->ni_nid == nid) {
3725 lnet_net_unlock(cpt);
3730 lnet_net_unlock(cpt);
3734 EXPORT_SYMBOL(LNetIsPeerLocal);
3737 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
3738 * Note that all interfaces share a same PID, as requested by LNetNIInit().
3740 * \param index Index of the interface to look up.
3741 * \param id On successful return, this location will hold the
3742 * struct lnet_process_id ID of the interface.
3744 * \retval 0 If an interface exists at \a index.
3745 * \retval -ENOENT If no interface has been found.
3748 LNetGetId(unsigned int index, struct lnet_process_id *id)
3751 struct lnet_net *net;
3755 LASSERT(the_lnet.ln_refcount > 0);
3757 cpt = lnet_net_lock_current();
3759 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3760 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3764 id->nid = ni->ni_nid;
3765 id->pid = the_lnet.ln_pid;
3771 lnet_net_unlock(cpt);
3774 EXPORT_SYMBOL(LNetGetId);
3776 static int lnet_ping(struct lnet_process_id id, signed long timeout,
3777 struct lnet_process_id __user *ids, int n_ids)
3779 struct lnet_handle_eq eqh;
3780 struct lnet_handle_md mdh;
3781 struct lnet_event event;
3782 struct lnet_md md = { NULL };
3786 const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
3787 struct lnet_ping_buffer *pbuf;
3788 struct lnet_process_id tmpid;
3795 /* n_ids limit is arbitrary */
3796 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
3800 * if the user buffer has more space than the lnet_interfaces_max
3801 * then only fill it up to lnet_interfaces_max
3803 if (n_ids > lnet_interfaces_max)
3804 n_ids = lnet_interfaces_max;
3806 if (id.pid == LNET_PID_ANY)
3807 id.pid = LNET_PID_LUSTRE;
3809 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
3813 /* NB 2 events max (including any unlink event) */
3814 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
3816 CERROR("Can't allocate EQ: %d\n", rc);
3817 goto fail_ping_buffer_decref;
3820 /* initialize md content */
3821 md.start = &pbuf->pb_info;
3822 md.length = LNET_PING_INFO_SIZE(n_ids);
3823 md.threshold = 2; /* GET/REPLY */
3825 md.options = LNET_MD_TRUNCATE;
3829 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
3831 CERROR("Can't bind MD: %d\n", rc);
3835 rc = LNetGet(LNET_NID_ANY, mdh, id,
3836 LNET_RESERVED_PORTAL,
3837 LNET_PROTO_PING_MATCHBITS, 0, false);
3840 /* Don't CERROR; this could be deliberate! */
3841 rc2 = LNetMDUnlink(mdh);
3844 /* NB must wait for the UNLINK event below... */
3846 timeout = a_long_time;
3850 /* MUST block for unlink to complete */
3852 blocked = cfs_block_allsigs();
3854 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
3857 cfs_restore_sigs(blocked);
3859 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
3860 (rc2 <= 0) ? -1 : event.type,
3861 (rc2 <= 0) ? -1 : event.status,
3862 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
3864 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
3866 if (rc2 <= 0 || event.status != 0) {
3867 /* timeout or error */
3868 if (!replied && rc == 0)
3869 rc = (rc2 < 0) ? rc2 :
3870 (rc2 == 0) ? -ETIMEDOUT :
3874 /* Ensure completion in finite time... */
3876 /* No assertion (racing with network) */
3878 timeout = a_long_time;
3879 } else if (rc2 == 0) {
3880 /* timed out waiting for unlink */
3881 CWARN("ping %s: late network completion\n",
3884 } else if (event.type == LNET_EVENT_REPLY) {
3888 } while (rc2 <= 0 || !event.unlinked);
3892 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
3899 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
3901 rc = -EPROTO; /* if I can't parse... */
3904 CERROR("%s: ping info too short %d\n",
3905 libcfs_id2str(id), nob);
3909 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
3910 lnet_swap_pinginfo(pbuf);
3911 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
3912 CERROR("%s: Unexpected magic %08x\n",
3913 libcfs_id2str(id), pbuf->pb_info.pi_magic);
3917 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
3918 CERROR("%s: ping w/o NI status: 0x%x\n",
3919 libcfs_id2str(id), pbuf->pb_info.pi_features);
3923 if (nob < LNET_PING_INFO_SIZE(0)) {
3924 CERROR("%s: Short reply %d(%d min)\n",
3926 nob, (int)LNET_PING_INFO_SIZE(0));
3930 if (pbuf->pb_info.pi_nnis < n_ids)
3931 n_ids = pbuf->pb_info.pi_nnis;
3933 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
3934 CERROR("%s: Short reply %d(%d expected)\n",
3936 nob, (int)LNET_PING_INFO_SIZE(n_ids));
3940 rc = -EFAULT; /* if I segv in copy_to_user()... */
3942 memset(&tmpid, 0, sizeof(tmpid));
3943 for (i = 0; i < n_ids; i++) {
3944 tmpid.pid = pbuf->pb_info.pi_pid;
3945 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
3946 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
3949 rc = pbuf->pb_info.pi_nnis;
3952 rc2 = LNetEQFree(eqh);
3954 CERROR("rc2 %d\n", rc2);
3957 fail_ping_buffer_decref:
3958 lnet_ping_buffer_decref(pbuf);
3963 lnet_discover(struct lnet_process_id id, __u32 force,
3964 struct lnet_process_id __user *ids, int n_ids)
3966 struct lnet_peer_ni *lpni;
3967 struct lnet_peer_ni *p;
3968 struct lnet_peer *lp;
3969 struct lnet_process_id *buf;
3973 int max_intf = lnet_interfaces_max;
3977 id.nid == LNET_NID_ANY)
3980 if (id.pid == LNET_PID_ANY)
3981 id.pid = LNET_PID_LUSTRE;
3984 * if the user buffer has more space than the max_intf
3985 * then only fill it up to max_intf
3987 if (n_ids > max_intf)
3990 buf_size = n_ids * sizeof(*buf);
3992 LIBCFS_ALLOC(buf, buf_size);
3996 cpt = lnet_net_lock_current();
3997 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4004 * Clearing the NIDS_UPTODATE flag ensures the peer will
4005 * be discovered, provided discovery has not been disabled.
4007 lp = lpni->lpni_peer_net->lpn_peer;
4008 spin_lock(&lp->lp_lock);
4009 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4010 /* If the force flag is set, force a PING and PUSH as well. */
4012 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4013 spin_unlock(&lp->lp_lock);
4014 rc = lnet_discover_peer_locked(lpni, cpt, true);
4018 /* Peer may have changed. */
4019 lp = lpni->lpni_peer_net->lpn_peer;
4020 if (lp->lp_nnis < n_ids)
4021 n_ids = lp->lp_nnis;
4025 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4026 buf[i].pid = id.pid;
4027 buf[i].nid = p->lpni_nid;
4032 lnet_net_unlock(cpt);
4035 if (copy_to_user(ids, buf, n_ids * sizeof(*buf)))
4041 lnet_peer_ni_decref_locked(lpni);
4043 lnet_net_unlock(cpt);
4045 LIBCFS_FREE(buf, buf_size);