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>
40 #ifdef HAVE_SCHED_HEADERS
41 #include <linux/sched/signal.h>
44 #include <lnet/udsp.h>
45 #include <lnet/lib-lnet.h>
47 #define D_LNI D_CONSOLE
50 * initialize ln_api_mutex statically, since it needs to be used in
51 * discovery_set callback. That module parameter callback can be called
52 * before module init completes. The mutex needs to be ready for use then.
54 struct lnet the_lnet = {
55 .ln_api_mutex = __MUTEX_INITIALIZER(the_lnet.ln_api_mutex),
56 }; /* THE state of the network */
57 EXPORT_SYMBOL(the_lnet);
59 static char *ip2nets = "";
60 module_param(ip2nets, charp, 0444);
61 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
63 static char *networks = "";
64 module_param(networks, charp, 0444);
65 MODULE_PARM_DESC(networks, "local networks");
67 static char *routes = "";
68 module_param(routes, charp, 0444);
69 MODULE_PARM_DESC(routes, "routes to non-local networks");
71 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
72 module_param(rnet_htable_size, int, 0444);
73 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
75 static int use_tcp_bonding = false;
76 module_param(use_tcp_bonding, int, 0444);
77 MODULE_PARM_DESC(use_tcp_bonding,
78 "use_tcp_bonding parameter has been deprecated");
80 unsigned int lnet_numa_range = 0;
81 module_param(lnet_numa_range, uint, 0444);
82 MODULE_PARM_DESC(lnet_numa_range,
83 "NUMA range to consider during Multi-Rail selection");
86 * lnet_health_sensitivity determines by how much we decrement the health
87 * value on sending error. The value defaults to 100, which means health
88 * interface health is decremented by 100 points every failure.
90 unsigned int lnet_health_sensitivity = 100;
91 static int sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
92 #ifdef HAVE_KERNEL_PARAM_OPS
93 static struct kernel_param_ops param_ops_health_sensitivity = {
94 .set = sensitivity_set,
97 #define param_check_health_sensitivity(name, p) \
98 __param_check(name, p, int)
99 module_param(lnet_health_sensitivity, health_sensitivity, S_IRUGO|S_IWUSR);
101 module_param_call(lnet_health_sensitivity, sensitivity_set, param_get_int,
102 &lnet_health_sensitivity, S_IRUGO|S_IWUSR);
104 MODULE_PARM_DESC(lnet_health_sensitivity,
105 "Value to decrement the health value by on error");
108 * lnet_recovery_interval determines how often we should perform recovery
109 * on unhealthy interfaces.
111 unsigned int lnet_recovery_interval = 1;
112 static int recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp);
113 #ifdef HAVE_KERNEL_PARAM_OPS
114 static struct kernel_param_ops param_ops_recovery_interval = {
115 .set = recovery_interval_set,
116 .get = param_get_int,
118 #define param_check_recovery_interval(name, p) \
119 __param_check(name, p, int)
120 module_param(lnet_recovery_interval, recovery_interval, S_IRUGO|S_IWUSR);
122 module_param_call(lnet_recovery_interval, recovery_interval_set, param_get_int,
123 &lnet_recovery_interval, S_IRUGO|S_IWUSR);
125 MODULE_PARM_DESC(lnet_recovery_interval,
126 "Interval to recover unhealthy interfaces in seconds");
128 unsigned int lnet_recovery_limit;
129 module_param(lnet_recovery_limit, uint, 0644);
130 MODULE_PARM_DESC(lnet_recovery_limit,
131 "How long to attempt recovery of unhealthy peer interfaces in seconds. Set to 0 to allow indefinite recovery");
133 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
134 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
136 static struct kernel_param_ops param_ops_interfaces_max = {
138 .get = param_get_int,
141 #define param_check_interfaces_max(name, p) \
142 __param_check(name, p, int)
144 #ifdef HAVE_KERNEL_PARAM_OPS
145 module_param(lnet_interfaces_max, interfaces_max, 0644);
147 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
148 ¶m_ops_interfaces_max, 0644);
150 MODULE_PARM_DESC(lnet_interfaces_max,
151 "Maximum number of interfaces in a node.");
153 unsigned lnet_peer_discovery_disabled = 0;
154 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
156 static struct kernel_param_ops param_ops_discovery_disabled = {
157 .set = discovery_set,
158 .get = param_get_int,
161 #define param_check_discovery_disabled(name, p) \
162 __param_check(name, p, int)
163 #ifdef HAVE_KERNEL_PARAM_OPS
164 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
166 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
167 ¶m_ops_discovery_disabled, 0644);
169 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
170 "Set to 1 to disable peer discovery on this node.");
172 unsigned int lnet_drop_asym_route;
173 static int drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp);
175 static struct kernel_param_ops param_ops_drop_asym_route = {
176 .set = drop_asym_route_set,
177 .get = param_get_int,
180 #define param_check_drop_asym_route(name, p) \
181 __param_check(name, p, int)
182 #ifdef HAVE_KERNEL_PARAM_OPS
183 module_param(lnet_drop_asym_route, drop_asym_route, 0644);
185 module_param_call(lnet_drop_asym_route, drop_asym_route_set, param_get_int,
186 ¶m_ops_drop_asym_route, 0644);
188 MODULE_PARM_DESC(lnet_drop_asym_route,
189 "Set to 1 to drop asymmetrical route messages.");
191 #define LNET_TRANSACTION_TIMEOUT_DEFAULT 50
192 unsigned int lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_DEFAULT;
193 static int transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp);
194 #ifdef HAVE_KERNEL_PARAM_OPS
195 static struct kernel_param_ops param_ops_transaction_timeout = {
196 .set = transaction_to_set,
197 .get = param_get_int,
200 #define param_check_transaction_timeout(name, p) \
201 __param_check(name, p, int)
202 module_param(lnet_transaction_timeout, transaction_timeout, S_IRUGO|S_IWUSR);
204 module_param_call(lnet_transaction_timeout, transaction_to_set, param_get_int,
205 &lnet_transaction_timeout, S_IRUGO|S_IWUSR);
207 MODULE_PARM_DESC(lnet_transaction_timeout,
208 "Maximum number of seconds to wait for a peer response.");
210 #define LNET_RETRY_COUNT_DEFAULT 2
211 unsigned int lnet_retry_count = LNET_RETRY_COUNT_DEFAULT;
212 static int retry_count_set(const char *val, cfs_kernel_param_arg_t *kp);
213 #ifdef HAVE_KERNEL_PARAM_OPS
214 static struct kernel_param_ops param_ops_retry_count = {
215 .set = retry_count_set,
216 .get = param_get_int,
219 #define param_check_retry_count(name, p) \
220 __param_check(name, p, int)
221 module_param(lnet_retry_count, retry_count, S_IRUGO|S_IWUSR);
223 module_param_call(lnet_retry_count, retry_count_set, param_get_int,
224 &lnet_retry_count, S_IRUGO|S_IWUSR);
226 MODULE_PARM_DESC(lnet_retry_count,
227 "Maximum number of times to retry transmitting a message");
229 unsigned int lnet_response_tracking = 3;
230 static int response_tracking_set(const char *val, cfs_kernel_param_arg_t *kp);
232 #ifdef HAVE_KERNEL_PARAM_OPS
233 static struct kernel_param_ops param_ops_response_tracking = {
234 .set = response_tracking_set,
235 .get = param_get_int,
238 #define param_check_response_tracking(name, p) \
239 __param_check(name, p, int)
240 module_param(lnet_response_tracking, response_tracking, 0644);
242 module_param_call(lnet_response_tracking, response_tracking_set, param_get_int,
243 &lnet_response_tracking, 0644);
245 MODULE_PARM_DESC(lnet_response_tracking,
246 "(0|1|2|3) LNet Internal Only|GET Reply only|PUT ACK only|Full Tracking (default)");
248 #define LNET_LND_TIMEOUT_DEFAULT ((LNET_TRANSACTION_TIMEOUT_DEFAULT - 1) / \
249 (LNET_RETRY_COUNT_DEFAULT + 1))
250 unsigned int lnet_lnd_timeout = LNET_LND_TIMEOUT_DEFAULT;
251 static void lnet_set_lnd_timeout(void)
253 lnet_lnd_timeout = (lnet_transaction_timeout - 1) /
254 (lnet_retry_count + 1);
257 unsigned int lnet_current_net_count;
260 * This sequence number keeps track of how many times DLC was used to
261 * update the local NIs. It is incremented when a NI is added or
262 * removed and checked when sending a message to determine if there is
263 * a need to re-run the selection algorithm. See lnet_select_pathway()
264 * for more details on its usage.
266 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
268 static int lnet_ping(struct lnet_process_id id, signed long timeout,
269 struct lnet_process_id __user *ids, int n_ids);
271 static int lnet_discover(struct lnet_process_id id, __u32 force,
272 struct lnet_process_id __user *ids, int n_ids);
275 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
278 unsigned *sensitivity = (unsigned *)kp->arg;
281 rc = kstrtoul(val, 0, &value);
283 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
288 * The purpose of locking the api_mutex here is to ensure that
289 * the correct value ends up stored properly.
291 mutex_lock(&the_lnet.ln_api_mutex);
293 if (value > LNET_MAX_HEALTH_VALUE) {
294 mutex_unlock(&the_lnet.ln_api_mutex);
295 CERROR("Invalid health value. Maximum: %d value = %lu\n",
296 LNET_MAX_HEALTH_VALUE, value);
300 if (*sensitivity != 0 && value == 0 && lnet_retry_count != 0) {
301 lnet_retry_count = 0;
302 lnet_set_lnd_timeout();
305 *sensitivity = value;
307 mutex_unlock(&the_lnet.ln_api_mutex);
313 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
316 unsigned *interval = (unsigned *)kp->arg;
319 rc = kstrtoul(val, 0, &value);
321 CERROR("Invalid module parameter value for 'lnet_recovery_interval'\n");
326 CERROR("lnet_recovery_interval must be at least 1 second\n");
331 * The purpose of locking the api_mutex here is to ensure that
332 * the correct value ends up stored properly.
334 mutex_lock(&the_lnet.ln_api_mutex);
338 mutex_unlock(&the_lnet.ln_api_mutex);
344 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
347 unsigned *discovery_off = (unsigned *)kp->arg;
349 struct lnet_ping_buffer *pbuf;
351 rc = kstrtoul(val, 0, &value);
353 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
357 value = (value) ? 1 : 0;
360 * The purpose of locking the api_mutex here is to ensure that
361 * the correct value ends up stored properly.
363 mutex_lock(&the_lnet.ln_api_mutex);
365 if (value == *discovery_off) {
366 mutex_unlock(&the_lnet.ln_api_mutex);
371 * We still want to set the discovery value even when LNet is not
372 * running. This is the case when LNet is being loaded and we want
373 * the module parameters to take effect. Otherwise if we're
374 * changing the value dynamically, we want to set it after
377 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
378 *discovery_off = value;
379 mutex_unlock(&the_lnet.ln_api_mutex);
383 /* tell peers that discovery setting has changed */
384 lnet_net_lock(LNET_LOCK_EX);
385 pbuf = the_lnet.ln_ping_target;
387 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
389 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
390 lnet_net_unlock(LNET_LOCK_EX);
392 /* only send a push when we're turning off discovery */
393 if (*discovery_off <= 0 && value > 0)
394 lnet_push_update_to_peers(1);
395 *discovery_off = value;
397 mutex_unlock(&the_lnet.ln_api_mutex);
403 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
406 unsigned int *drop_asym_route = (unsigned int *)kp->arg;
409 rc = kstrtoul(val, 0, &value);
411 CERROR("Invalid module parameter value for "
412 "'lnet_drop_asym_route'\n");
417 * The purpose of locking the api_mutex here is to ensure that
418 * the correct value ends up stored properly.
420 mutex_lock(&the_lnet.ln_api_mutex);
422 if (value == *drop_asym_route) {
423 mutex_unlock(&the_lnet.ln_api_mutex);
427 *drop_asym_route = value;
429 mutex_unlock(&the_lnet.ln_api_mutex);
435 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
438 unsigned *transaction_to = (unsigned *)kp->arg;
441 rc = kstrtoul(val, 0, &value);
443 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
448 * The purpose of locking the api_mutex here is to ensure that
449 * the correct value ends up stored properly.
451 mutex_lock(&the_lnet.ln_api_mutex);
453 if (value <= lnet_retry_count || value == 0) {
454 mutex_unlock(&the_lnet.ln_api_mutex);
455 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
456 "Has to be greater than lnet_retry_count (%u)\n",
457 value, lnet_retry_count);
461 if (value == *transaction_to) {
462 mutex_unlock(&the_lnet.ln_api_mutex);
466 *transaction_to = value;
467 /* Update the lnet_lnd_timeout now that we've modified the
468 * transaction timeout
470 lnet_set_lnd_timeout();
472 mutex_unlock(&the_lnet.ln_api_mutex);
478 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
481 unsigned *retry_count = (unsigned *)kp->arg;
484 rc = kstrtoul(val, 0, &value);
486 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
491 * The purpose of locking the api_mutex here is to ensure that
492 * the correct value ends up stored properly.
494 mutex_lock(&the_lnet.ln_api_mutex);
496 if (lnet_health_sensitivity == 0 && value > 0) {
497 mutex_unlock(&the_lnet.ln_api_mutex);
498 CERROR("Can not set lnet_retry_count when health feature is turned off\n");
502 if (value > lnet_transaction_timeout) {
503 mutex_unlock(&the_lnet.ln_api_mutex);
504 CERROR("Invalid value for lnet_retry_count (%lu). "
505 "Has to be smaller than lnet_transaction_timeout (%u)\n",
506 value, lnet_transaction_timeout);
510 *retry_count = value;
512 /* Update the lnet_lnd_timeout now that we've modified the
515 lnet_set_lnd_timeout();
517 mutex_unlock(&the_lnet.ln_api_mutex);
523 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
527 rc = kstrtoint(val, 0, &value);
529 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
533 if (value < LNET_INTERFACES_MIN) {
534 CWARN("max interfaces provided are too small, setting to %d\n",
535 LNET_INTERFACES_MAX_DEFAULT);
536 value = LNET_INTERFACES_MAX_DEFAULT;
539 *(int *)kp->arg = value;
545 response_tracking_set(const char *val, cfs_kernel_param_arg_t *kp)
548 unsigned long new_value;
550 rc = kstrtoul(val, 0, &new_value);
552 CERROR("Invalid value for 'lnet_response_tracking'\n");
556 if (new_value < 0 || new_value > 3) {
557 CWARN("Invalid value (%lu) for 'lnet_response_tracking'\n",
562 lnet_response_tracking = new_value;
568 lnet_get_routes(void)
574 lnet_get_networks(void)
579 if (*networks != 0 && *ip2nets != 0) {
580 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
581 "'ip2nets' but not both at once\n");
586 rc = lnet_parse_ip2nets(&nets, ip2nets);
587 return (rc == 0) ? nets : NULL;
597 lnet_init_locks(void)
599 spin_lock_init(&the_lnet.ln_eq_wait_lock);
600 spin_lock_init(&the_lnet.ln_msg_resend_lock);
601 init_completion(&the_lnet.ln_mt_wait_complete);
602 mutex_init(&the_lnet.ln_lnd_mutex);
605 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
606 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
608 struct kmem_cache *lnet_udsp_cachep; /* udsp cache */
609 struct kmem_cache *lnet_rspt_cachep; /* response tracker cache */
610 struct kmem_cache *lnet_msg_cachep;
613 lnet_slab_setup(void)
615 /* create specific kmem_cache for MEs and small MDs (i.e., originally
616 * allocated in <size-xxx> kmem_cache).
618 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
620 if (!lnet_mes_cachep)
623 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
624 LNET_SMALL_MD_SIZE, 0, 0,
626 if (!lnet_small_mds_cachep)
629 lnet_udsp_cachep = kmem_cache_create("lnet_udsp",
630 sizeof(struct lnet_udsp),
632 if (!lnet_udsp_cachep)
635 lnet_rspt_cachep = kmem_cache_create("lnet_rspt", sizeof(struct lnet_rsp_tracker),
637 if (!lnet_rspt_cachep)
640 lnet_msg_cachep = kmem_cache_create("lnet_msg", sizeof(struct lnet_msg),
642 if (!lnet_msg_cachep)
649 lnet_slab_cleanup(void)
651 if (lnet_msg_cachep) {
652 kmem_cache_destroy(lnet_msg_cachep);
653 lnet_msg_cachep = NULL;
656 if (lnet_rspt_cachep) {
657 kmem_cache_destroy(lnet_rspt_cachep);
658 lnet_rspt_cachep = NULL;
661 if (lnet_udsp_cachep) {
662 kmem_cache_destroy(lnet_udsp_cachep);
663 lnet_udsp_cachep = NULL;
666 if (lnet_small_mds_cachep) {
667 kmem_cache_destroy(lnet_small_mds_cachep);
668 lnet_small_mds_cachep = NULL;
671 if (lnet_mes_cachep) {
672 kmem_cache_destroy(lnet_mes_cachep);
673 lnet_mes_cachep = NULL;
678 lnet_create_remote_nets_table(void)
681 struct list_head *hash;
683 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
684 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
685 CFS_ALLOC_PTR_ARRAY(hash, LNET_REMOTE_NETS_HASH_SIZE);
687 CERROR("Failed to create remote nets hash table\n");
691 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
692 INIT_LIST_HEAD(&hash[i]);
693 the_lnet.ln_remote_nets_hash = hash;
698 lnet_destroy_remote_nets_table(void)
702 if (the_lnet.ln_remote_nets_hash == NULL)
705 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
706 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
708 CFS_FREE_PTR_ARRAY(the_lnet.ln_remote_nets_hash,
709 LNET_REMOTE_NETS_HASH_SIZE);
710 the_lnet.ln_remote_nets_hash = NULL;
714 lnet_destroy_locks(void)
716 if (the_lnet.ln_res_lock != NULL) {
717 cfs_percpt_lock_free(the_lnet.ln_res_lock);
718 the_lnet.ln_res_lock = NULL;
721 if (the_lnet.ln_net_lock != NULL) {
722 cfs_percpt_lock_free(the_lnet.ln_net_lock);
723 the_lnet.ln_net_lock = NULL;
728 lnet_create_locks(void)
732 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
733 if (the_lnet.ln_res_lock == NULL)
736 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
737 if (the_lnet.ln_net_lock == NULL)
743 lnet_destroy_locks();
747 static void lnet_assert_wire_constants(void)
749 /* Wire protocol assertions generated by 'wirecheck'
750 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
751 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
752 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
755 BUILD_BUG_ON(LNET_PROTO_TCP_MAGIC != 0xeebc0ded);
756 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MAJOR != 1);
757 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MINOR != 0);
758 BUILD_BUG_ON(LNET_MSG_ACK != 0);
759 BUILD_BUG_ON(LNET_MSG_PUT != 1);
760 BUILD_BUG_ON(LNET_MSG_GET != 2);
761 BUILD_BUG_ON(LNET_MSG_REPLY != 3);
762 BUILD_BUG_ON(LNET_MSG_HELLO != 4);
764 /* Checks for struct lnet_handle_wire */
765 BUILD_BUG_ON((int)sizeof(struct lnet_handle_wire) != 16);
766 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
767 wh_interface_cookie) != 0);
768 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) != 8);
769 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
770 wh_object_cookie) != 8);
771 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) != 8);
773 /* Checks for struct struct lnet_magicversion */
774 BUILD_BUG_ON((int)sizeof(struct lnet_magicversion) != 8);
775 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, magic) != 0);
776 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->magic) != 4);
777 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, version_major) != 4);
778 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_major) != 2);
779 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion,
780 version_minor) != 6);
781 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_minor) != 2);
783 /* Checks for struct struct lnet_hdr */
784 BUILD_BUG_ON((int)sizeof(struct lnet_hdr) != 72);
785 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_nid) != 0);
786 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_nid) != 8);
787 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_nid) != 8);
788 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_nid) != 8);
789 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_pid) != 16);
790 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_pid) != 4);
791 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_pid) != 20);
792 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_pid) != 4);
793 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, type) != 24);
794 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->type) != 4);
795 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, payload_length) != 28);
796 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->payload_length) != 4);
797 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg) != 32);
798 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg) != 40);
801 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) != 32);
802 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) != 16);
803 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.match_bits) != 48);
804 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) != 8);
805 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.mlength) != 56);
806 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) != 4);
809 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) != 32);
810 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) != 16);
811 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.match_bits) != 48);
812 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) != 8);
813 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.hdr_data) != 56);
814 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) != 8);
815 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ptl_index) != 64);
816 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) != 4);
817 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.offset) != 68);
818 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) != 4);
821 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.return_wmd) != 32);
822 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) != 16);
823 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.match_bits) != 48);
824 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) != 8);
825 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.ptl_index) != 56);
826 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) != 4);
827 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.src_offset) != 60);
828 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) != 4);
829 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.sink_length) != 64);
830 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) != 4);
833 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) != 32);
834 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) != 16);
837 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.incarnation) != 32);
838 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) != 8);
839 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.type) != 40);
840 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) != 4);
842 /* Checks for struct lnet_ni_status and related constants */
843 BUILD_BUG_ON(LNET_NI_STATUS_INVALID != 0x00000000);
844 BUILD_BUG_ON(LNET_NI_STATUS_UP != 0x15aac0de);
845 BUILD_BUG_ON(LNET_NI_STATUS_DOWN != 0xdeadface);
847 /* Checks for struct lnet_ni_status */
848 BUILD_BUG_ON((int)sizeof(struct lnet_ni_status) != 16);
849 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_nid) != 0);
850 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) != 8);
851 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_status) != 8);
852 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_status) != 4);
853 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_unused) != 12);
854 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) != 4);
856 /* Checks for struct lnet_ping_info and related constants */
857 BUILD_BUG_ON(LNET_PROTO_PING_MAGIC != 0x70696E67);
858 BUILD_BUG_ON(LNET_PING_FEAT_INVAL != 0);
859 BUILD_BUG_ON(LNET_PING_FEAT_BASE != 1);
860 BUILD_BUG_ON(LNET_PING_FEAT_NI_STATUS != 2);
861 BUILD_BUG_ON(LNET_PING_FEAT_RTE_DISABLED != 4);
862 BUILD_BUG_ON(LNET_PING_FEAT_MULTI_RAIL != 8);
863 BUILD_BUG_ON(LNET_PING_FEAT_DISCOVERY != 16);
864 BUILD_BUG_ON(LNET_PING_FEAT_BITS != 31);
866 /* Checks for struct lnet_ping_info */
867 BUILD_BUG_ON((int)sizeof(struct lnet_ping_info) != 16);
868 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_magic) != 0);
869 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) != 4);
870 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_features) != 4);
871 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_features) != 4);
872 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_pid) != 8);
873 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) != 4);
874 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_nnis) != 12);
875 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) != 4);
876 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_ni) != 16);
877 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) != 0);
880 static const struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
882 const struct lnet_lnd *lnd;
884 /* holding lnd mutex */
885 if (type >= NUM_LNDS)
887 lnd = the_lnet.ln_lnds[type];
888 LASSERT(!lnd || lnd->lnd_type == type);
894 lnet_get_lnd_timeout(void)
896 return lnet_lnd_timeout;
898 EXPORT_SYMBOL(lnet_get_lnd_timeout);
901 lnet_register_lnd(const struct lnet_lnd *lnd)
903 mutex_lock(&the_lnet.ln_lnd_mutex);
905 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
906 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
908 the_lnet.ln_lnds[lnd->lnd_type] = lnd;
910 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
912 mutex_unlock(&the_lnet.ln_lnd_mutex);
914 EXPORT_SYMBOL(lnet_register_lnd);
917 lnet_unregister_lnd(const struct lnet_lnd *lnd)
919 mutex_lock(&the_lnet.ln_lnd_mutex);
921 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
923 the_lnet.ln_lnds[lnd->lnd_type] = NULL;
924 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
926 mutex_unlock(&the_lnet.ln_lnd_mutex);
928 EXPORT_SYMBOL(lnet_unregister_lnd);
931 lnet_counters_get_common_locked(struct lnet_counters_common *common)
933 struct lnet_counters *ctr;
936 /* FIXME !!! Their is no assert_lnet_net_locked() to ensure this
937 * actually called under the protection of the lnet_net_lock.
939 memset(common, 0, sizeof(*common));
941 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
942 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
943 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
944 common->lcc_errors += ctr->lct_common.lcc_errors;
945 common->lcc_send_count += ctr->lct_common.lcc_send_count;
946 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
947 common->lcc_route_count += ctr->lct_common.lcc_route_count;
948 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
949 common->lcc_send_length += ctr->lct_common.lcc_send_length;
950 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
951 common->lcc_route_length += ctr->lct_common.lcc_route_length;
952 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
957 lnet_counters_get_common(struct lnet_counters_common *common)
959 lnet_net_lock(LNET_LOCK_EX);
960 lnet_counters_get_common_locked(common);
961 lnet_net_unlock(LNET_LOCK_EX);
963 EXPORT_SYMBOL(lnet_counters_get_common);
966 lnet_counters_get(struct lnet_counters *counters)
968 struct lnet_counters *ctr;
969 struct lnet_counters_health *health = &counters->lct_health;
972 memset(counters, 0, sizeof(*counters));
974 lnet_net_lock(LNET_LOCK_EX);
976 if (the_lnet.ln_state != LNET_STATE_RUNNING)
977 GOTO(out_unlock, rc = -ENODEV);
979 lnet_counters_get_common_locked(&counters->lct_common);
981 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
982 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
983 health->lch_resend_count += ctr->lct_health.lch_resend_count;
984 health->lch_response_timeout_count +=
985 ctr->lct_health.lch_response_timeout_count;
986 health->lch_local_interrupt_count +=
987 ctr->lct_health.lch_local_interrupt_count;
988 health->lch_local_dropped_count +=
989 ctr->lct_health.lch_local_dropped_count;
990 health->lch_local_aborted_count +=
991 ctr->lct_health.lch_local_aborted_count;
992 health->lch_local_no_route_count +=
993 ctr->lct_health.lch_local_no_route_count;
994 health->lch_local_timeout_count +=
995 ctr->lct_health.lch_local_timeout_count;
996 health->lch_local_error_count +=
997 ctr->lct_health.lch_local_error_count;
998 health->lch_remote_dropped_count +=
999 ctr->lct_health.lch_remote_dropped_count;
1000 health->lch_remote_error_count +=
1001 ctr->lct_health.lch_remote_error_count;
1002 health->lch_remote_timeout_count +=
1003 ctr->lct_health.lch_remote_timeout_count;
1004 health->lch_network_timeout_count +=
1005 ctr->lct_health.lch_network_timeout_count;
1008 lnet_net_unlock(LNET_LOCK_EX);
1011 EXPORT_SYMBOL(lnet_counters_get);
1014 lnet_counters_reset(void)
1016 struct lnet_counters *counters;
1019 lnet_net_lock(LNET_LOCK_EX);
1021 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1024 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
1025 memset(counters, 0, sizeof(struct lnet_counters));
1027 lnet_net_unlock(LNET_LOCK_EX);
1031 lnet_res_type2str(int type)
1036 case LNET_COOKIE_TYPE_MD:
1038 case LNET_COOKIE_TYPE_ME:
1040 case LNET_COOKIE_TYPE_EQ:
1046 lnet_res_container_cleanup(struct lnet_res_container *rec)
1050 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
1053 while (!list_empty(&rec->rec_active)) {
1054 struct list_head *e = rec->rec_active.next;
1057 if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
1058 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
1060 } else { /* NB: Active MEs should be attached on portals */
1067 /* Found alive MD/ME/EQ, user really should unlink/free
1068 * all of them before finalize LNet, but if someone didn't,
1069 * we have to recycle garbage for him */
1070 CERROR("%d active elements on exit of %s container\n",
1071 count, lnet_res_type2str(rec->rec_type));
1074 if (rec->rec_lh_hash != NULL) {
1075 CFS_FREE_PTR_ARRAY(rec->rec_lh_hash, LNET_LH_HASH_SIZE);
1076 rec->rec_lh_hash = NULL;
1079 rec->rec_type = 0; /* mark it as finalized */
1083 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1088 LASSERT(rec->rec_type == 0);
1090 rec->rec_type = type;
1091 INIT_LIST_HEAD(&rec->rec_active);
1093 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1095 /* Arbitrary choice of hash table size */
1096 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1097 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1098 if (rec->rec_lh_hash == NULL) {
1103 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1104 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1109 CERROR("Failed to setup %s resource container\n",
1110 lnet_res_type2str(type));
1111 lnet_res_container_cleanup(rec);
1116 lnet_res_containers_destroy(struct lnet_res_container **recs)
1118 struct lnet_res_container *rec;
1121 cfs_percpt_for_each(rec, i, recs)
1122 lnet_res_container_cleanup(rec);
1124 cfs_percpt_free(recs);
1127 static struct lnet_res_container **
1128 lnet_res_containers_create(int type)
1130 struct lnet_res_container **recs;
1131 struct lnet_res_container *rec;
1135 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1137 CERROR("Failed to allocate %s resource containers\n",
1138 lnet_res_type2str(type));
1142 cfs_percpt_for_each(rec, i, recs) {
1143 rc = lnet_res_container_setup(rec, i, type);
1145 lnet_res_containers_destroy(recs);
1153 struct lnet_libhandle *
1154 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1156 /* ALWAYS called with lnet_res_lock held */
1157 struct list_head *head;
1158 struct lnet_libhandle *lh;
1161 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1164 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1165 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1167 list_for_each_entry(lh, head, lh_hash_chain) {
1168 if (lh->lh_cookie == cookie)
1176 lnet_res_lh_initialize(struct lnet_res_container *rec,
1177 struct lnet_libhandle *lh)
1179 /* ALWAYS called with lnet_res_lock held */
1180 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1183 lh->lh_cookie = rec->rec_lh_cookie;
1184 rec->rec_lh_cookie += 1 << ibits;
1186 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1188 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1192 lnet_create_array_of_queues(void)
1194 struct list_head **qs;
1195 struct list_head *q;
1198 qs = cfs_percpt_alloc(lnet_cpt_table(),
1199 sizeof(struct list_head));
1201 CERROR("Failed to allocate queues\n");
1205 cfs_percpt_for_each(q, i, qs)
1211 static int lnet_unprepare(void);
1214 lnet_prepare(lnet_pid_t requested_pid)
1216 /* Prepare to bring up the network */
1217 struct lnet_res_container **recs;
1220 if (requested_pid == LNET_PID_ANY) {
1221 /* Don't instantiate LNET just for me */
1225 LASSERT(the_lnet.ln_refcount == 0);
1227 the_lnet.ln_routing = 0;
1229 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1230 the_lnet.ln_pid = requested_pid;
1232 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1233 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1234 INIT_LIST_HEAD(&the_lnet.ln_nets);
1235 INIT_LIST_HEAD(&the_lnet.ln_routers);
1236 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1237 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1238 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1239 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1240 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1241 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1242 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1243 INIT_LIST_HEAD(&the_lnet.ln_udsp_list);
1244 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1245 the_lnet.ln_mt_handler = NULL;
1246 init_completion(&the_lnet.ln_started);
1248 rc = lnet_slab_setup();
1252 rc = lnet_create_remote_nets_table();
1257 * NB the interface cookie in wire handles guards against delayed
1258 * replies and ACKs appearing valid after reboot.
1260 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1262 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1263 sizeof(struct lnet_counters));
1264 if (the_lnet.ln_counters == NULL) {
1265 CERROR("Failed to allocate counters for LNet\n");
1270 rc = lnet_peer_tables_create();
1274 rc = lnet_msg_containers_create();
1278 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1279 LNET_COOKIE_TYPE_EQ);
1283 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1289 the_lnet.ln_md_containers = recs;
1291 rc = lnet_portals_create();
1293 CERROR("Failed to create portals for LNet: %d\n", rc);
1297 the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1298 if (!the_lnet.ln_mt_zombie_rstqs) {
1311 lnet_unprepare (void)
1313 /* NB no LNET_LOCK since this is the last reference. All LND instances
1314 * have shut down already, so it is safe to unlink and free all
1315 * descriptors, even those that appear committed to a network op (eg MD
1316 * with non-zero pending count) */
1318 lnet_fail_nid(LNET_NID_ANY, 0);
1320 LASSERT(the_lnet.ln_refcount == 0);
1321 LASSERT(list_empty(&the_lnet.ln_test_peers));
1322 LASSERT(list_empty(&the_lnet.ln_nets));
1324 if (the_lnet.ln_mt_zombie_rstqs) {
1325 lnet_clean_zombie_rstqs();
1326 the_lnet.ln_mt_zombie_rstqs = NULL;
1329 lnet_assert_handler_unused(the_lnet.ln_mt_handler);
1330 the_lnet.ln_mt_handler = NULL;
1332 lnet_portals_destroy();
1334 if (the_lnet.ln_md_containers != NULL) {
1335 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1336 the_lnet.ln_md_containers = NULL;
1339 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1341 lnet_msg_containers_destroy();
1343 lnet_rtrpools_free(0);
1345 if (the_lnet.ln_counters != NULL) {
1346 cfs_percpt_free(the_lnet.ln_counters);
1347 the_lnet.ln_counters = NULL;
1349 lnet_destroy_remote_nets_table();
1350 lnet_udsp_destroy(true);
1351 lnet_slab_cleanup();
1357 lnet_net2ni_locked(__u32 net_id, int cpt)
1360 struct lnet_net *net;
1362 LASSERT(cpt != LNET_LOCK_EX);
1364 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1365 if (net->net_id == net_id) {
1366 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1376 lnet_net2ni_addref(__u32 net)
1381 ni = lnet_net2ni_locked(net, 0);
1383 lnet_ni_addref_locked(ni, 0);
1388 EXPORT_SYMBOL(lnet_net2ni_addref);
1391 lnet_get_net_locked(__u32 net_id)
1393 struct lnet_net *net;
1395 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1396 if (net->net_id == net_id)
1404 lnet_net_clr_pref_rtrs(struct lnet_net *net)
1406 struct list_head zombies;
1407 struct lnet_nid_list *ne;
1408 struct lnet_nid_list *tmp;
1410 INIT_LIST_HEAD(&zombies);
1412 lnet_net_lock(LNET_LOCK_EX);
1413 list_splice_init(&net->net_rtr_pref_nids, &zombies);
1414 lnet_net_unlock(LNET_LOCK_EX);
1416 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1417 list_del_init(&ne->nl_list);
1418 LIBCFS_FREE(ne, sizeof(*ne));
1423 lnet_net_add_pref_rtr(struct lnet_net *net,
1425 __must_hold(&the_lnet.ln_api_mutex)
1427 struct lnet_nid_list *ne;
1429 /* This function is called with api_mutex held. When the api_mutex
1430 * is held the list can not be modified, as it is only modified as
1431 * a result of applying a UDSP and that happens under api_mutex
1434 list_for_each_entry(ne, &net->net_rtr_pref_nids, nl_list) {
1435 if (ne->nl_nid == gw_nid)
1439 LIBCFS_ALLOC(ne, sizeof(*ne));
1443 ne->nl_nid = gw_nid;
1445 /* Lock the cpt to protect against addition and checks in the
1446 * selection algorithm
1448 lnet_net_lock(LNET_LOCK_EX);
1449 list_add(&ne->nl_list, &net->net_rtr_pref_nids);
1450 lnet_net_unlock(LNET_LOCK_EX);
1456 lnet_net_is_pref_rtr_locked(struct lnet_net *net, lnet_nid_t rtr_nid)
1458 struct lnet_nid_list *ne;
1460 CDEBUG(D_NET, "%s: rtr pref emtpy: %d\n",
1461 libcfs_net2str(net->net_id),
1462 list_empty(&net->net_rtr_pref_nids));
1464 if (list_empty(&net->net_rtr_pref_nids))
1467 list_for_each_entry(ne, &net->net_rtr_pref_nids, nl_list) {
1468 CDEBUG(D_NET, "Comparing pref %s with gw %s\n",
1469 libcfs_nid2str(ne->nl_nid),
1470 libcfs_nid2str(rtr_nid));
1471 if (rtr_nid == ne->nl_nid)
1479 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1484 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1489 val = hash_long(key, LNET_CPT_BITS);
1490 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1494 return (unsigned int)(key + val + (val >> 1)) % number;
1498 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1500 struct lnet_net *net;
1502 /* must called with hold of lnet_net_lock */
1503 if (LNET_CPT_NUMBER == 1)
1504 return 0; /* the only one */
1507 * If NI is provided then use the CPT identified in the NI cpt
1508 * list if one exists. If one doesn't exist, then that NI is
1509 * associated with all CPTs and it follows that the net it belongs
1510 * to is implicitly associated with all CPTs, so just hash the nid
1514 if (ni->ni_cpts != NULL)
1515 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1518 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1521 /* no NI provided so look at the net */
1522 net = lnet_get_net_locked(LNET_NIDNET(nid));
1524 if (net != NULL && net->net_cpts != NULL) {
1525 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1528 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1532 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1537 if (LNET_CPT_NUMBER == 1)
1538 return 0; /* the only one */
1540 cpt = lnet_net_lock_current();
1542 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1544 lnet_net_unlock(cpt);
1548 EXPORT_SYMBOL(lnet_cpt_of_nid);
1551 lnet_islocalnet_locked(__u32 net_id)
1553 struct lnet_net *net;
1556 net = lnet_get_net_locked(net_id);
1558 local = net != NULL;
1564 lnet_islocalnet(__u32 net_id)
1569 cpt = lnet_net_lock_current();
1571 local = lnet_islocalnet_locked(net_id);
1573 lnet_net_unlock(cpt);
1579 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1581 struct lnet_net *net;
1584 LASSERT(cpt != LNET_LOCK_EX);
1586 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1587 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1588 if (ni->ni_nid == nid)
1597 lnet_nid2ni_addref(lnet_nid_t nid)
1602 ni = lnet_nid2ni_locked(nid, 0);
1604 lnet_ni_addref_locked(ni, 0);
1609 EXPORT_SYMBOL(lnet_nid2ni_addref);
1612 lnet_islocalnid(lnet_nid_t nid)
1617 cpt = lnet_net_lock_current();
1618 ni = lnet_nid2ni_locked(nid, cpt);
1619 lnet_net_unlock(cpt);
1625 lnet_count_acceptor_nets(void)
1627 /* Return the # of NIs that need the acceptor. */
1629 struct lnet_net *net;
1632 cpt = lnet_net_lock_current();
1633 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1634 /* all socklnd type networks should have the acceptor
1636 if (net->net_lnd->lnd_accept != NULL)
1640 lnet_net_unlock(cpt);
1645 struct lnet_ping_buffer *
1646 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1648 struct lnet_ping_buffer *pbuf;
1650 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1652 pbuf->pb_nnis = nnis;
1653 pbuf->pb_needs_post = false;
1654 atomic_set(&pbuf->pb_refcnt, 1);
1661 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1663 LASSERT(atomic_read(&pbuf->pb_refcnt) == 0);
1664 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1667 static struct lnet_ping_buffer *
1668 lnet_ping_target_create(int nnis)
1670 struct lnet_ping_buffer *pbuf;
1672 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1674 CERROR("Can't allocate ping source [%d]\n", nnis);
1678 pbuf->pb_info.pi_nnis = nnis;
1679 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1680 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1681 pbuf->pb_info.pi_features =
1682 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1688 lnet_get_net_ni_count_locked(struct lnet_net *net)
1693 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1700 lnet_get_net_ni_count_pre(struct lnet_net *net)
1705 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1712 lnet_get_ni_count(void)
1715 struct lnet_net *net;
1720 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1721 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1731 lnet_get_net_count(void)
1733 struct lnet_net *net;
1738 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1748 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1750 struct lnet_ni_status *stat;
1754 __swab32s(&pbuf->pb_info.pi_magic);
1755 __swab32s(&pbuf->pb_info.pi_features);
1756 __swab32s(&pbuf->pb_info.pi_pid);
1757 __swab32s(&pbuf->pb_info.pi_nnis);
1758 nnis = pbuf->pb_info.pi_nnis;
1759 if (nnis > pbuf->pb_nnis)
1760 nnis = pbuf->pb_nnis;
1761 for (i = 0; i < nnis; i++) {
1762 stat = &pbuf->pb_info.pi_ni[i];
1763 __swab64s(&stat->ns_nid);
1764 __swab32s(&stat->ns_status);
1769 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1773 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1775 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1777 /* Loopback is guaranteed to be present */
1778 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1780 if (LNET_PING_INFO_LONI(pinfo) != LNET_NID_LO_0)
1786 lnet_ping_target_destroy(void)
1788 struct lnet_net *net;
1791 lnet_net_lock(LNET_LOCK_EX);
1793 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1794 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1796 ni->ni_status = NULL;
1801 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1802 the_lnet.ln_ping_target = NULL;
1804 lnet_net_unlock(LNET_LOCK_EX);
1808 lnet_ping_target_event_handler(struct lnet_event *event)
1810 struct lnet_ping_buffer *pbuf = event->md_user_ptr;
1812 if (event->unlinked)
1813 lnet_ping_buffer_decref(pbuf);
1817 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1818 struct lnet_handle_md *ping_mdh,
1819 int ni_count, bool set_eq)
1821 struct lnet_process_id id = {
1822 .nid = LNET_NID_ANY,
1826 struct lnet_md md = { NULL };
1830 the_lnet.ln_ping_target_handler =
1831 lnet_ping_target_event_handler;
1833 *ppbuf = lnet_ping_target_create(ni_count);
1834 if (*ppbuf == NULL) {
1839 /* Ping target ME/MD */
1840 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1841 LNET_PROTO_PING_MATCHBITS, 0,
1842 LNET_UNLINK, LNET_INS_AFTER);
1845 CERROR("Can't create ping target ME: %d\n", rc);
1846 goto fail_decref_ping_buffer;
1849 /* initialize md content */
1850 md.start = &(*ppbuf)->pb_info;
1851 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1852 md.threshold = LNET_MD_THRESH_INF;
1854 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1855 LNET_MD_MANAGE_REMOTE;
1856 md.handler = the_lnet.ln_ping_target_handler;
1857 md.user_ptr = *ppbuf;
1859 rc = LNetMDAttach(me, &md, LNET_RETAIN, ping_mdh);
1861 CERROR("Can't attach ping target MD: %d\n", rc);
1862 goto fail_decref_ping_buffer;
1864 lnet_ping_buffer_addref(*ppbuf);
1868 fail_decref_ping_buffer:
1869 LASSERT(atomic_read(&(*ppbuf)->pb_refcnt) == 1);
1870 lnet_ping_buffer_decref(*ppbuf);
1877 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1878 struct lnet_handle_md *ping_mdh)
1880 LNetMDUnlink(*ping_mdh);
1881 LNetInvalidateMDHandle(ping_mdh);
1883 /* NB the MD could be busy; this just starts the unlink */
1884 wait_var_event_warning(&pbuf->pb_refcnt,
1885 atomic_read(&pbuf->pb_refcnt) <= 1,
1886 "Still waiting for ping data MD to unlink\n");
1890 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1893 struct lnet_net *net;
1894 struct lnet_ni_status *ns;
1899 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1900 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1901 LASSERT(i < pbuf->pb_nnis);
1903 ns = &pbuf->pb_info.pi_ni[i];
1905 ns->ns_nid = ni->ni_nid;
1908 ns->ns_status = (ni->ni_status != NULL) ?
1909 ni->ni_status->ns_status :
1918 * We (ab)use the ns_status of the loopback interface to
1919 * transmit the sequence number. The first interface listed
1920 * must be the loopback interface.
1922 rc = lnet_ping_info_validate(&pbuf->pb_info);
1924 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1927 LNET_PING_BUFFER_SEQNO(pbuf) =
1928 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1932 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1933 struct lnet_handle_md ping_mdh)
1935 struct lnet_ping_buffer *old_pbuf = NULL;
1936 struct lnet_handle_md old_ping_md;
1938 /* switch the NIs to point to the new ping info created */
1939 lnet_net_lock(LNET_LOCK_EX);
1941 if (!the_lnet.ln_routing)
1942 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1943 if (!lnet_peer_discovery_disabled)
1944 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1946 /* Ensure only known feature bits have been set. */
1947 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1948 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1950 lnet_ping_target_install_locked(pbuf);
1952 if (the_lnet.ln_ping_target) {
1953 old_pbuf = the_lnet.ln_ping_target;
1954 old_ping_md = the_lnet.ln_ping_target_md;
1956 the_lnet.ln_ping_target_md = ping_mdh;
1957 the_lnet.ln_ping_target = pbuf;
1959 lnet_net_unlock(LNET_LOCK_EX);
1962 /* unlink and free the old ping info */
1963 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1964 lnet_ping_buffer_decref(old_pbuf);
1967 lnet_push_update_to_peers(0);
1971 lnet_ping_target_fini(void)
1973 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1974 &the_lnet.ln_ping_target_md);
1976 lnet_assert_handler_unused(the_lnet.ln_ping_target_handler);
1977 lnet_ping_target_destroy();
1980 /* Resize the push target. */
1981 int lnet_push_target_resize(void)
1983 struct lnet_handle_md mdh;
1984 struct lnet_handle_md old_mdh;
1985 struct lnet_ping_buffer *pbuf;
1986 struct lnet_ping_buffer *old_pbuf;
1991 nnis = the_lnet.ln_push_target_nnis;
1993 CDEBUG(D_NET, "Invalid nnis %d\n", nnis);
1997 /* NB: lnet_ping_buffer_alloc() sets pbuf refcount to 1. That ref is
1998 * dropped when we need to resize again (see "old_pbuf" below) or when
1999 * LNet is shutdown (see lnet_push_target_fini())
2001 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
2003 CDEBUG(D_NET, "Can't allocate pbuf for nnis %d\n", nnis);
2007 rc = lnet_push_target_post(pbuf, &mdh);
2009 CDEBUG(D_NET, "Failed to post push target: %d\n", rc);
2010 lnet_ping_buffer_decref(pbuf);
2014 lnet_net_lock(LNET_LOCK_EX);
2015 old_pbuf = the_lnet.ln_push_target;
2016 old_mdh = the_lnet.ln_push_target_md;
2017 the_lnet.ln_push_target = pbuf;
2018 the_lnet.ln_push_target_md = mdh;
2019 lnet_net_unlock(LNET_LOCK_EX);
2022 LNetMDUnlink(old_mdh);
2023 /* Drop ref set by lnet_ping_buffer_alloc() */
2024 lnet_ping_buffer_decref(old_pbuf);
2027 /* Received another push or reply that requires a larger buffer */
2028 if (nnis < the_lnet.ln_push_target_nnis)
2031 CDEBUG(D_NET, "nnis %d success\n", nnis);
2035 int lnet_push_target_post(struct lnet_ping_buffer *pbuf,
2036 struct lnet_handle_md *mdhp)
2038 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
2039 struct lnet_md md = { NULL };
2043 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
2044 LNET_PROTO_PING_MATCHBITS, 0,
2045 LNET_UNLINK, LNET_INS_AFTER);
2048 CERROR("Can't create push target ME: %d\n", rc);
2052 pbuf->pb_needs_post = false;
2054 /* This reference is dropped by lnet_push_target_event_handler() */
2055 lnet_ping_buffer_addref(pbuf);
2057 /* initialize md content */
2058 md.start = &pbuf->pb_info;
2059 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2062 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE;
2064 md.handler = the_lnet.ln_push_target_handler;
2066 rc = LNetMDAttach(me, &md, LNET_UNLINK, mdhp);
2068 CERROR("Can't attach push MD: %d\n", rc);
2069 lnet_ping_buffer_decref(pbuf);
2070 pbuf->pb_needs_post = true;
2074 CDEBUG(D_NET, "posted push target %p\n", pbuf);
2079 static void lnet_push_target_event_handler(struct lnet_event *ev)
2081 struct lnet_ping_buffer *pbuf = ev->md_user_ptr;
2083 CDEBUG(D_NET, "type %d status %d unlinked %d\n", ev->type, ev->status,
2086 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2087 lnet_swap_pinginfo(pbuf);
2089 if (ev->type == LNET_EVENT_UNLINK) {
2090 /* Drop ref added by lnet_push_target_post() */
2091 lnet_ping_buffer_decref(pbuf);
2095 lnet_peer_push_event(ev);
2097 /* Drop ref added by lnet_push_target_post */
2098 lnet_ping_buffer_decref(pbuf);
2101 /* Initialize the push target. */
2102 static int lnet_push_target_init(void)
2106 if (the_lnet.ln_push_target)
2109 the_lnet.ln_push_target_handler =
2110 lnet_push_target_event_handler;
2112 rc = LNetSetLazyPortal(LNET_RESERVED_PORTAL);
2115 /* Start at the required minimum, we'll enlarge if required. */
2116 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
2118 rc = lnet_push_target_resize();
2121 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2122 the_lnet.ln_push_target_handler = NULL;
2128 /* Clean up the push target. */
2129 static void lnet_push_target_fini(void)
2131 if (!the_lnet.ln_push_target)
2134 /* Unlink and invalidate to prevent new references. */
2135 LNetMDUnlink(the_lnet.ln_push_target_md);
2136 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
2138 /* Wait for the unlink to complete. */
2139 wait_var_event_warning(&the_lnet.ln_push_target->pb_refcnt,
2140 atomic_read(&the_lnet.ln_push_target->pb_refcnt) <= 1,
2141 "Still waiting for ping data MD to unlink\n");
2143 /* Drop ref set by lnet_ping_buffer_alloc() */
2144 lnet_ping_buffer_decref(the_lnet.ln_push_target);
2145 the_lnet.ln_push_target = NULL;
2146 the_lnet.ln_push_target_nnis = 0;
2148 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2149 lnet_assert_handler_unused(the_lnet.ln_push_target_handler);
2150 the_lnet.ln_push_target_handler = NULL;
2154 lnet_ni_tq_credits(struct lnet_ni *ni)
2158 LASSERT(ni->ni_ncpts >= 1);
2160 if (ni->ni_ncpts == 1)
2161 return ni->ni_net->net_tunables.lct_max_tx_credits;
2163 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2164 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2165 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2171 lnet_ni_unlink_locked(struct lnet_ni *ni)
2173 /* move it to zombie list and nobody can find it anymore */
2174 LASSERT(!list_empty(&ni->ni_netlist));
2175 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2176 lnet_ni_decref_locked(ni, 0);
2180 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2185 struct list_head *zombie_list = &net->net_ni_zombie;
2188 * Now wait for the NIs I just nuked to show up on the zombie
2189 * list and shut them down in guaranteed thread context
2192 while (!list_empty(zombie_list)) {
2196 ni = list_entry(zombie_list->next,
2197 struct lnet_ni, ni_netlist);
2198 list_del_init(&ni->ni_netlist);
2199 /* the ni should be in deleting state. If it's not it's
2201 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2202 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2205 /* still busy, add it back to zombie list */
2206 list_add(&ni->ni_netlist, zombie_list);
2210 if (!list_empty(&ni->ni_netlist)) {
2211 /* Unlock mutex while waiting to allow other
2212 * threads to read the LNet state and fall through
2215 lnet_net_unlock(LNET_LOCK_EX);
2216 mutex_unlock(&the_lnet.ln_api_mutex);
2219 if ((i & (-i)) == i) {
2221 "Waiting for zombie LNI %s\n",
2222 libcfs_nid2str(ni->ni_nid));
2224 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2226 mutex_lock(&the_lnet.ln_api_mutex);
2227 lnet_net_lock(LNET_LOCK_EX);
2231 lnet_net_unlock(LNET_LOCK_EX);
2233 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2235 LASSERT(!in_interrupt());
2236 /* Holding the mutex makes it safe for lnd_shutdown
2237 * to call module_put(). Module unload cannot finish
2238 * until lnet_unregister_lnd() completes, and that
2239 * requires the mutex.
2241 mutex_lock(&the_lnet.ln_lnd_mutex);
2242 (net->net_lnd->lnd_shutdown)(ni);
2243 mutex_unlock(&the_lnet.ln_lnd_mutex);
2246 CDEBUG(D_LNI, "Removed LNI %s\n",
2247 libcfs_nid2str(ni->ni_nid));
2251 lnet_net_lock(LNET_LOCK_EX);
2255 /* shutdown down the NI and release refcount */
2257 lnet_shutdown_lndni(struct lnet_ni *ni)
2260 struct lnet_net *net = ni->ni_net;
2262 lnet_net_lock(LNET_LOCK_EX);
2264 ni->ni_state = LNET_NI_STATE_DELETING;
2266 lnet_ni_unlink_locked(ni);
2267 lnet_incr_dlc_seq();
2268 lnet_net_unlock(LNET_LOCK_EX);
2270 /* clear messages for this NI on the lazy portal */
2271 for (i = 0; i < the_lnet.ln_nportals; i++)
2272 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2274 lnet_net_lock(LNET_LOCK_EX);
2275 lnet_clear_zombies_nis_locked(net);
2276 lnet_net_unlock(LNET_LOCK_EX);
2280 lnet_shutdown_lndnet(struct lnet_net *net)
2284 lnet_net_lock(LNET_LOCK_EX);
2286 list_del_init(&net->net_list);
2288 while (!list_empty(&net->net_ni_list)) {
2289 ni = list_entry(net->net_ni_list.next,
2290 struct lnet_ni, ni_netlist);
2291 lnet_net_unlock(LNET_LOCK_EX);
2292 lnet_shutdown_lndni(ni);
2293 lnet_net_lock(LNET_LOCK_EX);
2296 lnet_net_unlock(LNET_LOCK_EX);
2298 /* Do peer table cleanup for this net */
2299 lnet_peer_tables_cleanup(net);
2305 lnet_shutdown_lndnets(void)
2307 struct lnet_net *net;
2309 struct lnet_msg *msg, *tmp;
2311 /* NB called holding the global mutex */
2313 /* All quiet on the API front */
2314 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2315 LASSERT(the_lnet.ln_refcount == 0);
2317 lnet_net_lock(LNET_LOCK_EX);
2318 the_lnet.ln_state = LNET_STATE_STOPPING;
2321 * move the nets to the zombie list to avoid them being
2322 * picked up for new work. LONET is also included in the
2323 * Nets that will be moved to the zombie list
2325 list_splice_init(&the_lnet.ln_nets, &the_lnet.ln_net_zombie);
2327 /* Drop the cached loopback Net. */
2328 if (the_lnet.ln_loni != NULL) {
2329 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2330 the_lnet.ln_loni = NULL;
2332 lnet_net_unlock(LNET_LOCK_EX);
2334 /* iterate through the net zombie list and delete each net */
2335 while (!list_empty(&the_lnet.ln_net_zombie)) {
2336 net = list_entry(the_lnet.ln_net_zombie.next,
2337 struct lnet_net, net_list);
2338 lnet_shutdown_lndnet(net);
2341 spin_lock(&the_lnet.ln_msg_resend_lock);
2342 list_splice(&the_lnet.ln_msg_resend, &resend);
2343 spin_unlock(&the_lnet.ln_msg_resend_lock);
2345 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2346 list_del_init(&msg->msg_list);
2347 msg->msg_no_resend = true;
2348 lnet_finalize(msg, -ECANCELED);
2351 lnet_net_lock(LNET_LOCK_EX);
2352 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2353 lnet_net_unlock(LNET_LOCK_EX);
2357 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2360 struct lnet_tx_queue *tq;
2362 struct lnet_net *net = ni->ni_net;
2364 mutex_lock(&the_lnet.ln_lnd_mutex);
2367 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2368 ni->ni_lnd_tunables_set = true;
2371 rc = (net->net_lnd->lnd_startup)(ni);
2373 mutex_unlock(&the_lnet.ln_lnd_mutex);
2376 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2377 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2382 ni->ni_state = LNET_NI_STATE_ACTIVE;
2385 /* We keep a reference on the loopback net through the loopback NI */
2386 if (net->net_lnd->lnd_type == LOLND) {
2388 LASSERT(the_lnet.ln_loni == NULL);
2389 the_lnet.ln_loni = ni;
2390 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2391 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2392 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2393 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2397 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2398 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2399 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2400 libcfs_lnd2str(net->net_lnd->lnd_type),
2401 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2403 /* shutdown the NI since if we get here then it must've already
2406 lnet_shutdown_lndni(ni);
2410 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2411 tq->tq_credits_min =
2412 tq->tq_credits_max =
2413 tq->tq_credits = lnet_ni_tq_credits(ni);
2416 atomic_set(&ni->ni_tx_credits,
2417 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2418 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2420 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2421 libcfs_nid2str(ni->ni_nid),
2422 ni->ni_net->net_tunables.lct_peer_tx_credits,
2423 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2424 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2425 ni->ni_net->net_tunables.lct_peer_timeout);
2434 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2437 struct lnet_net *net_l = NULL;
2438 LIST_HEAD(local_ni_list);
2442 const struct lnet_lnd *lnd;
2444 net->net_tunables.lct_peer_timeout;
2446 net->net_tunables.lct_max_tx_credits;
2447 int peerrtrcredits =
2448 net->net_tunables.lct_peer_rtr_credits;
2451 * make sure that this net is unique. If it isn't then
2452 * we are adding interfaces to an already existing network, and
2453 * 'net' is just a convenient way to pass in the list.
2454 * if it is unique we need to find the LND and load it if
2457 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2458 lnd_type = LNET_NETTYP(net->net_id);
2460 mutex_lock(&the_lnet.ln_lnd_mutex);
2461 lnd = lnet_find_lnd_by_type(lnd_type);
2464 mutex_unlock(&the_lnet.ln_lnd_mutex);
2465 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2466 mutex_lock(&the_lnet.ln_lnd_mutex);
2468 lnd = lnet_find_lnd_by_type(lnd_type);
2470 mutex_unlock(&the_lnet.ln_lnd_mutex);
2471 CERROR("Can't load LND %s, module %s, rc=%d\n",
2472 libcfs_lnd2str(lnd_type),
2473 libcfs_lnd2modname(lnd_type), rc);
2474 #ifndef HAVE_MODULE_LOADING_SUPPORT
2475 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2476 "compiled with kernel module "
2477 "loading support.");
2486 mutex_unlock(&the_lnet.ln_lnd_mutex);
2492 * net_l: if the network being added is unique then net_l
2493 * will point to that network
2494 * if the network being added is not unique then
2495 * net_l points to the existing network.
2497 * When we enter the loop below, we'll pick NIs off he
2498 * network beign added and start them up, then add them to
2499 * a local ni list. Once we've successfully started all
2500 * the NIs then we join the local NI list (of started up
2501 * networks) with the net_l->net_ni_list, which should
2502 * point to the correct network to add the new ni list to
2504 * If any of the new NIs fail to start up, then we want to
2505 * iterate through the local ni list, which should include
2506 * any NIs which were successfully started up, and shut
2509 * After than we want to delete the network being added,
2510 * to avoid a memory leak.
2514 * When a network uses TCP bonding then all its interfaces
2515 * must be specified when the network is first defined: the
2516 * TCP bonding code doesn't allow for interfaces to be added
2519 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2520 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2525 while (!list_empty(&net->net_ni_added)) {
2526 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2528 list_del_init(&ni->ni_netlist);
2530 /* make sure that the the NI we're about to start
2531 * up is actually unique. if it's not fail. */
2532 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2533 ni->ni_interfaces[0])) {
2538 /* adjust the pointer the parent network, just in case it
2539 * the net is a duplicate */
2542 rc = lnet_startup_lndni(ni, tun);
2548 list_add_tail(&ni->ni_netlist, &local_ni_list);
2553 lnet_net_lock(LNET_LOCK_EX);
2554 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2555 lnet_incr_dlc_seq();
2556 lnet_net_unlock(LNET_LOCK_EX);
2558 /* if the network is not unique then we don't want to keep
2559 * it around after we're done. Free it. Otherwise add that
2560 * net to the global the_lnet.ln_nets */
2561 if (net_l != net && net_l != NULL) {
2563 * TODO - note. currently the tunables can not be updated
2569 * restore tunables after it has been overwitten by the
2572 if (peer_timeout != -1)
2573 net->net_tunables.lct_peer_timeout = peer_timeout;
2574 if (maxtxcredits != -1)
2575 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2576 if (peerrtrcredits != -1)
2577 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2579 lnet_net_lock(LNET_LOCK_EX);
2580 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2581 lnet_net_unlock(LNET_LOCK_EX);
2584 /* update net count */
2585 lnet_current_net_count = lnet_get_net_count();
2591 * shutdown the new NIs that are being started up
2592 * free the NET being started
2594 while (!list_empty(&local_ni_list)) {
2595 ni = list_entry(local_ni_list.next, struct lnet_ni,
2598 lnet_shutdown_lndni(ni);
2608 lnet_startup_lndnets(struct list_head *netlist)
2610 struct lnet_net *net;
2615 * Change to running state before bringing up the LNDs. This
2616 * allows lnet_shutdown_lndnets() to assert that we've passed
2619 lnet_net_lock(LNET_LOCK_EX);
2620 the_lnet.ln_state = LNET_STATE_RUNNING;
2621 lnet_net_unlock(LNET_LOCK_EX);
2623 while (!list_empty(netlist)) {
2624 net = list_entry(netlist->next, struct lnet_net, net_list);
2625 list_del_init(&net->net_list);
2627 rc = lnet_startup_lndnet(net, NULL);
2637 lnet_shutdown_lndnets();
2643 * Initialize LNet library.
2645 * Automatically called at module loading time. Caller has to call
2646 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2647 * latter returned 0. It must be called exactly once.
2649 * \retval 0 on success
2650 * \retval -ve on failures.
2652 int lnet_lib_init(void)
2656 lnet_assert_wire_constants();
2658 /* refer to global cfs_cpt_table for now */
2659 the_lnet.ln_cpt_table = cfs_cpt_tab;
2660 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_tab);
2662 LASSERT(the_lnet.ln_cpt_number > 0);
2663 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2664 /* we are under risk of consuming all lh_cookie */
2665 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2666 "please change setting of CPT-table and retry\n",
2667 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2671 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2672 the_lnet.ln_cpt_bits++;
2674 rc = lnet_create_locks();
2676 CERROR("Can't create LNet global locks: %d\n", rc);
2680 the_lnet.ln_refcount = 0;
2681 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2682 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2684 /* The hash table size is the number of bits it takes to express the set
2685 * ln_num_routes, minus 1 (better to under estimate than over so we
2686 * don't waste memory). */
2687 if (rnet_htable_size <= 0)
2688 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2689 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2690 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2691 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2692 order_base_2(rnet_htable_size) - 1);
2694 /* All LNDs apart from the LOLND are in separate modules. They
2695 * register themselves when their module loads, and unregister
2696 * themselves when their module is unloaded. */
2697 lnet_register_lnd(&the_lolnd);
2702 * Finalize LNet library.
2704 * \pre lnet_lib_init() called with success.
2705 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2707 * As this happens at module-unload, all lnds must already be unloaded,
2708 * so they must already be unregistered.
2710 void lnet_lib_exit(void)
2714 LASSERT(the_lnet.ln_refcount == 0);
2715 lnet_unregister_lnd(&the_lolnd);
2716 for (i = 0; i < NUM_LNDS; i++)
2717 LASSERT(!the_lnet.ln_lnds[i]);
2718 lnet_destroy_locks();
2722 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2724 * Users must call this function at least once before any other functions.
2725 * For each successful call there must be a corresponding call to
2726 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2729 * The PID used by LNet may be different from the one requested.
2732 * \param requested_pid PID requested by the caller.
2734 * \return >= 0 on success, and < 0 error code on failures.
2737 LNetNIInit(lnet_pid_t requested_pid)
2739 int im_a_router = 0;
2742 struct lnet_ping_buffer *pbuf;
2743 struct lnet_handle_md ping_mdh;
2744 LIST_HEAD(net_head);
2745 struct lnet_net *net;
2747 mutex_lock(&the_lnet.ln_api_mutex);
2749 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2751 if (the_lnet.ln_refcount > 0) {
2752 rc = the_lnet.ln_refcount++;
2753 mutex_unlock(&the_lnet.ln_api_mutex);
2757 rc = lnet_prepare(requested_pid);
2759 mutex_unlock(&the_lnet.ln_api_mutex);
2763 /* create a network for Loopback network */
2764 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2767 goto err_empty_list;
2770 /* Add in the loopback NI */
2771 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2773 goto err_empty_list;
2776 if (use_tcp_bonding)
2777 CWARN("'use_tcp_bonding' option has been deprecated. See LU-13641\n");
2779 /* If LNet is being initialized via DLC it is possible
2780 * that the user requests not to load module parameters (ones which
2781 * are supported by DLC) on initialization. Therefore, make sure not
2782 * to load networks, routes and forwarding from module parameters
2783 * in this case. On cleanup in case of failure only clean up
2784 * routes if it has been loaded */
2785 if (!the_lnet.ln_nis_from_mod_params) {
2786 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2789 goto err_empty_list;
2792 ni_count = lnet_startup_lndnets(&net_head);
2795 goto err_empty_list;
2798 if (!the_lnet.ln_nis_from_mod_params) {
2799 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2801 goto err_shutdown_lndnis;
2803 rc = lnet_rtrpools_alloc(im_a_router);
2805 goto err_destroy_routes;
2808 rc = lnet_acceptor_start();
2810 goto err_destroy_routes;
2812 the_lnet.ln_refcount = 1;
2813 /* Now I may use my own API functions... */
2815 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2817 goto err_acceptor_stop;
2819 lnet_ping_target_update(pbuf, ping_mdh);
2821 the_lnet.ln_mt_handler = lnet_mt_event_handler;
2823 rc = lnet_push_target_init();
2827 rc = lnet_peer_discovery_start();
2829 goto err_destroy_push_target;
2831 rc = lnet_monitor_thr_start();
2833 goto err_stop_discovery_thr;
2836 lnet_router_debugfs_init();
2838 mutex_unlock(&the_lnet.ln_api_mutex);
2840 complete_all(&the_lnet.ln_started);
2842 /* wait for all routers to start */
2843 lnet_wait_router_start();
2847 err_stop_discovery_thr:
2848 lnet_peer_discovery_stop();
2849 err_destroy_push_target:
2850 lnet_push_target_fini();
2852 lnet_ping_target_fini();
2854 the_lnet.ln_refcount = 0;
2855 lnet_acceptor_stop();
2857 if (!the_lnet.ln_nis_from_mod_params)
2858 lnet_destroy_routes();
2859 err_shutdown_lndnis:
2860 lnet_shutdown_lndnets();
2864 mutex_unlock(&the_lnet.ln_api_mutex);
2865 while (!list_empty(&net_head)) {
2866 struct lnet_net *net;
2868 net = list_entry(net_head.next, struct lnet_net, net_list);
2869 list_del_init(&net->net_list);
2874 EXPORT_SYMBOL(LNetNIInit);
2877 * Stop LNet interfaces, routing, and forwarding.
2879 * Users must call this function once for each successful call to LNetNIInit().
2880 * Once the LNetNIFini() operation has been started, the results of pending
2881 * API operations are undefined.
2883 * \return always 0 for current implementation.
2888 mutex_lock(&the_lnet.ln_api_mutex);
2890 LASSERT(the_lnet.ln_refcount > 0);
2892 if (the_lnet.ln_refcount != 1) {
2893 the_lnet.ln_refcount--;
2895 LASSERT(!the_lnet.ln_niinit_self);
2899 lnet_router_debugfs_fini();
2900 lnet_monitor_thr_stop();
2901 lnet_peer_discovery_stop();
2902 lnet_push_target_fini();
2903 lnet_ping_target_fini();
2905 /* Teardown fns that use my own API functions BEFORE here */
2906 the_lnet.ln_refcount = 0;
2908 lnet_acceptor_stop();
2909 lnet_destroy_routes();
2910 lnet_shutdown_lndnets();
2914 mutex_unlock(&the_lnet.ln_api_mutex);
2917 EXPORT_SYMBOL(LNetNIFini);
2920 * Grabs the ni data from the ni structure and fills the out
2923 * \param[in] ni network interface structure
2924 * \param[out] cfg_ni NI config information
2925 * \param[out] tun network and LND tunables
2928 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2929 struct lnet_ioctl_config_lnd_tunables *tun,
2930 struct lnet_ioctl_element_stats *stats,
2933 size_t min_size = 0;
2936 if (!ni || !cfg_ni || !tun)
2939 if (ni->ni_interfaces[0] != NULL) {
2940 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2941 if (ni->ni_interfaces[i] != NULL) {
2942 strncpy(cfg_ni->lic_ni_intf[i],
2943 ni->ni_interfaces[i],
2944 sizeof(cfg_ni->lic_ni_intf[i]));
2949 cfg_ni->lic_nid = ni->ni_nid;
2950 if (ni->ni_nid == LNET_NID_LO_0)
2951 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2953 cfg_ni->lic_status = ni->ni_status->ns_status;
2954 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2955 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2957 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2960 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2961 LNET_STATS_TYPE_SEND);
2962 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2963 LNET_STATS_TYPE_RECV);
2964 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2965 LNET_STATS_TYPE_DROP);
2969 * tun->lt_tun will always be present, but in order to be
2970 * backwards compatible, we need to deal with the cases when
2971 * tun->lt_tun is smaller than what the kernel has, because it
2972 * comes from an older version of a userspace program, then we'll
2973 * need to copy as much information as we have available space.
2975 min_size = tun_size - sizeof(tun->lt_cmn);
2976 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2978 /* copy over the cpts */
2979 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2980 ni->ni_cpts == NULL) {
2981 for (i = 0; i < ni->ni_ncpts; i++)
2982 cfg_ni->lic_cpts[i] = i;
2985 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2986 i < LNET_MAX_SHOW_NUM_CPT;
2988 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2990 cfg_ni->lic_ncpts = ni->ni_ncpts;
2994 * NOTE: This is a legacy function left in the code to be backwards
2995 * compatible with older userspace programs. It should eventually be
2998 * Grabs the ni data from the ni structure and fills the out
3001 * \param[in] ni network interface structure
3002 * \param[out] config config information
3005 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
3006 struct lnet_ioctl_config_data *config)
3008 struct lnet_ioctl_net_config *net_config;
3009 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
3010 size_t min_size, tunable_size = 0;
3016 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
3020 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
3021 ARRAY_SIZE(net_config->ni_interfaces));
3023 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
3024 if (!ni->ni_interfaces[i])
3027 strncpy(net_config->ni_interfaces[i],
3028 ni->ni_interfaces[i],
3029 sizeof(net_config->ni_interfaces[i]));
3032 config->cfg_nid = ni->ni_nid;
3033 config->cfg_config_u.cfg_net.net_peer_timeout =
3034 ni->ni_net->net_tunables.lct_peer_timeout;
3035 config->cfg_config_u.cfg_net.net_max_tx_credits =
3036 ni->ni_net->net_tunables.lct_max_tx_credits;
3037 config->cfg_config_u.cfg_net.net_peer_tx_credits =
3038 ni->ni_net->net_tunables.lct_peer_tx_credits;
3039 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
3040 ni->ni_net->net_tunables.lct_peer_rtr_credits;
3042 if (ni->ni_nid == LNET_NID_LO_0)
3043 net_config->ni_status = LNET_NI_STATUS_UP;
3045 net_config->ni_status = ni->ni_status->ns_status;
3048 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
3050 for (i = 0; i < num_cpts; i++)
3051 net_config->ni_cpts[i] = ni->ni_cpts[i];
3053 config->cfg_ncpts = num_cpts;
3057 * See if user land tools sent in a newer and larger version
3058 * of struct lnet_tunables than what the kernel uses.
3060 min_size = sizeof(*config) + sizeof(*net_config);
3062 if (config->cfg_hdr.ioc_len > min_size)
3063 tunable_size = config->cfg_hdr.ioc_len - min_size;
3065 /* Don't copy too much data to user space */
3066 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
3067 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
3069 if (lnd_cfg && min_size) {
3070 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
3071 config->cfg_config_u.cfg_net.net_interface_count = 1;
3073 /* Tell user land that kernel side has less data */
3074 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
3075 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
3076 config->cfg_hdr.ioc_len -= min_size;
3082 lnet_get_ni_idx_locked(int idx)
3085 struct lnet_net *net;
3087 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3088 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3097 int lnet_get_net_healthv_locked(struct lnet_net *net)
3100 int best_healthv = 0;
3103 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3104 healthv = atomic_read(&ni->ni_healthv);
3105 if (healthv > best_healthv)
3106 best_healthv = healthv;
3109 return best_healthv;
3113 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
3116 struct lnet_net *net = mynet;
3119 * It is possible that the net has been cleaned out while there is
3120 * a message being sent. This function accessed the net without
3121 * checking if the list is empty
3125 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
3127 if (list_empty(&net->net_ni_list))
3129 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3135 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
3136 /* if you reached the end of the ni list and the net is
3137 * specified, then there are no more nis in that net */
3141 /* we reached the end of this net ni list. move to the
3143 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
3144 /* no more nets and no more NIs. */
3147 /* get the next net */
3148 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
3150 if (list_empty(&net->net_ni_list))
3152 /* get the ni on it */
3153 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3159 if (list_empty(&prev->ni_netlist))
3162 /* there are more nis left */
3163 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3169 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3174 int idx = config->cfg_count;
3176 cpt = lnet_net_lock_current();
3178 ni = lnet_get_ni_idx_locked(idx);
3183 lnet_fill_ni_info_legacy(ni, config);
3187 lnet_net_unlock(cpt);
3192 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3193 struct lnet_ioctl_config_lnd_tunables *tun,
3194 struct lnet_ioctl_element_stats *stats,
3201 if (!cfg_ni || !tun || !stats)
3204 cpt = lnet_net_lock_current();
3206 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3211 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3215 lnet_net_unlock(cpt);
3219 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3228 cpt = lnet_net_lock_current();
3230 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3233 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3237 lnet_net_unlock(cpt);
3242 static int lnet_add_net_common(struct lnet_net *net,
3243 struct lnet_ioctl_config_lnd_tunables *tun)
3245 struct lnet_handle_md ping_mdh;
3246 struct lnet_ping_buffer *pbuf;
3247 struct lnet_remotenet *rnet;
3253 lnet_net_lock(LNET_LOCK_EX);
3254 rnet = lnet_find_rnet_locked(net->net_id);
3255 lnet_net_unlock(LNET_LOCK_EX);
3257 * make sure that the net added doesn't invalidate the current
3258 * configuration LNet is keeping
3261 CERROR("Adding net %s will invalidate routing configuration\n",
3262 libcfs_net2str(net->net_id));
3268 * make sure you calculate the correct number of slots in the ping
3269 * buffer. Since the ping info is a flattened list of all the NIs,
3270 * we should allocate enough slots to accomodate the number of NIs
3271 * which will be added.
3273 * since ni hasn't been configured yet, use
3274 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3276 net_ni_count = lnet_get_net_ni_count_pre(net);
3278 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3279 net_ni_count + lnet_get_ni_count(),
3287 memcpy(&net->net_tunables,
3288 &tun->lt_cmn, sizeof(net->net_tunables));
3290 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3292 net_id = net->net_id;
3294 rc = lnet_startup_lndnet(net,
3295 (tun) ? &tun->lt_tun : NULL);
3299 lnet_net_lock(LNET_LOCK_EX);
3300 net = lnet_get_net_locked(net_id);
3303 /* apply the UDSPs */
3304 rc = lnet_udsp_apply_policies_on_net(net);
3306 CERROR("Failed to apply UDSPs on local net %s\n",
3307 libcfs_net2str(net->net_id));
3309 /* At this point we lost track of which NI was just added, so we
3310 * just re-apply the policies on all of the NIs on this net
3312 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3313 rc = lnet_udsp_apply_policies_on_ni(ni);
3315 CERROR("Failed to apply UDSPs on ni %s\n",
3316 libcfs_nid2str(ni->ni_nid));
3318 lnet_net_unlock(LNET_LOCK_EX);
3321 * Start the acceptor thread if this is the first network
3322 * being added that requires the thread.
3324 if (net->net_lnd->lnd_accept) {
3325 rc = lnet_acceptor_start();
3327 /* shutdown the net that we just started */
3328 CERROR("Failed to start up acceptor thread\n");
3329 lnet_shutdown_lndnet(net);
3334 lnet_net_lock(LNET_LOCK_EX);
3335 lnet_peer_net_added(net);
3336 lnet_net_unlock(LNET_LOCK_EX);
3338 lnet_ping_target_update(pbuf, ping_mdh);
3343 lnet_ping_md_unlink(pbuf, &ping_mdh);
3344 lnet_ping_buffer_decref(pbuf);
3349 lnet_set_tune_defaults(struct lnet_ioctl_config_lnd_tunables *tun)
3352 if (!tun->lt_cmn.lct_peer_timeout)
3353 tun->lt_cmn.lct_peer_timeout = DEFAULT_PEER_TIMEOUT;
3354 if (!tun->lt_cmn.lct_peer_tx_credits)
3355 tun->lt_cmn.lct_peer_tx_credits = DEFAULT_PEER_CREDITS;
3356 if (!tun->lt_cmn.lct_max_tx_credits)
3357 tun->lt_cmn.lct_max_tx_credits = DEFAULT_CREDITS;
3361 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3362 struct lnet_ioctl_config_lnd_tunables *tun)
3364 struct lnet_net *net;
3367 LIST_HEAD(net_head);
3369 rc = lnet_parse_ip2nets(&nets, ip2nets);
3373 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3377 lnet_set_tune_defaults(tun);
3379 mutex_lock(&the_lnet.ln_api_mutex);
3380 while (!list_empty(&net_head)) {
3381 net = list_entry(net_head.next, struct lnet_net, net_list);
3382 list_del_init(&net->net_list);
3383 rc = lnet_add_net_common(net, tun);
3389 mutex_unlock(&the_lnet.ln_api_mutex);
3391 while (!list_empty(&net_head)) {
3392 net = list_entry(net_head.next, struct lnet_net, net_list);
3393 list_del_init(&net->net_list);
3399 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3401 struct lnet_net *net;
3403 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3405 __u32 net_id, lnd_type;
3407 /* get the tunables if they are available */
3408 if (conf->lic_cfg_hdr.ioc_len >=
3409 sizeof(*conf) + sizeof(*tun))
3410 tun = (struct lnet_ioctl_config_lnd_tunables *)
3413 /* handle legacy ip2nets from DLC */
3414 if (conf->lic_legacy_ip2nets[0] != '\0')
3415 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3418 net_id = LNET_NIDNET(conf->lic_nid);
3419 lnd_type = LNET_NETTYP(net_id);
3421 if (!libcfs_isknown_lnd(lnd_type)) {
3422 CERROR("No valid net and lnd information provided\n");
3426 net = lnet_net_alloc(net_id, NULL);
3430 for (i = 0; i < conf->lic_ncpts; i++) {
3431 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3435 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3436 conf->lic_ni_intf[0]);
3440 lnet_set_tune_defaults(tun);
3442 mutex_lock(&the_lnet.ln_api_mutex);
3444 rc = lnet_add_net_common(net, tun);
3446 mutex_unlock(&the_lnet.ln_api_mutex);
3451 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3453 struct lnet_net *net;
3455 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3456 struct lnet_ping_buffer *pbuf;
3457 struct lnet_handle_md ping_mdh;
3462 /* don't allow userspace to shutdown the LOLND */
3463 if (LNET_NETTYP(net_id) == LOLND)
3466 mutex_lock(&the_lnet.ln_api_mutex);
3470 net = lnet_get_net_locked(net_id);
3472 CERROR("net %s not found\n",
3473 libcfs_net2str(net_id));
3478 addr = LNET_NIDADDR(conf->lic_nid);
3480 /* remove the entire net */
3481 net_count = lnet_get_net_ni_count_locked(net);
3485 /* create and link a new ping info, before removing the old one */
3486 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3487 lnet_get_ni_count() - net_count,
3490 goto unlock_api_mutex;
3492 lnet_shutdown_lndnet(net);
3494 lnet_acceptor_stop();
3496 lnet_ping_target_update(pbuf, ping_mdh);
3498 goto unlock_api_mutex;
3501 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3503 CERROR("nid %s not found\n",
3504 libcfs_nid2str(conf->lic_nid));
3509 net_count = lnet_get_net_ni_count_locked(net);
3513 /* create and link a new ping info, before removing the old one */
3514 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3515 lnet_get_ni_count() - 1, false);
3517 goto unlock_api_mutex;
3519 lnet_shutdown_lndni(ni);
3521 lnet_acceptor_stop();
3523 lnet_ping_target_update(pbuf, ping_mdh);
3525 /* check if the net is empty and remove it if it is */
3527 lnet_shutdown_lndnet(net);
3529 goto unlock_api_mutex;
3534 mutex_unlock(&the_lnet.ln_api_mutex);
3540 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3541 * They are only expected to be called for unique networks.
3542 * That can be as a result of older DLC library
3543 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3546 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3548 struct lnet_net *net;
3549 LIST_HEAD(net_head);
3551 struct lnet_ioctl_config_lnd_tunables tun;
3552 const char *nets = conf->cfg_config_u.cfg_net.net_intf;
3554 /* Create a net/ni structures for the network string */
3555 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3557 return rc == 0 ? -EINVAL : rc;
3559 mutex_lock(&the_lnet.ln_api_mutex);
3562 rc = -EINVAL; /* only add one network per call */
3563 goto out_unlock_clean;
3566 net = list_entry(net_head.next, struct lnet_net, net_list);
3567 list_del_init(&net->net_list);
3569 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3571 memset(&tun, 0, sizeof(tun));
3573 tun.lt_cmn.lct_peer_timeout =
3574 (!conf->cfg_config_u.cfg_net.net_peer_timeout) ? DEFAULT_PEER_TIMEOUT :
3575 conf->cfg_config_u.cfg_net.net_peer_timeout;
3576 tun.lt_cmn.lct_peer_tx_credits =
3577 (!conf->cfg_config_u.cfg_net.net_peer_tx_credits) ? DEFAULT_PEER_CREDITS :
3578 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3579 tun.lt_cmn.lct_peer_rtr_credits =
3580 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3581 tun.lt_cmn.lct_max_tx_credits =
3582 (!conf->cfg_config_u.cfg_net.net_max_tx_credits) ? DEFAULT_CREDITS :
3583 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3585 rc = lnet_add_net_common(net, &tun);
3588 mutex_unlock(&the_lnet.ln_api_mutex);
3589 while (!list_empty(&net_head)) {
3590 /* net_head list is empty in success case */
3591 net = list_entry(net_head.next, struct lnet_net, net_list);
3592 list_del_init(&net->net_list);
3599 lnet_dyn_del_net(__u32 net_id)
3601 struct lnet_net *net;
3602 struct lnet_ping_buffer *pbuf;
3603 struct lnet_handle_md ping_mdh;
3607 /* don't allow userspace to shutdown the LOLND */
3608 if (LNET_NETTYP(net_id) == LOLND)
3611 mutex_lock(&the_lnet.ln_api_mutex);
3615 net = lnet_get_net_locked(net_id);
3622 net_ni_count = lnet_get_net_ni_count_locked(net);
3626 /* create and link a new ping info, before removing the old one */
3627 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3628 lnet_get_ni_count() - net_ni_count, false);
3632 lnet_shutdown_lndnet(net);
3634 lnet_acceptor_stop();
3636 lnet_ping_target_update(pbuf, ping_mdh);
3639 mutex_unlock(&the_lnet.ln_api_mutex);
3644 void lnet_incr_dlc_seq(void)
3646 atomic_inc(&lnet_dlc_seq_no);
3649 __u32 lnet_get_dlc_seq_locked(void)
3651 return atomic_read(&lnet_dlc_seq_no);
3655 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3657 struct lnet_net *net;
3660 lnet_net_lock(LNET_LOCK_EX);
3661 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3662 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3663 if (ni->ni_nid == nid || all) {
3664 atomic_set(&ni->ni_healthv, value);
3665 if (list_empty(&ni->ni_recovery) &&
3666 value < LNET_MAX_HEALTH_VALUE) {
3667 CERROR("manually adding local NI %s to recovery\n",
3668 libcfs_nid2str(ni->ni_nid));
3669 list_add_tail(&ni->ni_recovery,
3670 &the_lnet.ln_mt_localNIRecovq);
3671 lnet_ni_addref_locked(ni, 0);
3674 lnet_net_unlock(LNET_LOCK_EX);
3680 lnet_net_unlock(LNET_LOCK_EX);
3684 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3688 lnet_nid_t nid = stats->hlni_nid;
3690 cpt = lnet_net_lock_current();
3691 ni = lnet_nid2ni_locked(nid, cpt);
3698 stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3699 stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3700 stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3701 stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3702 stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3703 stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3704 stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3707 lnet_net_unlock(cpt);
3713 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3718 lnet_net_lock(LNET_LOCK_EX);
3719 list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3720 list->rlst_nid_array[i] = ni->ni_nid;
3722 if (i >= LNET_MAX_SHOW_NUM_NID)
3725 lnet_net_unlock(LNET_LOCK_EX);
3726 list->rlst_num_nids = i;
3732 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3734 struct lnet_peer_ni *lpni;
3737 lnet_net_lock(LNET_LOCK_EX);
3738 list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3739 list->rlst_nid_array[i] = lpni->lpni_nid;
3741 if (i >= LNET_MAX_SHOW_NUM_NID)
3744 lnet_net_unlock(LNET_LOCK_EX);
3745 list->rlst_num_nids = i;
3751 * LNet ioctl handler.
3755 LNetCtl(unsigned int cmd, void *arg)
3757 struct libcfs_ioctl_data *data = arg;
3758 struct lnet_ioctl_config_data *config;
3759 struct lnet_process_id id = {0};
3763 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3764 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3767 case IOC_LIBCFS_GET_NI:
3768 rc = LNetGetId(data->ioc_count, &id);
3769 data->ioc_nid = id.nid;
3772 case IOC_LIBCFS_FAIL_NID:
3773 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3775 case IOC_LIBCFS_ADD_ROUTE: {
3776 /* default router sensitivity to 1 */
3777 unsigned int sensitivity = 1;
3780 if (config->cfg_hdr.ioc_len < sizeof(*config))
3783 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3785 config->cfg_config_u.cfg_route.rtr_sensitivity;
3788 mutex_lock(&the_lnet.ln_api_mutex);
3789 rc = lnet_add_route(config->cfg_net,
3790 config->cfg_config_u.cfg_route.rtr_hop,
3792 config->cfg_config_u.cfg_route.
3793 rtr_priority, sensitivity);
3794 mutex_unlock(&the_lnet.ln_api_mutex);
3798 case IOC_LIBCFS_DEL_ROUTE:
3801 if (config->cfg_hdr.ioc_len < sizeof(*config))
3804 mutex_lock(&the_lnet.ln_api_mutex);
3805 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3806 mutex_unlock(&the_lnet.ln_api_mutex);
3809 case IOC_LIBCFS_GET_ROUTE:
3812 if (config->cfg_hdr.ioc_len < sizeof(*config))
3815 mutex_lock(&the_lnet.ln_api_mutex);
3816 rc = lnet_get_route(config->cfg_count,
3818 &config->cfg_config_u.cfg_route.rtr_hop,
3820 &config->cfg_config_u.cfg_route.rtr_flags,
3821 &config->cfg_config_u.cfg_route.
3823 &config->cfg_config_u.cfg_route.
3825 mutex_unlock(&the_lnet.ln_api_mutex);
3828 case IOC_LIBCFS_GET_LOCAL_NI: {
3829 struct lnet_ioctl_config_ni *cfg_ni;
3830 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3831 struct lnet_ioctl_element_stats *stats;
3836 /* get the tunables if they are available */
3837 if (cfg_ni->lic_cfg_hdr.ioc_len <
3838 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3841 stats = (struct lnet_ioctl_element_stats *)
3843 tun = (struct lnet_ioctl_config_lnd_tunables *)
3844 (cfg_ni->lic_bulk + sizeof(*stats));
3846 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3849 mutex_lock(&the_lnet.ln_api_mutex);
3850 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3851 mutex_unlock(&the_lnet.ln_api_mutex);
3855 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3856 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3858 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3861 mutex_lock(&the_lnet.ln_api_mutex);
3862 rc = lnet_get_ni_stats(msg_stats);
3863 mutex_unlock(&the_lnet.ln_api_mutex);
3868 case IOC_LIBCFS_GET_NET: {
3869 size_t total = sizeof(*config) +
3870 sizeof(struct lnet_ioctl_net_config);
3873 if (config->cfg_hdr.ioc_len < total)
3876 mutex_lock(&the_lnet.ln_api_mutex);
3877 rc = lnet_get_net_config(config);
3878 mutex_unlock(&the_lnet.ln_api_mutex);
3882 case IOC_LIBCFS_GET_LNET_STATS:
3884 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3886 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3889 mutex_lock(&the_lnet.ln_api_mutex);
3890 rc = lnet_counters_get(&lnet_stats->st_cntrs);
3891 mutex_unlock(&the_lnet.ln_api_mutex);
3895 case IOC_LIBCFS_CONFIG_RTR:
3898 if (config->cfg_hdr.ioc_len < sizeof(*config))
3901 mutex_lock(&the_lnet.ln_api_mutex);
3902 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3903 rc = lnet_rtrpools_enable();
3904 mutex_unlock(&the_lnet.ln_api_mutex);
3907 lnet_rtrpools_disable();
3908 mutex_unlock(&the_lnet.ln_api_mutex);
3911 case IOC_LIBCFS_ADD_BUF:
3914 if (config->cfg_hdr.ioc_len < sizeof(*config))
3917 mutex_lock(&the_lnet.ln_api_mutex);
3918 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3920 config->cfg_config_u.cfg_buffers.
3922 config->cfg_config_u.cfg_buffers.
3924 mutex_unlock(&the_lnet.ln_api_mutex);
3927 case IOC_LIBCFS_SET_NUMA_RANGE: {
3928 struct lnet_ioctl_set_value *numa;
3930 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3932 lnet_net_lock(LNET_LOCK_EX);
3933 lnet_numa_range = numa->sv_value;
3934 lnet_net_unlock(LNET_LOCK_EX);
3938 case IOC_LIBCFS_GET_NUMA_RANGE: {
3939 struct lnet_ioctl_set_value *numa;
3941 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3943 numa->sv_value = lnet_numa_range;
3947 case IOC_LIBCFS_GET_BUF: {
3948 struct lnet_ioctl_pool_cfg *pool_cfg;
3949 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3953 if (config->cfg_hdr.ioc_len < total)
3956 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3958 mutex_lock(&the_lnet.ln_api_mutex);
3959 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3960 mutex_unlock(&the_lnet.ln_api_mutex);
3964 case IOC_LIBCFS_GET_LOCAL_HSTATS: {
3965 struct lnet_ioctl_local_ni_hstats *stats = arg;
3967 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
3970 mutex_lock(&the_lnet.ln_api_mutex);
3971 rc = lnet_get_local_ni_hstats(stats);
3972 mutex_unlock(&the_lnet.ln_api_mutex);
3977 case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
3978 struct lnet_ioctl_recovery_list *list = arg;
3979 if (list->rlst_hdr.ioc_len < sizeof(*list))
3982 mutex_lock(&the_lnet.ln_api_mutex);
3983 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
3984 rc = lnet_get_local_ni_recovery_list(list);
3986 rc = lnet_get_peer_ni_recovery_list(list);
3987 mutex_unlock(&the_lnet.ln_api_mutex);
3991 case IOC_LIBCFS_ADD_PEER_NI: {
3992 struct lnet_ioctl_peer_cfg *cfg = arg;
3994 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3997 mutex_lock(&the_lnet.ln_api_mutex);
3998 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
4001 mutex_unlock(&the_lnet.ln_api_mutex);
4005 case IOC_LIBCFS_DEL_PEER_NI: {
4006 struct lnet_ioctl_peer_cfg *cfg = arg;
4008 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4011 mutex_lock(&the_lnet.ln_api_mutex);
4012 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
4013 cfg->prcfg_cfg_nid);
4014 mutex_unlock(&the_lnet.ln_api_mutex);
4018 case IOC_LIBCFS_GET_PEER_INFO: {
4019 struct lnet_ioctl_peer *peer_info = arg;
4021 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
4024 mutex_lock(&the_lnet.ln_api_mutex);
4025 rc = lnet_get_peer_ni_info(
4026 peer_info->pr_count,
4028 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
4029 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
4030 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
4031 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
4032 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
4033 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
4034 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
4035 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
4036 mutex_unlock(&the_lnet.ln_api_mutex);
4040 case IOC_LIBCFS_GET_PEER_NI: {
4041 struct lnet_ioctl_peer_cfg *cfg = arg;
4043 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4046 mutex_lock(&the_lnet.ln_api_mutex);
4047 rc = lnet_get_peer_info(cfg,
4048 (void __user *)cfg->prcfg_bulk);
4049 mutex_unlock(&the_lnet.ln_api_mutex);
4053 case IOC_LIBCFS_GET_PEER_LIST: {
4054 struct lnet_ioctl_peer_cfg *cfg = arg;
4056 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4059 mutex_lock(&the_lnet.ln_api_mutex);
4060 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
4061 (struct lnet_process_id __user *)cfg->prcfg_bulk);
4062 mutex_unlock(&the_lnet.ln_api_mutex);
4066 case IOC_LIBCFS_SET_HEALHV: {
4067 struct lnet_ioctl_reset_health_cfg *cfg = arg;
4069 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
4071 if (cfg->rh_value < 0 ||
4072 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
4073 value = LNET_MAX_HEALTH_VALUE;
4075 value = cfg->rh_value;
4076 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
4077 value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
4078 "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
4079 mutex_lock(&the_lnet.ln_api_mutex);
4080 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
4081 lnet_ni_set_healthv(cfg->rh_nid, value,
4084 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
4086 mutex_unlock(&the_lnet.ln_api_mutex);
4090 case IOC_LIBCFS_NOTIFY_ROUTER: {
4091 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
4093 /* The deadline passed in by the user should be some time in
4094 * seconds in the future since the UNIX epoch. We have to map
4095 * that deadline to the wall clock.
4097 deadline += ktime_get_seconds();
4098 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
4102 case IOC_LIBCFS_LNET_DIST:
4103 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
4104 if (rc < 0 && rc != -EHOSTUNREACH)
4107 data->ioc_u32[0] = rc;
4110 case IOC_LIBCFS_TESTPROTOCOMPAT:
4111 the_lnet.ln_testprotocompat = data->ioc_flags;
4114 case IOC_LIBCFS_LNET_FAULT:
4115 return lnet_fault_ctl(data->ioc_flags, data);
4117 case IOC_LIBCFS_PING: {
4118 signed long timeout;
4120 id.nid = data->ioc_nid;
4121 id.pid = data->ioc_u32[0];
4123 /* If timeout is negative then set default of 3 minutes */
4124 if (((s32)data->ioc_u32[1] <= 0) ||
4125 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
4126 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
4128 timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
4130 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
4131 data->ioc_plen1 / sizeof(struct lnet_process_id));
4136 data->ioc_count = rc;
4140 case IOC_LIBCFS_PING_PEER: {
4141 struct lnet_ioctl_ping_data *ping = arg;
4142 struct lnet_peer *lp;
4143 signed long timeout;
4145 /* If timeout is negative then set default of 3 minutes */
4146 if (((s32)ping->op_param) <= 0 ||
4147 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
4148 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
4150 timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
4152 rc = lnet_ping(ping->ping_id, timeout,
4158 mutex_lock(&the_lnet.ln_api_mutex);
4159 lp = lnet_find_peer(ping->ping_id.nid);
4161 ping->ping_id.nid = lp->lp_primary_nid;
4162 ping->mr_info = lnet_peer_is_multi_rail(lp);
4163 lnet_peer_decref_locked(lp);
4165 mutex_unlock(&the_lnet.ln_api_mutex);
4167 ping->ping_count = rc;
4171 case IOC_LIBCFS_DISCOVER: {
4172 struct lnet_ioctl_ping_data *discover = arg;
4173 struct lnet_peer *lp;
4175 rc = lnet_discover(discover->ping_id, discover->op_param,
4177 discover->ping_count);
4181 mutex_lock(&the_lnet.ln_api_mutex);
4182 lp = lnet_find_peer(discover->ping_id.nid);
4184 discover->ping_id.nid = lp->lp_primary_nid;
4185 discover->mr_info = lnet_peer_is_multi_rail(lp);
4186 lnet_peer_decref_locked(lp);
4188 mutex_unlock(&the_lnet.ln_api_mutex);
4190 discover->ping_count = rc;
4194 case IOC_LIBCFS_ADD_UDSP: {
4195 struct lnet_ioctl_udsp *ioc_udsp = arg;
4196 __u32 bulk_size = ioc_udsp->iou_hdr.ioc_len;
4198 mutex_lock(&the_lnet.ln_api_mutex);
4199 rc = lnet_udsp_demarshal_add(arg, bulk_size);
4201 rc = lnet_udsp_apply_policies(NULL, false);
4202 CDEBUG(D_NET, "policy application returned %d\n", rc);
4205 mutex_unlock(&the_lnet.ln_api_mutex);
4210 case IOC_LIBCFS_DEL_UDSP: {
4211 struct lnet_ioctl_udsp *ioc_udsp = arg;
4212 int idx = ioc_udsp->iou_idx;
4214 if (ioc_udsp->iou_hdr.ioc_len < sizeof(*ioc_udsp))
4217 mutex_lock(&the_lnet.ln_api_mutex);
4218 rc = lnet_udsp_del_policy(idx);
4220 rc = lnet_udsp_apply_policies(NULL, false);
4221 CDEBUG(D_NET, "policy re-application returned %d\n",
4225 mutex_unlock(&the_lnet.ln_api_mutex);
4230 case IOC_LIBCFS_GET_UDSP_SIZE: {
4231 struct lnet_ioctl_udsp *ioc_udsp = arg;
4232 struct lnet_udsp *udsp;
4234 if (ioc_udsp->iou_hdr.ioc_len < sizeof(*ioc_udsp))
4239 mutex_lock(&the_lnet.ln_api_mutex);
4240 udsp = lnet_udsp_get_policy(ioc_udsp->iou_idx);
4244 /* coming in iou_idx will hold the idx of the udsp
4245 * to get the size of. going out the iou_idx will
4246 * hold the size of the UDSP found at the passed
4249 ioc_udsp->iou_idx = lnet_get_udsp_size(udsp);
4250 if (ioc_udsp->iou_idx < 0)
4253 mutex_unlock(&the_lnet.ln_api_mutex);
4258 case IOC_LIBCFS_GET_UDSP: {
4259 struct lnet_ioctl_udsp *ioc_udsp = arg;
4260 struct lnet_udsp *udsp;
4262 if (ioc_udsp->iou_hdr.ioc_len < sizeof(*ioc_udsp))
4267 mutex_lock(&the_lnet.ln_api_mutex);
4268 udsp = lnet_udsp_get_policy(ioc_udsp->iou_idx);
4272 rc = lnet_udsp_marshal(udsp, ioc_udsp);
4273 mutex_unlock(&the_lnet.ln_api_mutex);
4278 case IOC_LIBCFS_GET_CONST_UDSP_INFO: {
4279 struct lnet_ioctl_construct_udsp_info *info = arg;
4281 if (info->cud_hdr.ioc_len < sizeof(*info))
4284 CDEBUG(D_NET, "GET_UDSP_INFO for %s\n",
4285 libcfs_nid2str(info->cud_nid));
4287 mutex_lock(&the_lnet.ln_api_mutex);
4288 lnet_udsp_get_construct_info(info);
4289 mutex_unlock(&the_lnet.ln_api_mutex);
4295 ni = lnet_net2ni_addref(data->ioc_net);
4299 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
4302 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4309 EXPORT_SYMBOL(LNetCtl);
4311 void LNetDebugPeer(struct lnet_process_id id)
4313 lnet_debug_peer(id.nid);
4315 EXPORT_SYMBOL(LNetDebugPeer);
4318 * Determine if the specified peer \a nid is on the local node.
4320 * \param nid peer nid to check
4322 * \retval true If peer NID is on the local node.
4323 * \retval false If peer NID is not on the local node.
4325 bool LNetIsPeerLocal(lnet_nid_t nid)
4327 struct lnet_net *net;
4331 cpt = lnet_net_lock_current();
4332 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4333 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4334 if (ni->ni_nid == nid) {
4335 lnet_net_unlock(cpt);
4340 lnet_net_unlock(cpt);
4344 EXPORT_SYMBOL(LNetIsPeerLocal);
4347 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4348 * Note that all interfaces share a same PID, as requested by LNetNIInit().
4350 * \param index Index of the interface to look up.
4351 * \param id On successful return, this location will hold the
4352 * struct lnet_process_id ID of the interface.
4354 * \retval 0 If an interface exists at \a index.
4355 * \retval -ENOENT If no interface has been found.
4358 LNetGetId(unsigned int index, struct lnet_process_id *id)
4361 struct lnet_net *net;
4365 LASSERT(the_lnet.ln_refcount > 0);
4367 cpt = lnet_net_lock_current();
4369 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4370 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4374 id->nid = ni->ni_nid;
4375 id->pid = the_lnet.ln_pid;
4381 lnet_net_unlock(cpt);
4384 EXPORT_SYMBOL(LNetGetId);
4389 struct lnet_handle_md mdh;
4390 struct completion completion;
4394 lnet_ping_event_handler(struct lnet_event *event)
4396 struct ping_data *pd = event->md_user_ptr;
4398 CDEBUG(D_NET, "ping event (%d %d)%s\n",
4399 event->type, event->status,
4400 event->unlinked ? " unlinked" : "");
4402 if (event->status) {
4404 pd->rc = event->status;
4405 } else if (event->type == LNET_EVENT_REPLY) {
4407 pd->rc = event->mlength;
4409 if (event->unlinked)
4410 complete(&pd->completion);
4413 static int lnet_ping(struct lnet_process_id id, signed long timeout,
4414 struct lnet_process_id __user *ids, int n_ids)
4416 struct lnet_md md = { NULL };
4417 struct ping_data pd = { 0 };
4418 struct lnet_ping_buffer *pbuf;
4419 struct lnet_process_id tmpid;
4425 /* n_ids limit is arbitrary */
4426 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4430 * if the user buffer has more space than the lnet_interfaces_max
4431 * then only fill it up to lnet_interfaces_max
4433 if (n_ids > lnet_interfaces_max)
4434 n_ids = lnet_interfaces_max;
4436 if (id.pid == LNET_PID_ANY)
4437 id.pid = LNET_PID_LUSTRE;
4439 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4443 /* initialize md content */
4444 md.start = &pbuf->pb_info;
4445 md.length = LNET_PING_INFO_SIZE(n_ids);
4446 md.threshold = 2; /* GET/REPLY */
4448 md.options = LNET_MD_TRUNCATE;
4450 md.handler = lnet_ping_event_handler;
4452 init_completion(&pd.completion);
4454 rc = LNetMDBind(&md, LNET_UNLINK, &pd.mdh);
4456 CERROR("Can't bind MD: %d\n", rc);
4457 goto fail_ping_buffer_decref;
4460 rc = LNetGet(LNET_NID_ANY, pd.mdh, id,
4461 LNET_RESERVED_PORTAL,
4462 LNET_PROTO_PING_MATCHBITS, 0, false);
4465 /* Don't CERROR; this could be deliberate! */
4466 rc2 = LNetMDUnlink(pd.mdh);
4469 /* NB must wait for the UNLINK event below... */
4472 if (wait_for_completion_timeout(&pd.completion, timeout) == 0) {
4473 /* Ensure completion in finite time... */
4474 LNetMDUnlink(pd.mdh);
4475 wait_for_completion(&pd.completion);
4479 goto fail_ping_buffer_decref;
4483 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4485 rc = -EPROTO; /* if I can't parse... */
4488 CERROR("%s: ping info too short %d\n",
4489 libcfs_id2str(id), nob);
4490 goto fail_ping_buffer_decref;
4493 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4494 lnet_swap_pinginfo(pbuf);
4495 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4496 CERROR("%s: Unexpected magic %08x\n",
4497 libcfs_id2str(id), pbuf->pb_info.pi_magic);
4498 goto fail_ping_buffer_decref;
4501 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4502 CERROR("%s: ping w/o NI status: 0x%x\n",
4503 libcfs_id2str(id), pbuf->pb_info.pi_features);
4504 goto fail_ping_buffer_decref;
4507 if (nob < LNET_PING_INFO_SIZE(0)) {
4508 CERROR("%s: Short reply %d(%d min)\n",
4510 nob, (int)LNET_PING_INFO_SIZE(0));
4511 goto fail_ping_buffer_decref;
4514 if (pbuf->pb_info.pi_nnis < n_ids)
4515 n_ids = pbuf->pb_info.pi_nnis;
4517 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4518 CERROR("%s: Short reply %d(%d expected)\n",
4520 nob, (int)LNET_PING_INFO_SIZE(n_ids));
4521 goto fail_ping_buffer_decref;
4524 rc = -EFAULT; /* if I segv in copy_to_user()... */
4526 memset(&tmpid, 0, sizeof(tmpid));
4527 for (i = 0; i < n_ids; i++) {
4528 tmpid.pid = pbuf->pb_info.pi_pid;
4529 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4530 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4531 goto fail_ping_buffer_decref;
4533 rc = pbuf->pb_info.pi_nnis;
4535 fail_ping_buffer_decref:
4536 lnet_ping_buffer_decref(pbuf);
4541 lnet_discover(struct lnet_process_id id, __u32 force,
4542 struct lnet_process_id __user *ids, int n_ids)
4544 struct lnet_peer_ni *lpni;
4545 struct lnet_peer_ni *p;
4546 struct lnet_peer *lp;
4547 struct lnet_process_id *buf;
4553 id.nid == LNET_NID_ANY)
4556 if (id.pid == LNET_PID_ANY)
4557 id.pid = LNET_PID_LUSTRE;
4560 * If the user buffer has more space than the lnet_interfaces_max,
4561 * then only fill it up to lnet_interfaces_max.
4563 if (n_ids > lnet_interfaces_max)
4564 n_ids = lnet_interfaces_max;
4566 CFS_ALLOC_PTR_ARRAY(buf, n_ids);
4570 cpt = lnet_net_lock_current();
4571 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4578 * Clearing the NIDS_UPTODATE flag ensures the peer will
4579 * be discovered, provided discovery has not been disabled.
4581 lp = lpni->lpni_peer_net->lpn_peer;
4582 spin_lock(&lp->lp_lock);
4583 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4584 /* If the force flag is set, force a PING and PUSH as well. */
4586 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4587 spin_unlock(&lp->lp_lock);
4588 rc = lnet_discover_peer_locked(lpni, cpt, true);
4594 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4595 buf[i].pid = id.pid;
4596 buf[i].nid = p->lpni_nid;
4603 lnet_peer_ni_decref_locked(lpni);
4605 lnet_net_unlock(cpt);
4608 if (copy_to_user(ids, buf, rc * sizeof(*buf)))
4610 CFS_FREE_PTR_ARRAY(buf, n_ids);
4616 * Retrieve peer discovery status.
4618 * \retval 1 if lnet_peer_discovery_disabled is 0
4619 * \retval 0 if lnet_peer_discovery_disabled is 1
4622 LNetGetPeerDiscoveryStatus(void)
4624 return !lnet_peer_discovery_disabled;
4626 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);