4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LNET
35 #include <linux/ctype.h>
36 #include <linux/log2.h>
37 #include <linux/ktime.h>
38 #include <linux/moduleparam.h>
39 #include <linux/uaccess.h>
40 #ifdef HAVE_SCHED_HEADERS
41 #include <linux/sched/signal.h>
44 #include <lnet/lib-lnet.h>
46 #define D_LNI D_CONSOLE
49 * initialize ln_api_mutex statically, since it needs to be used in
50 * discovery_set callback. That module parameter callback can be called
51 * before module init completes. The mutex needs to be ready for use then.
53 struct lnet the_lnet = {
54 .ln_api_mutex = __MUTEX_INITIALIZER(the_lnet.ln_api_mutex),
55 }; /* THE state of the network */
56 EXPORT_SYMBOL(the_lnet);
58 static char *ip2nets = "";
59 module_param(ip2nets, charp, 0444);
60 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
62 static char *networks = "";
63 module_param(networks, charp, 0444);
64 MODULE_PARM_DESC(networks, "local networks");
66 static char *routes = "";
67 module_param(routes, charp, 0444);
68 MODULE_PARM_DESC(routes, "routes to non-local networks");
70 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
71 module_param(rnet_htable_size, int, 0444);
72 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
74 static int use_tcp_bonding = false;
75 module_param(use_tcp_bonding, int, 0444);
76 MODULE_PARM_DESC(use_tcp_bonding,
77 "use_tcp_bonding parameter has been deprecated");
79 unsigned int lnet_numa_range = 0;
80 module_param(lnet_numa_range, uint, 0444);
81 MODULE_PARM_DESC(lnet_numa_range,
82 "NUMA range to consider during Multi-Rail selection");
85 * lnet_health_sensitivity determines by how much we decrement the health
86 * value on sending error. The value defaults to 100, which means health
87 * interface health is decremented by 100 points every failure.
89 unsigned int lnet_health_sensitivity = 100;
90 static int sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
91 #ifdef HAVE_KERNEL_PARAM_OPS
92 static struct kernel_param_ops param_ops_health_sensitivity = {
93 .set = sensitivity_set,
96 #define param_check_health_sensitivity(name, p) \
97 __param_check(name, p, int)
98 module_param(lnet_health_sensitivity, health_sensitivity, S_IRUGO|S_IWUSR);
100 module_param_call(lnet_health_sensitivity, sensitivity_set, param_get_int,
101 &lnet_health_sensitivity, S_IRUGO|S_IWUSR);
103 MODULE_PARM_DESC(lnet_health_sensitivity,
104 "Value to decrement the health value by on error");
107 * lnet_recovery_interval determines how often we should perform recovery
108 * on unhealthy interfaces.
110 unsigned int lnet_recovery_interval = 1;
111 static int recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp);
112 #ifdef HAVE_KERNEL_PARAM_OPS
113 static struct kernel_param_ops param_ops_recovery_interval = {
114 .set = recovery_interval_set,
115 .get = param_get_int,
117 #define param_check_recovery_interval(name, p) \
118 __param_check(name, p, int)
119 module_param(lnet_recovery_interval, recovery_interval, S_IRUGO|S_IWUSR);
121 module_param_call(lnet_recovery_interval, recovery_interval_set, param_get_int,
122 &lnet_recovery_interval, S_IRUGO|S_IWUSR);
124 MODULE_PARM_DESC(lnet_recovery_interval,
125 "Interval to recover unhealthy interfaces in seconds");
127 unsigned int lnet_recovery_limit;
128 module_param(lnet_recovery_limit, uint, 0644);
129 MODULE_PARM_DESC(lnet_recovery_limit,
130 "How long to attempt recovery of unhealthy peer interfaces in seconds. Set to 0 to allow indefinite recovery");
132 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
133 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
135 static struct kernel_param_ops param_ops_interfaces_max = {
137 .get = param_get_int,
140 #define param_check_interfaces_max(name, p) \
141 __param_check(name, p, int)
143 #ifdef HAVE_KERNEL_PARAM_OPS
144 module_param(lnet_interfaces_max, interfaces_max, 0644);
146 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
147 ¶m_ops_interfaces_max, 0644);
149 MODULE_PARM_DESC(lnet_interfaces_max,
150 "Maximum number of interfaces in a node.");
152 unsigned lnet_peer_discovery_disabled = 0;
153 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
155 static struct kernel_param_ops param_ops_discovery_disabled = {
156 .set = discovery_set,
157 .get = param_get_int,
160 #define param_check_discovery_disabled(name, p) \
161 __param_check(name, p, int)
162 #ifdef HAVE_KERNEL_PARAM_OPS
163 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
165 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
166 ¶m_ops_discovery_disabled, 0644);
168 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
169 "Set to 1 to disable peer discovery on this node.");
171 unsigned int lnet_drop_asym_route;
172 static int drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp);
174 static struct kernel_param_ops param_ops_drop_asym_route = {
175 .set = drop_asym_route_set,
176 .get = param_get_int,
179 #define param_check_drop_asym_route(name, p) \
180 __param_check(name, p, int)
181 #ifdef HAVE_KERNEL_PARAM_OPS
182 module_param(lnet_drop_asym_route, drop_asym_route, 0644);
184 module_param_call(lnet_drop_asym_route, drop_asym_route_set, param_get_int,
185 ¶m_ops_drop_asym_route, 0644);
187 MODULE_PARM_DESC(lnet_drop_asym_route,
188 "Set to 1 to drop asymmetrical route messages.");
190 #define LNET_TRANSACTION_TIMEOUT_DEFAULT 50
191 unsigned int lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_DEFAULT;
192 static int transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp);
193 #ifdef HAVE_KERNEL_PARAM_OPS
194 static struct kernel_param_ops param_ops_transaction_timeout = {
195 .set = transaction_to_set,
196 .get = param_get_int,
199 #define param_check_transaction_timeout(name, p) \
200 __param_check(name, p, int)
201 module_param(lnet_transaction_timeout, transaction_timeout, S_IRUGO|S_IWUSR);
203 module_param_call(lnet_transaction_timeout, transaction_to_set, param_get_int,
204 &lnet_transaction_timeout, S_IRUGO|S_IWUSR);
206 MODULE_PARM_DESC(lnet_transaction_timeout,
207 "Maximum number of seconds to wait for a peer response.");
209 #define LNET_RETRY_COUNT_DEFAULT 2
210 unsigned int lnet_retry_count = LNET_RETRY_COUNT_DEFAULT;
211 static int retry_count_set(const char *val, cfs_kernel_param_arg_t *kp);
212 #ifdef HAVE_KERNEL_PARAM_OPS
213 static struct kernel_param_ops param_ops_retry_count = {
214 .set = retry_count_set,
215 .get = param_get_int,
218 #define param_check_retry_count(name, p) \
219 __param_check(name, p, int)
220 module_param(lnet_retry_count, retry_count, S_IRUGO|S_IWUSR);
222 module_param_call(lnet_retry_count, retry_count_set, param_get_int,
223 &lnet_retry_count, S_IRUGO|S_IWUSR);
225 MODULE_PARM_DESC(lnet_retry_count,
226 "Maximum number of times to retry transmitting a message");
228 unsigned int lnet_response_tracking = 3;
229 static int response_tracking_set(const char *val, cfs_kernel_param_arg_t *kp);
231 #ifdef HAVE_KERNEL_PARAM_OPS
232 static struct kernel_param_ops param_ops_response_tracking = {
233 .set = response_tracking_set,
234 .get = param_get_int,
237 #define param_check_response_tracking(name, p) \
238 __param_check(name, p, int)
239 module_param(lnet_response_tracking, response_tracking, 0644);
241 module_param_call(lnet_response_tracking, response_tracking_set, param_get_int,
242 &lnet_response_tracking, 0644);
244 MODULE_PARM_DESC(lnet_response_tracking,
245 "(0|1|2|3) LNet Internal Only|GET Reply only|PUT ACK only|Full Tracking (default)");
247 #define LNET_LND_TIMEOUT_DEFAULT ((LNET_TRANSACTION_TIMEOUT_DEFAULT - 1) / \
248 (LNET_RETRY_COUNT_DEFAULT + 1))
249 unsigned int lnet_lnd_timeout = LNET_LND_TIMEOUT_DEFAULT;
250 static void lnet_set_lnd_timeout(void)
252 lnet_lnd_timeout = (lnet_transaction_timeout - 1) /
253 (lnet_retry_count + 1);
256 unsigned int lnet_current_net_count;
259 * This sequence number keeps track of how many times DLC was used to
260 * update the local NIs. It is incremented when a NI is added or
261 * removed and checked when sending a message to determine if there is
262 * a need to re-run the selection algorithm. See lnet_select_pathway()
263 * for more details on its usage.
265 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
267 static int lnet_ping(struct lnet_process_id id, signed long timeout,
268 struct lnet_process_id __user *ids, int n_ids);
270 static int lnet_discover(struct lnet_process_id id, __u32 force,
271 struct lnet_process_id __user *ids, int n_ids);
274 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
277 unsigned *sensitivity = (unsigned *)kp->arg;
280 rc = kstrtoul(val, 0, &value);
282 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
287 * The purpose of locking the api_mutex here is to ensure that
288 * the correct value ends up stored properly.
290 mutex_lock(&the_lnet.ln_api_mutex);
292 if (value > LNET_MAX_HEALTH_VALUE) {
293 mutex_unlock(&the_lnet.ln_api_mutex);
294 CERROR("Invalid health value. Maximum: %d value = %lu\n",
295 LNET_MAX_HEALTH_VALUE, value);
299 if (*sensitivity != 0 && value == 0 && lnet_retry_count != 0) {
300 lnet_retry_count = 0;
301 lnet_set_lnd_timeout();
304 *sensitivity = value;
306 mutex_unlock(&the_lnet.ln_api_mutex);
312 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
315 unsigned *interval = (unsigned *)kp->arg;
318 rc = kstrtoul(val, 0, &value);
320 CERROR("Invalid module parameter value for 'lnet_recovery_interval'\n");
325 CERROR("lnet_recovery_interval must be at least 1 second\n");
330 * The purpose of locking the api_mutex here is to ensure that
331 * the correct value ends up stored properly.
333 mutex_lock(&the_lnet.ln_api_mutex);
337 mutex_unlock(&the_lnet.ln_api_mutex);
343 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
346 unsigned *discovery_off = (unsigned *)kp->arg;
348 struct lnet_ping_buffer *pbuf;
350 rc = kstrtoul(val, 0, &value);
352 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
356 value = (value) ? 1 : 0;
359 * The purpose of locking the api_mutex here is to ensure that
360 * the correct value ends up stored properly.
362 mutex_lock(&the_lnet.ln_api_mutex);
364 if (value == *discovery_off) {
365 mutex_unlock(&the_lnet.ln_api_mutex);
370 * We still want to set the discovery value even when LNet is not
371 * running. This is the case when LNet is being loaded and we want
372 * the module parameters to take effect. Otherwise if we're
373 * changing the value dynamically, we want to set it after
376 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
377 *discovery_off = value;
378 mutex_unlock(&the_lnet.ln_api_mutex);
382 /* tell peers that discovery setting has changed */
383 lnet_net_lock(LNET_LOCK_EX);
384 pbuf = the_lnet.ln_ping_target;
386 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
388 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
389 lnet_net_unlock(LNET_LOCK_EX);
391 /* only send a push when we're turning off discovery */
392 if (*discovery_off <= 0 && value > 0)
393 lnet_push_update_to_peers(1);
394 *discovery_off = value;
396 mutex_unlock(&the_lnet.ln_api_mutex);
402 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
405 unsigned int *drop_asym_route = (unsigned int *)kp->arg;
408 rc = kstrtoul(val, 0, &value);
410 CERROR("Invalid module parameter value for "
411 "'lnet_drop_asym_route'\n");
416 * The purpose of locking the api_mutex here is to ensure that
417 * the correct value ends up stored properly.
419 mutex_lock(&the_lnet.ln_api_mutex);
421 if (value == *drop_asym_route) {
422 mutex_unlock(&the_lnet.ln_api_mutex);
426 *drop_asym_route = value;
428 mutex_unlock(&the_lnet.ln_api_mutex);
434 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
437 unsigned *transaction_to = (unsigned *)kp->arg;
440 rc = kstrtoul(val, 0, &value);
442 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
447 * The purpose of locking the api_mutex here is to ensure that
448 * the correct value ends up stored properly.
450 mutex_lock(&the_lnet.ln_api_mutex);
452 if (value <= lnet_retry_count || value == 0) {
453 mutex_unlock(&the_lnet.ln_api_mutex);
454 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
455 "Has to be greater than lnet_retry_count (%u)\n",
456 value, lnet_retry_count);
460 if (value == *transaction_to) {
461 mutex_unlock(&the_lnet.ln_api_mutex);
465 *transaction_to = value;
466 /* Update the lnet_lnd_timeout now that we've modified the
467 * transaction timeout
469 lnet_set_lnd_timeout();
471 mutex_unlock(&the_lnet.ln_api_mutex);
477 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
480 unsigned *retry_count = (unsigned *)kp->arg;
483 rc = kstrtoul(val, 0, &value);
485 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
490 * The purpose of locking the api_mutex here is to ensure that
491 * the correct value ends up stored properly.
493 mutex_lock(&the_lnet.ln_api_mutex);
495 if (lnet_health_sensitivity == 0 && value > 0) {
496 mutex_unlock(&the_lnet.ln_api_mutex);
497 CERROR("Can not set lnet_retry_count when health feature is turned off\n");
501 if (value > lnet_transaction_timeout) {
502 mutex_unlock(&the_lnet.ln_api_mutex);
503 CERROR("Invalid value for lnet_retry_count (%lu). "
504 "Has to be smaller than lnet_transaction_timeout (%u)\n",
505 value, lnet_transaction_timeout);
509 *retry_count = value;
511 /* Update the lnet_lnd_timeout now that we've modified the
514 lnet_set_lnd_timeout();
516 mutex_unlock(&the_lnet.ln_api_mutex);
522 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
526 rc = kstrtoint(val, 0, &value);
528 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
532 if (value < LNET_INTERFACES_MIN) {
533 CWARN("max interfaces provided are too small, setting to %d\n",
534 LNET_INTERFACES_MAX_DEFAULT);
535 value = LNET_INTERFACES_MAX_DEFAULT;
538 *(int *)kp->arg = value;
544 response_tracking_set(const char *val, cfs_kernel_param_arg_t *kp)
547 unsigned long new_value;
549 rc = kstrtoul(val, 0, &new_value);
551 CERROR("Invalid value for 'lnet_response_tracking'\n");
555 if (new_value < 0 || new_value > 3) {
556 CWARN("Invalid value (%lu) for 'lnet_response_tracking'\n",
561 lnet_response_tracking = new_value;
567 lnet_get_routes(void)
573 lnet_get_networks(void)
578 if (*networks != 0 && *ip2nets != 0) {
579 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
580 "'ip2nets' but not both at once\n");
585 rc = lnet_parse_ip2nets(&nets, ip2nets);
586 return (rc == 0) ? nets : NULL;
596 lnet_init_locks(void)
598 spin_lock_init(&the_lnet.ln_eq_wait_lock);
599 spin_lock_init(&the_lnet.ln_msg_resend_lock);
600 init_completion(&the_lnet.ln_mt_wait_complete);
601 mutex_init(&the_lnet.ln_lnd_mutex);
604 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
605 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
607 struct kmem_cache *lnet_rspt_cachep; /* response tracker cache */
608 struct kmem_cache *lnet_msg_cachep;
611 lnet_slab_setup(void)
613 /* create specific kmem_cache for MEs and small MDs (i.e., originally
614 * allocated in <size-xxx> kmem_cache).
616 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
618 if (!lnet_mes_cachep)
621 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
622 LNET_SMALL_MD_SIZE, 0, 0,
624 if (!lnet_small_mds_cachep)
627 lnet_rspt_cachep = kmem_cache_create("lnet_rspt", sizeof(struct lnet_rsp_tracker),
629 if (!lnet_rspt_cachep)
632 lnet_msg_cachep = kmem_cache_create("lnet_msg", sizeof(struct lnet_msg),
634 if (!lnet_msg_cachep)
641 lnet_slab_cleanup(void)
643 if (lnet_msg_cachep) {
644 kmem_cache_destroy(lnet_msg_cachep);
645 lnet_msg_cachep = NULL;
649 if (lnet_rspt_cachep) {
650 kmem_cache_destroy(lnet_rspt_cachep);
651 lnet_rspt_cachep = NULL;
654 if (lnet_small_mds_cachep) {
655 kmem_cache_destroy(lnet_small_mds_cachep);
656 lnet_small_mds_cachep = NULL;
659 if (lnet_mes_cachep) {
660 kmem_cache_destroy(lnet_mes_cachep);
661 lnet_mes_cachep = NULL;
666 lnet_create_remote_nets_table(void)
669 struct list_head *hash;
671 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
672 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
673 CFS_ALLOC_PTR_ARRAY(hash, LNET_REMOTE_NETS_HASH_SIZE);
675 CERROR("Failed to create remote nets hash table\n");
679 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
680 INIT_LIST_HEAD(&hash[i]);
681 the_lnet.ln_remote_nets_hash = hash;
686 lnet_destroy_remote_nets_table(void)
690 if (the_lnet.ln_remote_nets_hash == NULL)
693 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
694 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
696 CFS_FREE_PTR_ARRAY(the_lnet.ln_remote_nets_hash,
697 LNET_REMOTE_NETS_HASH_SIZE);
698 the_lnet.ln_remote_nets_hash = NULL;
702 lnet_destroy_locks(void)
704 if (the_lnet.ln_res_lock != NULL) {
705 cfs_percpt_lock_free(the_lnet.ln_res_lock);
706 the_lnet.ln_res_lock = NULL;
709 if (the_lnet.ln_net_lock != NULL) {
710 cfs_percpt_lock_free(the_lnet.ln_net_lock);
711 the_lnet.ln_net_lock = NULL;
716 lnet_create_locks(void)
720 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
721 if (the_lnet.ln_res_lock == NULL)
724 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
725 if (the_lnet.ln_net_lock == NULL)
731 lnet_destroy_locks();
735 static void lnet_assert_wire_constants(void)
737 /* Wire protocol assertions generated by 'wirecheck'
738 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
739 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
740 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
743 BUILD_BUG_ON(LNET_PROTO_TCP_MAGIC != 0xeebc0ded);
744 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MAJOR != 1);
745 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MINOR != 0);
746 BUILD_BUG_ON(LNET_MSG_ACK != 0);
747 BUILD_BUG_ON(LNET_MSG_PUT != 1);
748 BUILD_BUG_ON(LNET_MSG_GET != 2);
749 BUILD_BUG_ON(LNET_MSG_REPLY != 3);
750 BUILD_BUG_ON(LNET_MSG_HELLO != 4);
752 /* Checks for struct lnet_handle_wire */
753 BUILD_BUG_ON((int)sizeof(struct lnet_handle_wire) != 16);
754 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
755 wh_interface_cookie) != 0);
756 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) != 8);
757 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
758 wh_object_cookie) != 8);
759 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) != 8);
761 /* Checks for struct struct lnet_magicversion */
762 BUILD_BUG_ON((int)sizeof(struct lnet_magicversion) != 8);
763 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, magic) != 0);
764 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->magic) != 4);
765 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, version_major) != 4);
766 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_major) != 2);
767 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion,
768 version_minor) != 6);
769 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_minor) != 2);
771 /* Checks for struct struct lnet_hdr */
772 BUILD_BUG_ON((int)sizeof(struct lnet_hdr) != 72);
773 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_nid) != 0);
774 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_nid) != 8);
775 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_nid) != 8);
776 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_nid) != 8);
777 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_pid) != 16);
778 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_pid) != 4);
779 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_pid) != 20);
780 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_pid) != 4);
781 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, type) != 24);
782 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->type) != 4);
783 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, payload_length) != 28);
784 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->payload_length) != 4);
785 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg) != 32);
786 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg) != 40);
789 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) != 32);
790 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) != 16);
791 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.match_bits) != 48);
792 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) != 8);
793 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.mlength) != 56);
794 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) != 4);
797 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) != 32);
798 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) != 16);
799 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.match_bits) != 48);
800 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) != 8);
801 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.hdr_data) != 56);
802 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) != 8);
803 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ptl_index) != 64);
804 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) != 4);
805 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.offset) != 68);
806 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) != 4);
809 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.return_wmd) != 32);
810 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) != 16);
811 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.match_bits) != 48);
812 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) != 8);
813 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.ptl_index) != 56);
814 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) != 4);
815 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.src_offset) != 60);
816 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) != 4);
817 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.sink_length) != 64);
818 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) != 4);
821 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) != 32);
822 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) != 16);
825 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.incarnation) != 32);
826 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) != 8);
827 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.type) != 40);
828 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) != 4);
830 /* Checks for struct lnet_ni_status and related constants */
831 BUILD_BUG_ON(LNET_NI_STATUS_INVALID != 0x00000000);
832 BUILD_BUG_ON(LNET_NI_STATUS_UP != 0x15aac0de);
833 BUILD_BUG_ON(LNET_NI_STATUS_DOWN != 0xdeadface);
835 /* Checks for struct lnet_ni_status */
836 BUILD_BUG_ON((int)sizeof(struct lnet_ni_status) != 16);
837 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_nid) != 0);
838 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) != 8);
839 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_status) != 8);
840 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_status) != 4);
841 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_unused) != 12);
842 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) != 4);
844 /* Checks for struct lnet_ping_info and related constants */
845 BUILD_BUG_ON(LNET_PROTO_PING_MAGIC != 0x70696E67);
846 BUILD_BUG_ON(LNET_PING_FEAT_INVAL != 0);
847 BUILD_BUG_ON(LNET_PING_FEAT_BASE != 1);
848 BUILD_BUG_ON(LNET_PING_FEAT_NI_STATUS != 2);
849 BUILD_BUG_ON(LNET_PING_FEAT_RTE_DISABLED != 4);
850 BUILD_BUG_ON(LNET_PING_FEAT_MULTI_RAIL != 8);
851 BUILD_BUG_ON(LNET_PING_FEAT_DISCOVERY != 16);
852 BUILD_BUG_ON(LNET_PING_FEAT_BITS != 31);
854 /* Checks for struct lnet_ping_info */
855 BUILD_BUG_ON((int)sizeof(struct lnet_ping_info) != 16);
856 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_magic) != 0);
857 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) != 4);
858 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_features) != 4);
859 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_features) != 4);
860 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_pid) != 8);
861 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) != 4);
862 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_nnis) != 12);
863 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) != 4);
864 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_ni) != 16);
865 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) != 0);
868 static const struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
870 const struct lnet_lnd *lnd;
872 /* holding lnd mutex */
873 if (type >= NUM_LNDS)
875 lnd = the_lnet.ln_lnds[type];
876 LASSERT(!lnd || lnd->lnd_type == type);
882 lnet_get_lnd_timeout(void)
884 return lnet_lnd_timeout;
886 EXPORT_SYMBOL(lnet_get_lnd_timeout);
889 lnet_register_lnd(const struct lnet_lnd *lnd)
891 mutex_lock(&the_lnet.ln_lnd_mutex);
893 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
894 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
896 the_lnet.ln_lnds[lnd->lnd_type] = lnd;
898 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
900 mutex_unlock(&the_lnet.ln_lnd_mutex);
902 EXPORT_SYMBOL(lnet_register_lnd);
905 lnet_unregister_lnd(const struct lnet_lnd *lnd)
907 mutex_lock(&the_lnet.ln_lnd_mutex);
909 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
911 the_lnet.ln_lnds[lnd->lnd_type] = NULL;
912 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
914 mutex_unlock(&the_lnet.ln_lnd_mutex);
916 EXPORT_SYMBOL(lnet_unregister_lnd);
919 lnet_counters_get_common_locked(struct lnet_counters_common *common)
921 struct lnet_counters *ctr;
924 /* FIXME !!! Their is no assert_lnet_net_locked() to ensure this
925 * actually called under the protection of the lnet_net_lock.
927 memset(common, 0, sizeof(*common));
929 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
930 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
931 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
932 common->lcc_errors += ctr->lct_common.lcc_errors;
933 common->lcc_send_count += ctr->lct_common.lcc_send_count;
934 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
935 common->lcc_route_count += ctr->lct_common.lcc_route_count;
936 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
937 common->lcc_send_length += ctr->lct_common.lcc_send_length;
938 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
939 common->lcc_route_length += ctr->lct_common.lcc_route_length;
940 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
945 lnet_counters_get_common(struct lnet_counters_common *common)
947 lnet_net_lock(LNET_LOCK_EX);
948 lnet_counters_get_common_locked(common);
949 lnet_net_unlock(LNET_LOCK_EX);
951 EXPORT_SYMBOL(lnet_counters_get_common);
954 lnet_counters_get(struct lnet_counters *counters)
956 struct lnet_counters *ctr;
957 struct lnet_counters_health *health = &counters->lct_health;
960 memset(counters, 0, sizeof(*counters));
962 lnet_net_lock(LNET_LOCK_EX);
964 if (the_lnet.ln_state != LNET_STATE_RUNNING)
965 GOTO(out_unlock, rc = -ENODEV);
967 lnet_counters_get_common_locked(&counters->lct_common);
969 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
970 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
971 health->lch_resend_count += ctr->lct_health.lch_resend_count;
972 health->lch_response_timeout_count +=
973 ctr->lct_health.lch_response_timeout_count;
974 health->lch_local_interrupt_count +=
975 ctr->lct_health.lch_local_interrupt_count;
976 health->lch_local_dropped_count +=
977 ctr->lct_health.lch_local_dropped_count;
978 health->lch_local_aborted_count +=
979 ctr->lct_health.lch_local_aborted_count;
980 health->lch_local_no_route_count +=
981 ctr->lct_health.lch_local_no_route_count;
982 health->lch_local_timeout_count +=
983 ctr->lct_health.lch_local_timeout_count;
984 health->lch_local_error_count +=
985 ctr->lct_health.lch_local_error_count;
986 health->lch_remote_dropped_count +=
987 ctr->lct_health.lch_remote_dropped_count;
988 health->lch_remote_error_count +=
989 ctr->lct_health.lch_remote_error_count;
990 health->lch_remote_timeout_count +=
991 ctr->lct_health.lch_remote_timeout_count;
992 health->lch_network_timeout_count +=
993 ctr->lct_health.lch_network_timeout_count;
996 lnet_net_unlock(LNET_LOCK_EX);
999 EXPORT_SYMBOL(lnet_counters_get);
1002 lnet_counters_reset(void)
1004 struct lnet_counters *counters;
1007 lnet_net_lock(LNET_LOCK_EX);
1009 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1012 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
1013 memset(counters, 0, sizeof(struct lnet_counters));
1015 lnet_net_unlock(LNET_LOCK_EX);
1019 lnet_res_type2str(int type)
1024 case LNET_COOKIE_TYPE_MD:
1026 case LNET_COOKIE_TYPE_ME:
1028 case LNET_COOKIE_TYPE_EQ:
1034 lnet_res_container_cleanup(struct lnet_res_container *rec)
1038 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
1041 while (!list_empty(&rec->rec_active)) {
1042 struct list_head *e = rec->rec_active.next;
1045 if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
1046 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
1048 } else { /* NB: Active MEs should be attached on portals */
1055 /* Found alive MD/ME/EQ, user really should unlink/free
1056 * all of them before finalize LNet, but if someone didn't,
1057 * we have to recycle garbage for him */
1058 CERROR("%d active elements on exit of %s container\n",
1059 count, lnet_res_type2str(rec->rec_type));
1062 if (rec->rec_lh_hash != NULL) {
1063 CFS_FREE_PTR_ARRAY(rec->rec_lh_hash, LNET_LH_HASH_SIZE);
1064 rec->rec_lh_hash = NULL;
1067 rec->rec_type = 0; /* mark it as finalized */
1071 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1076 LASSERT(rec->rec_type == 0);
1078 rec->rec_type = type;
1079 INIT_LIST_HEAD(&rec->rec_active);
1081 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1083 /* Arbitrary choice of hash table size */
1084 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1085 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1086 if (rec->rec_lh_hash == NULL) {
1091 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1092 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1097 CERROR("Failed to setup %s resource container\n",
1098 lnet_res_type2str(type));
1099 lnet_res_container_cleanup(rec);
1104 lnet_res_containers_destroy(struct lnet_res_container **recs)
1106 struct lnet_res_container *rec;
1109 cfs_percpt_for_each(rec, i, recs)
1110 lnet_res_container_cleanup(rec);
1112 cfs_percpt_free(recs);
1115 static struct lnet_res_container **
1116 lnet_res_containers_create(int type)
1118 struct lnet_res_container **recs;
1119 struct lnet_res_container *rec;
1123 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1125 CERROR("Failed to allocate %s resource containers\n",
1126 lnet_res_type2str(type));
1130 cfs_percpt_for_each(rec, i, recs) {
1131 rc = lnet_res_container_setup(rec, i, type);
1133 lnet_res_containers_destroy(recs);
1141 struct lnet_libhandle *
1142 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1144 /* ALWAYS called with lnet_res_lock held */
1145 struct list_head *head;
1146 struct lnet_libhandle *lh;
1149 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1152 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1153 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1155 list_for_each_entry(lh, head, lh_hash_chain) {
1156 if (lh->lh_cookie == cookie)
1164 lnet_res_lh_initialize(struct lnet_res_container *rec,
1165 struct lnet_libhandle *lh)
1167 /* ALWAYS called with lnet_res_lock held */
1168 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1171 lh->lh_cookie = rec->rec_lh_cookie;
1172 rec->rec_lh_cookie += 1 << ibits;
1174 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1176 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1180 lnet_create_array_of_queues(void)
1182 struct list_head **qs;
1183 struct list_head *q;
1186 qs = cfs_percpt_alloc(lnet_cpt_table(),
1187 sizeof(struct list_head));
1189 CERROR("Failed to allocate queues\n");
1193 cfs_percpt_for_each(q, i, qs)
1199 static int lnet_unprepare(void);
1202 lnet_prepare(lnet_pid_t requested_pid)
1204 /* Prepare to bring up the network */
1205 struct lnet_res_container **recs;
1208 if (requested_pid == LNET_PID_ANY) {
1209 /* Don't instantiate LNET just for me */
1213 LASSERT(the_lnet.ln_refcount == 0);
1215 the_lnet.ln_routing = 0;
1217 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1218 the_lnet.ln_pid = requested_pid;
1220 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1221 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1222 INIT_LIST_HEAD(&the_lnet.ln_nets);
1223 INIT_LIST_HEAD(&the_lnet.ln_routers);
1224 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1225 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1226 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1227 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1228 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1229 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1230 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1231 INIT_LIST_HEAD(&the_lnet.ln_udsp_list);
1232 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1233 the_lnet.ln_mt_handler = NULL;
1234 init_completion(&the_lnet.ln_started);
1236 rc = lnet_slab_setup();
1240 rc = lnet_create_remote_nets_table();
1245 * NB the interface cookie in wire handles guards against delayed
1246 * replies and ACKs appearing valid after reboot.
1248 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1250 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1251 sizeof(struct lnet_counters));
1252 if (the_lnet.ln_counters == NULL) {
1253 CERROR("Failed to allocate counters for LNet\n");
1258 rc = lnet_peer_tables_create();
1262 rc = lnet_msg_containers_create();
1266 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1267 LNET_COOKIE_TYPE_EQ);
1271 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1277 the_lnet.ln_md_containers = recs;
1279 rc = lnet_portals_create();
1281 CERROR("Failed to create portals for LNet: %d\n", rc);
1285 the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1286 if (!the_lnet.ln_mt_zombie_rstqs) {
1299 lnet_unprepare (void)
1301 /* NB no LNET_LOCK since this is the last reference. All LND instances
1302 * have shut down already, so it is safe to unlink and free all
1303 * descriptors, even those that appear committed to a network op (eg MD
1304 * with non-zero pending count) */
1306 lnet_fail_nid(LNET_NID_ANY, 0);
1308 LASSERT(the_lnet.ln_refcount == 0);
1309 LASSERT(list_empty(&the_lnet.ln_test_peers));
1310 LASSERT(list_empty(&the_lnet.ln_nets));
1312 if (the_lnet.ln_mt_zombie_rstqs) {
1313 lnet_clean_zombie_rstqs();
1314 the_lnet.ln_mt_zombie_rstqs = NULL;
1317 lnet_assert_handler_unused(the_lnet.ln_mt_handler);
1318 the_lnet.ln_mt_handler = NULL;
1320 lnet_portals_destroy();
1322 if (the_lnet.ln_md_containers != NULL) {
1323 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1324 the_lnet.ln_md_containers = NULL;
1327 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1329 lnet_msg_containers_destroy();
1331 lnet_rtrpools_free(0);
1333 if (the_lnet.ln_counters != NULL) {
1334 cfs_percpt_free(the_lnet.ln_counters);
1335 the_lnet.ln_counters = NULL;
1337 lnet_destroy_remote_nets_table();
1338 lnet_slab_cleanup();
1344 lnet_net2ni_locked(__u32 net_id, int cpt)
1347 struct lnet_net *net;
1349 LASSERT(cpt != LNET_LOCK_EX);
1351 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1352 if (net->net_id == net_id) {
1353 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1363 lnet_net2ni_addref(__u32 net)
1368 ni = lnet_net2ni_locked(net, 0);
1370 lnet_ni_addref_locked(ni, 0);
1375 EXPORT_SYMBOL(lnet_net2ni_addref);
1378 lnet_get_net_locked(__u32 net_id)
1380 struct lnet_net *net;
1382 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1383 if (net->net_id == net_id)
1391 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1396 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1401 val = hash_long(key, LNET_CPT_BITS);
1402 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1406 return (unsigned int)(key + val + (val >> 1)) % number;
1410 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1412 struct lnet_net *net;
1414 /* must called with hold of lnet_net_lock */
1415 if (LNET_CPT_NUMBER == 1)
1416 return 0; /* the only one */
1419 * If NI is provided then use the CPT identified in the NI cpt
1420 * list if one exists. If one doesn't exist, then that NI is
1421 * associated with all CPTs and it follows that the net it belongs
1422 * to is implicitly associated with all CPTs, so just hash the nid
1426 if (ni->ni_cpts != NULL)
1427 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1430 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1433 /* no NI provided so look at the net */
1434 net = lnet_get_net_locked(LNET_NIDNET(nid));
1436 if (net != NULL && net->net_cpts != NULL) {
1437 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1440 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1444 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1449 if (LNET_CPT_NUMBER == 1)
1450 return 0; /* the only one */
1452 cpt = lnet_net_lock_current();
1454 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1456 lnet_net_unlock(cpt);
1460 EXPORT_SYMBOL(lnet_cpt_of_nid);
1463 lnet_islocalnet_locked(__u32 net_id)
1465 struct lnet_net *net;
1468 net = lnet_get_net_locked(net_id);
1470 local = net != NULL;
1476 lnet_islocalnet(__u32 net_id)
1481 cpt = lnet_net_lock_current();
1483 local = lnet_islocalnet_locked(net_id);
1485 lnet_net_unlock(cpt);
1491 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1493 struct lnet_net *net;
1496 LASSERT(cpt != LNET_LOCK_EX);
1498 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1499 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1500 if (ni->ni_nid == nid)
1509 lnet_nid2ni_addref(lnet_nid_t nid)
1514 ni = lnet_nid2ni_locked(nid, 0);
1516 lnet_ni_addref_locked(ni, 0);
1521 EXPORT_SYMBOL(lnet_nid2ni_addref);
1524 lnet_islocalnid(lnet_nid_t nid)
1529 cpt = lnet_net_lock_current();
1530 ni = lnet_nid2ni_locked(nid, cpt);
1531 lnet_net_unlock(cpt);
1537 lnet_count_acceptor_nets(void)
1539 /* Return the # of NIs that need the acceptor. */
1541 struct lnet_net *net;
1544 cpt = lnet_net_lock_current();
1545 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1546 /* all socklnd type networks should have the acceptor
1548 if (net->net_lnd->lnd_accept != NULL)
1552 lnet_net_unlock(cpt);
1557 struct lnet_ping_buffer *
1558 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1560 struct lnet_ping_buffer *pbuf;
1562 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1564 pbuf->pb_nnis = nnis;
1565 pbuf->pb_needs_post = false;
1566 atomic_set(&pbuf->pb_refcnt, 1);
1573 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1575 LASSERT(atomic_read(&pbuf->pb_refcnt) == 0);
1576 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1579 static struct lnet_ping_buffer *
1580 lnet_ping_target_create(int nnis)
1582 struct lnet_ping_buffer *pbuf;
1584 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1586 CERROR("Can't allocate ping source [%d]\n", nnis);
1590 pbuf->pb_info.pi_nnis = nnis;
1591 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1592 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1593 pbuf->pb_info.pi_features =
1594 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1600 lnet_get_net_ni_count_locked(struct lnet_net *net)
1605 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1612 lnet_get_net_ni_count_pre(struct lnet_net *net)
1617 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1624 lnet_get_ni_count(void)
1627 struct lnet_net *net;
1632 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1633 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1643 lnet_get_net_count(void)
1645 struct lnet_net *net;
1650 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1660 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1662 struct lnet_ni_status *stat;
1666 __swab32s(&pbuf->pb_info.pi_magic);
1667 __swab32s(&pbuf->pb_info.pi_features);
1668 __swab32s(&pbuf->pb_info.pi_pid);
1669 __swab32s(&pbuf->pb_info.pi_nnis);
1670 nnis = pbuf->pb_info.pi_nnis;
1671 if (nnis > pbuf->pb_nnis)
1672 nnis = pbuf->pb_nnis;
1673 for (i = 0; i < nnis; i++) {
1674 stat = &pbuf->pb_info.pi_ni[i];
1675 __swab64s(&stat->ns_nid);
1676 __swab32s(&stat->ns_status);
1681 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1685 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1687 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1689 /* Loopback is guaranteed to be present */
1690 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1692 if (LNET_PING_INFO_LONI(pinfo) != LNET_NID_LO_0)
1698 lnet_ping_target_destroy(void)
1700 struct lnet_net *net;
1703 lnet_net_lock(LNET_LOCK_EX);
1705 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1706 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1708 ni->ni_status = NULL;
1713 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1714 the_lnet.ln_ping_target = NULL;
1716 lnet_net_unlock(LNET_LOCK_EX);
1720 lnet_ping_target_event_handler(struct lnet_event *event)
1722 struct lnet_ping_buffer *pbuf = event->md_user_ptr;
1724 if (event->unlinked)
1725 lnet_ping_buffer_decref(pbuf);
1729 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1730 struct lnet_handle_md *ping_mdh,
1731 int ni_count, bool set_eq)
1733 struct lnet_process_id id = {
1734 .nid = LNET_NID_ANY,
1738 struct lnet_md md = { NULL };
1742 the_lnet.ln_ping_target_handler =
1743 lnet_ping_target_event_handler;
1745 *ppbuf = lnet_ping_target_create(ni_count);
1746 if (*ppbuf == NULL) {
1751 /* Ping target ME/MD */
1752 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1753 LNET_PROTO_PING_MATCHBITS, 0,
1754 LNET_UNLINK, LNET_INS_AFTER);
1757 CERROR("Can't create ping target ME: %d\n", rc);
1758 goto fail_decref_ping_buffer;
1761 /* initialize md content */
1762 md.start = &(*ppbuf)->pb_info;
1763 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1764 md.threshold = LNET_MD_THRESH_INF;
1766 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1767 LNET_MD_MANAGE_REMOTE;
1768 md.handler = the_lnet.ln_ping_target_handler;
1769 md.user_ptr = *ppbuf;
1771 rc = LNetMDAttach(me, &md, LNET_RETAIN, ping_mdh);
1773 CERROR("Can't attach ping target MD: %d\n", rc);
1774 goto fail_decref_ping_buffer;
1776 lnet_ping_buffer_addref(*ppbuf);
1780 fail_decref_ping_buffer:
1781 LASSERT(atomic_read(&(*ppbuf)->pb_refcnt) == 1);
1782 lnet_ping_buffer_decref(*ppbuf);
1789 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1790 struct lnet_handle_md *ping_mdh)
1792 LNetMDUnlink(*ping_mdh);
1793 LNetInvalidateMDHandle(ping_mdh);
1795 /* NB the MD could be busy; this just starts the unlink */
1796 wait_var_event_warning(&pbuf->pb_refcnt,
1797 atomic_read(&pbuf->pb_refcnt) <= 1,
1798 "Still waiting for ping data MD to unlink\n");
1802 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1805 struct lnet_net *net;
1806 struct lnet_ni_status *ns;
1811 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1812 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1813 LASSERT(i < pbuf->pb_nnis);
1815 ns = &pbuf->pb_info.pi_ni[i];
1817 ns->ns_nid = ni->ni_nid;
1820 ns->ns_status = (ni->ni_status != NULL) ?
1821 ni->ni_status->ns_status :
1830 * We (ab)use the ns_status of the loopback interface to
1831 * transmit the sequence number. The first interface listed
1832 * must be the loopback interface.
1834 rc = lnet_ping_info_validate(&pbuf->pb_info);
1836 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1839 LNET_PING_BUFFER_SEQNO(pbuf) =
1840 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1844 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1845 struct lnet_handle_md ping_mdh)
1847 struct lnet_ping_buffer *old_pbuf = NULL;
1848 struct lnet_handle_md old_ping_md;
1850 /* switch the NIs to point to the new ping info created */
1851 lnet_net_lock(LNET_LOCK_EX);
1853 if (!the_lnet.ln_routing)
1854 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1855 if (!lnet_peer_discovery_disabled)
1856 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1858 /* Ensure only known feature bits have been set. */
1859 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1860 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1862 lnet_ping_target_install_locked(pbuf);
1864 if (the_lnet.ln_ping_target) {
1865 old_pbuf = the_lnet.ln_ping_target;
1866 old_ping_md = the_lnet.ln_ping_target_md;
1868 the_lnet.ln_ping_target_md = ping_mdh;
1869 the_lnet.ln_ping_target = pbuf;
1871 lnet_net_unlock(LNET_LOCK_EX);
1874 /* unlink and free the old ping info */
1875 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1876 lnet_ping_buffer_decref(old_pbuf);
1879 lnet_push_update_to_peers(0);
1883 lnet_ping_target_fini(void)
1885 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1886 &the_lnet.ln_ping_target_md);
1888 lnet_assert_handler_unused(the_lnet.ln_ping_target_handler);
1889 lnet_ping_target_destroy();
1892 /* Resize the push target. */
1893 int lnet_push_target_resize(void)
1895 struct lnet_handle_md mdh;
1896 struct lnet_handle_md old_mdh;
1897 struct lnet_ping_buffer *pbuf;
1898 struct lnet_ping_buffer *old_pbuf;
1903 nnis = the_lnet.ln_push_target_nnis;
1905 CDEBUG(D_NET, "Invalid nnis %d\n", nnis);
1909 /* NB: lnet_ping_buffer_alloc() sets pbuf refcount to 1. That ref is
1910 * dropped when we need to resize again (see "old_pbuf" below) or when
1911 * LNet is shutdown (see lnet_push_target_fini())
1913 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1915 CDEBUG(D_NET, "Can't allocate pbuf for nnis %d\n", nnis);
1919 rc = lnet_push_target_post(pbuf, &mdh);
1921 CDEBUG(D_NET, "Failed to post push target: %d\n", rc);
1922 lnet_ping_buffer_decref(pbuf);
1926 lnet_net_lock(LNET_LOCK_EX);
1927 old_pbuf = the_lnet.ln_push_target;
1928 old_mdh = the_lnet.ln_push_target_md;
1929 the_lnet.ln_push_target = pbuf;
1930 the_lnet.ln_push_target_md = mdh;
1931 lnet_net_unlock(LNET_LOCK_EX);
1934 LNetMDUnlink(old_mdh);
1935 /* Drop ref set by lnet_ping_buffer_alloc() */
1936 lnet_ping_buffer_decref(old_pbuf);
1939 /* Received another push or reply that requires a larger buffer */
1940 if (nnis < the_lnet.ln_push_target_nnis)
1943 CDEBUG(D_NET, "nnis %d success\n", nnis);
1947 int lnet_push_target_post(struct lnet_ping_buffer *pbuf,
1948 struct lnet_handle_md *mdhp)
1950 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1951 struct lnet_md md = { NULL };
1955 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1956 LNET_PROTO_PING_MATCHBITS, 0,
1957 LNET_UNLINK, LNET_INS_AFTER);
1960 CERROR("Can't create push target ME: %d\n", rc);
1964 pbuf->pb_needs_post = false;
1966 /* This reference is dropped by lnet_push_target_event_handler() */
1967 lnet_ping_buffer_addref(pbuf);
1969 /* initialize md content */
1970 md.start = &pbuf->pb_info;
1971 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
1974 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE;
1976 md.handler = the_lnet.ln_push_target_handler;
1978 rc = LNetMDAttach(me, &md, LNET_UNLINK, mdhp);
1980 CERROR("Can't attach push MD: %d\n", rc);
1981 lnet_ping_buffer_decref(pbuf);
1982 pbuf->pb_needs_post = true;
1986 CDEBUG(D_NET, "posted push target %p\n", pbuf);
1991 static void lnet_push_target_event_handler(struct lnet_event *ev)
1993 struct lnet_ping_buffer *pbuf = ev->md_user_ptr;
1995 CDEBUG(D_NET, "type %d status %d unlinked %d\n", ev->type, ev->status,
1998 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1999 lnet_swap_pinginfo(pbuf);
2001 if (ev->type == LNET_EVENT_UNLINK) {
2002 /* Drop ref added by lnet_push_target_post() */
2003 lnet_ping_buffer_decref(pbuf);
2007 lnet_peer_push_event(ev);
2009 /* Drop ref added by lnet_push_target_post */
2010 lnet_ping_buffer_decref(pbuf);
2013 /* Initialize the push target. */
2014 static int lnet_push_target_init(void)
2018 if (the_lnet.ln_push_target)
2021 the_lnet.ln_push_target_handler =
2022 lnet_push_target_event_handler;
2024 rc = LNetSetLazyPortal(LNET_RESERVED_PORTAL);
2027 /* Start at the required minimum, we'll enlarge if required. */
2028 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
2030 rc = lnet_push_target_resize();
2033 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2034 the_lnet.ln_push_target_handler = NULL;
2040 /* Clean up the push target. */
2041 static void lnet_push_target_fini(void)
2043 if (!the_lnet.ln_push_target)
2046 /* Unlink and invalidate to prevent new references. */
2047 LNetMDUnlink(the_lnet.ln_push_target_md);
2048 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
2050 /* Wait for the unlink to complete. */
2051 wait_var_event_warning(&the_lnet.ln_push_target->pb_refcnt,
2052 atomic_read(&the_lnet.ln_push_target->pb_refcnt) <= 1,
2053 "Still waiting for ping data MD to unlink\n");
2055 /* Drop ref set by lnet_ping_buffer_alloc() */
2056 lnet_ping_buffer_decref(the_lnet.ln_push_target);
2057 the_lnet.ln_push_target = NULL;
2058 the_lnet.ln_push_target_nnis = 0;
2060 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2061 lnet_assert_handler_unused(the_lnet.ln_push_target_handler);
2062 the_lnet.ln_push_target_handler = NULL;
2066 lnet_ni_tq_credits(struct lnet_ni *ni)
2070 LASSERT(ni->ni_ncpts >= 1);
2072 if (ni->ni_ncpts == 1)
2073 return ni->ni_net->net_tunables.lct_max_tx_credits;
2075 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2076 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2077 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2083 lnet_ni_unlink_locked(struct lnet_ni *ni)
2085 /* move it to zombie list and nobody can find it anymore */
2086 LASSERT(!list_empty(&ni->ni_netlist));
2087 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2088 lnet_ni_decref_locked(ni, 0);
2092 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2097 struct list_head *zombie_list = &net->net_ni_zombie;
2100 * Now wait for the NIs I just nuked to show up on the zombie
2101 * list and shut them down in guaranteed thread context
2104 while (!list_empty(zombie_list)) {
2108 ni = list_entry(zombie_list->next,
2109 struct lnet_ni, ni_netlist);
2110 list_del_init(&ni->ni_netlist);
2111 /* the ni should be in deleting state. If it's not it's
2113 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2114 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2117 /* still busy, add it back to zombie list */
2118 list_add(&ni->ni_netlist, zombie_list);
2122 if (!list_empty(&ni->ni_netlist)) {
2123 /* Unlock mutex while waiting to allow other
2124 * threads to read the LNet state and fall through
2127 lnet_net_unlock(LNET_LOCK_EX);
2128 mutex_unlock(&the_lnet.ln_api_mutex);
2131 if ((i & (-i)) == i) {
2133 "Waiting for zombie LNI %s\n",
2134 libcfs_nid2str(ni->ni_nid));
2136 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2138 mutex_lock(&the_lnet.ln_api_mutex);
2139 lnet_net_lock(LNET_LOCK_EX);
2143 lnet_net_unlock(LNET_LOCK_EX);
2145 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2147 LASSERT(!in_interrupt());
2148 /* Holding the mutex makes it safe for lnd_shutdown
2149 * to call module_put(). Module unload cannot finish
2150 * until lnet_unregister_lnd() completes, and that
2151 * requires the mutex.
2153 mutex_lock(&the_lnet.ln_lnd_mutex);
2154 (net->net_lnd->lnd_shutdown)(ni);
2155 mutex_unlock(&the_lnet.ln_lnd_mutex);
2158 CDEBUG(D_LNI, "Removed LNI %s\n",
2159 libcfs_nid2str(ni->ni_nid));
2163 lnet_net_lock(LNET_LOCK_EX);
2167 /* shutdown down the NI and release refcount */
2169 lnet_shutdown_lndni(struct lnet_ni *ni)
2172 struct lnet_net *net = ni->ni_net;
2174 lnet_net_lock(LNET_LOCK_EX);
2176 ni->ni_state = LNET_NI_STATE_DELETING;
2178 lnet_ni_unlink_locked(ni);
2179 lnet_incr_dlc_seq();
2180 lnet_net_unlock(LNET_LOCK_EX);
2182 /* clear messages for this NI on the lazy portal */
2183 for (i = 0; i < the_lnet.ln_nportals; i++)
2184 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2186 lnet_net_lock(LNET_LOCK_EX);
2187 lnet_clear_zombies_nis_locked(net);
2188 lnet_net_unlock(LNET_LOCK_EX);
2192 lnet_shutdown_lndnet(struct lnet_net *net)
2196 lnet_net_lock(LNET_LOCK_EX);
2198 list_del_init(&net->net_list);
2200 while (!list_empty(&net->net_ni_list)) {
2201 ni = list_entry(net->net_ni_list.next,
2202 struct lnet_ni, ni_netlist);
2203 lnet_net_unlock(LNET_LOCK_EX);
2204 lnet_shutdown_lndni(ni);
2205 lnet_net_lock(LNET_LOCK_EX);
2208 lnet_net_unlock(LNET_LOCK_EX);
2210 /* Do peer table cleanup for this net */
2211 lnet_peer_tables_cleanup(net);
2217 lnet_shutdown_lndnets(void)
2219 struct lnet_net *net;
2221 struct lnet_msg *msg, *tmp;
2223 /* NB called holding the global mutex */
2225 /* All quiet on the API front */
2226 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2227 LASSERT(the_lnet.ln_refcount == 0);
2229 lnet_net_lock(LNET_LOCK_EX);
2230 the_lnet.ln_state = LNET_STATE_STOPPING;
2233 * move the nets to the zombie list to avoid them being
2234 * picked up for new work. LONET is also included in the
2235 * Nets that will be moved to the zombie list
2237 list_splice_init(&the_lnet.ln_nets, &the_lnet.ln_net_zombie);
2239 /* Drop the cached loopback Net. */
2240 if (the_lnet.ln_loni != NULL) {
2241 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2242 the_lnet.ln_loni = NULL;
2244 lnet_net_unlock(LNET_LOCK_EX);
2246 /* iterate through the net zombie list and delete each net */
2247 while (!list_empty(&the_lnet.ln_net_zombie)) {
2248 net = list_entry(the_lnet.ln_net_zombie.next,
2249 struct lnet_net, net_list);
2250 lnet_shutdown_lndnet(net);
2253 spin_lock(&the_lnet.ln_msg_resend_lock);
2254 list_splice(&the_lnet.ln_msg_resend, &resend);
2255 spin_unlock(&the_lnet.ln_msg_resend_lock);
2257 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2258 list_del_init(&msg->msg_list);
2259 msg->msg_no_resend = true;
2260 lnet_finalize(msg, -ECANCELED);
2263 lnet_net_lock(LNET_LOCK_EX);
2264 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2265 lnet_net_unlock(LNET_LOCK_EX);
2269 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2272 struct lnet_tx_queue *tq;
2274 struct lnet_net *net = ni->ni_net;
2276 mutex_lock(&the_lnet.ln_lnd_mutex);
2279 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2280 ni->ni_lnd_tunables_set = true;
2283 rc = (net->net_lnd->lnd_startup)(ni);
2285 mutex_unlock(&the_lnet.ln_lnd_mutex);
2288 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2289 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2294 ni->ni_state = LNET_NI_STATE_ACTIVE;
2297 /* We keep a reference on the loopback net through the loopback NI */
2298 if (net->net_lnd->lnd_type == LOLND) {
2300 LASSERT(the_lnet.ln_loni == NULL);
2301 the_lnet.ln_loni = ni;
2302 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2303 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2304 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2305 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2309 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2310 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2311 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2312 libcfs_lnd2str(net->net_lnd->lnd_type),
2313 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2315 /* shutdown the NI since if we get here then it must've already
2318 lnet_shutdown_lndni(ni);
2322 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2323 tq->tq_credits_min =
2324 tq->tq_credits_max =
2325 tq->tq_credits = lnet_ni_tq_credits(ni);
2328 atomic_set(&ni->ni_tx_credits,
2329 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2330 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2332 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2333 libcfs_nid2str(ni->ni_nid),
2334 ni->ni_net->net_tunables.lct_peer_tx_credits,
2335 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2336 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2337 ni->ni_net->net_tunables.lct_peer_timeout);
2346 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2349 struct lnet_net *net_l = NULL;
2350 LIST_HEAD(local_ni_list);
2354 const struct lnet_lnd *lnd;
2356 net->net_tunables.lct_peer_timeout;
2358 net->net_tunables.lct_max_tx_credits;
2359 int peerrtrcredits =
2360 net->net_tunables.lct_peer_rtr_credits;
2363 * make sure that this net is unique. If it isn't then
2364 * we are adding interfaces to an already existing network, and
2365 * 'net' is just a convenient way to pass in the list.
2366 * if it is unique we need to find the LND and load it if
2369 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2370 lnd_type = LNET_NETTYP(net->net_id);
2372 mutex_lock(&the_lnet.ln_lnd_mutex);
2373 lnd = lnet_find_lnd_by_type(lnd_type);
2376 mutex_unlock(&the_lnet.ln_lnd_mutex);
2377 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2378 mutex_lock(&the_lnet.ln_lnd_mutex);
2380 lnd = lnet_find_lnd_by_type(lnd_type);
2382 mutex_unlock(&the_lnet.ln_lnd_mutex);
2383 CERROR("Can't load LND %s, module %s, rc=%d\n",
2384 libcfs_lnd2str(lnd_type),
2385 libcfs_lnd2modname(lnd_type), rc);
2386 #ifndef HAVE_MODULE_LOADING_SUPPORT
2387 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2388 "compiled with kernel module "
2389 "loading support.");
2398 mutex_unlock(&the_lnet.ln_lnd_mutex);
2404 * net_l: if the network being added is unique then net_l
2405 * will point to that network
2406 * if the network being added is not unique then
2407 * net_l points to the existing network.
2409 * When we enter the loop below, we'll pick NIs off he
2410 * network beign added and start them up, then add them to
2411 * a local ni list. Once we've successfully started all
2412 * the NIs then we join the local NI list (of started up
2413 * networks) with the net_l->net_ni_list, which should
2414 * point to the correct network to add the new ni list to
2416 * If any of the new NIs fail to start up, then we want to
2417 * iterate through the local ni list, which should include
2418 * any NIs which were successfully started up, and shut
2421 * After than we want to delete the network being added,
2422 * to avoid a memory leak.
2426 * When a network uses TCP bonding then all its interfaces
2427 * must be specified when the network is first defined: the
2428 * TCP bonding code doesn't allow for interfaces to be added
2431 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2432 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2437 while (!list_empty(&net->net_ni_added)) {
2438 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2440 list_del_init(&ni->ni_netlist);
2442 /* make sure that the the NI we're about to start
2443 * up is actually unique. if it's not fail. */
2444 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2445 ni->ni_interfaces[0])) {
2450 /* adjust the pointer the parent network, just in case it
2451 * the net is a duplicate */
2454 rc = lnet_startup_lndni(ni, tun);
2460 list_add_tail(&ni->ni_netlist, &local_ni_list);
2465 lnet_net_lock(LNET_LOCK_EX);
2466 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2467 lnet_incr_dlc_seq();
2468 lnet_net_unlock(LNET_LOCK_EX);
2470 /* if the network is not unique then we don't want to keep
2471 * it around after we're done. Free it. Otherwise add that
2472 * net to the global the_lnet.ln_nets */
2473 if (net_l != net && net_l != NULL) {
2475 * TODO - note. currently the tunables can not be updated
2481 * restore tunables after it has been overwitten by the
2484 if (peer_timeout != -1)
2485 net->net_tunables.lct_peer_timeout = peer_timeout;
2486 if (maxtxcredits != -1)
2487 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2488 if (peerrtrcredits != -1)
2489 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2491 lnet_net_lock(LNET_LOCK_EX);
2492 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2493 lnet_net_unlock(LNET_LOCK_EX);
2496 /* update net count */
2497 lnet_current_net_count = lnet_get_net_count();
2503 * shutdown the new NIs that are being started up
2504 * free the NET being started
2506 while (!list_empty(&local_ni_list)) {
2507 ni = list_entry(local_ni_list.next, struct lnet_ni,
2510 lnet_shutdown_lndni(ni);
2520 lnet_startup_lndnets(struct list_head *netlist)
2522 struct lnet_net *net;
2527 * Change to running state before bringing up the LNDs. This
2528 * allows lnet_shutdown_lndnets() to assert that we've passed
2531 lnet_net_lock(LNET_LOCK_EX);
2532 the_lnet.ln_state = LNET_STATE_RUNNING;
2533 lnet_net_unlock(LNET_LOCK_EX);
2535 while (!list_empty(netlist)) {
2536 net = list_entry(netlist->next, struct lnet_net, net_list);
2537 list_del_init(&net->net_list);
2539 rc = lnet_startup_lndnet(net, NULL);
2549 lnet_shutdown_lndnets();
2555 * Initialize LNet library.
2557 * Automatically called at module loading time. Caller has to call
2558 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2559 * latter returned 0. It must be called exactly once.
2561 * \retval 0 on success
2562 * \retval -ve on failures.
2564 int lnet_lib_init(void)
2568 lnet_assert_wire_constants();
2570 /* refer to global cfs_cpt_table for now */
2571 the_lnet.ln_cpt_table = cfs_cpt_tab;
2572 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_tab);
2574 LASSERT(the_lnet.ln_cpt_number > 0);
2575 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2576 /* we are under risk of consuming all lh_cookie */
2577 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2578 "please change setting of CPT-table and retry\n",
2579 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2583 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2584 the_lnet.ln_cpt_bits++;
2586 rc = lnet_create_locks();
2588 CERROR("Can't create LNet global locks: %d\n", rc);
2592 the_lnet.ln_refcount = 0;
2593 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2594 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2596 /* The hash table size is the number of bits it takes to express the set
2597 * ln_num_routes, minus 1 (better to under estimate than over so we
2598 * don't waste memory). */
2599 if (rnet_htable_size <= 0)
2600 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2601 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2602 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2603 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2604 order_base_2(rnet_htable_size) - 1);
2606 /* All LNDs apart from the LOLND are in separate modules. They
2607 * register themselves when their module loads, and unregister
2608 * themselves when their module is unloaded. */
2609 lnet_register_lnd(&the_lolnd);
2614 * Finalize LNet library.
2616 * \pre lnet_lib_init() called with success.
2617 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2619 * As this happens at module-unload, all lnds must already be unloaded,
2620 * so they must already be unregistered.
2622 void lnet_lib_exit(void)
2626 LASSERT(the_lnet.ln_refcount == 0);
2627 lnet_unregister_lnd(&the_lolnd);
2628 for (i = 0; i < NUM_LNDS; i++)
2629 LASSERT(!the_lnet.ln_lnds[i]);
2630 lnet_destroy_locks();
2634 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2636 * Users must call this function at least once before any other functions.
2637 * For each successful call there must be a corresponding call to
2638 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2641 * The PID used by LNet may be different from the one requested.
2644 * \param requested_pid PID requested by the caller.
2646 * \return >= 0 on success, and < 0 error code on failures.
2649 LNetNIInit(lnet_pid_t requested_pid)
2651 int im_a_router = 0;
2654 struct lnet_ping_buffer *pbuf;
2655 struct lnet_handle_md ping_mdh;
2656 LIST_HEAD(net_head);
2657 struct lnet_net *net;
2659 mutex_lock(&the_lnet.ln_api_mutex);
2661 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2663 if (the_lnet.ln_refcount > 0) {
2664 rc = the_lnet.ln_refcount++;
2665 mutex_unlock(&the_lnet.ln_api_mutex);
2669 rc = lnet_prepare(requested_pid);
2671 mutex_unlock(&the_lnet.ln_api_mutex);
2675 /* create a network for Loopback network */
2676 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2679 goto err_empty_list;
2682 /* Add in the loopback NI */
2683 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2685 goto err_empty_list;
2688 if (use_tcp_bonding)
2689 CWARN("'use_tcp_bonding' option has been deprecated. See LU-13641\n");
2691 /* If LNet is being initialized via DLC it is possible
2692 * that the user requests not to load module parameters (ones which
2693 * are supported by DLC) on initialization. Therefore, make sure not
2694 * to load networks, routes and forwarding from module parameters
2695 * in this case. On cleanup in case of failure only clean up
2696 * routes if it has been loaded */
2697 if (!the_lnet.ln_nis_from_mod_params) {
2698 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2701 goto err_empty_list;
2704 ni_count = lnet_startup_lndnets(&net_head);
2707 goto err_empty_list;
2710 if (!the_lnet.ln_nis_from_mod_params) {
2711 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2713 goto err_shutdown_lndnis;
2715 rc = lnet_rtrpools_alloc(im_a_router);
2717 goto err_destroy_routes;
2720 rc = lnet_acceptor_start();
2722 goto err_destroy_routes;
2724 the_lnet.ln_refcount = 1;
2725 /* Now I may use my own API functions... */
2727 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2729 goto err_acceptor_stop;
2731 lnet_ping_target_update(pbuf, ping_mdh);
2733 the_lnet.ln_mt_handler = lnet_mt_event_handler;
2735 rc = lnet_push_target_init();
2739 rc = lnet_peer_discovery_start();
2741 goto err_destroy_push_target;
2743 rc = lnet_monitor_thr_start();
2745 goto err_stop_discovery_thr;
2748 lnet_router_debugfs_init();
2750 mutex_unlock(&the_lnet.ln_api_mutex);
2752 complete_all(&the_lnet.ln_started);
2754 /* wait for all routers to start */
2755 lnet_wait_router_start();
2759 err_stop_discovery_thr:
2760 lnet_peer_discovery_stop();
2761 err_destroy_push_target:
2762 lnet_push_target_fini();
2764 lnet_ping_target_fini();
2766 the_lnet.ln_refcount = 0;
2767 lnet_acceptor_stop();
2769 if (!the_lnet.ln_nis_from_mod_params)
2770 lnet_destroy_routes();
2771 err_shutdown_lndnis:
2772 lnet_shutdown_lndnets();
2776 mutex_unlock(&the_lnet.ln_api_mutex);
2777 while (!list_empty(&net_head)) {
2778 struct lnet_net *net;
2780 net = list_entry(net_head.next, struct lnet_net, net_list);
2781 list_del_init(&net->net_list);
2786 EXPORT_SYMBOL(LNetNIInit);
2789 * Stop LNet interfaces, routing, and forwarding.
2791 * Users must call this function once for each successful call to LNetNIInit().
2792 * Once the LNetNIFini() operation has been started, the results of pending
2793 * API operations are undefined.
2795 * \return always 0 for current implementation.
2800 mutex_lock(&the_lnet.ln_api_mutex);
2802 LASSERT(the_lnet.ln_refcount > 0);
2804 if (the_lnet.ln_refcount != 1) {
2805 the_lnet.ln_refcount--;
2807 LASSERT(!the_lnet.ln_niinit_self);
2811 lnet_router_debugfs_fini();
2812 lnet_monitor_thr_stop();
2813 lnet_peer_discovery_stop();
2814 lnet_push_target_fini();
2815 lnet_ping_target_fini();
2817 /* Teardown fns that use my own API functions BEFORE here */
2818 the_lnet.ln_refcount = 0;
2820 lnet_acceptor_stop();
2821 lnet_destroy_routes();
2822 lnet_shutdown_lndnets();
2826 mutex_unlock(&the_lnet.ln_api_mutex);
2829 EXPORT_SYMBOL(LNetNIFini);
2832 * Grabs the ni data from the ni structure and fills the out
2835 * \param[in] ni network interface structure
2836 * \param[out] cfg_ni NI config information
2837 * \param[out] tun network and LND tunables
2840 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2841 struct lnet_ioctl_config_lnd_tunables *tun,
2842 struct lnet_ioctl_element_stats *stats,
2845 size_t min_size = 0;
2848 if (!ni || !cfg_ni || !tun)
2851 if (ni->ni_interfaces[0] != NULL) {
2852 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2853 if (ni->ni_interfaces[i] != NULL) {
2854 strncpy(cfg_ni->lic_ni_intf[i],
2855 ni->ni_interfaces[i],
2856 sizeof(cfg_ni->lic_ni_intf[i]));
2861 cfg_ni->lic_nid = ni->ni_nid;
2862 if (ni->ni_nid == LNET_NID_LO_0)
2863 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2865 cfg_ni->lic_status = ni->ni_status->ns_status;
2866 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2867 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2869 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2872 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2873 LNET_STATS_TYPE_SEND);
2874 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2875 LNET_STATS_TYPE_RECV);
2876 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2877 LNET_STATS_TYPE_DROP);
2881 * tun->lt_tun will always be present, but in order to be
2882 * backwards compatible, we need to deal with the cases when
2883 * tun->lt_tun is smaller than what the kernel has, because it
2884 * comes from an older version of a userspace program, then we'll
2885 * need to copy as much information as we have available space.
2887 min_size = tun_size - sizeof(tun->lt_cmn);
2888 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2890 /* copy over the cpts */
2891 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2892 ni->ni_cpts == NULL) {
2893 for (i = 0; i < ni->ni_ncpts; i++)
2894 cfg_ni->lic_cpts[i] = i;
2897 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2898 i < LNET_MAX_SHOW_NUM_CPT;
2900 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2902 cfg_ni->lic_ncpts = ni->ni_ncpts;
2906 * NOTE: This is a legacy function left in the code to be backwards
2907 * compatible with older userspace programs. It should eventually be
2910 * Grabs the ni data from the ni structure and fills the out
2913 * \param[in] ni network interface structure
2914 * \param[out] config config information
2917 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2918 struct lnet_ioctl_config_data *config)
2920 struct lnet_ioctl_net_config *net_config;
2921 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2922 size_t min_size, tunable_size = 0;
2928 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2932 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2933 ARRAY_SIZE(net_config->ni_interfaces));
2935 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2936 if (!ni->ni_interfaces[i])
2939 strncpy(net_config->ni_interfaces[i],
2940 ni->ni_interfaces[i],
2941 sizeof(net_config->ni_interfaces[i]));
2944 config->cfg_nid = ni->ni_nid;
2945 config->cfg_config_u.cfg_net.net_peer_timeout =
2946 ni->ni_net->net_tunables.lct_peer_timeout;
2947 config->cfg_config_u.cfg_net.net_max_tx_credits =
2948 ni->ni_net->net_tunables.lct_max_tx_credits;
2949 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2950 ni->ni_net->net_tunables.lct_peer_tx_credits;
2951 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2952 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2954 if (ni->ni_nid == LNET_NID_LO_0)
2955 net_config->ni_status = LNET_NI_STATUS_UP;
2957 net_config->ni_status = ni->ni_status->ns_status;
2960 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2962 for (i = 0; i < num_cpts; i++)
2963 net_config->ni_cpts[i] = ni->ni_cpts[i];
2965 config->cfg_ncpts = num_cpts;
2969 * See if user land tools sent in a newer and larger version
2970 * of struct lnet_tunables than what the kernel uses.
2972 min_size = sizeof(*config) + sizeof(*net_config);
2974 if (config->cfg_hdr.ioc_len > min_size)
2975 tunable_size = config->cfg_hdr.ioc_len - min_size;
2977 /* Don't copy too much data to user space */
2978 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2979 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2981 if (lnd_cfg && min_size) {
2982 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2983 config->cfg_config_u.cfg_net.net_interface_count = 1;
2985 /* Tell user land that kernel side has less data */
2986 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2987 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2988 config->cfg_hdr.ioc_len -= min_size;
2994 lnet_get_ni_idx_locked(int idx)
2997 struct lnet_net *net;
2999 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3000 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3010 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
3013 struct lnet_net *net = mynet;
3016 * It is possible that the net has been cleaned out while there is
3017 * a message being sent. This function accessed the net without
3018 * checking if the list is empty
3022 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
3024 if (list_empty(&net->net_ni_list))
3026 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3032 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
3033 /* if you reached the end of the ni list and the net is
3034 * specified, then there are no more nis in that net */
3038 /* we reached the end of this net ni list. move to the
3040 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
3041 /* no more nets and no more NIs. */
3044 /* get the next net */
3045 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
3047 if (list_empty(&net->net_ni_list))
3049 /* get the ni on it */
3050 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3056 if (list_empty(&prev->ni_netlist))
3059 /* there are more nis left */
3060 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3066 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3071 int idx = config->cfg_count;
3073 cpt = lnet_net_lock_current();
3075 ni = lnet_get_ni_idx_locked(idx);
3080 lnet_fill_ni_info_legacy(ni, config);
3084 lnet_net_unlock(cpt);
3089 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3090 struct lnet_ioctl_config_lnd_tunables *tun,
3091 struct lnet_ioctl_element_stats *stats,
3098 if (!cfg_ni || !tun || !stats)
3101 cpt = lnet_net_lock_current();
3103 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3108 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3112 lnet_net_unlock(cpt);
3116 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3125 cpt = lnet_net_lock_current();
3127 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3130 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3134 lnet_net_unlock(cpt);
3139 static int lnet_add_net_common(struct lnet_net *net,
3140 struct lnet_ioctl_config_lnd_tunables *tun)
3143 struct lnet_ping_buffer *pbuf;
3144 struct lnet_handle_md ping_mdh;
3146 struct lnet_remotenet *rnet;
3149 lnet_net_lock(LNET_LOCK_EX);
3150 rnet = lnet_find_rnet_locked(net->net_id);
3151 lnet_net_unlock(LNET_LOCK_EX);
3153 * make sure that the net added doesn't invalidate the current
3154 * configuration LNet is keeping
3157 CERROR("Adding net %s will invalidate routing configuration\n",
3158 libcfs_net2str(net->net_id));
3164 * make sure you calculate the correct number of slots in the ping
3165 * buffer. Since the ping info is a flattened list of all the NIs,
3166 * we should allocate enough slots to accomodate the number of NIs
3167 * which will be added.
3169 * since ni hasn't been configured yet, use
3170 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3172 net_ni_count = lnet_get_net_ni_count_pre(net);
3174 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3175 net_ni_count + lnet_get_ni_count(),
3183 memcpy(&net->net_tunables,
3184 &tun->lt_cmn, sizeof(net->net_tunables));
3186 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3188 net_id = net->net_id;
3190 rc = lnet_startup_lndnet(net,
3191 (tun) ? &tun->lt_tun : NULL);
3195 lnet_net_lock(LNET_LOCK_EX);
3196 net = lnet_get_net_locked(net_id);
3197 lnet_net_unlock(LNET_LOCK_EX);
3202 * Start the acceptor thread if this is the first network
3203 * being added that requires the thread.
3205 if (net->net_lnd->lnd_accept) {
3206 rc = lnet_acceptor_start();
3208 /* shutdown the net that we just started */
3209 CERROR("Failed to start up acceptor thread\n");
3210 lnet_shutdown_lndnet(net);
3215 lnet_net_lock(LNET_LOCK_EX);
3216 lnet_peer_net_added(net);
3217 lnet_net_unlock(LNET_LOCK_EX);
3219 lnet_ping_target_update(pbuf, ping_mdh);
3224 lnet_ping_md_unlink(pbuf, &ping_mdh);
3225 lnet_ping_buffer_decref(pbuf);
3230 lnet_set_tune_defaults(struct lnet_ioctl_config_lnd_tunables *tun)
3233 if (!tun->lt_cmn.lct_peer_timeout)
3234 tun->lt_cmn.lct_peer_timeout = DEFAULT_PEER_TIMEOUT;
3235 if (!tun->lt_cmn.lct_peer_tx_credits)
3236 tun->lt_cmn.lct_peer_tx_credits = DEFAULT_PEER_CREDITS;
3237 if (!tun->lt_cmn.lct_max_tx_credits)
3238 tun->lt_cmn.lct_max_tx_credits = DEFAULT_CREDITS;
3242 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3243 struct lnet_ioctl_config_lnd_tunables *tun)
3245 struct lnet_net *net;
3248 LIST_HEAD(net_head);
3250 rc = lnet_parse_ip2nets(&nets, ip2nets);
3254 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3258 lnet_set_tune_defaults(tun);
3260 mutex_lock(&the_lnet.ln_api_mutex);
3261 while (!list_empty(&net_head)) {
3262 net = list_entry(net_head.next, struct lnet_net, net_list);
3263 list_del_init(&net->net_list);
3264 rc = lnet_add_net_common(net, tun);
3270 mutex_unlock(&the_lnet.ln_api_mutex);
3272 while (!list_empty(&net_head)) {
3273 net = list_entry(net_head.next, struct lnet_net, net_list);
3274 list_del_init(&net->net_list);
3280 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3282 struct lnet_net *net;
3284 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3286 __u32 net_id, lnd_type;
3288 /* get the tunables if they are available */
3289 if (conf->lic_cfg_hdr.ioc_len >=
3290 sizeof(*conf) + sizeof(*tun))
3291 tun = (struct lnet_ioctl_config_lnd_tunables *)
3294 /* handle legacy ip2nets from DLC */
3295 if (conf->lic_legacy_ip2nets[0] != '\0')
3296 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3299 net_id = LNET_NIDNET(conf->lic_nid);
3300 lnd_type = LNET_NETTYP(net_id);
3302 if (!libcfs_isknown_lnd(lnd_type)) {
3303 CERROR("No valid net and lnd information provided\n");
3307 net = lnet_net_alloc(net_id, NULL);
3311 for (i = 0; i < conf->lic_ncpts; i++) {
3312 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3316 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3317 conf->lic_ni_intf[0]);
3321 lnet_set_tune_defaults(tun);
3323 mutex_lock(&the_lnet.ln_api_mutex);
3325 rc = lnet_add_net_common(net, tun);
3327 mutex_unlock(&the_lnet.ln_api_mutex);
3332 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3334 struct lnet_net *net;
3336 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3337 struct lnet_ping_buffer *pbuf;
3338 struct lnet_handle_md ping_mdh;
3343 /* don't allow userspace to shutdown the LOLND */
3344 if (LNET_NETTYP(net_id) == LOLND)
3347 mutex_lock(&the_lnet.ln_api_mutex);
3351 net = lnet_get_net_locked(net_id);
3353 CERROR("net %s not found\n",
3354 libcfs_net2str(net_id));
3359 addr = LNET_NIDADDR(conf->lic_nid);
3361 /* remove the entire net */
3362 net_count = lnet_get_net_ni_count_locked(net);
3366 /* create and link a new ping info, before removing the old one */
3367 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3368 lnet_get_ni_count() - net_count,
3371 goto unlock_api_mutex;
3373 lnet_shutdown_lndnet(net);
3375 lnet_acceptor_stop();
3377 lnet_ping_target_update(pbuf, ping_mdh);
3379 goto unlock_api_mutex;
3382 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3384 CERROR("nid %s not found\n",
3385 libcfs_nid2str(conf->lic_nid));
3390 net_count = lnet_get_net_ni_count_locked(net);
3394 /* create and link a new ping info, before removing the old one */
3395 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3396 lnet_get_ni_count() - 1, false);
3398 goto unlock_api_mutex;
3400 lnet_shutdown_lndni(ni);
3402 lnet_acceptor_stop();
3404 lnet_ping_target_update(pbuf, ping_mdh);
3406 /* check if the net is empty and remove it if it is */
3408 lnet_shutdown_lndnet(net);
3410 goto unlock_api_mutex;
3415 mutex_unlock(&the_lnet.ln_api_mutex);
3421 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3422 * They are only expected to be called for unique networks.
3423 * That can be as a result of older DLC library
3424 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3427 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3429 struct lnet_net *net;
3430 LIST_HEAD(net_head);
3432 struct lnet_ioctl_config_lnd_tunables tun;
3433 const char *nets = conf->cfg_config_u.cfg_net.net_intf;
3435 /* Create a net/ni structures for the network string */
3436 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3438 return rc == 0 ? -EINVAL : rc;
3440 mutex_lock(&the_lnet.ln_api_mutex);
3443 rc = -EINVAL; /* only add one network per call */
3444 goto out_unlock_clean;
3447 net = list_entry(net_head.next, struct lnet_net, net_list);
3448 list_del_init(&net->net_list);
3450 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3452 memset(&tun, 0, sizeof(tun));
3454 tun.lt_cmn.lct_peer_timeout =
3455 (!conf->cfg_config_u.cfg_net.net_peer_timeout) ? DEFAULT_PEER_TIMEOUT :
3456 conf->cfg_config_u.cfg_net.net_peer_timeout;
3457 tun.lt_cmn.lct_peer_tx_credits =
3458 (!conf->cfg_config_u.cfg_net.net_peer_tx_credits) ? DEFAULT_PEER_CREDITS :
3459 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3460 tun.lt_cmn.lct_peer_rtr_credits =
3461 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3462 tun.lt_cmn.lct_max_tx_credits =
3463 (!conf->cfg_config_u.cfg_net.net_max_tx_credits) ? DEFAULT_CREDITS :
3464 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3466 rc = lnet_add_net_common(net, &tun);
3469 mutex_unlock(&the_lnet.ln_api_mutex);
3470 while (!list_empty(&net_head)) {
3471 /* net_head list is empty in success case */
3472 net = list_entry(net_head.next, struct lnet_net, net_list);
3473 list_del_init(&net->net_list);
3480 lnet_dyn_del_net(__u32 net_id)
3482 struct lnet_net *net;
3483 struct lnet_ping_buffer *pbuf;
3484 struct lnet_handle_md ping_mdh;
3488 /* don't allow userspace to shutdown the LOLND */
3489 if (LNET_NETTYP(net_id) == LOLND)
3492 mutex_lock(&the_lnet.ln_api_mutex);
3496 net = lnet_get_net_locked(net_id);
3503 net_ni_count = lnet_get_net_ni_count_locked(net);
3507 /* create and link a new ping info, before removing the old one */
3508 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3509 lnet_get_ni_count() - net_ni_count, false);
3513 lnet_shutdown_lndnet(net);
3515 lnet_acceptor_stop();
3517 lnet_ping_target_update(pbuf, ping_mdh);
3520 mutex_unlock(&the_lnet.ln_api_mutex);
3525 void lnet_incr_dlc_seq(void)
3527 atomic_inc(&lnet_dlc_seq_no);
3530 __u32 lnet_get_dlc_seq_locked(void)
3532 return atomic_read(&lnet_dlc_seq_no);
3536 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3538 struct lnet_net *net;
3541 lnet_net_lock(LNET_LOCK_EX);
3542 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3543 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3544 if (ni->ni_nid == nid || all) {
3545 atomic_set(&ni->ni_healthv, value);
3546 if (list_empty(&ni->ni_recovery) &&
3547 value < LNET_MAX_HEALTH_VALUE) {
3548 CERROR("manually adding local NI %s to recovery\n",
3549 libcfs_nid2str(ni->ni_nid));
3550 list_add_tail(&ni->ni_recovery,
3551 &the_lnet.ln_mt_localNIRecovq);
3552 lnet_ni_addref_locked(ni, 0);
3555 lnet_net_unlock(LNET_LOCK_EX);
3561 lnet_net_unlock(LNET_LOCK_EX);
3565 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3569 lnet_nid_t nid = stats->hlni_nid;
3571 cpt = lnet_net_lock_current();
3572 ni = lnet_nid2ni_locked(nid, cpt);
3579 stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3580 stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3581 stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3582 stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3583 stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3584 stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3585 stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3588 lnet_net_unlock(cpt);
3594 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3599 lnet_net_lock(LNET_LOCK_EX);
3600 list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3601 list->rlst_nid_array[i] = ni->ni_nid;
3603 if (i >= LNET_MAX_SHOW_NUM_NID)
3606 lnet_net_unlock(LNET_LOCK_EX);
3607 list->rlst_num_nids = i;
3613 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3615 struct lnet_peer_ni *lpni;
3618 lnet_net_lock(LNET_LOCK_EX);
3619 list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3620 list->rlst_nid_array[i] = lpni->lpni_nid;
3622 if (i >= LNET_MAX_SHOW_NUM_NID)
3625 lnet_net_unlock(LNET_LOCK_EX);
3626 list->rlst_num_nids = i;
3632 * LNet ioctl handler.
3636 LNetCtl(unsigned int cmd, void *arg)
3638 struct libcfs_ioctl_data *data = arg;
3639 struct lnet_ioctl_config_data *config;
3640 struct lnet_process_id id = {0};
3644 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3645 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3648 case IOC_LIBCFS_GET_NI:
3649 rc = LNetGetId(data->ioc_count, &id);
3650 data->ioc_nid = id.nid;
3653 case IOC_LIBCFS_FAIL_NID:
3654 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3656 case IOC_LIBCFS_ADD_ROUTE: {
3657 /* default router sensitivity to 1 */
3658 unsigned int sensitivity = 1;
3661 if (config->cfg_hdr.ioc_len < sizeof(*config))
3664 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3666 config->cfg_config_u.cfg_route.rtr_sensitivity;
3669 mutex_lock(&the_lnet.ln_api_mutex);
3670 rc = lnet_add_route(config->cfg_net,
3671 config->cfg_config_u.cfg_route.rtr_hop,
3673 config->cfg_config_u.cfg_route.
3674 rtr_priority, sensitivity);
3675 mutex_unlock(&the_lnet.ln_api_mutex);
3679 case IOC_LIBCFS_DEL_ROUTE:
3682 if (config->cfg_hdr.ioc_len < sizeof(*config))
3685 mutex_lock(&the_lnet.ln_api_mutex);
3686 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3687 mutex_unlock(&the_lnet.ln_api_mutex);
3690 case IOC_LIBCFS_GET_ROUTE:
3693 if (config->cfg_hdr.ioc_len < sizeof(*config))
3696 mutex_lock(&the_lnet.ln_api_mutex);
3697 rc = lnet_get_route(config->cfg_count,
3699 &config->cfg_config_u.cfg_route.rtr_hop,
3701 &config->cfg_config_u.cfg_route.rtr_flags,
3702 &config->cfg_config_u.cfg_route.
3704 &config->cfg_config_u.cfg_route.
3706 mutex_unlock(&the_lnet.ln_api_mutex);
3709 case IOC_LIBCFS_GET_LOCAL_NI: {
3710 struct lnet_ioctl_config_ni *cfg_ni;
3711 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3712 struct lnet_ioctl_element_stats *stats;
3717 /* get the tunables if they are available */
3718 if (cfg_ni->lic_cfg_hdr.ioc_len <
3719 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3722 stats = (struct lnet_ioctl_element_stats *)
3724 tun = (struct lnet_ioctl_config_lnd_tunables *)
3725 (cfg_ni->lic_bulk + sizeof(*stats));
3727 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3730 mutex_lock(&the_lnet.ln_api_mutex);
3731 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3732 mutex_unlock(&the_lnet.ln_api_mutex);
3736 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3737 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3739 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3742 mutex_lock(&the_lnet.ln_api_mutex);
3743 rc = lnet_get_ni_stats(msg_stats);
3744 mutex_unlock(&the_lnet.ln_api_mutex);
3749 case IOC_LIBCFS_GET_NET: {
3750 size_t total = sizeof(*config) +
3751 sizeof(struct lnet_ioctl_net_config);
3754 if (config->cfg_hdr.ioc_len < total)
3757 mutex_lock(&the_lnet.ln_api_mutex);
3758 rc = lnet_get_net_config(config);
3759 mutex_unlock(&the_lnet.ln_api_mutex);
3763 case IOC_LIBCFS_GET_LNET_STATS:
3765 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3767 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3770 mutex_lock(&the_lnet.ln_api_mutex);
3771 rc = lnet_counters_get(&lnet_stats->st_cntrs);
3772 mutex_unlock(&the_lnet.ln_api_mutex);
3776 case IOC_LIBCFS_CONFIG_RTR:
3779 if (config->cfg_hdr.ioc_len < sizeof(*config))
3782 mutex_lock(&the_lnet.ln_api_mutex);
3783 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3784 rc = lnet_rtrpools_enable();
3785 mutex_unlock(&the_lnet.ln_api_mutex);
3788 lnet_rtrpools_disable();
3789 mutex_unlock(&the_lnet.ln_api_mutex);
3792 case IOC_LIBCFS_ADD_BUF:
3795 if (config->cfg_hdr.ioc_len < sizeof(*config))
3798 mutex_lock(&the_lnet.ln_api_mutex);
3799 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3801 config->cfg_config_u.cfg_buffers.
3803 config->cfg_config_u.cfg_buffers.
3805 mutex_unlock(&the_lnet.ln_api_mutex);
3808 case IOC_LIBCFS_SET_NUMA_RANGE: {
3809 struct lnet_ioctl_set_value *numa;
3811 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3813 lnet_net_lock(LNET_LOCK_EX);
3814 lnet_numa_range = numa->sv_value;
3815 lnet_net_unlock(LNET_LOCK_EX);
3819 case IOC_LIBCFS_GET_NUMA_RANGE: {
3820 struct lnet_ioctl_set_value *numa;
3822 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3824 numa->sv_value = lnet_numa_range;
3828 case IOC_LIBCFS_GET_BUF: {
3829 struct lnet_ioctl_pool_cfg *pool_cfg;
3830 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3834 if (config->cfg_hdr.ioc_len < total)
3837 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3839 mutex_lock(&the_lnet.ln_api_mutex);
3840 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3841 mutex_unlock(&the_lnet.ln_api_mutex);
3845 case IOC_LIBCFS_GET_LOCAL_HSTATS: {
3846 struct lnet_ioctl_local_ni_hstats *stats = arg;
3848 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
3851 mutex_lock(&the_lnet.ln_api_mutex);
3852 rc = lnet_get_local_ni_hstats(stats);
3853 mutex_unlock(&the_lnet.ln_api_mutex);
3858 case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
3859 struct lnet_ioctl_recovery_list *list = arg;
3860 if (list->rlst_hdr.ioc_len < sizeof(*list))
3863 mutex_lock(&the_lnet.ln_api_mutex);
3864 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
3865 rc = lnet_get_local_ni_recovery_list(list);
3867 rc = lnet_get_peer_ni_recovery_list(list);
3868 mutex_unlock(&the_lnet.ln_api_mutex);
3872 case IOC_LIBCFS_ADD_PEER_NI: {
3873 struct lnet_ioctl_peer_cfg *cfg = arg;
3875 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3878 mutex_lock(&the_lnet.ln_api_mutex);
3879 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3882 mutex_unlock(&the_lnet.ln_api_mutex);
3886 case IOC_LIBCFS_DEL_PEER_NI: {
3887 struct lnet_ioctl_peer_cfg *cfg = arg;
3889 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3892 mutex_lock(&the_lnet.ln_api_mutex);
3893 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3894 cfg->prcfg_cfg_nid);
3895 mutex_unlock(&the_lnet.ln_api_mutex);
3899 case IOC_LIBCFS_GET_PEER_INFO: {
3900 struct lnet_ioctl_peer *peer_info = arg;
3902 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3905 mutex_lock(&the_lnet.ln_api_mutex);
3906 rc = lnet_get_peer_ni_info(
3907 peer_info->pr_count,
3909 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3910 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3911 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3912 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3913 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3914 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3915 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3916 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3917 mutex_unlock(&the_lnet.ln_api_mutex);
3921 case IOC_LIBCFS_GET_PEER_NI: {
3922 struct lnet_ioctl_peer_cfg *cfg = arg;
3924 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3927 mutex_lock(&the_lnet.ln_api_mutex);
3928 rc = lnet_get_peer_info(cfg,
3929 (void __user *)cfg->prcfg_bulk);
3930 mutex_unlock(&the_lnet.ln_api_mutex);
3934 case IOC_LIBCFS_GET_PEER_LIST: {
3935 struct lnet_ioctl_peer_cfg *cfg = arg;
3937 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3940 mutex_lock(&the_lnet.ln_api_mutex);
3941 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3942 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3943 mutex_unlock(&the_lnet.ln_api_mutex);
3947 case IOC_LIBCFS_SET_HEALHV: {
3948 struct lnet_ioctl_reset_health_cfg *cfg = arg;
3950 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
3952 if (cfg->rh_value < 0 ||
3953 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
3954 value = LNET_MAX_HEALTH_VALUE;
3956 value = cfg->rh_value;
3957 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
3958 value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
3959 "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
3960 mutex_lock(&the_lnet.ln_api_mutex);
3961 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
3962 lnet_ni_set_healthv(cfg->rh_nid, value,
3965 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
3967 mutex_unlock(&the_lnet.ln_api_mutex);
3971 case IOC_LIBCFS_NOTIFY_ROUTER: {
3972 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3974 /* The deadline passed in by the user should be some time in
3975 * seconds in the future since the UNIX epoch. We have to map
3976 * that deadline to the wall clock.
3978 deadline += ktime_get_seconds();
3979 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
3983 case IOC_LIBCFS_LNET_DIST:
3984 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3985 if (rc < 0 && rc != -EHOSTUNREACH)
3988 data->ioc_u32[0] = rc;
3991 case IOC_LIBCFS_TESTPROTOCOMPAT:
3992 the_lnet.ln_testprotocompat = data->ioc_flags;
3995 case IOC_LIBCFS_LNET_FAULT:
3996 return lnet_fault_ctl(data->ioc_flags, data);
3998 case IOC_LIBCFS_PING: {
3999 signed long timeout;
4001 id.nid = data->ioc_nid;
4002 id.pid = data->ioc_u32[0];
4004 /* If timeout is negative then set default of 3 minutes */
4005 if (((s32)data->ioc_u32[1] <= 0) ||
4006 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
4007 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
4009 timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
4011 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
4012 data->ioc_plen1 / sizeof(struct lnet_process_id));
4017 data->ioc_count = rc;
4021 case IOC_LIBCFS_PING_PEER: {
4022 struct lnet_ioctl_ping_data *ping = arg;
4023 struct lnet_peer *lp;
4024 signed long timeout;
4026 /* If timeout is negative then set default of 3 minutes */
4027 if (((s32)ping->op_param) <= 0 ||
4028 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
4029 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
4031 timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
4033 rc = lnet_ping(ping->ping_id, timeout,
4039 mutex_lock(&the_lnet.ln_api_mutex);
4040 lp = lnet_find_peer(ping->ping_id.nid);
4042 ping->ping_id.nid = lp->lp_primary_nid;
4043 ping->mr_info = lnet_peer_is_multi_rail(lp);
4044 lnet_peer_decref_locked(lp);
4046 mutex_unlock(&the_lnet.ln_api_mutex);
4048 ping->ping_count = rc;
4052 case IOC_LIBCFS_DISCOVER: {
4053 struct lnet_ioctl_ping_data *discover = arg;
4054 struct lnet_peer *lp;
4056 rc = lnet_discover(discover->ping_id, discover->op_param,
4058 discover->ping_count);
4062 mutex_lock(&the_lnet.ln_api_mutex);
4063 lp = lnet_find_peer(discover->ping_id.nid);
4065 discover->ping_id.nid = lp->lp_primary_nid;
4066 discover->mr_info = lnet_peer_is_multi_rail(lp);
4067 lnet_peer_decref_locked(lp);
4069 mutex_unlock(&the_lnet.ln_api_mutex);
4071 discover->ping_count = rc;
4076 ni = lnet_net2ni_addref(data->ioc_net);
4080 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
4083 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4090 EXPORT_SYMBOL(LNetCtl);
4092 void LNetDebugPeer(struct lnet_process_id id)
4094 lnet_debug_peer(id.nid);
4096 EXPORT_SYMBOL(LNetDebugPeer);
4099 * Determine if the specified peer \a nid is on the local node.
4101 * \param nid peer nid to check
4103 * \retval true If peer NID is on the local node.
4104 * \retval false If peer NID is not on the local node.
4106 bool LNetIsPeerLocal(lnet_nid_t nid)
4108 struct lnet_net *net;
4112 cpt = lnet_net_lock_current();
4113 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4114 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4115 if (ni->ni_nid == nid) {
4116 lnet_net_unlock(cpt);
4121 lnet_net_unlock(cpt);
4125 EXPORT_SYMBOL(LNetIsPeerLocal);
4128 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4129 * Note that all interfaces share a same PID, as requested by LNetNIInit().
4131 * \param index Index of the interface to look up.
4132 * \param id On successful return, this location will hold the
4133 * struct lnet_process_id ID of the interface.
4135 * \retval 0 If an interface exists at \a index.
4136 * \retval -ENOENT If no interface has been found.
4139 LNetGetId(unsigned int index, struct lnet_process_id *id)
4142 struct lnet_net *net;
4146 LASSERT(the_lnet.ln_refcount > 0);
4148 cpt = lnet_net_lock_current();
4150 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4151 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4155 id->nid = ni->ni_nid;
4156 id->pid = the_lnet.ln_pid;
4162 lnet_net_unlock(cpt);
4165 EXPORT_SYMBOL(LNetGetId);
4170 struct lnet_handle_md mdh;
4171 struct completion completion;
4175 lnet_ping_event_handler(struct lnet_event *event)
4177 struct ping_data *pd = event->md_user_ptr;
4179 CDEBUG(D_NET, "ping event (%d %d)%s\n",
4180 event->type, event->status,
4181 event->unlinked ? " unlinked" : "");
4183 if (event->status) {
4185 pd->rc = event->status;
4186 } else if (event->type == LNET_EVENT_REPLY) {
4188 pd->rc = event->mlength;
4190 if (event->unlinked)
4191 complete(&pd->completion);
4194 static int lnet_ping(struct lnet_process_id id, signed long timeout,
4195 struct lnet_process_id __user *ids, int n_ids)
4197 struct lnet_md md = { NULL };
4198 struct ping_data pd = { 0 };
4199 struct lnet_ping_buffer *pbuf;
4200 struct lnet_process_id tmpid;
4206 /* n_ids limit is arbitrary */
4207 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4211 * if the user buffer has more space than the lnet_interfaces_max
4212 * then only fill it up to lnet_interfaces_max
4214 if (n_ids > lnet_interfaces_max)
4215 n_ids = lnet_interfaces_max;
4217 if (id.pid == LNET_PID_ANY)
4218 id.pid = LNET_PID_LUSTRE;
4220 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4224 /* initialize md content */
4225 md.start = &pbuf->pb_info;
4226 md.length = LNET_PING_INFO_SIZE(n_ids);
4227 md.threshold = 2; /* GET/REPLY */
4229 md.options = LNET_MD_TRUNCATE;
4231 md.handler = lnet_ping_event_handler;
4233 init_completion(&pd.completion);
4235 rc = LNetMDBind(&md, LNET_UNLINK, &pd.mdh);
4237 CERROR("Can't bind MD: %d\n", rc);
4238 goto fail_ping_buffer_decref;
4241 rc = LNetGet(LNET_NID_ANY, pd.mdh, id,
4242 LNET_RESERVED_PORTAL,
4243 LNET_PROTO_PING_MATCHBITS, 0, false);
4246 /* Don't CERROR; this could be deliberate! */
4247 rc2 = LNetMDUnlink(pd.mdh);
4250 /* NB must wait for the UNLINK event below... */
4253 if (wait_for_completion_timeout(&pd.completion, timeout) == 0) {
4254 /* Ensure completion in finite time... */
4255 LNetMDUnlink(pd.mdh);
4256 wait_for_completion(&pd.completion);
4260 goto fail_ping_buffer_decref;
4264 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4266 rc = -EPROTO; /* if I can't parse... */
4269 CERROR("%s: ping info too short %d\n",
4270 libcfs_id2str(id), nob);
4271 goto fail_ping_buffer_decref;
4274 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4275 lnet_swap_pinginfo(pbuf);
4276 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4277 CERROR("%s: Unexpected magic %08x\n",
4278 libcfs_id2str(id), pbuf->pb_info.pi_magic);
4279 goto fail_ping_buffer_decref;
4282 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4283 CERROR("%s: ping w/o NI status: 0x%x\n",
4284 libcfs_id2str(id), pbuf->pb_info.pi_features);
4285 goto fail_ping_buffer_decref;
4288 if (nob < LNET_PING_INFO_SIZE(0)) {
4289 CERROR("%s: Short reply %d(%d min)\n",
4291 nob, (int)LNET_PING_INFO_SIZE(0));
4292 goto fail_ping_buffer_decref;
4295 if (pbuf->pb_info.pi_nnis < n_ids)
4296 n_ids = pbuf->pb_info.pi_nnis;
4298 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4299 CERROR("%s: Short reply %d(%d expected)\n",
4301 nob, (int)LNET_PING_INFO_SIZE(n_ids));
4302 goto fail_ping_buffer_decref;
4305 rc = -EFAULT; /* if I segv in copy_to_user()... */
4307 memset(&tmpid, 0, sizeof(tmpid));
4308 for (i = 0; i < n_ids; i++) {
4309 tmpid.pid = pbuf->pb_info.pi_pid;
4310 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4311 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4312 goto fail_ping_buffer_decref;
4314 rc = pbuf->pb_info.pi_nnis;
4316 fail_ping_buffer_decref:
4317 lnet_ping_buffer_decref(pbuf);
4322 lnet_discover(struct lnet_process_id id, __u32 force,
4323 struct lnet_process_id __user *ids, int n_ids)
4325 struct lnet_peer_ni *lpni;
4326 struct lnet_peer_ni *p;
4327 struct lnet_peer *lp;
4328 struct lnet_process_id *buf;
4334 id.nid == LNET_NID_ANY)
4337 if (id.pid == LNET_PID_ANY)
4338 id.pid = LNET_PID_LUSTRE;
4341 * If the user buffer has more space than the lnet_interfaces_max,
4342 * then only fill it up to lnet_interfaces_max.
4344 if (n_ids > lnet_interfaces_max)
4345 n_ids = lnet_interfaces_max;
4347 CFS_ALLOC_PTR_ARRAY(buf, n_ids);
4351 cpt = lnet_net_lock_current();
4352 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4359 * Clearing the NIDS_UPTODATE flag ensures the peer will
4360 * be discovered, provided discovery has not been disabled.
4362 lp = lpni->lpni_peer_net->lpn_peer;
4363 spin_lock(&lp->lp_lock);
4364 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4365 /* If the force flag is set, force a PING and PUSH as well. */
4367 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4368 spin_unlock(&lp->lp_lock);
4369 rc = lnet_discover_peer_locked(lpni, cpt, true);
4375 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4376 buf[i].pid = id.pid;
4377 buf[i].nid = p->lpni_nid;
4384 lnet_peer_ni_decref_locked(lpni);
4386 lnet_net_unlock(cpt);
4389 if (copy_to_user(ids, buf, rc * sizeof(*buf)))
4391 CFS_FREE_PTR_ARRAY(buf, n_ids);
4397 * Retrieve peer discovery status.
4399 * \retval 1 if lnet_peer_discovery_disabled is 0
4400 * \retval 0 if lnet_peer_discovery_disabled is 1
4403 LNetGetPeerDiscoveryStatus(void)
4405 return !lnet_peer_discovery_disabled;
4407 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);