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/
32 #define DEBUG_SUBSYSTEM S_LNET
34 #include <linux/ctype.h>
35 #include <linux/log2.h>
36 #include <linux/ktime.h>
37 #include <linux/moduleparam.h>
38 #include <linux/uaccess.h>
39 #ifdef HAVE_SCHED_HEADERS
40 #include <linux/sched/signal.h>
42 #include <lnet/udsp.h>
43 #include <lnet/lib-lnet.h>
45 #define D_LNI D_CONSOLE
48 * initialize ln_api_mutex statically, since it needs to be used in
49 * discovery_set callback. That module parameter callback can be called
50 * before module init completes. The mutex needs to be ready for use then.
52 struct lnet the_lnet = {
53 .ln_api_mutex = __MUTEX_INITIALIZER(the_lnet.ln_api_mutex),
54 }; /* THE state of the network */
55 EXPORT_SYMBOL(the_lnet);
57 static char *ip2nets = "";
58 module_param(ip2nets, charp, 0444);
59 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
61 static char *networks = "";
62 module_param(networks, charp, 0444);
63 MODULE_PARM_DESC(networks, "local networks");
65 static char *routes = "";
66 module_param(routes, charp, 0444);
67 MODULE_PARM_DESC(routes, "routes to non-local networks");
69 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
70 module_param(rnet_htable_size, int, 0444);
71 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
73 static int use_tcp_bonding;
74 module_param(use_tcp_bonding, int, 0444);
75 MODULE_PARM_DESC(use_tcp_bonding,
76 "use_tcp_bonding parameter has been removed");
78 unsigned int lnet_numa_range = 0;
79 module_param(lnet_numa_range, uint, 0444);
80 MODULE_PARM_DESC(lnet_numa_range,
81 "NUMA range to consider during Multi-Rail selection");
84 * lnet_health_sensitivity determines by how much we decrement the health
85 * value on sending error. The value defaults to 100, which means health
86 * interface health is decremented by 100 points every failure.
88 unsigned int lnet_health_sensitivity = 100;
89 static int sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
90 #ifdef HAVE_KERNEL_PARAM_OPS
91 static struct kernel_param_ops param_ops_health_sensitivity = {
92 .set = sensitivity_set,
95 #define param_check_health_sensitivity(name, p) \
96 __param_check(name, p, int)
97 module_param(lnet_health_sensitivity, health_sensitivity, S_IRUGO|S_IWUSR);
99 module_param_call(lnet_health_sensitivity, sensitivity_set, param_get_int,
100 &lnet_health_sensitivity, S_IRUGO|S_IWUSR);
102 MODULE_PARM_DESC(lnet_health_sensitivity,
103 "Value to decrement the health value by on error");
106 * lnet_recovery_interval determines how often we should perform recovery
107 * on unhealthy interfaces.
109 unsigned int lnet_recovery_interval = 1;
110 static int recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp);
111 #ifdef HAVE_KERNEL_PARAM_OPS
112 static struct kernel_param_ops param_ops_recovery_interval = {
113 .set = recovery_interval_set,
114 .get = param_get_int,
116 #define param_check_recovery_interval(name, p) \
117 __param_check(name, p, int)
118 module_param(lnet_recovery_interval, recovery_interval, S_IRUGO|S_IWUSR);
120 module_param_call(lnet_recovery_interval, recovery_interval_set, param_get_int,
121 &lnet_recovery_interval, S_IRUGO|S_IWUSR);
123 MODULE_PARM_DESC(lnet_recovery_interval,
124 "DEPRECATED - Interval to recover unhealthy interfaces in seconds");
126 unsigned int lnet_recovery_limit;
127 module_param(lnet_recovery_limit, uint, 0644);
128 MODULE_PARM_DESC(lnet_recovery_limit,
129 "How long to attempt recovery of unhealthy peer interfaces in seconds. Set to 0 to allow indefinite recovery");
131 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
132 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
134 static struct kernel_param_ops param_ops_interfaces_max = {
136 .get = param_get_int,
139 #define param_check_interfaces_max(name, p) \
140 __param_check(name, p, int)
142 #ifdef HAVE_KERNEL_PARAM_OPS
143 module_param(lnet_interfaces_max, interfaces_max, 0644);
145 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
146 ¶m_ops_interfaces_max, 0644);
148 MODULE_PARM_DESC(lnet_interfaces_max,
149 "Maximum number of interfaces in a node.");
151 unsigned lnet_peer_discovery_disabled = 0;
152 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
154 static struct kernel_param_ops param_ops_discovery_disabled = {
155 .set = discovery_set,
156 .get = param_get_int,
159 #define param_check_discovery_disabled(name, p) \
160 __param_check(name, p, int)
161 #ifdef HAVE_KERNEL_PARAM_OPS
162 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
164 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
165 ¶m_ops_discovery_disabled, 0644);
167 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
168 "Set to 1 to disable peer discovery on this node.");
170 unsigned int lnet_drop_asym_route;
171 static int drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp);
173 static struct kernel_param_ops param_ops_drop_asym_route = {
174 .set = drop_asym_route_set,
175 .get = param_get_int,
178 #define param_check_drop_asym_route(name, p) \
179 __param_check(name, p, int)
180 #ifdef HAVE_KERNEL_PARAM_OPS
181 module_param(lnet_drop_asym_route, drop_asym_route, 0644);
183 module_param_call(lnet_drop_asym_route, drop_asym_route_set, param_get_int,
184 ¶m_ops_drop_asym_route, 0644);
186 MODULE_PARM_DESC(lnet_drop_asym_route,
187 "Set to 1 to drop asymmetrical route messages.");
189 #define LNET_TRANSACTION_TIMEOUT_DEFAULT 50
190 unsigned int lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_DEFAULT;
191 static int transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp);
192 #ifdef HAVE_KERNEL_PARAM_OPS
193 static struct kernel_param_ops param_ops_transaction_timeout = {
194 .set = transaction_to_set,
195 .get = param_get_int,
198 #define param_check_transaction_timeout(name, p) \
199 __param_check(name, p, int)
200 module_param(lnet_transaction_timeout, transaction_timeout, S_IRUGO|S_IWUSR);
202 module_param_call(lnet_transaction_timeout, transaction_to_set, param_get_int,
203 &lnet_transaction_timeout, S_IRUGO|S_IWUSR);
205 MODULE_PARM_DESC(lnet_transaction_timeout,
206 "Maximum number of seconds to wait for a peer response.");
208 #define LNET_RETRY_COUNT_DEFAULT 2
209 unsigned int lnet_retry_count = LNET_RETRY_COUNT_DEFAULT;
210 static int retry_count_set(const char *val, cfs_kernel_param_arg_t *kp);
211 #ifdef HAVE_KERNEL_PARAM_OPS
212 static struct kernel_param_ops param_ops_retry_count = {
213 .set = retry_count_set,
214 .get = param_get_int,
217 #define param_check_retry_count(name, p) \
218 __param_check(name, p, int)
219 module_param(lnet_retry_count, retry_count, S_IRUGO|S_IWUSR);
221 module_param_call(lnet_retry_count, retry_count_set, param_get_int,
222 &lnet_retry_count, S_IRUGO|S_IWUSR);
224 MODULE_PARM_DESC(lnet_retry_count,
225 "Maximum number of times to retry transmitting a message");
227 unsigned int lnet_response_tracking = 3;
228 static int response_tracking_set(const char *val, cfs_kernel_param_arg_t *kp);
230 #ifdef HAVE_KERNEL_PARAM_OPS
231 static struct kernel_param_ops param_ops_response_tracking = {
232 .set = response_tracking_set,
233 .get = param_get_int,
236 #define param_check_response_tracking(name, p) \
237 __param_check(name, p, int)
238 module_param(lnet_response_tracking, response_tracking, 0644);
240 module_param_call(lnet_response_tracking, response_tracking_set, param_get_int,
241 &lnet_response_tracking, 0644);
243 MODULE_PARM_DESC(lnet_response_tracking,
244 "(0|1|2|3) LNet Internal Only|GET Reply only|PUT ACK only|Full Tracking (default)");
246 #define LNET_LND_TIMEOUT_DEFAULT ((LNET_TRANSACTION_TIMEOUT_DEFAULT - 1) / \
247 (LNET_RETRY_COUNT_DEFAULT + 1))
248 unsigned int lnet_lnd_timeout = LNET_LND_TIMEOUT_DEFAULT;
249 static void lnet_set_lnd_timeout(void)
251 lnet_lnd_timeout = (lnet_transaction_timeout - 1) /
252 (lnet_retry_count + 1);
256 * This sequence number keeps track of how many times DLC was used to
257 * update the local NIs. It is incremented when a NI is added or
258 * removed and checked when sending a message to determine if there is
259 * a need to re-run the selection algorithm. See lnet_select_pathway()
260 * for more details on its usage.
262 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
264 static int lnet_ping(struct lnet_process_id id, struct lnet_nid *src_nid,
265 signed long timeout, struct lnet_process_id __user *ids,
268 static int lnet_discover(struct lnet_process_id id, __u32 force,
269 struct lnet_process_id __user *ids, int n_ids);
272 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
275 unsigned *sensitivity = (unsigned *)kp->arg;
278 rc = kstrtoul(val, 0, &value);
280 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
285 * The purpose of locking the api_mutex here is to ensure that
286 * the correct value ends up stored properly.
288 mutex_lock(&the_lnet.ln_api_mutex);
290 if (value > LNET_MAX_HEALTH_VALUE) {
291 mutex_unlock(&the_lnet.ln_api_mutex);
292 CERROR("Invalid health value. Maximum: %d value = %lu\n",
293 LNET_MAX_HEALTH_VALUE, value);
297 if (*sensitivity != 0 && value == 0 && lnet_retry_count != 0) {
298 lnet_retry_count = 0;
299 lnet_set_lnd_timeout();
302 *sensitivity = value;
304 mutex_unlock(&the_lnet.ln_api_mutex);
310 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
312 CWARN("'lnet_recovery_interval' has been deprecated\n");
318 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
321 unsigned *discovery_off = (unsigned *)kp->arg;
323 struct lnet_ping_buffer *pbuf;
325 rc = kstrtoul(val, 0, &value);
327 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
331 value = (value) ? 1 : 0;
334 * The purpose of locking the api_mutex here is to ensure that
335 * the correct value ends up stored properly.
337 mutex_lock(&the_lnet.ln_api_mutex);
339 if (value == *discovery_off) {
340 mutex_unlock(&the_lnet.ln_api_mutex);
345 * We still want to set the discovery value even when LNet is not
346 * running. This is the case when LNet is being loaded and we want
347 * the module parameters to take effect. Otherwise if we're
348 * changing the value dynamically, we want to set it after
351 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
352 *discovery_off = value;
353 mutex_unlock(&the_lnet.ln_api_mutex);
357 /* tell peers that discovery setting has changed */
358 lnet_net_lock(LNET_LOCK_EX);
359 pbuf = the_lnet.ln_ping_target;
361 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
363 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
364 lnet_net_unlock(LNET_LOCK_EX);
366 /* only send a push when we're turning off discovery */
367 if (*discovery_off <= 0 && value > 0)
368 lnet_push_update_to_peers(1);
369 *discovery_off = value;
371 mutex_unlock(&the_lnet.ln_api_mutex);
377 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
380 unsigned int *drop_asym_route = (unsigned int *)kp->arg;
383 rc = kstrtoul(val, 0, &value);
385 CERROR("Invalid module parameter value for "
386 "'lnet_drop_asym_route'\n");
391 * The purpose of locking the api_mutex here is to ensure that
392 * the correct value ends up stored properly.
394 mutex_lock(&the_lnet.ln_api_mutex);
396 if (value == *drop_asym_route) {
397 mutex_unlock(&the_lnet.ln_api_mutex);
401 *drop_asym_route = value;
403 mutex_unlock(&the_lnet.ln_api_mutex);
409 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
412 unsigned *transaction_to = (unsigned *)kp->arg;
415 rc = kstrtoul(val, 0, &value);
417 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
422 * The purpose of locking the api_mutex here is to ensure that
423 * the correct value ends up stored properly.
425 mutex_lock(&the_lnet.ln_api_mutex);
427 if (value <= lnet_retry_count || value == 0) {
428 mutex_unlock(&the_lnet.ln_api_mutex);
429 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
430 "Has to be greater than lnet_retry_count (%u)\n",
431 value, lnet_retry_count);
435 if (value == *transaction_to) {
436 mutex_unlock(&the_lnet.ln_api_mutex);
440 *transaction_to = value;
441 /* Update the lnet_lnd_timeout now that we've modified the
442 * transaction timeout
444 lnet_set_lnd_timeout();
446 mutex_unlock(&the_lnet.ln_api_mutex);
452 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
455 unsigned *retry_count = (unsigned *)kp->arg;
458 rc = kstrtoul(val, 0, &value);
460 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
465 * The purpose of locking the api_mutex here is to ensure that
466 * the correct value ends up stored properly.
468 mutex_lock(&the_lnet.ln_api_mutex);
470 if (lnet_health_sensitivity == 0 && value > 0) {
471 mutex_unlock(&the_lnet.ln_api_mutex);
472 CERROR("Can not set lnet_retry_count when health feature is turned off\n");
476 if (value > lnet_transaction_timeout) {
477 mutex_unlock(&the_lnet.ln_api_mutex);
478 CERROR("Invalid value for lnet_retry_count (%lu). "
479 "Has to be smaller than lnet_transaction_timeout (%u)\n",
480 value, lnet_transaction_timeout);
484 *retry_count = value;
486 /* Update the lnet_lnd_timeout now that we've modified the
489 lnet_set_lnd_timeout();
491 mutex_unlock(&the_lnet.ln_api_mutex);
497 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
501 rc = kstrtoint(val, 0, &value);
503 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
507 if (value < LNET_INTERFACES_MIN) {
508 CWARN("max interfaces provided are too small, setting to %d\n",
509 LNET_INTERFACES_MAX_DEFAULT);
510 value = LNET_INTERFACES_MAX_DEFAULT;
513 *(int *)kp->arg = value;
519 response_tracking_set(const char *val, cfs_kernel_param_arg_t *kp)
522 unsigned long new_value;
524 rc = kstrtoul(val, 0, &new_value);
526 CERROR("Invalid value for 'lnet_response_tracking'\n");
530 if (new_value < 0 || new_value > 3) {
531 CWARN("Invalid value (%lu) for 'lnet_response_tracking'\n",
536 lnet_response_tracking = new_value;
542 lnet_get_routes(void)
548 lnet_get_networks(void)
553 if (*networks != 0 && *ip2nets != 0) {
554 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
555 "'ip2nets' but not both at once\n");
560 rc = lnet_parse_ip2nets(&nets, ip2nets);
561 return (rc == 0) ? nets : NULL;
571 lnet_init_locks(void)
573 spin_lock_init(&the_lnet.ln_eq_wait_lock);
574 spin_lock_init(&the_lnet.ln_msg_resend_lock);
575 init_completion(&the_lnet.ln_mt_wait_complete);
576 mutex_init(&the_lnet.ln_lnd_mutex);
579 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
580 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
582 struct kmem_cache *lnet_udsp_cachep; /* udsp cache */
583 struct kmem_cache *lnet_rspt_cachep; /* response tracker cache */
584 struct kmem_cache *lnet_msg_cachep;
587 lnet_slab_setup(void)
589 /* create specific kmem_cache for MEs and small MDs (i.e., originally
590 * allocated in <size-xxx> kmem_cache).
592 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
594 if (!lnet_mes_cachep)
597 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
598 LNET_SMALL_MD_SIZE, 0, 0,
600 if (!lnet_small_mds_cachep)
603 lnet_udsp_cachep = kmem_cache_create("lnet_udsp",
604 sizeof(struct lnet_udsp),
606 if (!lnet_udsp_cachep)
609 lnet_rspt_cachep = kmem_cache_create("lnet_rspt", sizeof(struct lnet_rsp_tracker),
611 if (!lnet_rspt_cachep)
614 lnet_msg_cachep = kmem_cache_create("lnet_msg", sizeof(struct lnet_msg),
616 if (!lnet_msg_cachep)
623 lnet_slab_cleanup(void)
625 if (lnet_msg_cachep) {
626 kmem_cache_destroy(lnet_msg_cachep);
627 lnet_msg_cachep = NULL;
630 if (lnet_rspt_cachep) {
631 kmem_cache_destroy(lnet_rspt_cachep);
632 lnet_rspt_cachep = NULL;
635 if (lnet_udsp_cachep) {
636 kmem_cache_destroy(lnet_udsp_cachep);
637 lnet_udsp_cachep = NULL;
640 if (lnet_small_mds_cachep) {
641 kmem_cache_destroy(lnet_small_mds_cachep);
642 lnet_small_mds_cachep = NULL;
645 if (lnet_mes_cachep) {
646 kmem_cache_destroy(lnet_mes_cachep);
647 lnet_mes_cachep = NULL;
652 lnet_create_remote_nets_table(void)
655 struct list_head *hash;
657 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
658 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
659 CFS_ALLOC_PTR_ARRAY(hash, LNET_REMOTE_NETS_HASH_SIZE);
661 CERROR("Failed to create remote nets hash table\n");
665 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
666 INIT_LIST_HEAD(&hash[i]);
667 the_lnet.ln_remote_nets_hash = hash;
672 lnet_destroy_remote_nets_table(void)
676 if (the_lnet.ln_remote_nets_hash == NULL)
679 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
680 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
682 CFS_FREE_PTR_ARRAY(the_lnet.ln_remote_nets_hash,
683 LNET_REMOTE_NETS_HASH_SIZE);
684 the_lnet.ln_remote_nets_hash = NULL;
688 lnet_destroy_locks(void)
690 if (the_lnet.ln_res_lock != NULL) {
691 cfs_percpt_lock_free(the_lnet.ln_res_lock);
692 the_lnet.ln_res_lock = NULL;
695 if (the_lnet.ln_net_lock != NULL) {
696 cfs_percpt_lock_free(the_lnet.ln_net_lock);
697 the_lnet.ln_net_lock = NULL;
702 lnet_create_locks(void)
706 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
707 if (the_lnet.ln_res_lock == NULL)
710 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
711 if (the_lnet.ln_net_lock == NULL)
717 lnet_destroy_locks();
721 static void lnet_assert_wire_constants(void)
723 /* Wire protocol assertions generated by 'wirecheck'
724 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
725 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
726 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
730 BUILD_BUG_ON(LNET_PROTO_TCP_MAGIC != 0xeebc0ded);
731 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MAJOR != 1);
732 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MINOR != 0);
733 BUILD_BUG_ON(LNET_MSG_ACK != 0);
734 BUILD_BUG_ON(LNET_MSG_PUT != 1);
735 BUILD_BUG_ON(LNET_MSG_GET != 2);
736 BUILD_BUG_ON(LNET_MSG_REPLY != 3);
737 BUILD_BUG_ON(LNET_MSG_HELLO != 4);
739 BUILD_BUG_ON((int)sizeof(lnet_nid_t) != 8);
740 BUILD_BUG_ON((int)sizeof(lnet_pid_t) != 4);
742 /* Checks for struct lnet_nid */
743 BUILD_BUG_ON((int)sizeof(struct lnet_nid) != 20);
744 BUILD_BUG_ON((int)offsetof(struct lnet_nid, nid_size) != 0);
745 BUILD_BUG_ON((int)sizeof(((struct lnet_nid *)0)->nid_size) != 1);
746 BUILD_BUG_ON((int)offsetof(struct lnet_nid, nid_type) != 1);
747 BUILD_BUG_ON((int)sizeof(((struct lnet_nid *)0)->nid_type) != 1);
748 BUILD_BUG_ON((int)offsetof(struct lnet_nid, nid_num) != 2);
749 BUILD_BUG_ON((int)sizeof(((struct lnet_nid *)0)->nid_num) != 2);
750 BUILD_BUG_ON((int)offsetof(struct lnet_nid, nid_addr) != 4);
751 BUILD_BUG_ON((int)sizeof(((struct lnet_nid *)0)->nid_addr) != 16);
753 /* Checks for struct lnet_process_id_packed */
754 BUILD_BUG_ON((int)sizeof(struct lnet_process_id_packed) != 12);
755 BUILD_BUG_ON((int)offsetof(struct lnet_process_id_packed, nid) != 0);
756 BUILD_BUG_ON((int)sizeof(((struct lnet_process_id_packed *)0)->nid) != 8);
757 BUILD_BUG_ON((int)offsetof(struct lnet_process_id_packed, pid) != 8);
758 BUILD_BUG_ON((int)sizeof(((struct lnet_process_id_packed *)0)->pid) != 4);
760 /* Checks for struct lnet_handle_wire */
761 BUILD_BUG_ON((int)sizeof(struct lnet_handle_wire) != 16);
762 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
763 wh_interface_cookie) != 0);
764 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) != 8);
765 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
766 wh_object_cookie) != 8);
767 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) != 8);
769 /* Checks for struct struct lnet_magicversion */
770 BUILD_BUG_ON((int)sizeof(struct lnet_magicversion) != 8);
771 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, magic) != 0);
772 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->magic) != 4);
773 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, version_major) != 4);
774 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_major) != 2);
775 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion,
776 version_minor) != 6);
777 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_minor) != 2);
779 /* Checks for struct _lnet_hdr_nid4 */
780 BUILD_BUG_ON((int)sizeof(struct _lnet_hdr_nid4) != 72);
781 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, dest_nid) != 0);
782 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->dest_nid) != 8);
783 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, src_nid) != 8);
784 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->src_nid) != 8);
785 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, dest_pid) != 16);
786 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->dest_pid) != 4);
787 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, src_pid) != 20);
788 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->src_pid) != 4);
789 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, type) != 24);
790 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->type) != 4);
791 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, payload_length) != 28);
792 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->payload_length) != 4);
793 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg) != 32);
794 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg) != 40);
797 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.ack.dst_wmd) != 32);
798 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.ack.dst_wmd) != 16);
799 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.ack.match_bits) != 48);
800 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.ack.match_bits) != 8);
801 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.ack.mlength) != 56);
802 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.ack.mlength) != 4);
805 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.put.ack_wmd) != 32);
806 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.put.ack_wmd) != 16);
807 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.put.match_bits) != 48);
808 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.put.match_bits) != 8);
809 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.put.hdr_data) != 56);
810 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.put.hdr_data) != 8);
811 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.put.ptl_index) != 64);
812 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.put.ptl_index) != 4);
813 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.put.offset) != 68);
814 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.put.offset) != 4);
817 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.get.return_wmd) != 32);
818 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.get.return_wmd) != 16);
819 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.get.match_bits) != 48);
820 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.get.match_bits) != 8);
821 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.get.ptl_index) != 56);
822 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.get.ptl_index) != 4);
823 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.get.src_offset) != 60);
824 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.get.src_offset) != 4);
825 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.get.sink_length) != 64);
826 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.get.sink_length) != 4);
829 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.reply.dst_wmd) != 32);
830 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.reply.dst_wmd) != 16);
833 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.hello.incarnation) != 32);
834 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.hello.incarnation) != 8);
835 BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.hello.type) != 40);
836 BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.hello.type) != 4);
838 /* Checks for struct lnet_ni_status and related constants */
839 BUILD_BUG_ON(LNET_NI_STATUS_INVALID != 0x00000000);
840 BUILD_BUG_ON(LNET_NI_STATUS_UP != 0x15aac0de);
841 BUILD_BUG_ON(LNET_NI_STATUS_DOWN != 0xdeadface);
843 /* Checks for struct lnet_ni_status */
844 BUILD_BUG_ON((int)sizeof(struct lnet_ni_status) != 16);
845 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_nid) != 0);
846 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) != 8);
847 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_status) != 8);
848 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_status) != 4);
849 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_unused) != 12);
850 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) != 4);
852 /* Checks for struct lnet_ping_info and related constants */
853 BUILD_BUG_ON(LNET_PROTO_PING_MAGIC != 0x70696E67);
854 BUILD_BUG_ON(LNET_PING_FEAT_INVAL != 0);
855 BUILD_BUG_ON(LNET_PING_FEAT_BASE != 1);
856 BUILD_BUG_ON(LNET_PING_FEAT_NI_STATUS != 2);
857 BUILD_BUG_ON(LNET_PING_FEAT_RTE_DISABLED != 4);
858 BUILD_BUG_ON(LNET_PING_FEAT_MULTI_RAIL != 8);
859 BUILD_BUG_ON(LNET_PING_FEAT_DISCOVERY != 16);
860 BUILD_BUG_ON(LNET_PING_FEAT_BITS != 31);
862 /* Checks for struct lnet_ping_info */
863 BUILD_BUG_ON((int)sizeof(struct lnet_ping_info) != 16);
864 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_magic) != 0);
865 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) != 4);
866 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_features) != 4);
867 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_features) != 4);
868 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_pid) != 8);
869 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) != 4);
870 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_nnis) != 12);
871 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) != 4);
872 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_ni) != 16);
873 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) != 0);
875 /* Acceptor connection request */
876 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
878 /* Checks for struct lnet_acceptor_connreq */
879 BUILD_BUG_ON((int)sizeof(struct lnet_acceptor_connreq) != 16);
880 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq, acr_magic) != 0);
881 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq *)0)->acr_magic) != 4);
882 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq, acr_version) != 4);
883 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq *)0)->acr_version) != 4);
884 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq, acr_nid) != 8);
885 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq *)0)->acr_nid) != 8);
887 /* Checks for struct lnet_acceptor_connreq_v2 */
888 BUILD_BUG_ON((int)sizeof(struct lnet_acceptor_connreq_v2) != 28);
889 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq_v2, acr_magic) != 0);
890 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq_v2 *)0)->acr_magic) != 4);
891 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq_v2, acr_version) != 4);
892 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq_v2 *)0)->acr_version) != 4);
893 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq_v2, acr_nid) != 8);
894 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq_v2 *)0)->acr_nid) != 20);
896 /* Checks for struct lnet_counters_common */
897 BUILD_BUG_ON((int)sizeof(struct lnet_counters_common) != 60);
898 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_msgs_alloc) != 0);
899 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_msgs_alloc) != 4);
900 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_msgs_max) != 4);
901 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_msgs_max) != 4);
902 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_errors) != 8);
903 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_errors) != 4);
904 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_send_count) != 12);
905 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_send_count) != 4);
906 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_recv_count) != 16);
907 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_recv_count) != 4);
908 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_route_count) != 20);
909 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_route_count) != 4);
910 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_drop_count) != 24);
911 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_drop_count) != 4);
912 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_send_length) != 28);
913 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_send_length) != 8);
914 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_recv_length) != 36);
915 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_recv_length) != 8);
916 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_route_length) != 44);
917 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_route_length) != 8);
918 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_drop_length) != 52);
919 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_drop_length) != 8);
922 static const struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
924 const struct lnet_lnd *lnd;
926 /* holding lnd mutex */
927 if (type >= NUM_LNDS)
929 lnd = the_lnet.ln_lnds[type];
930 LASSERT(!lnd || lnd->lnd_type == type);
936 lnet_get_lnd_timeout(void)
938 return lnet_lnd_timeout;
940 EXPORT_SYMBOL(lnet_get_lnd_timeout);
943 lnet_register_lnd(const struct lnet_lnd *lnd)
945 mutex_lock(&the_lnet.ln_lnd_mutex);
947 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
948 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
950 the_lnet.ln_lnds[lnd->lnd_type] = lnd;
952 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
954 mutex_unlock(&the_lnet.ln_lnd_mutex);
956 EXPORT_SYMBOL(lnet_register_lnd);
959 lnet_unregister_lnd(const struct lnet_lnd *lnd)
961 mutex_lock(&the_lnet.ln_lnd_mutex);
963 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
965 the_lnet.ln_lnds[lnd->lnd_type] = NULL;
966 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
968 mutex_unlock(&the_lnet.ln_lnd_mutex);
970 EXPORT_SYMBOL(lnet_unregister_lnd);
973 lnet_counters_get_common_locked(struct lnet_counters_common *common)
975 struct lnet_counters *ctr;
978 /* FIXME !!! Their is no assert_lnet_net_locked() to ensure this
979 * actually called under the protection of the lnet_net_lock.
981 memset(common, 0, sizeof(*common));
983 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
984 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
985 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
986 common->lcc_errors += ctr->lct_common.lcc_errors;
987 common->lcc_send_count += ctr->lct_common.lcc_send_count;
988 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
989 common->lcc_route_count += ctr->lct_common.lcc_route_count;
990 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
991 common->lcc_send_length += ctr->lct_common.lcc_send_length;
992 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
993 common->lcc_route_length += ctr->lct_common.lcc_route_length;
994 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
999 lnet_counters_get_common(struct lnet_counters_common *common)
1001 lnet_net_lock(LNET_LOCK_EX);
1002 lnet_counters_get_common_locked(common);
1003 lnet_net_unlock(LNET_LOCK_EX);
1005 EXPORT_SYMBOL(lnet_counters_get_common);
1008 lnet_counters_get(struct lnet_counters *counters)
1010 struct lnet_counters *ctr;
1011 struct lnet_counters_health *health = &counters->lct_health;
1014 memset(counters, 0, sizeof(*counters));
1016 lnet_net_lock(LNET_LOCK_EX);
1018 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1019 GOTO(out_unlock, rc = -ENODEV);
1021 lnet_counters_get_common_locked(&counters->lct_common);
1023 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
1024 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
1025 health->lch_resend_count += ctr->lct_health.lch_resend_count;
1026 health->lch_response_timeout_count +=
1027 ctr->lct_health.lch_response_timeout_count;
1028 health->lch_local_interrupt_count +=
1029 ctr->lct_health.lch_local_interrupt_count;
1030 health->lch_local_dropped_count +=
1031 ctr->lct_health.lch_local_dropped_count;
1032 health->lch_local_aborted_count +=
1033 ctr->lct_health.lch_local_aborted_count;
1034 health->lch_local_no_route_count +=
1035 ctr->lct_health.lch_local_no_route_count;
1036 health->lch_local_timeout_count +=
1037 ctr->lct_health.lch_local_timeout_count;
1038 health->lch_local_error_count +=
1039 ctr->lct_health.lch_local_error_count;
1040 health->lch_remote_dropped_count +=
1041 ctr->lct_health.lch_remote_dropped_count;
1042 health->lch_remote_error_count +=
1043 ctr->lct_health.lch_remote_error_count;
1044 health->lch_remote_timeout_count +=
1045 ctr->lct_health.lch_remote_timeout_count;
1046 health->lch_network_timeout_count +=
1047 ctr->lct_health.lch_network_timeout_count;
1050 lnet_net_unlock(LNET_LOCK_EX);
1053 EXPORT_SYMBOL(lnet_counters_get);
1056 lnet_counters_reset(void)
1058 struct lnet_counters *counters;
1061 lnet_net_lock(LNET_LOCK_EX);
1063 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1066 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
1067 memset(counters, 0, sizeof(struct lnet_counters));
1069 lnet_net_unlock(LNET_LOCK_EX);
1073 lnet_res_type2str(int type)
1078 case LNET_COOKIE_TYPE_MD:
1080 case LNET_COOKIE_TYPE_ME:
1082 case LNET_COOKIE_TYPE_EQ:
1088 lnet_res_container_cleanup(struct lnet_res_container *rec)
1092 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
1095 while (!list_empty(&rec->rec_active)) {
1096 struct list_head *e = rec->rec_active.next;
1099 if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
1100 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
1102 } else { /* NB: Active MEs should be attached on portals */
1109 /* Found alive MD/ME/EQ, user really should unlink/free
1110 * all of them before finalize LNet, but if someone didn't,
1111 * we have to recycle garbage for him */
1112 CERROR("%d active elements on exit of %s container\n",
1113 count, lnet_res_type2str(rec->rec_type));
1116 if (rec->rec_lh_hash != NULL) {
1117 CFS_FREE_PTR_ARRAY(rec->rec_lh_hash, LNET_LH_HASH_SIZE);
1118 rec->rec_lh_hash = NULL;
1121 rec->rec_type = 0; /* mark it as finalized */
1125 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1130 LASSERT(rec->rec_type == 0);
1132 rec->rec_type = type;
1133 INIT_LIST_HEAD(&rec->rec_active);
1135 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1137 /* Arbitrary choice of hash table size */
1138 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1139 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1140 if (rec->rec_lh_hash == NULL) {
1145 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1146 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1151 CERROR("Failed to setup %s resource container\n",
1152 lnet_res_type2str(type));
1153 lnet_res_container_cleanup(rec);
1158 lnet_res_containers_destroy(struct lnet_res_container **recs)
1160 struct lnet_res_container *rec;
1163 cfs_percpt_for_each(rec, i, recs)
1164 lnet_res_container_cleanup(rec);
1166 cfs_percpt_free(recs);
1169 static struct lnet_res_container **
1170 lnet_res_containers_create(int type)
1172 struct lnet_res_container **recs;
1173 struct lnet_res_container *rec;
1177 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1179 CERROR("Failed to allocate %s resource containers\n",
1180 lnet_res_type2str(type));
1184 cfs_percpt_for_each(rec, i, recs) {
1185 rc = lnet_res_container_setup(rec, i, type);
1187 lnet_res_containers_destroy(recs);
1195 struct lnet_libhandle *
1196 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1198 /* ALWAYS called with lnet_res_lock held */
1199 struct list_head *head;
1200 struct lnet_libhandle *lh;
1203 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1206 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1207 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1209 list_for_each_entry(lh, head, lh_hash_chain) {
1210 if (lh->lh_cookie == cookie)
1218 lnet_res_lh_initialize(struct lnet_res_container *rec,
1219 struct lnet_libhandle *lh)
1221 /* ALWAYS called with lnet_res_lock held */
1222 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1225 lh->lh_cookie = rec->rec_lh_cookie;
1226 rec->rec_lh_cookie += 1 << ibits;
1228 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1230 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1234 lnet_create_array_of_queues(void)
1236 struct list_head **qs;
1237 struct list_head *q;
1240 qs = cfs_percpt_alloc(lnet_cpt_table(),
1241 sizeof(struct list_head));
1243 CERROR("Failed to allocate queues\n");
1247 cfs_percpt_for_each(q, i, qs)
1253 static int lnet_unprepare(void);
1256 lnet_prepare(lnet_pid_t requested_pid)
1258 /* Prepare to bring up the network */
1259 struct lnet_res_container **recs;
1262 if (requested_pid == LNET_PID_ANY) {
1263 /* Don't instantiate LNET just for me */
1267 LASSERT(the_lnet.ln_refcount == 0);
1269 the_lnet.ln_routing = 0;
1271 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1272 the_lnet.ln_pid = requested_pid;
1274 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1275 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1276 INIT_LIST_HEAD(&the_lnet.ln_nets);
1277 INIT_LIST_HEAD(&the_lnet.ln_routers);
1278 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1279 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1280 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1281 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1282 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1283 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1284 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1285 INIT_LIST_HEAD(&the_lnet.ln_udsp_list);
1286 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1287 the_lnet.ln_mt_handler = NULL;
1288 init_completion(&the_lnet.ln_started);
1290 rc = lnet_slab_setup();
1294 rc = lnet_create_remote_nets_table();
1299 * NB the interface cookie in wire handles guards against delayed
1300 * replies and ACKs appearing valid after reboot.
1302 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1304 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1305 sizeof(struct lnet_counters));
1306 if (the_lnet.ln_counters == NULL) {
1307 CERROR("Failed to allocate counters for LNet\n");
1312 rc = lnet_peer_tables_create();
1316 rc = lnet_msg_containers_create();
1320 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1321 LNET_COOKIE_TYPE_EQ);
1325 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1331 the_lnet.ln_md_containers = recs;
1333 rc = lnet_portals_create();
1335 CERROR("Failed to create portals for LNet: %d\n", rc);
1339 the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1340 if (!the_lnet.ln_mt_zombie_rstqs) {
1353 lnet_unprepare(void)
1355 /* NB no LNET_LOCK since this is the last reference. All LND instances
1356 * have shut down already, so it is safe to unlink and free all
1357 * descriptors, even those that appear committed to a network op (eg MD
1358 * with non-zero pending count) */
1360 lnet_fail_nid(LNET_NID_ANY, 0);
1362 LASSERT(the_lnet.ln_refcount == 0);
1363 LASSERT(list_empty(&the_lnet.ln_test_peers));
1364 LASSERT(list_empty(&the_lnet.ln_nets));
1366 if (the_lnet.ln_mt_zombie_rstqs) {
1367 lnet_clean_zombie_rstqs();
1368 the_lnet.ln_mt_zombie_rstqs = NULL;
1371 lnet_assert_handler_unused(the_lnet.ln_mt_handler);
1372 the_lnet.ln_mt_handler = NULL;
1374 lnet_portals_destroy();
1376 if (the_lnet.ln_md_containers != NULL) {
1377 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1378 the_lnet.ln_md_containers = NULL;
1381 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1383 lnet_msg_containers_destroy();
1385 lnet_rtrpools_free(0);
1387 if (the_lnet.ln_counters != NULL) {
1388 cfs_percpt_free(the_lnet.ln_counters);
1389 the_lnet.ln_counters = NULL;
1391 lnet_destroy_remote_nets_table();
1392 lnet_udsp_destroy(true);
1393 lnet_slab_cleanup();
1399 lnet_net2ni_locked(__u32 net_id, int cpt)
1402 struct lnet_net *net;
1404 LASSERT(cpt != LNET_LOCK_EX);
1406 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1407 if (net->net_id == net_id) {
1408 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1418 lnet_net2ni_addref(__u32 net)
1423 ni = lnet_net2ni_locked(net, 0);
1425 lnet_ni_addref_locked(ni, 0);
1430 EXPORT_SYMBOL(lnet_net2ni_addref);
1433 lnet_get_net_locked(__u32 net_id)
1435 struct lnet_net *net;
1437 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1438 if (net->net_id == net_id)
1446 lnet_net_clr_pref_rtrs(struct lnet_net *net)
1448 struct list_head zombies;
1449 struct lnet_nid_list *ne;
1450 struct lnet_nid_list *tmp;
1452 INIT_LIST_HEAD(&zombies);
1454 lnet_net_lock(LNET_LOCK_EX);
1455 list_splice_init(&net->net_rtr_pref_nids, &zombies);
1456 lnet_net_unlock(LNET_LOCK_EX);
1458 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1459 list_del_init(&ne->nl_list);
1460 LIBCFS_FREE(ne, sizeof(*ne));
1465 lnet_net_add_pref_rtr(struct lnet_net *net,
1466 struct lnet_nid *gw_nid)
1467 __must_hold(&the_lnet.ln_api_mutex)
1469 struct lnet_nid_list *ne;
1471 /* This function is called with api_mutex held. When the api_mutex
1472 * is held the list can not be modified, as it is only modified as
1473 * a result of applying a UDSP and that happens under api_mutex
1476 list_for_each_entry(ne, &net->net_rtr_pref_nids, nl_list) {
1477 if (nid_same(&ne->nl_nid, gw_nid))
1481 LIBCFS_ALLOC(ne, sizeof(*ne));
1485 ne->nl_nid = *gw_nid;
1487 /* Lock the cpt to protect against addition and checks in the
1488 * selection algorithm
1490 lnet_net_lock(LNET_LOCK_EX);
1491 list_add(&ne->nl_list, &net->net_rtr_pref_nids);
1492 lnet_net_unlock(LNET_LOCK_EX);
1498 lnet_net_is_pref_rtr_locked(struct lnet_net *net, struct lnet_nid *rtr_nid)
1500 struct lnet_nid_list *ne;
1502 CDEBUG(D_NET, "%s: rtr pref empty: %d\n",
1503 libcfs_net2str(net->net_id),
1504 list_empty(&net->net_rtr_pref_nids));
1506 if (list_empty(&net->net_rtr_pref_nids))
1509 list_for_each_entry(ne, &net->net_rtr_pref_nids, nl_list) {
1510 CDEBUG(D_NET, "Comparing pref %s with gw %s\n",
1511 libcfs_nidstr(&ne->nl_nid),
1512 libcfs_nidstr(rtr_nid));
1513 if (nid_same(rtr_nid, &ne->nl_nid))
1521 lnet_nid4_cpt_hash(lnet_nid_t nid, unsigned int number)
1524 __u64 pair_bits = 0x0001000100010001LLU;
1525 __u64 mask = pair_bits * 0xFF;
1528 /* Use (sum-by-multiplication of nid bytes) mod (number of CPTs)
1529 * to match nid to a CPT.
1531 pair_sum = (key & mask) + ((key >> 8) & mask);
1532 pair_sum = (pair_sum * pair_bits) >> 48;
1534 CDEBUG(D_NET, "Match nid %s to cpt %u\n",
1535 libcfs_nid2str(nid), (unsigned int)(pair_sum) % number);
1537 return (unsigned int)(pair_sum) % number;
1541 lnet_nid_cpt_hash(struct lnet_nid *nid, unsigned int number)
1547 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1552 if (nid_is_nid4(nid))
1553 return lnet_nid4_cpt_hash(lnet_nid_to_nid4(nid), number);
1555 for (i = 0; i < 4; i++)
1556 h = hash_32(nid->nid_addr[i]^h, 32);
1557 val = hash_32(LNET_NID_NET(nid) ^ h, LNET_CPT_BITS);
1560 return (unsigned int)(h + val + (val >> 1)) % number;
1564 lnet_cpt_of_nid_locked(struct lnet_nid *nid, struct lnet_ni *ni)
1566 struct lnet_net *net;
1568 /* must called with hold of lnet_net_lock */
1569 if (LNET_CPT_NUMBER == 1)
1570 return 0; /* the only one */
1573 * If NI is provided then use the CPT identified in the NI cpt
1574 * list if one exists. If one doesn't exist, then that NI is
1575 * associated with all CPTs and it follows that the net it belongs
1576 * to is implicitly associated with all CPTs, so just hash the nid
1580 if (ni->ni_cpts != NULL)
1581 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1584 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1587 /* no NI provided so look at the net */
1588 net = lnet_get_net_locked(LNET_NID_NET(nid));
1590 if (net != NULL && net->net_cpts != NULL) {
1591 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1594 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1598 lnet_nid2cpt(struct lnet_nid *nid, struct lnet_ni *ni)
1603 if (LNET_CPT_NUMBER == 1)
1604 return 0; /* the only one */
1606 cpt = lnet_net_lock_current();
1608 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1610 lnet_net_unlock(cpt);
1614 EXPORT_SYMBOL(lnet_nid2cpt);
1617 lnet_cpt_of_nid(lnet_nid_t nid4, struct lnet_ni *ni)
1619 struct lnet_nid nid;
1621 if (LNET_CPT_NUMBER == 1)
1622 return 0; /* the only one */
1624 lnet_nid4_to_nid(nid4, &nid);
1625 return lnet_nid2cpt(&nid, ni);
1627 EXPORT_SYMBOL(lnet_cpt_of_nid);
1630 lnet_islocalnet_locked(__u32 net_id)
1632 struct lnet_net *net;
1635 net = lnet_get_net_locked(net_id);
1637 local = net != NULL;
1643 lnet_islocalnet(__u32 net_id)
1648 cpt = lnet_net_lock_current();
1650 local = lnet_islocalnet_locked(net_id);
1652 lnet_net_unlock(cpt);
1658 lnet_nid_to_ni_locked(struct lnet_nid *nid, int cpt)
1660 struct lnet_net *net;
1663 LASSERT(cpt != LNET_LOCK_EX);
1665 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1666 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1667 if (nid_same(&ni->ni_nid, nid))
1676 lnet_nid2ni_locked(lnet_nid_t nid4, int cpt)
1678 struct lnet_nid nid;
1680 lnet_nid4_to_nid(nid4, &nid);
1681 return lnet_nid_to_ni_locked(&nid, cpt);
1685 lnet_nid2ni_addref(lnet_nid_t nid4)
1688 struct lnet_nid nid;
1690 lnet_nid4_to_nid(nid4, &nid);
1693 ni = lnet_nid_to_ni_locked(&nid, 0);
1695 lnet_ni_addref_locked(ni, 0);
1700 EXPORT_SYMBOL(lnet_nid2ni_addref);
1703 lnet_nid_to_ni_addref(struct lnet_nid *nid)
1708 ni = lnet_nid_to_ni_locked(nid, 0);
1710 lnet_ni_addref_locked(ni, 0);
1715 EXPORT_SYMBOL(lnet_nid_to_ni_addref);
1718 lnet_islocalnid(struct lnet_nid *nid)
1723 cpt = lnet_net_lock_current();
1724 ni = lnet_nid_to_ni_locked(nid, cpt);
1725 lnet_net_unlock(cpt);
1731 lnet_count_acceptor_nets(void)
1733 /* Return the # of NIs that need the acceptor. */
1735 struct lnet_net *net;
1738 cpt = lnet_net_lock_current();
1739 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1740 /* all socklnd type networks should have the acceptor
1742 if (net->net_lnd->lnd_accept != NULL)
1746 lnet_net_unlock(cpt);
1751 struct lnet_ping_buffer *
1752 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1754 struct lnet_ping_buffer *pbuf;
1756 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1758 pbuf->pb_nnis = nnis;
1759 pbuf->pb_needs_post = false;
1760 atomic_set(&pbuf->pb_refcnt, 1);
1767 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1769 LASSERT(atomic_read(&pbuf->pb_refcnt) == 0);
1770 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1773 static struct lnet_ping_buffer *
1774 lnet_ping_target_create(int nnis)
1776 struct lnet_ping_buffer *pbuf;
1778 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1780 CERROR("Can't allocate ping source [%d]\n", nnis);
1784 pbuf->pb_info.pi_nnis = nnis;
1785 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1786 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1787 pbuf->pb_info.pi_features =
1788 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1794 lnet_get_net_ni_count_locked(struct lnet_net *net)
1799 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1806 lnet_get_net_ni_count_pre(struct lnet_net *net)
1811 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1818 lnet_get_ni_count(void)
1821 struct lnet_net *net;
1826 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1827 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1837 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1839 struct lnet_ni_status *stat;
1843 __swab32s(&pbuf->pb_info.pi_magic);
1844 __swab32s(&pbuf->pb_info.pi_features);
1845 __swab32s(&pbuf->pb_info.pi_pid);
1846 __swab32s(&pbuf->pb_info.pi_nnis);
1847 nnis = pbuf->pb_info.pi_nnis;
1848 if (nnis > pbuf->pb_nnis)
1849 nnis = pbuf->pb_nnis;
1850 for (i = 0; i < nnis; i++) {
1851 stat = &pbuf->pb_info.pi_ni[i];
1852 __swab64s(&stat->ns_nid);
1853 __swab32s(&stat->ns_status);
1858 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1862 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1864 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1866 /* Loopback is guaranteed to be present */
1867 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1869 if (LNET_PING_INFO_LONI(pinfo) != LNET_NID_LO_0)
1875 lnet_ping_target_destroy(void)
1877 struct lnet_net *net;
1880 lnet_net_lock(LNET_LOCK_EX);
1882 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1883 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1885 ni->ni_status = NULL;
1890 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1891 the_lnet.ln_ping_target = NULL;
1893 lnet_net_unlock(LNET_LOCK_EX);
1897 lnet_ping_target_event_handler(struct lnet_event *event)
1899 struct lnet_ping_buffer *pbuf = event->md_user_ptr;
1901 if (event->unlinked)
1902 lnet_ping_buffer_decref(pbuf);
1906 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1907 struct lnet_handle_md *ping_mdh,
1908 int ni_count, bool set_eq)
1910 struct lnet_processid id = {
1911 .nid = LNET_ANY_NID,
1915 struct lnet_md md = { NULL };
1919 the_lnet.ln_ping_target_handler =
1920 lnet_ping_target_event_handler;
1922 *ppbuf = lnet_ping_target_create(ni_count);
1923 if (*ppbuf == NULL) {
1928 /* Ping target ME/MD */
1929 me = LNetMEAttach(LNET_RESERVED_PORTAL, &id,
1930 LNET_PROTO_PING_MATCHBITS, 0,
1931 LNET_UNLINK, LNET_INS_AFTER);
1934 CERROR("Can't create ping target ME: %d\n", rc);
1935 goto fail_decref_ping_buffer;
1938 /* initialize md content */
1939 md.start = &(*ppbuf)->pb_info;
1940 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1941 md.threshold = LNET_MD_THRESH_INF;
1943 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1944 LNET_MD_MANAGE_REMOTE;
1945 md.handler = the_lnet.ln_ping_target_handler;
1946 md.user_ptr = *ppbuf;
1948 rc = LNetMDAttach(me, &md, LNET_RETAIN, ping_mdh);
1950 CERROR("Can't attach ping target MD: %d\n", rc);
1951 goto fail_decref_ping_buffer;
1953 lnet_ping_buffer_addref(*ppbuf);
1957 fail_decref_ping_buffer:
1958 LASSERT(atomic_read(&(*ppbuf)->pb_refcnt) == 1);
1959 lnet_ping_buffer_decref(*ppbuf);
1966 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1967 struct lnet_handle_md *ping_mdh)
1969 LNetMDUnlink(*ping_mdh);
1970 LNetInvalidateMDHandle(ping_mdh);
1972 /* NB the MD could be busy; this just starts the unlink */
1973 wait_var_event_warning(&pbuf->pb_refcnt,
1974 atomic_read(&pbuf->pb_refcnt) <= 1,
1975 "Still waiting for ping data MD to unlink\n");
1979 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1982 struct lnet_net *net;
1983 struct lnet_ni_status *ns;
1988 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1989 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1990 LASSERT(i < pbuf->pb_nnis);
1992 ns = &pbuf->pb_info.pi_ni[i];
1994 if (!nid_is_nid4(&ni->ni_nid))
1996 ns->ns_nid = lnet_nid_to_nid4(&ni->ni_nid);
1999 ns->ns_status = lnet_ni_get_status_locked(ni);
2007 * We (ab)use the ns_status of the loopback interface to
2008 * transmit the sequence number. The first interface listed
2009 * must be the loopback interface.
2011 rc = lnet_ping_info_validate(&pbuf->pb_info);
2013 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
2016 LNET_PING_BUFFER_SEQNO(pbuf) =
2017 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
2021 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
2022 struct lnet_handle_md ping_mdh)
2024 struct lnet_ping_buffer *old_pbuf = NULL;
2025 struct lnet_handle_md old_ping_md;
2027 /* switch the NIs to point to the new ping info created */
2028 lnet_net_lock(LNET_LOCK_EX);
2030 if (!the_lnet.ln_routing)
2031 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
2032 if (!lnet_peer_discovery_disabled)
2033 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
2035 /* Ensure only known feature bits have been set. */
2036 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
2037 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
2039 lnet_ping_target_install_locked(pbuf);
2041 if (the_lnet.ln_ping_target) {
2042 old_pbuf = the_lnet.ln_ping_target;
2043 old_ping_md = the_lnet.ln_ping_target_md;
2045 the_lnet.ln_ping_target_md = ping_mdh;
2046 the_lnet.ln_ping_target = pbuf;
2048 lnet_net_unlock(LNET_LOCK_EX);
2051 /* unlink and free the old ping info */
2052 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
2053 lnet_ping_buffer_decref(old_pbuf);
2056 lnet_push_update_to_peers(0);
2060 lnet_ping_target_fini(void)
2062 lnet_ping_md_unlink(the_lnet.ln_ping_target,
2063 &the_lnet.ln_ping_target_md);
2065 lnet_assert_handler_unused(the_lnet.ln_ping_target_handler);
2066 lnet_ping_target_destroy();
2069 /* Resize the push target. */
2070 int lnet_push_target_resize(void)
2072 struct lnet_handle_md mdh;
2073 struct lnet_handle_md old_mdh;
2074 struct lnet_ping_buffer *pbuf;
2075 struct lnet_ping_buffer *old_pbuf;
2080 nnis = the_lnet.ln_push_target_nnis;
2082 CDEBUG(D_NET, "Invalid nnis %d\n", nnis);
2086 /* NB: lnet_ping_buffer_alloc() sets pbuf refcount to 1. That ref is
2087 * dropped when we need to resize again (see "old_pbuf" below) or when
2088 * LNet is shutdown (see lnet_push_target_fini())
2090 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
2092 CDEBUG(D_NET, "Can't allocate pbuf for nnis %d\n", nnis);
2096 rc = lnet_push_target_post(pbuf, &mdh);
2098 CDEBUG(D_NET, "Failed to post push target: %d\n", rc);
2099 lnet_ping_buffer_decref(pbuf);
2103 lnet_net_lock(LNET_LOCK_EX);
2104 old_pbuf = the_lnet.ln_push_target;
2105 old_mdh = the_lnet.ln_push_target_md;
2106 the_lnet.ln_push_target = pbuf;
2107 the_lnet.ln_push_target_md = mdh;
2108 lnet_net_unlock(LNET_LOCK_EX);
2111 LNetMDUnlink(old_mdh);
2112 /* Drop ref set by lnet_ping_buffer_alloc() */
2113 lnet_ping_buffer_decref(old_pbuf);
2116 /* Received another push or reply that requires a larger buffer */
2117 if (nnis < the_lnet.ln_push_target_nnis)
2120 CDEBUG(D_NET, "nnis %d success\n", nnis);
2124 int lnet_push_target_post(struct lnet_ping_buffer *pbuf,
2125 struct lnet_handle_md *mdhp)
2127 struct lnet_processid id = { LNET_ANY_NID, LNET_PID_ANY };
2128 struct lnet_md md = { NULL };
2132 me = LNetMEAttach(LNET_RESERVED_PORTAL, &id,
2133 LNET_PROTO_PING_MATCHBITS, 0,
2134 LNET_UNLINK, LNET_INS_AFTER);
2137 CERROR("Can't create push target ME: %d\n", rc);
2141 pbuf->pb_needs_post = false;
2143 /* This reference is dropped by lnet_push_target_event_handler() */
2144 lnet_ping_buffer_addref(pbuf);
2146 /* initialize md content */
2147 md.start = &pbuf->pb_info;
2148 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2151 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE;
2153 md.handler = the_lnet.ln_push_target_handler;
2155 rc = LNetMDAttach(me, &md, LNET_UNLINK, mdhp);
2157 CERROR("Can't attach push MD: %d\n", rc);
2158 lnet_ping_buffer_decref(pbuf);
2159 pbuf->pb_needs_post = true;
2163 CDEBUG(D_NET, "posted push target %p\n", pbuf);
2168 static void lnet_push_target_event_handler(struct lnet_event *ev)
2170 struct lnet_ping_buffer *pbuf = ev->md_user_ptr;
2172 CDEBUG(D_NET, "type %d status %d unlinked %d\n", ev->type, ev->status,
2175 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2176 lnet_swap_pinginfo(pbuf);
2178 if (ev->type == LNET_EVENT_UNLINK) {
2179 /* Drop ref added by lnet_push_target_post() */
2180 lnet_ping_buffer_decref(pbuf);
2184 lnet_peer_push_event(ev);
2186 /* Drop ref added by lnet_push_target_post */
2187 lnet_ping_buffer_decref(pbuf);
2190 /* Initialize the push target. */
2191 static int lnet_push_target_init(void)
2195 if (the_lnet.ln_push_target)
2198 the_lnet.ln_push_target_handler =
2199 lnet_push_target_event_handler;
2201 rc = LNetSetLazyPortal(LNET_RESERVED_PORTAL);
2204 /* Start at the required minimum, we'll enlarge if required. */
2205 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
2207 rc = lnet_push_target_resize();
2210 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2211 the_lnet.ln_push_target_handler = NULL;
2217 /* Clean up the push target. */
2218 static void lnet_push_target_fini(void)
2220 if (!the_lnet.ln_push_target)
2223 /* Unlink and invalidate to prevent new references. */
2224 LNetMDUnlink(the_lnet.ln_push_target_md);
2225 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
2227 /* Wait for the unlink to complete. */
2228 wait_var_event_warning(&the_lnet.ln_push_target->pb_refcnt,
2229 atomic_read(&the_lnet.ln_push_target->pb_refcnt) <= 1,
2230 "Still waiting for ping data MD to unlink\n");
2232 /* Drop ref set by lnet_ping_buffer_alloc() */
2233 lnet_ping_buffer_decref(the_lnet.ln_push_target);
2234 the_lnet.ln_push_target = NULL;
2235 the_lnet.ln_push_target_nnis = 0;
2237 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2238 lnet_assert_handler_unused(the_lnet.ln_push_target_handler);
2239 the_lnet.ln_push_target_handler = NULL;
2243 lnet_ni_tq_credits(struct lnet_ni *ni)
2247 LASSERT(ni->ni_ncpts >= 1);
2249 if (ni->ni_ncpts == 1)
2250 return ni->ni_net->net_tunables.lct_max_tx_credits;
2252 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2253 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2254 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2260 lnet_ni_unlink_locked(struct lnet_ni *ni)
2262 /* move it to zombie list and nobody can find it anymore */
2263 LASSERT(!list_empty(&ni->ni_netlist));
2264 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2265 lnet_ni_decref_locked(ni, 0);
2269 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2274 struct list_head *zombie_list = &net->net_ni_zombie;
2277 * Now wait for the NIs I just nuked to show up on the zombie
2278 * list and shut them down in guaranteed thread context
2281 while (!list_empty(zombie_list)) {
2285 ni = list_entry(zombie_list->next,
2286 struct lnet_ni, ni_netlist);
2287 list_del_init(&ni->ni_netlist);
2288 /* the ni should be in deleting state. If it's not it's
2290 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2291 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2294 /* still busy, add it back to zombie list */
2295 list_add(&ni->ni_netlist, zombie_list);
2299 if (!list_empty(&ni->ni_netlist)) {
2300 /* Unlock mutex while waiting to allow other
2301 * threads to read the LNet state and fall through
2304 lnet_net_unlock(LNET_LOCK_EX);
2305 mutex_unlock(&the_lnet.ln_api_mutex);
2308 if ((i & (-i)) == i) {
2310 "Waiting for zombie LNI %s\n",
2311 libcfs_nidstr(&ni->ni_nid));
2313 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2315 mutex_lock(&the_lnet.ln_api_mutex);
2316 lnet_net_lock(LNET_LOCK_EX);
2320 lnet_net_unlock(LNET_LOCK_EX);
2322 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2324 LASSERT(!in_interrupt());
2325 /* Holding the LND mutex makes it safe for lnd_shutdown
2326 * to call module_put(). Module unload cannot finish
2327 * until lnet_unregister_lnd() completes, and that
2328 * requires the LND mutex.
2330 mutex_unlock(&the_lnet.ln_api_mutex);
2331 mutex_lock(&the_lnet.ln_lnd_mutex);
2332 (net->net_lnd->lnd_shutdown)(ni);
2333 mutex_unlock(&the_lnet.ln_lnd_mutex);
2334 mutex_lock(&the_lnet.ln_api_mutex);
2337 CDEBUG(D_LNI, "Removed LNI %s\n",
2338 libcfs_nidstr(&ni->ni_nid));
2342 lnet_net_lock(LNET_LOCK_EX);
2346 /* shutdown down the NI and release refcount */
2348 lnet_shutdown_lndni(struct lnet_ni *ni)
2351 struct lnet_net *net = ni->ni_net;
2353 lnet_net_lock(LNET_LOCK_EX);
2355 ni->ni_state = LNET_NI_STATE_DELETING;
2357 lnet_ni_unlink_locked(ni);
2358 lnet_incr_dlc_seq();
2359 lnet_net_unlock(LNET_LOCK_EX);
2361 /* clear messages for this NI on the lazy portal */
2362 for (i = 0; i < the_lnet.ln_nportals; i++)
2363 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2365 lnet_net_lock(LNET_LOCK_EX);
2366 lnet_clear_zombies_nis_locked(net);
2367 lnet_net_unlock(LNET_LOCK_EX);
2371 lnet_shutdown_lndnet(struct lnet_net *net)
2375 lnet_net_lock(LNET_LOCK_EX);
2377 list_del_init(&net->net_list);
2379 while (!list_empty(&net->net_ni_list)) {
2380 ni = list_entry(net->net_ni_list.next,
2381 struct lnet_ni, ni_netlist);
2382 lnet_net_unlock(LNET_LOCK_EX);
2383 lnet_shutdown_lndni(ni);
2384 lnet_net_lock(LNET_LOCK_EX);
2387 lnet_net_unlock(LNET_LOCK_EX);
2389 /* Do peer table cleanup for this net */
2390 lnet_peer_tables_cleanup(net);
2396 lnet_shutdown_lndnets(void)
2398 struct lnet_net *net;
2400 struct lnet_msg *msg, *tmp;
2402 /* NB called holding the global mutex */
2404 /* All quiet on the API front */
2405 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING ||
2406 the_lnet.ln_state == LNET_STATE_STOPPING);
2407 LASSERT(the_lnet.ln_refcount == 0);
2409 lnet_net_lock(LNET_LOCK_EX);
2410 the_lnet.ln_state = LNET_STATE_STOPPING;
2413 * move the nets to the zombie list to avoid them being
2414 * picked up for new work. LONET is also included in the
2415 * Nets that will be moved to the zombie list
2417 list_splice_init(&the_lnet.ln_nets, &the_lnet.ln_net_zombie);
2419 /* Drop the cached loopback Net. */
2420 if (the_lnet.ln_loni != NULL) {
2421 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2422 the_lnet.ln_loni = NULL;
2424 lnet_net_unlock(LNET_LOCK_EX);
2426 /* iterate through the net zombie list and delete each net */
2427 while (!list_empty(&the_lnet.ln_net_zombie)) {
2428 net = list_entry(the_lnet.ln_net_zombie.next,
2429 struct lnet_net, net_list);
2430 lnet_shutdown_lndnet(net);
2433 spin_lock(&the_lnet.ln_msg_resend_lock);
2434 list_splice(&the_lnet.ln_msg_resend, &resend);
2435 spin_unlock(&the_lnet.ln_msg_resend_lock);
2437 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2438 list_del_init(&msg->msg_list);
2439 msg->msg_no_resend = true;
2440 lnet_finalize(msg, -ECANCELED);
2443 lnet_net_lock(LNET_LOCK_EX);
2444 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2445 lnet_net_unlock(LNET_LOCK_EX);
2449 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2452 struct lnet_tx_queue *tq;
2454 struct lnet_net *net = ni->ni_net;
2456 mutex_lock(&the_lnet.ln_lnd_mutex);
2459 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2460 ni->ni_lnd_tunables_set = true;
2463 rc = (net->net_lnd->lnd_startup)(ni);
2465 mutex_unlock(&the_lnet.ln_lnd_mutex);
2468 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2469 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2474 ni->ni_state = LNET_NI_STATE_ACTIVE;
2477 /* We keep a reference on the loopback net through the loopback NI */
2478 if (net->net_lnd->lnd_type == LOLND) {
2480 LASSERT(the_lnet.ln_loni == NULL);
2481 the_lnet.ln_loni = ni;
2482 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2483 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2484 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2485 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2489 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2490 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2491 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2492 libcfs_lnd2str(net->net_lnd->lnd_type),
2493 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2495 /* shutdown the NI since if we get here then it must've already
2498 lnet_shutdown_lndni(ni);
2502 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2503 tq->tq_credits_min =
2504 tq->tq_credits_max =
2505 tq->tq_credits = lnet_ni_tq_credits(ni);
2508 atomic_set(&ni->ni_tx_credits,
2509 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2510 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2512 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2513 libcfs_nidstr(&ni->ni_nid),
2514 ni->ni_net->net_tunables.lct_peer_tx_credits,
2515 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2516 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2517 ni->ni_net->net_tunables.lct_peer_timeout);
2526 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2529 struct lnet_net *net_l = NULL;
2530 LIST_HEAD(local_ni_list);
2534 const struct lnet_lnd *lnd;
2536 net->net_tunables.lct_peer_timeout;
2538 net->net_tunables.lct_max_tx_credits;
2539 int peerrtrcredits =
2540 net->net_tunables.lct_peer_rtr_credits;
2543 * make sure that this net is unique. If it isn't then
2544 * we are adding interfaces to an already existing network, and
2545 * 'net' is just a convenient way to pass in the list.
2546 * if it is unique we need to find the LND and load it if
2549 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2550 lnd_type = LNET_NETTYP(net->net_id);
2552 mutex_lock(&the_lnet.ln_lnd_mutex);
2553 lnd = lnet_find_lnd_by_type(lnd_type);
2556 mutex_unlock(&the_lnet.ln_lnd_mutex);
2557 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2558 mutex_lock(&the_lnet.ln_lnd_mutex);
2560 lnd = lnet_find_lnd_by_type(lnd_type);
2562 mutex_unlock(&the_lnet.ln_lnd_mutex);
2563 CERROR("Can't load LND %s, module %s, rc=%d\n",
2564 libcfs_lnd2str(lnd_type),
2565 libcfs_lnd2modname(lnd_type), rc);
2566 #ifndef HAVE_MODULE_LOADING_SUPPORT
2567 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2568 "compiled with kernel module "
2569 "loading support.");
2578 mutex_unlock(&the_lnet.ln_lnd_mutex);
2584 * net_l: if the network being added is unique then net_l
2585 * will point to that network
2586 * if the network being added is not unique then
2587 * net_l points to the existing network.
2589 * When we enter the loop below, we'll pick NIs off he
2590 * network beign added and start them up, then add them to
2591 * a local ni list. Once we've successfully started all
2592 * the NIs then we join the local NI list (of started up
2593 * networks) with the net_l->net_ni_list, which should
2594 * point to the correct network to add the new ni list to
2596 * If any of the new NIs fail to start up, then we want to
2597 * iterate through the local ni list, which should include
2598 * any NIs which were successfully started up, and shut
2601 * After than we want to delete the network being added,
2602 * to avoid a memory leak.
2604 while (!list_empty(&net->net_ni_added)) {
2605 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2607 list_del_init(&ni->ni_netlist);
2609 /* make sure that the the NI we're about to start
2610 * up is actually unique. if it's not fail. */
2611 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2612 ni->ni_interface)) {
2617 /* adjust the pointer the parent network, just in case it
2618 * the net is a duplicate */
2621 rc = lnet_startup_lndni(ni, tun);
2627 list_add_tail(&ni->ni_netlist, &local_ni_list);
2632 lnet_net_lock(LNET_LOCK_EX);
2633 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2634 lnet_incr_dlc_seq();
2635 lnet_net_unlock(LNET_LOCK_EX);
2637 /* if the network is not unique then we don't want to keep
2638 * it around after we're done. Free it. Otherwise add that
2639 * net to the global the_lnet.ln_nets */
2640 if (net_l != net && net_l != NULL) {
2642 * TODO - note. currently the tunables can not be updated
2648 * restore tunables after it has been overwitten by the
2651 if (peer_timeout != -1)
2652 net->net_tunables.lct_peer_timeout = peer_timeout;
2653 if (maxtxcredits != -1)
2654 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2655 if (peerrtrcredits != -1)
2656 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2658 lnet_net_lock(LNET_LOCK_EX);
2659 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2660 lnet_net_unlock(LNET_LOCK_EX);
2667 * shutdown the new NIs that are being started up
2668 * free the NET being started
2670 while (!list_empty(&local_ni_list)) {
2671 ni = list_entry(local_ni_list.next, struct lnet_ni,
2674 lnet_shutdown_lndni(ni);
2684 lnet_startup_lndnets(struct list_head *netlist)
2686 struct lnet_net *net;
2691 * Change to running state before bringing up the LNDs. This
2692 * allows lnet_shutdown_lndnets() to assert that we've passed
2695 lnet_net_lock(LNET_LOCK_EX);
2696 the_lnet.ln_state = LNET_STATE_RUNNING;
2697 lnet_net_unlock(LNET_LOCK_EX);
2699 while (!list_empty(netlist)) {
2700 net = list_entry(netlist->next, struct lnet_net, net_list);
2701 list_del_init(&net->net_list);
2703 rc = lnet_startup_lndnet(net, NULL);
2713 lnet_shutdown_lndnets();
2718 static int lnet_genl_parse_list(struct sk_buff *msg,
2719 const struct ln_key_list *data[], u16 idx)
2721 const struct ln_key_list *list = data[idx];
2722 const struct ln_key_props *props;
2723 struct nlattr *node;
2729 if (!list->lkl_maxattr)
2732 props = list->lkl_list;
2736 node = nla_nest_start(msg, LN_SCALAR_ATTR_LIST);
2740 for (count = 1; count <= list->lkl_maxattr; count++) {
2741 struct nlattr *key = nla_nest_start(msg, count);
2744 nla_put_u16(msg, LN_SCALAR_ATTR_LIST_SIZE,
2747 nla_put_u16(msg, LN_SCALAR_ATTR_INDEX, count);
2748 if (props[count].lkp_value)
2749 nla_put_string(msg, LN_SCALAR_ATTR_VALUE,
2750 props[count].lkp_value);
2751 if (props[count].lkp_key_format)
2752 nla_put_u16(msg, LN_SCALAR_ATTR_KEY_FORMAT,
2753 props[count].lkp_key_format);
2754 nla_put_u16(msg, LN_SCALAR_ATTR_NLA_TYPE,
2755 props[count].lkp_data_type);
2756 if (props[count].lkp_data_type == NLA_NESTED) {
2759 rc = lnet_genl_parse_list(msg, data, ++idx);
2765 nla_nest_end(msg, key);
2768 nla_nest_end(msg, node);
2772 int lnet_genl_send_scalar_list(struct sk_buff *msg, u32 portid, u32 seq,
2773 const struct genl_family *family, int flags,
2774 u8 cmd, const struct ln_key_list *data[])
2782 hdr = genlmsg_put(msg, portid, seq, family, flags, cmd);
2784 GOTO(canceled, rc = -EMSGSIZE);
2786 rc = lnet_genl_parse_list(msg, data, 0);
2790 genlmsg_end(msg, hdr);
2793 genlmsg_cancel(msg, hdr);
2794 return rc > 0 ? 0 : rc;
2796 EXPORT_SYMBOL(lnet_genl_send_scalar_list);
2799 * Initialize LNet library.
2801 * Automatically called at module loading time. Caller has to call
2802 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2803 * latter returned 0. It must be called exactly once.
2805 * \retval 0 on success
2806 * \retval -ve on failures.
2808 int lnet_lib_init(void)
2812 lnet_assert_wire_constants();
2814 /* refer to global cfs_cpt_table for now */
2815 the_lnet.ln_cpt_table = cfs_cpt_tab;
2816 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_tab);
2818 LASSERT(the_lnet.ln_cpt_number > 0);
2819 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2820 /* we are under risk of consuming all lh_cookie */
2821 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2822 "please change setting of CPT-table and retry\n",
2823 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2827 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2828 the_lnet.ln_cpt_bits++;
2830 rc = lnet_create_locks();
2832 CERROR("Can't create LNet global locks: %d\n", rc);
2836 the_lnet.ln_refcount = 0;
2837 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2838 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2840 /* The hash table size is the number of bits it takes to express the set
2841 * ln_num_routes, minus 1 (better to under estimate than over so we
2842 * don't waste memory). */
2843 if (rnet_htable_size <= 0)
2844 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2845 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2846 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2847 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2848 order_base_2(rnet_htable_size) - 1);
2850 /* All LNDs apart from the LOLND are in separate modules. They
2851 * register themselves when their module loads, and unregister
2852 * themselves when their module is unloaded. */
2853 lnet_register_lnd(&the_lolnd);
2858 * Finalize LNet library.
2860 * \pre lnet_lib_init() called with success.
2861 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2863 * As this happens at module-unload, all lnds must already be unloaded,
2864 * so they must already be unregistered.
2866 void lnet_lib_exit(void)
2870 LASSERT(the_lnet.ln_refcount == 0);
2871 lnet_unregister_lnd(&the_lolnd);
2872 for (i = 0; i < NUM_LNDS; i++)
2873 LASSERT(!the_lnet.ln_lnds[i]);
2874 lnet_destroy_locks();
2878 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2880 * Users must call this function at least once before any other functions.
2881 * For each successful call there must be a corresponding call to
2882 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2885 * The PID used by LNet may be different from the one requested.
2888 * \param requested_pid PID requested by the caller.
2890 * \return >= 0 on success, and < 0 error code on failures.
2893 LNetNIInit(lnet_pid_t requested_pid)
2895 int im_a_router = 0;
2898 struct lnet_ping_buffer *pbuf;
2899 struct lnet_handle_md ping_mdh;
2900 LIST_HEAD(net_head);
2901 struct lnet_net *net;
2903 mutex_lock(&the_lnet.ln_api_mutex);
2905 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2907 if (the_lnet.ln_state == LNET_STATE_STOPPING) {
2908 mutex_unlock(&the_lnet.ln_api_mutex);
2912 if (the_lnet.ln_refcount > 0) {
2913 rc = the_lnet.ln_refcount++;
2914 mutex_unlock(&the_lnet.ln_api_mutex);
2918 rc = lnet_prepare(requested_pid);
2920 mutex_unlock(&the_lnet.ln_api_mutex);
2924 /* create a network for Loopback network */
2925 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2928 goto err_empty_list;
2931 /* Add in the loopback NI */
2932 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2934 goto err_empty_list;
2937 if (use_tcp_bonding)
2938 CWARN("use_tcp_bonding has been removed. Use Multi-Rail and Dynamic Discovery instead, see LU-13641\n");
2940 /* If LNet is being initialized via DLC it is possible
2941 * that the user requests not to load module parameters (ones which
2942 * are supported by DLC) on initialization. Therefore, make sure not
2943 * to load networks, routes and forwarding from module parameters
2944 * in this case. On cleanup in case of failure only clean up
2945 * routes if it has been loaded */
2946 if (!the_lnet.ln_nis_from_mod_params) {
2947 rc = lnet_parse_networks(&net_head, lnet_get_networks());
2949 goto err_empty_list;
2952 ni_count = lnet_startup_lndnets(&net_head);
2955 goto err_empty_list;
2958 if (!the_lnet.ln_nis_from_mod_params) {
2959 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2961 goto err_shutdown_lndnis;
2963 rc = lnet_rtrpools_alloc(im_a_router);
2965 goto err_destroy_routes;
2968 rc = lnet_acceptor_start();
2970 goto err_destroy_routes;
2972 the_lnet.ln_refcount = 1;
2973 /* Now I may use my own API functions... */
2975 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2977 goto err_acceptor_stop;
2979 lnet_ping_target_update(pbuf, ping_mdh);
2981 the_lnet.ln_mt_handler = lnet_mt_event_handler;
2983 rc = lnet_push_target_init();
2987 rc = lnet_peer_discovery_start();
2989 goto err_destroy_push_target;
2991 rc = lnet_monitor_thr_start();
2993 goto err_stop_discovery_thr;
2996 lnet_router_debugfs_init();
2998 mutex_unlock(&the_lnet.ln_api_mutex);
3000 complete_all(&the_lnet.ln_started);
3002 /* wait for all routers to start */
3003 lnet_wait_router_start();
3007 err_stop_discovery_thr:
3008 lnet_peer_discovery_stop();
3009 err_destroy_push_target:
3010 lnet_push_target_fini();
3012 lnet_ping_target_fini();
3014 the_lnet.ln_refcount = 0;
3015 lnet_acceptor_stop();
3017 if (!the_lnet.ln_nis_from_mod_params)
3018 lnet_destroy_routes();
3019 err_shutdown_lndnis:
3020 lnet_shutdown_lndnets();
3024 mutex_unlock(&the_lnet.ln_api_mutex);
3025 while (!list_empty(&net_head)) {
3026 struct lnet_net *net;
3028 net = list_entry(net_head.next, struct lnet_net, net_list);
3029 list_del_init(&net->net_list);
3034 EXPORT_SYMBOL(LNetNIInit);
3037 * Stop LNet interfaces, routing, and forwarding.
3039 * Users must call this function once for each successful call to LNetNIInit().
3040 * Once the LNetNIFini() operation has been started, the results of pending
3041 * API operations are undefined.
3043 * \return always 0 for current implementation.
3048 mutex_lock(&the_lnet.ln_api_mutex);
3050 LASSERT(the_lnet.ln_refcount > 0);
3052 if (the_lnet.ln_refcount != 1) {
3053 the_lnet.ln_refcount--;
3055 LASSERT(!the_lnet.ln_niinit_self);
3057 lnet_net_lock(LNET_LOCK_EX);
3058 the_lnet.ln_state = LNET_STATE_STOPPING;
3059 lnet_net_unlock(LNET_LOCK_EX);
3063 lnet_router_debugfs_fini();
3064 lnet_monitor_thr_stop();
3065 lnet_peer_discovery_stop();
3066 lnet_push_target_fini();
3067 lnet_ping_target_fini();
3069 /* Teardown fns that use my own API functions BEFORE here */
3070 the_lnet.ln_refcount = 0;
3072 lnet_acceptor_stop();
3073 lnet_destroy_routes();
3074 lnet_shutdown_lndnets();
3078 mutex_unlock(&the_lnet.ln_api_mutex);
3081 EXPORT_SYMBOL(LNetNIFini);
3084 * Grabs the ni data from the ni structure and fills the out
3087 * \param[in] ni network interface structure
3088 * \param[out] cfg_ni NI config information
3089 * \param[out] tun network and LND tunables
3092 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
3093 struct lnet_ioctl_config_lnd_tunables *tun,
3094 struct lnet_ioctl_element_stats *stats,
3097 size_t min_size = 0;
3100 if (!ni || !cfg_ni || !tun || !nid_is_nid4(&ni->ni_nid))
3103 if (ni->ni_interface != NULL) {
3104 strncpy(cfg_ni->lic_ni_intf,
3106 sizeof(cfg_ni->lic_ni_intf));
3109 cfg_ni->lic_nid = lnet_nid_to_nid4(&ni->ni_nid);
3110 cfg_ni->lic_status = lnet_ni_get_status_locked(ni);
3111 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
3113 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
3116 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
3117 LNET_STATS_TYPE_SEND);
3118 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
3119 LNET_STATS_TYPE_RECV);
3120 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
3121 LNET_STATS_TYPE_DROP);
3125 * tun->lt_tun will always be present, but in order to be
3126 * backwards compatible, we need to deal with the cases when
3127 * tun->lt_tun is smaller than what the kernel has, because it
3128 * comes from an older version of a userspace program, then we'll
3129 * need to copy as much information as we have available space.
3131 min_size = tun_size - sizeof(tun->lt_cmn);
3132 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
3134 /* copy over the cpts */
3135 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
3136 ni->ni_cpts == NULL) {
3137 for (i = 0; i < ni->ni_ncpts; i++)
3138 cfg_ni->lic_cpts[i] = i;
3141 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
3142 i < LNET_MAX_SHOW_NUM_CPT;
3144 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
3146 cfg_ni->lic_ncpts = ni->ni_ncpts;
3150 * NOTE: This is a legacy function left in the code to be backwards
3151 * compatible with older userspace programs. It should eventually be
3154 * Grabs the ni data from the ni structure and fills the out
3157 * \param[in] ni network interface structure
3158 * \param[out] config config information
3161 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
3162 struct lnet_ioctl_config_data *config)
3164 struct lnet_ioctl_net_config *net_config;
3165 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
3166 size_t min_size, tunable_size = 0;
3169 if (!ni || !config || !nid_is_nid4(&ni->ni_nid))
3172 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
3176 if (!ni->ni_interface)
3179 strncpy(net_config->ni_interface,
3181 sizeof(net_config->ni_interface));
3183 config->cfg_nid = lnet_nid_to_nid4(&ni->ni_nid);
3184 config->cfg_config_u.cfg_net.net_peer_timeout =
3185 ni->ni_net->net_tunables.lct_peer_timeout;
3186 config->cfg_config_u.cfg_net.net_max_tx_credits =
3187 ni->ni_net->net_tunables.lct_max_tx_credits;
3188 config->cfg_config_u.cfg_net.net_peer_tx_credits =
3189 ni->ni_net->net_tunables.lct_peer_tx_credits;
3190 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
3191 ni->ni_net->net_tunables.lct_peer_rtr_credits;
3193 net_config->ni_status = lnet_ni_get_status_locked(ni);
3196 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
3198 for (i = 0; i < num_cpts; i++)
3199 net_config->ni_cpts[i] = ni->ni_cpts[i];
3201 config->cfg_ncpts = num_cpts;
3205 * See if user land tools sent in a newer and larger version
3206 * of struct lnet_tunables than what the kernel uses.
3208 min_size = sizeof(*config) + sizeof(*net_config);
3210 if (config->cfg_hdr.ioc_len > min_size)
3211 tunable_size = config->cfg_hdr.ioc_len - min_size;
3213 /* Don't copy too much data to user space */
3214 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
3215 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
3217 if (lnd_cfg && min_size) {
3218 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
3219 config->cfg_config_u.cfg_net.net_interface_count = 1;
3221 /* Tell user land that kernel side has less data */
3222 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
3223 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
3224 config->cfg_hdr.ioc_len -= min_size;
3230 lnet_get_ni_idx_locked(int idx)
3233 struct lnet_net *net;
3235 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3236 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3245 int lnet_get_net_healthv_locked(struct lnet_net *net)
3248 int best_healthv = 0;
3249 int healthv, ni_fatal;
3251 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3252 healthv = atomic_read(&ni->ni_healthv);
3253 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
3254 if (!ni_fatal && healthv > best_healthv)
3255 best_healthv = healthv;
3258 return best_healthv;
3262 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
3265 struct lnet_net *net = mynet;
3268 * It is possible that the net has been cleaned out while there is
3269 * a message being sent. This function accessed the net without
3270 * checking if the list is empty
3274 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
3276 if (list_empty(&net->net_ni_list))
3278 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3284 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
3285 /* if you reached the end of the ni list and the net is
3286 * specified, then there are no more nis in that net */
3290 /* we reached the end of this net ni list. move to the
3292 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
3293 /* no more nets and no more NIs. */
3296 /* get the next net */
3297 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
3299 if (list_empty(&net->net_ni_list))
3301 /* get the ni on it */
3302 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3308 if (list_empty(&prev->ni_netlist))
3311 /* there are more nis left */
3312 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3318 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3323 int idx = config->cfg_count;
3325 cpt = lnet_net_lock_current();
3327 ni = lnet_get_ni_idx_locked(idx);
3332 lnet_fill_ni_info_legacy(ni, config);
3336 lnet_net_unlock(cpt);
3341 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3342 struct lnet_ioctl_config_lnd_tunables *tun,
3343 struct lnet_ioctl_element_stats *stats,
3350 if (!cfg_ni || !tun || !stats)
3353 cpt = lnet_net_lock_current();
3355 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3360 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3364 lnet_net_unlock(cpt);
3368 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3377 cpt = lnet_net_lock_current();
3379 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3382 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3386 lnet_net_unlock(cpt);
3391 static int lnet_add_net_common(struct lnet_net *net,
3392 struct lnet_ioctl_config_lnd_tunables *tun)
3394 struct lnet_handle_md ping_mdh;
3395 struct lnet_ping_buffer *pbuf;
3396 struct lnet_remotenet *rnet;
3402 lnet_net_lock(LNET_LOCK_EX);
3403 rnet = lnet_find_rnet_locked(net->net_id);
3404 lnet_net_unlock(LNET_LOCK_EX);
3406 * make sure that the net added doesn't invalidate the current
3407 * configuration LNet is keeping
3410 CERROR("Adding net %s will invalidate routing configuration\n",
3411 libcfs_net2str(net->net_id));
3417 * make sure you calculate the correct number of slots in the ping
3418 * buffer. Since the ping info is a flattened list of all the NIs,
3419 * we should allocate enough slots to accomodate the number of NIs
3420 * which will be added.
3422 * since ni hasn't been configured yet, use
3423 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3425 net_ni_count = lnet_get_net_ni_count_pre(net);
3427 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3428 net_ni_count + lnet_get_ni_count(),
3436 memcpy(&net->net_tunables,
3437 &tun->lt_cmn, sizeof(net->net_tunables));
3439 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3441 net_id = net->net_id;
3443 rc = lnet_startup_lndnet(net,
3444 (tun) ? &tun->lt_tun : NULL);
3448 lnet_net_lock(LNET_LOCK_EX);
3449 net = lnet_get_net_locked(net_id);
3452 /* apply the UDSPs */
3453 rc = lnet_udsp_apply_policies_on_net(net);
3455 CERROR("Failed to apply UDSPs on local net %s\n",
3456 libcfs_net2str(net->net_id));
3458 /* At this point we lost track of which NI was just added, so we
3459 * just re-apply the policies on all of the NIs on this net
3461 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3462 rc = lnet_udsp_apply_policies_on_ni(ni);
3464 CERROR("Failed to apply UDSPs on ni %s\n",
3465 libcfs_nidstr(&ni->ni_nid));
3467 lnet_net_unlock(LNET_LOCK_EX);
3470 * Start the acceptor thread if this is the first network
3471 * being added that requires the thread.
3473 if (net->net_lnd->lnd_accept) {
3474 rc = lnet_acceptor_start();
3476 /* shutdown the net that we just started */
3477 CERROR("Failed to start up acceptor thread\n");
3478 lnet_shutdown_lndnet(net);
3483 lnet_net_lock(LNET_LOCK_EX);
3484 lnet_peer_net_added(net);
3485 lnet_net_unlock(LNET_LOCK_EX);
3487 lnet_ping_target_update(pbuf, ping_mdh);
3492 lnet_ping_md_unlink(pbuf, &ping_mdh);
3493 lnet_ping_buffer_decref(pbuf);
3498 lnet_set_tune_defaults(struct lnet_ioctl_config_lnd_tunables *tun)
3501 if (!tun->lt_cmn.lct_peer_timeout)
3502 tun->lt_cmn.lct_peer_timeout = DEFAULT_PEER_TIMEOUT;
3503 if (!tun->lt_cmn.lct_peer_tx_credits)
3504 tun->lt_cmn.lct_peer_tx_credits = DEFAULT_PEER_CREDITS;
3505 if (!tun->lt_cmn.lct_max_tx_credits)
3506 tun->lt_cmn.lct_max_tx_credits = DEFAULT_CREDITS;
3510 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3511 struct lnet_ioctl_config_lnd_tunables *tun)
3513 struct lnet_net *net;
3516 LIST_HEAD(net_head);
3518 rc = lnet_parse_ip2nets(&nets, ip2nets);
3522 rc = lnet_parse_networks(&net_head, nets);
3526 lnet_set_tune_defaults(tun);
3528 mutex_lock(&the_lnet.ln_api_mutex);
3529 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3533 while (!list_empty(&net_head)) {
3534 net = list_entry(net_head.next, struct lnet_net, net_list);
3535 list_del_init(&net->net_list);
3536 rc = lnet_add_net_common(net, tun);
3542 mutex_unlock(&the_lnet.ln_api_mutex);
3544 while (!list_empty(&net_head)) {
3545 net = list_entry(net_head.next, struct lnet_net, net_list);
3546 list_del_init(&net->net_list);
3552 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3554 struct lnet_net *net;
3556 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3558 __u32 net_id, lnd_type;
3560 /* get the tunables if they are available */
3561 if (conf->lic_cfg_hdr.ioc_len >=
3562 sizeof(*conf) + sizeof(*tun))
3563 tun = (struct lnet_ioctl_config_lnd_tunables *)
3566 /* handle legacy ip2nets from DLC */
3567 if (conf->lic_legacy_ip2nets[0] != '\0')
3568 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3571 net_id = LNET_NIDNET(conf->lic_nid);
3572 lnd_type = LNET_NETTYP(net_id);
3574 if (!libcfs_isknown_lnd(lnd_type)) {
3575 CERROR("No valid net and lnd information provided\n");
3579 net = lnet_net_alloc(net_id, NULL);
3583 for (i = 0; i < conf->lic_ncpts; i++) {
3584 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3588 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3593 lnet_set_tune_defaults(tun);
3595 mutex_lock(&the_lnet.ln_api_mutex);
3596 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3599 rc = lnet_add_net_common(net, tun);
3601 mutex_unlock(&the_lnet.ln_api_mutex);
3606 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3608 struct lnet_net *net;
3610 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3611 struct lnet_ping_buffer *pbuf;
3612 struct lnet_handle_md ping_mdh;
3617 /* don't allow userspace to shutdown the LOLND */
3618 if (LNET_NETTYP(net_id) == LOLND)
3621 mutex_lock(&the_lnet.ln_api_mutex);
3622 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3624 goto unlock_api_mutex;
3629 net = lnet_get_net_locked(net_id);
3631 CERROR("net %s not found\n",
3632 libcfs_net2str(net_id));
3637 addr = LNET_NIDADDR(conf->lic_nid);
3639 /* remove the entire net */
3640 net_count = lnet_get_net_ni_count_locked(net);
3644 /* create and link a new ping info, before removing the old one */
3645 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3646 lnet_get_ni_count() - net_count,
3649 goto unlock_api_mutex;
3651 lnet_shutdown_lndnet(net);
3653 lnet_acceptor_stop();
3655 lnet_ping_target_update(pbuf, ping_mdh);
3657 goto unlock_api_mutex;
3660 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3662 CERROR("nid %s not found\n",
3663 libcfs_nid2str(conf->lic_nid));
3668 net_count = lnet_get_net_ni_count_locked(net);
3672 /* create and link a new ping info, before removing the old one */
3673 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3674 lnet_get_ni_count() - 1, false);
3676 goto unlock_api_mutex;
3678 lnet_shutdown_lndni(ni);
3680 lnet_acceptor_stop();
3682 lnet_ping_target_update(pbuf, ping_mdh);
3684 /* check if the net is empty and remove it if it is */
3686 lnet_shutdown_lndnet(net);
3688 goto unlock_api_mutex;
3693 mutex_unlock(&the_lnet.ln_api_mutex);
3699 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3700 * They are only expected to be called for unique networks.
3701 * That can be as a result of older DLC library
3702 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3705 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3707 struct lnet_net *net;
3708 LIST_HEAD(net_head);
3710 struct lnet_ioctl_config_lnd_tunables tun;
3711 const char *nets = conf->cfg_config_u.cfg_net.net_intf;
3713 /* Create a net/ni structures for the network string */
3714 rc = lnet_parse_networks(&net_head, nets);
3716 return rc == 0 ? -EINVAL : rc;
3718 mutex_lock(&the_lnet.ln_api_mutex);
3719 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3721 goto out_unlock_clean;
3725 rc = -EINVAL; /* only add one network per call */
3726 goto out_unlock_clean;
3729 net = list_entry(net_head.next, struct lnet_net, net_list);
3730 list_del_init(&net->net_list);
3732 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3734 memset(&tun, 0, sizeof(tun));
3736 tun.lt_cmn.lct_peer_timeout =
3737 (!conf->cfg_config_u.cfg_net.net_peer_timeout) ? DEFAULT_PEER_TIMEOUT :
3738 conf->cfg_config_u.cfg_net.net_peer_timeout;
3739 tun.lt_cmn.lct_peer_tx_credits =
3740 (!conf->cfg_config_u.cfg_net.net_peer_tx_credits) ? DEFAULT_PEER_CREDITS :
3741 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3742 tun.lt_cmn.lct_peer_rtr_credits =
3743 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3744 tun.lt_cmn.lct_max_tx_credits =
3745 (!conf->cfg_config_u.cfg_net.net_max_tx_credits) ? DEFAULT_CREDITS :
3746 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3748 rc = lnet_add_net_common(net, &tun);
3751 mutex_unlock(&the_lnet.ln_api_mutex);
3752 while (!list_empty(&net_head)) {
3753 /* net_head list is empty in success case */
3754 net = list_entry(net_head.next, struct lnet_net, net_list);
3755 list_del_init(&net->net_list);
3762 lnet_dyn_del_net(__u32 net_id)
3764 struct lnet_net *net;
3765 struct lnet_ping_buffer *pbuf;
3766 struct lnet_handle_md ping_mdh;
3770 /* don't allow userspace to shutdown the LOLND */
3771 if (LNET_NETTYP(net_id) == LOLND)
3774 mutex_lock(&the_lnet.ln_api_mutex);
3775 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3782 net = lnet_get_net_locked(net_id);
3789 net_ni_count = lnet_get_net_ni_count_locked(net);
3793 /* create and link a new ping info, before removing the old one */
3794 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3795 lnet_get_ni_count() - net_ni_count, false);
3799 lnet_shutdown_lndnet(net);
3801 lnet_acceptor_stop();
3803 lnet_ping_target_update(pbuf, ping_mdh);
3806 mutex_unlock(&the_lnet.ln_api_mutex);
3811 void lnet_incr_dlc_seq(void)
3813 atomic_inc(&lnet_dlc_seq_no);
3816 __u32 lnet_get_dlc_seq_locked(void)
3818 return atomic_read(&lnet_dlc_seq_no);
3822 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3824 struct lnet_net *net;
3827 lnet_net_lock(LNET_LOCK_EX);
3828 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3829 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3830 if (all || (nid_is_nid4(&ni->ni_nid) &&
3831 lnet_nid_to_nid4(&ni->ni_nid) == nid)) {
3832 atomic_set(&ni->ni_healthv, value);
3833 if (list_empty(&ni->ni_recovery) &&
3834 value < LNET_MAX_HEALTH_VALUE) {
3835 CERROR("manually adding local NI %s to recovery\n",
3836 libcfs_nidstr(&ni->ni_nid));
3837 list_add_tail(&ni->ni_recovery,
3838 &the_lnet.ln_mt_localNIRecovq);
3839 lnet_ni_addref_locked(ni, 0);
3842 lnet_net_unlock(LNET_LOCK_EX);
3848 lnet_net_unlock(LNET_LOCK_EX);
3852 lnet_ni_set_conns_per_peer(lnet_nid_t nid, int value, bool all)
3854 struct lnet_net *net;
3857 lnet_net_lock(LNET_LOCK_EX);
3858 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3859 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3860 if (lnet_nid_to_nid4(&ni->ni_nid) != nid && !all)
3862 if (LNET_NETTYP(net->net_id) == SOCKLND)
3863 ni->ni_lnd_tunables.lnd_tun_u.lnd_sock.lnd_conns_per_peer = value;
3864 else if (LNET_NETTYP(net->net_id) == O2IBLND)
3865 ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib.lnd_conns_per_peer = value;
3867 lnet_net_unlock(LNET_LOCK_EX);
3872 lnet_net_unlock(LNET_LOCK_EX);
3876 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3880 lnet_nid_t nid = stats->hlni_nid;
3882 cpt = lnet_net_lock_current();
3883 ni = lnet_nid2ni_locked(nid, cpt);
3890 stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3891 stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3892 stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3893 stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3894 stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3895 stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3896 stats->hlni_fatal_error = atomic_read(&ni->ni_fatal_error_on);
3897 stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3898 stats->hlni_ping_count = ni->ni_ping_count;
3899 stats->hlni_next_ping = ni->ni_next_ping;
3902 lnet_net_unlock(cpt);
3908 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3913 lnet_net_lock(LNET_LOCK_EX);
3914 list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3915 if (!nid_is_nid4(&ni->ni_nid))
3917 list->rlst_nid_array[i] = lnet_nid_to_nid4(&ni->ni_nid);
3919 if (i >= LNET_MAX_SHOW_NUM_NID)
3922 lnet_net_unlock(LNET_LOCK_EX);
3923 list->rlst_num_nids = i;
3929 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3931 struct lnet_peer_ni *lpni;
3934 lnet_net_lock(LNET_LOCK_EX);
3935 list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3936 list->rlst_nid_array[i] = lnet_nid_to_nid4(&lpni->lpni_nid);
3938 if (i >= LNET_MAX_SHOW_NUM_NID)
3941 lnet_net_unlock(LNET_LOCK_EX);
3942 list->rlst_num_nids = i;
3948 * LNet ioctl handler.
3952 LNetCtl(unsigned int cmd, void *arg)
3954 struct libcfs_ioctl_data *data = arg;
3955 struct lnet_ioctl_config_data *config;
3956 struct lnet_process_id id4 = {};
3957 struct lnet_processid id = {};
3959 struct lnet_nid nid;
3962 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3963 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3966 case IOC_LIBCFS_GET_NI:
3967 rc = LNetGetId(data->ioc_count, &id);
3968 data->ioc_nid = lnet_nid_to_nid4(&id.nid);
3971 case IOC_LIBCFS_FAIL_NID:
3972 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3974 case IOC_LIBCFS_ADD_ROUTE: {
3975 /* default router sensitivity to 1 */
3976 unsigned int sensitivity = 1;
3979 if (config->cfg_hdr.ioc_len < sizeof(*config))
3982 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3984 config->cfg_config_u.cfg_route.rtr_sensitivity;
3987 lnet_nid4_to_nid(config->cfg_nid, &nid);
3988 mutex_lock(&the_lnet.ln_api_mutex);
3989 rc = lnet_add_route(config->cfg_net,
3990 config->cfg_config_u.cfg_route.rtr_hop,
3992 config->cfg_config_u.cfg_route.
3993 rtr_priority, sensitivity);
3994 mutex_unlock(&the_lnet.ln_api_mutex);
3998 case IOC_LIBCFS_DEL_ROUTE:
4001 if (config->cfg_hdr.ioc_len < sizeof(*config))
4004 lnet_nid4_to_nid(config->cfg_nid, &nid);
4005 mutex_lock(&the_lnet.ln_api_mutex);
4006 rc = lnet_del_route(config->cfg_net, &nid);
4007 mutex_unlock(&the_lnet.ln_api_mutex);
4010 case IOC_LIBCFS_GET_ROUTE:
4013 if (config->cfg_hdr.ioc_len < sizeof(*config))
4016 mutex_lock(&the_lnet.ln_api_mutex);
4017 rc = lnet_get_route(config->cfg_count,
4019 &config->cfg_config_u.cfg_route.rtr_hop,
4021 &config->cfg_config_u.cfg_route.rtr_flags,
4022 &config->cfg_config_u.cfg_route.
4024 &config->cfg_config_u.cfg_route.
4026 mutex_unlock(&the_lnet.ln_api_mutex);
4029 case IOC_LIBCFS_GET_LOCAL_NI: {
4030 struct lnet_ioctl_config_ni *cfg_ni;
4031 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
4032 struct lnet_ioctl_element_stats *stats;
4037 /* get the tunables if they are available */
4038 if (cfg_ni->lic_cfg_hdr.ioc_len <
4039 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
4042 stats = (struct lnet_ioctl_element_stats *)
4044 tun = (struct lnet_ioctl_config_lnd_tunables *)
4045 (cfg_ni->lic_bulk + sizeof(*stats));
4047 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
4050 mutex_lock(&the_lnet.ln_api_mutex);
4051 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
4052 mutex_unlock(&the_lnet.ln_api_mutex);
4056 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
4057 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
4059 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
4062 mutex_lock(&the_lnet.ln_api_mutex);
4063 rc = lnet_get_ni_stats(msg_stats);
4064 mutex_unlock(&the_lnet.ln_api_mutex);
4069 case IOC_LIBCFS_GET_NET: {
4070 size_t total = sizeof(*config) +
4071 sizeof(struct lnet_ioctl_net_config);
4074 if (config->cfg_hdr.ioc_len < total)
4077 mutex_lock(&the_lnet.ln_api_mutex);
4078 rc = lnet_get_net_config(config);
4079 mutex_unlock(&the_lnet.ln_api_mutex);
4083 case IOC_LIBCFS_GET_LNET_STATS:
4085 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
4087 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
4090 mutex_lock(&the_lnet.ln_api_mutex);
4091 rc = lnet_counters_get(&lnet_stats->st_cntrs);
4092 mutex_unlock(&the_lnet.ln_api_mutex);
4096 case IOC_LIBCFS_RESET_LNET_STATS:
4098 mutex_lock(&the_lnet.ln_api_mutex);
4099 lnet_counters_reset();
4100 mutex_unlock(&the_lnet.ln_api_mutex);
4104 case IOC_LIBCFS_CONFIG_RTR:
4107 if (config->cfg_hdr.ioc_len < sizeof(*config))
4110 mutex_lock(&the_lnet.ln_api_mutex);
4111 if (config->cfg_config_u.cfg_buffers.buf_enable) {
4112 rc = lnet_rtrpools_enable();
4113 mutex_unlock(&the_lnet.ln_api_mutex);
4116 lnet_rtrpools_disable();
4117 mutex_unlock(&the_lnet.ln_api_mutex);
4120 case IOC_LIBCFS_ADD_BUF:
4123 if (config->cfg_hdr.ioc_len < sizeof(*config))
4126 mutex_lock(&the_lnet.ln_api_mutex);
4127 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
4129 config->cfg_config_u.cfg_buffers.
4131 config->cfg_config_u.cfg_buffers.
4133 mutex_unlock(&the_lnet.ln_api_mutex);
4136 case IOC_LIBCFS_SET_NUMA_RANGE: {
4137 struct lnet_ioctl_set_value *numa;
4139 if (numa->sv_hdr.ioc_len != sizeof(*numa))
4141 lnet_net_lock(LNET_LOCK_EX);
4142 lnet_numa_range = numa->sv_value;
4143 lnet_net_unlock(LNET_LOCK_EX);
4147 case IOC_LIBCFS_GET_NUMA_RANGE: {
4148 struct lnet_ioctl_set_value *numa;
4150 if (numa->sv_hdr.ioc_len != sizeof(*numa))
4152 numa->sv_value = lnet_numa_range;
4156 case IOC_LIBCFS_GET_BUF: {
4157 struct lnet_ioctl_pool_cfg *pool_cfg;
4158 size_t total = sizeof(*config) + sizeof(*pool_cfg);
4162 if (config->cfg_hdr.ioc_len < total)
4165 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
4167 mutex_lock(&the_lnet.ln_api_mutex);
4168 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
4169 mutex_unlock(&the_lnet.ln_api_mutex);
4173 case IOC_LIBCFS_GET_LOCAL_HSTATS: {
4174 struct lnet_ioctl_local_ni_hstats *stats = arg;
4176 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
4179 mutex_lock(&the_lnet.ln_api_mutex);
4180 rc = lnet_get_local_ni_hstats(stats);
4181 mutex_unlock(&the_lnet.ln_api_mutex);
4186 case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
4187 struct lnet_ioctl_recovery_list *list = arg;
4188 if (list->rlst_hdr.ioc_len < sizeof(*list))
4191 mutex_lock(&the_lnet.ln_api_mutex);
4192 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
4193 rc = lnet_get_local_ni_recovery_list(list);
4195 rc = lnet_get_peer_ni_recovery_list(list);
4196 mutex_unlock(&the_lnet.ln_api_mutex);
4200 case IOC_LIBCFS_ADD_PEER_NI: {
4201 struct lnet_ioctl_peer_cfg *cfg = arg;
4203 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4206 mutex_lock(&the_lnet.ln_api_mutex);
4207 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
4209 cfg->prcfg_mr, false);
4210 mutex_unlock(&the_lnet.ln_api_mutex);
4214 case IOC_LIBCFS_DEL_PEER_NI: {
4215 struct lnet_ioctl_peer_cfg *cfg = arg;
4217 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4220 mutex_lock(&the_lnet.ln_api_mutex);
4221 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
4222 cfg->prcfg_cfg_nid);
4223 mutex_unlock(&the_lnet.ln_api_mutex);
4227 case IOC_LIBCFS_GET_PEER_INFO: {
4228 struct lnet_ioctl_peer *peer_info = arg;
4230 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
4233 mutex_lock(&the_lnet.ln_api_mutex);
4234 rc = lnet_get_peer_ni_info(
4235 peer_info->pr_count,
4237 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
4238 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
4239 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
4240 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
4241 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
4242 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
4243 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
4244 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
4245 mutex_unlock(&the_lnet.ln_api_mutex);
4249 case IOC_LIBCFS_GET_PEER_NI: {
4250 struct lnet_ioctl_peer_cfg *cfg = arg;
4252 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4255 mutex_lock(&the_lnet.ln_api_mutex);
4256 rc = lnet_get_peer_info(cfg,
4257 (void __user *)cfg->prcfg_bulk);
4258 mutex_unlock(&the_lnet.ln_api_mutex);
4262 case IOC_LIBCFS_GET_PEER_LIST: {
4263 struct lnet_ioctl_peer_cfg *cfg = arg;
4265 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4268 mutex_lock(&the_lnet.ln_api_mutex);
4269 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
4270 (struct lnet_process_id __user *)cfg->prcfg_bulk);
4271 mutex_unlock(&the_lnet.ln_api_mutex);
4275 case IOC_LIBCFS_SET_HEALHV: {
4276 struct lnet_ioctl_reset_health_cfg *cfg = arg;
4278 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
4280 if (cfg->rh_value < 0 ||
4281 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
4282 value = LNET_MAX_HEALTH_VALUE;
4284 value = cfg->rh_value;
4285 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
4286 value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
4287 "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
4288 mutex_lock(&the_lnet.ln_api_mutex);
4289 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
4290 lnet_ni_set_healthv(cfg->rh_nid, value,
4293 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
4295 mutex_unlock(&the_lnet.ln_api_mutex);
4299 case IOC_LIBCFS_SET_CONNS_PER_PEER: {
4300 struct lnet_ioctl_reset_conns_per_peer_cfg *cfg = arg;
4303 if (cfg->rcpp_hdr.ioc_len < sizeof(*cfg))
4305 if (cfg->rcpp_value < 0)
4308 value = cfg->rcpp_value;
4310 "Setting conns_per_peer to %d for %s. all = %d\n",
4311 value, libcfs_nid2str(cfg->rcpp_nid), cfg->rcpp_all);
4312 mutex_lock(&the_lnet.ln_api_mutex);
4313 lnet_ni_set_conns_per_peer(cfg->rcpp_nid, value, cfg->rcpp_all);
4314 mutex_unlock(&the_lnet.ln_api_mutex);
4318 case IOC_LIBCFS_NOTIFY_ROUTER: {
4319 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
4321 /* The deadline passed in by the user should be some time in
4322 * seconds in the future since the UNIX epoch. We have to map
4323 * that deadline to the wall clock.
4325 deadline += ktime_get_seconds();
4326 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
4330 case IOC_LIBCFS_LNET_DIST:
4331 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
4332 if (rc < 0 && rc != -EHOSTUNREACH)
4335 data->ioc_u32[0] = rc;
4338 case IOC_LIBCFS_TESTPROTOCOMPAT:
4339 the_lnet.ln_testprotocompat = data->ioc_flags;
4342 case IOC_LIBCFS_LNET_FAULT:
4343 return lnet_fault_ctl(data->ioc_flags, data);
4345 case IOC_LIBCFS_PING: {
4346 signed long timeout;
4348 id4.nid = data->ioc_nid;
4349 id4.pid = data->ioc_u32[0];
4351 /* If timeout is negative then set default of 3 minutes */
4352 if (((s32)data->ioc_u32[1] <= 0) ||
4353 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
4354 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
4356 timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
4358 rc = lnet_ping(id4, &LNET_ANY_NID, timeout, data->ioc_pbuf1,
4359 data->ioc_plen1 / sizeof(struct lnet_process_id));
4364 data->ioc_count = rc;
4368 case IOC_LIBCFS_PING_PEER: {
4369 struct lnet_ioctl_ping_data *ping = arg;
4370 struct lnet_nid src_nid = LNET_ANY_NID;
4371 struct lnet_peer *lp;
4372 signed long timeout;
4374 /* Check if the supplied ping data supports source nid
4375 * NB: This check is sufficient if lnet_ioctl_ping_data has
4376 * additional fields added, but if they are re-ordered or
4377 * fields removed then this will break. It is expected that
4378 * these ioctls will be replaced with netlink implementation, so
4379 * it is probably not worth coming up with a more robust version
4380 * compatibility scheme.
4382 if (ping->ping_hdr.ioc_len >= sizeof(struct lnet_ioctl_ping_data))
4383 lnet_nid4_to_nid(ping->ping_src, &src_nid);
4385 /* If timeout is negative then set default of 3 minutes */
4386 if (((s32)ping->op_param) <= 0 ||
4387 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
4388 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
4390 timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
4392 rc = lnet_ping(ping->ping_id, &src_nid, timeout,
4398 mutex_lock(&the_lnet.ln_api_mutex);
4399 lp = lnet_find_peer4(ping->ping_id.nid);
4402 lnet_nid_to_nid4(&lp->lp_primary_nid);
4403 ping->mr_info = lnet_peer_is_multi_rail(lp);
4404 lnet_peer_decref_locked(lp);
4406 mutex_unlock(&the_lnet.ln_api_mutex);
4408 ping->ping_count = rc;
4412 case IOC_LIBCFS_DISCOVER: {
4413 struct lnet_ioctl_ping_data *discover = arg;
4414 struct lnet_peer *lp;
4416 rc = lnet_discover(discover->ping_id, discover->op_param,
4418 discover->ping_count);
4422 mutex_lock(&the_lnet.ln_api_mutex);
4423 lp = lnet_find_peer4(discover->ping_id.nid);
4425 discover->ping_id.nid =
4426 lnet_nid_to_nid4(&lp->lp_primary_nid);
4427 discover->mr_info = lnet_peer_is_multi_rail(lp);
4428 lnet_peer_decref_locked(lp);
4430 mutex_unlock(&the_lnet.ln_api_mutex);
4432 discover->ping_count = rc;
4436 case IOC_LIBCFS_ADD_UDSP: {
4437 struct lnet_ioctl_udsp *ioc_udsp = arg;
4438 __u32 bulk_size = ioc_udsp->iou_hdr.ioc_len;
4440 mutex_lock(&the_lnet.ln_api_mutex);
4441 rc = lnet_udsp_demarshal_add(arg, bulk_size);
4443 rc = lnet_udsp_apply_policies(NULL, false);
4444 CDEBUG(D_NET, "policy application returned %d\n", rc);
4447 mutex_unlock(&the_lnet.ln_api_mutex);
4452 case IOC_LIBCFS_DEL_UDSP: {
4453 struct lnet_ioctl_udsp *ioc_udsp = arg;
4454 int idx = ioc_udsp->iou_idx;
4456 if (ioc_udsp->iou_hdr.ioc_len < sizeof(*ioc_udsp))
4459 mutex_lock(&the_lnet.ln_api_mutex);
4460 rc = lnet_udsp_del_policy(idx);
4462 rc = lnet_udsp_apply_policies(NULL, false);
4463 CDEBUG(D_NET, "policy re-application returned %d\n",
4467 mutex_unlock(&the_lnet.ln_api_mutex);
4472 case IOC_LIBCFS_GET_UDSP_SIZE: {
4473 struct lnet_ioctl_udsp *ioc_udsp = arg;
4474 struct lnet_udsp *udsp;
4476 if (ioc_udsp->iou_hdr.ioc_len < sizeof(*ioc_udsp))
4481 mutex_lock(&the_lnet.ln_api_mutex);
4482 udsp = lnet_udsp_get_policy(ioc_udsp->iou_idx);
4486 /* coming in iou_idx will hold the idx of the udsp
4487 * to get the size of. going out the iou_idx will
4488 * hold the size of the UDSP found at the passed
4491 ioc_udsp->iou_idx = lnet_get_udsp_size(udsp);
4492 if (ioc_udsp->iou_idx < 0)
4495 mutex_unlock(&the_lnet.ln_api_mutex);
4500 case IOC_LIBCFS_GET_UDSP: {
4501 struct lnet_ioctl_udsp *ioc_udsp = arg;
4502 struct lnet_udsp *udsp;
4504 if (ioc_udsp->iou_hdr.ioc_len < sizeof(*ioc_udsp))
4509 mutex_lock(&the_lnet.ln_api_mutex);
4510 udsp = lnet_udsp_get_policy(ioc_udsp->iou_idx);
4514 rc = lnet_udsp_marshal(udsp, ioc_udsp);
4515 mutex_unlock(&the_lnet.ln_api_mutex);
4520 case IOC_LIBCFS_GET_CONST_UDSP_INFO: {
4521 struct lnet_ioctl_construct_udsp_info *info = arg;
4523 if (info->cud_hdr.ioc_len < sizeof(*info))
4526 CDEBUG(D_NET, "GET_UDSP_INFO for %s\n",
4527 libcfs_nid2str(info->cud_nid));
4529 mutex_lock(&the_lnet.ln_api_mutex);
4530 lnet_udsp_get_construct_info(info);
4531 mutex_unlock(&the_lnet.ln_api_mutex);
4537 ni = lnet_net2ni_addref(data->ioc_net);
4541 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
4544 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4551 EXPORT_SYMBOL(LNetCtl);
4553 void LNetDebugPeer(struct lnet_processid *id)
4555 lnet_debug_peer(lnet_nid_to_nid4(&id->nid));
4557 EXPORT_SYMBOL(LNetDebugPeer);
4560 * Determine if the specified peer \a nid is on the local node.
4562 * \param nid peer nid to check
4564 * \retval true If peer NID is on the local node.
4565 * \retval false If peer NID is not on the local node.
4567 bool LNetIsPeerLocal(lnet_nid_t nid)
4569 struct lnet_net *net;
4573 cpt = lnet_net_lock_current();
4574 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4575 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4576 if (lnet_nid_to_nid4(&ni->ni_nid) == nid) {
4577 lnet_net_unlock(cpt);
4582 lnet_net_unlock(cpt);
4586 EXPORT_SYMBOL(LNetIsPeerLocal);
4589 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4590 * Note that all interfaces share a same PID, as requested by LNetNIInit().
4592 * \param index Index of the interface to look up.
4593 * \param id On successful return, this location will hold the
4594 * struct lnet_process_id ID of the interface.
4596 * \retval 0 If an interface exists at \a index.
4597 * \retval -ENOENT If no interface has been found.
4600 LNetGetId(unsigned int index, struct lnet_processid *id)
4603 struct lnet_net *net;
4607 LASSERT(the_lnet.ln_refcount > 0);
4609 cpt = lnet_net_lock_current();
4611 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4612 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4613 if (!nid_is_nid4(&ni->ni_nid))
4614 /* FIXME this needs to be handled */
4619 id->nid = ni->ni_nid;
4620 id->pid = the_lnet.ln_pid;
4626 lnet_net_unlock(cpt);
4629 EXPORT_SYMBOL(LNetGetId);
4634 struct lnet_handle_md mdh;
4635 struct completion completion;
4639 lnet_ping_event_handler(struct lnet_event *event)
4641 struct ping_data *pd = event->md_user_ptr;
4643 CDEBUG(D_NET, "ping event (%d %d)%s\n",
4644 event->type, event->status,
4645 event->unlinked ? " unlinked" : "");
4647 if (event->status) {
4649 pd->rc = event->status;
4650 } else if (event->type == LNET_EVENT_REPLY) {
4652 pd->rc = event->mlength;
4654 if (event->unlinked)
4655 complete(&pd->completion);
4658 static int lnet_ping(struct lnet_process_id id, struct lnet_nid *src_nid,
4659 signed long timeout, struct lnet_process_id __user *ids,
4662 struct lnet_md md = { NULL };
4663 struct ping_data pd = { 0 };
4664 struct lnet_ping_buffer *pbuf;
4665 struct lnet_process_id tmpid;
4671 /* n_ids limit is arbitrary */
4672 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4676 * if the user buffer has more space than the lnet_interfaces_max
4677 * then only fill it up to lnet_interfaces_max
4679 if (n_ids > lnet_interfaces_max)
4680 n_ids = lnet_interfaces_max;
4682 if (id.pid == LNET_PID_ANY)
4683 id.pid = LNET_PID_LUSTRE;
4685 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4689 /* initialize md content */
4690 md.start = &pbuf->pb_info;
4691 md.length = LNET_PING_INFO_SIZE(n_ids);
4692 md.threshold = 2; /* GET/REPLY */
4694 md.options = LNET_MD_TRUNCATE;
4696 md.handler = lnet_ping_event_handler;
4698 init_completion(&pd.completion);
4700 rc = LNetMDBind(&md, LNET_UNLINK, &pd.mdh);
4702 CERROR("Can't bind MD: %d\n", rc);
4703 goto fail_ping_buffer_decref;
4706 rc = LNetGet(lnet_nid_to_nid4(src_nid), pd.mdh, id,
4707 LNET_RESERVED_PORTAL,
4708 LNET_PROTO_PING_MATCHBITS, 0, false);
4711 /* Don't CERROR; this could be deliberate! */
4712 rc2 = LNetMDUnlink(pd.mdh);
4715 /* NB must wait for the UNLINK event below... */
4718 if (wait_for_completion_timeout(&pd.completion, timeout) == 0) {
4719 /* Ensure completion in finite time... */
4720 LNetMDUnlink(pd.mdh);
4721 wait_for_completion(&pd.completion);
4725 goto fail_ping_buffer_decref;
4729 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4731 rc = -EPROTO; /* if I can't parse... */
4734 CERROR("%s: ping info too short %d\n",
4735 libcfs_id2str(id), nob);
4736 goto fail_ping_buffer_decref;
4739 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4740 lnet_swap_pinginfo(pbuf);
4741 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4742 CERROR("%s: Unexpected magic %08x\n",
4743 libcfs_id2str(id), pbuf->pb_info.pi_magic);
4744 goto fail_ping_buffer_decref;
4747 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4748 CERROR("%s: ping w/o NI status: 0x%x\n",
4749 libcfs_id2str(id), pbuf->pb_info.pi_features);
4750 goto fail_ping_buffer_decref;
4753 if (nob < LNET_PING_INFO_SIZE(0)) {
4754 CERROR("%s: Short reply %d(%d min)\n",
4756 nob, (int)LNET_PING_INFO_SIZE(0));
4757 goto fail_ping_buffer_decref;
4760 if (pbuf->pb_info.pi_nnis < n_ids)
4761 n_ids = pbuf->pb_info.pi_nnis;
4763 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4764 CERROR("%s: Short reply %d(%d expected)\n",
4766 nob, (int)LNET_PING_INFO_SIZE(n_ids));
4767 goto fail_ping_buffer_decref;
4770 rc = -EFAULT; /* if I segv in copy_to_user()... */
4772 memset(&tmpid, 0, sizeof(tmpid));
4773 for (i = 0; i < n_ids; i++) {
4774 tmpid.pid = pbuf->pb_info.pi_pid;
4775 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4776 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4777 goto fail_ping_buffer_decref;
4779 rc = pbuf->pb_info.pi_nnis;
4781 fail_ping_buffer_decref:
4782 lnet_ping_buffer_decref(pbuf);
4787 lnet_discover(struct lnet_process_id id, __u32 force,
4788 struct lnet_process_id __user *ids, int n_ids)
4790 struct lnet_peer_ni *lpni;
4791 struct lnet_peer_ni *p;
4792 struct lnet_peer *lp;
4793 struct lnet_process_id *buf;
4799 id.nid == LNET_NID_ANY)
4802 if (id.pid == LNET_PID_ANY)
4803 id.pid = LNET_PID_LUSTRE;
4806 * If the user buffer has more space than the lnet_interfaces_max,
4807 * then only fill it up to lnet_interfaces_max.
4809 if (n_ids > lnet_interfaces_max)
4810 n_ids = lnet_interfaces_max;
4812 CFS_ALLOC_PTR_ARRAY(buf, n_ids);
4816 cpt = lnet_net_lock_current();
4817 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4824 * Clearing the NIDS_UPTODATE flag ensures the peer will
4825 * be discovered, provided discovery has not been disabled.
4827 lp = lpni->lpni_peer_net->lpn_peer;
4828 spin_lock(&lp->lp_lock);
4829 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4830 /* If the force flag is set, force a PING and PUSH as well. */
4832 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4833 spin_unlock(&lp->lp_lock);
4834 rc = lnet_discover_peer_locked(lpni, cpt, true);
4838 /* The lpni (or lp) for this NID may have changed and our ref is
4839 * the only thing keeping the old one around. Release the ref
4840 * and lookup the lpni again
4842 lnet_peer_ni_decref_locked(lpni);
4843 lpni = lnet_find_peer_ni_locked(id.nid);
4848 lp = lpni->lpni_peer_net->lpn_peer;
4852 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4853 buf[i].pid = id.pid;
4854 buf[i].nid = lnet_nid_to_nid4(&p->lpni_nid);
4861 lnet_peer_ni_decref_locked(lpni);
4863 lnet_net_unlock(cpt);
4866 if (copy_to_user(ids, buf, rc * sizeof(*buf)))
4868 CFS_FREE_PTR_ARRAY(buf, n_ids);
4874 * Retrieve peer discovery status.
4876 * \retval 1 if lnet_peer_discovery_disabled is 0
4877 * \retval 0 if lnet_peer_discovery_disabled is 1
4880 LNetGetPeerDiscoveryStatus(void)
4882 return !lnet_peer_discovery_disabled;
4884 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);