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 "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
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 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
128 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
130 static struct kernel_param_ops param_ops_interfaces_max = {
132 .get = param_get_int,
135 #define param_check_interfaces_max(name, p) \
136 __param_check(name, p, int)
138 #ifdef HAVE_KERNEL_PARAM_OPS
139 module_param(lnet_interfaces_max, interfaces_max, 0644);
141 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
142 ¶m_ops_interfaces_max, 0644);
144 MODULE_PARM_DESC(lnet_interfaces_max,
145 "Maximum number of interfaces in a node.");
147 unsigned lnet_peer_discovery_disabled = 0;
148 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
150 static struct kernel_param_ops param_ops_discovery_disabled = {
151 .set = discovery_set,
152 .get = param_get_int,
155 #define param_check_discovery_disabled(name, p) \
156 __param_check(name, p, int)
157 #ifdef HAVE_KERNEL_PARAM_OPS
158 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
160 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
161 ¶m_ops_discovery_disabled, 0644);
163 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
164 "Set to 1 to disable peer discovery on this node.");
166 unsigned int lnet_drop_asym_route;
167 static int drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp);
169 static struct kernel_param_ops param_ops_drop_asym_route = {
170 .set = drop_asym_route_set,
171 .get = param_get_int,
174 #define param_check_drop_asym_route(name, p) \
175 __param_check(name, p, int)
176 #ifdef HAVE_KERNEL_PARAM_OPS
177 module_param(lnet_drop_asym_route, drop_asym_route, 0644);
179 module_param_call(lnet_drop_asym_route, drop_asym_route_set, param_get_int,
180 ¶m_ops_drop_asym_route, 0644);
182 MODULE_PARM_DESC(lnet_drop_asym_route,
183 "Set to 1 to drop asymmetrical route messages.");
185 #define LNET_TRANSACTION_TIMEOUT_NO_HEALTH_DEFAULT 50
186 #define LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT 50
188 unsigned lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT;
189 static int transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp);
190 #ifdef HAVE_KERNEL_PARAM_OPS
191 static struct kernel_param_ops param_ops_transaction_timeout = {
192 .set = transaction_to_set,
193 .get = param_get_int,
196 #define param_check_transaction_timeout(name, p) \
197 __param_check(name, p, int)
198 module_param(lnet_transaction_timeout, transaction_timeout, S_IRUGO|S_IWUSR);
200 module_param_call(lnet_transaction_timeout, transaction_to_set, param_get_int,
201 &lnet_transaction_timeout, S_IRUGO|S_IWUSR);
203 MODULE_PARM_DESC(lnet_transaction_timeout,
204 "Maximum number of seconds to wait for a peer response.");
206 #define LNET_RETRY_COUNT_HEALTH_DEFAULT 2
207 unsigned lnet_retry_count = LNET_RETRY_COUNT_HEALTH_DEFAULT;
208 static int retry_count_set(const char *val, cfs_kernel_param_arg_t *kp);
209 #ifdef HAVE_KERNEL_PARAM_OPS
210 static struct kernel_param_ops param_ops_retry_count = {
211 .set = retry_count_set,
212 .get = param_get_int,
215 #define param_check_retry_count(name, p) \
216 __param_check(name, p, int)
217 module_param(lnet_retry_count, retry_count, S_IRUGO|S_IWUSR);
219 module_param_call(lnet_retry_count, retry_count_set, param_get_int,
220 &lnet_retry_count, S_IRUGO|S_IWUSR);
222 MODULE_PARM_DESC(lnet_retry_count,
223 "Maximum number of times to retry transmitting a message");
226 unsigned lnet_lnd_timeout = LNET_LND_DEFAULT_TIMEOUT;
227 unsigned int lnet_current_net_count;
230 * This sequence number keeps track of how many times DLC was used to
231 * update the local NIs. It is incremented when a NI is added or
232 * removed and checked when sending a message to determine if there is
233 * a need to re-run the selection algorithm. See lnet_select_pathway()
234 * for more details on its usage.
236 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
238 static int lnet_ping(struct lnet_process_id id, signed long timeout,
239 struct lnet_process_id __user *ids, int n_ids);
241 static int lnet_discover(struct lnet_process_id id, __u32 force,
242 struct lnet_process_id __user *ids, int n_ids);
245 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
248 unsigned *sensitivity = (unsigned *)kp->arg;
251 rc = kstrtoul(val, 0, &value);
253 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
258 * The purpose of locking the api_mutex here is to ensure that
259 * the correct value ends up stored properly.
261 mutex_lock(&the_lnet.ln_api_mutex);
263 if (value > LNET_MAX_HEALTH_VALUE) {
264 mutex_unlock(&the_lnet.ln_api_mutex);
265 CERROR("Invalid health value. Maximum: %d value = %lu\n",
266 LNET_MAX_HEALTH_VALUE, value);
271 * if we're turning on health then use the health timeout
274 if (*sensitivity == 0 && value != 0) {
275 lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT;
276 lnet_retry_count = LNET_RETRY_COUNT_HEALTH_DEFAULT;
278 * if we're turning off health then use the no health timeout
281 } else if (*sensitivity != 0 && value == 0) {
282 lnet_transaction_timeout =
283 LNET_TRANSACTION_TIMEOUT_NO_HEALTH_DEFAULT;
284 lnet_retry_count = 0;
287 *sensitivity = value;
289 mutex_unlock(&the_lnet.ln_api_mutex);
295 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
298 unsigned *interval = (unsigned *)kp->arg;
301 rc = kstrtoul(val, 0, &value);
303 CERROR("Invalid module parameter value for 'lnet_recovery_interval'\n");
308 CERROR("lnet_recovery_interval must be at least 1 second\n");
313 * The purpose of locking the api_mutex here is to ensure that
314 * the correct value ends up stored properly.
316 mutex_lock(&the_lnet.ln_api_mutex);
320 mutex_unlock(&the_lnet.ln_api_mutex);
326 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
329 unsigned *discovery_off = (unsigned *)kp->arg;
331 struct lnet_ping_buffer *pbuf;
333 rc = kstrtoul(val, 0, &value);
335 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
339 value = (value) ? 1 : 0;
342 * The purpose of locking the api_mutex here is to ensure that
343 * the correct value ends up stored properly.
345 mutex_lock(&the_lnet.ln_api_mutex);
347 if (value == *discovery_off) {
348 mutex_unlock(&the_lnet.ln_api_mutex);
353 * We still want to set the discovery value even when LNet is not
354 * running. This is the case when LNet is being loaded and we want
355 * the module parameters to take effect. Otherwise if we're
356 * changing the value dynamically, we want to set it after
359 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
360 *discovery_off = value;
361 mutex_unlock(&the_lnet.ln_api_mutex);
365 /* tell peers that discovery setting has changed */
366 lnet_net_lock(LNET_LOCK_EX);
367 pbuf = the_lnet.ln_ping_target;
369 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
371 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
372 lnet_net_unlock(LNET_LOCK_EX);
374 /* only send a push when we're turning off discovery */
375 if (*discovery_off <= 0 && value > 0)
376 lnet_push_update_to_peers(1);
377 *discovery_off = value;
379 mutex_unlock(&the_lnet.ln_api_mutex);
385 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
388 unsigned int *drop_asym_route = (unsigned int *)kp->arg;
391 rc = kstrtoul(val, 0, &value);
393 CERROR("Invalid module parameter value for "
394 "'lnet_drop_asym_route'\n");
399 * The purpose of locking the api_mutex here is to ensure that
400 * the correct value ends up stored properly.
402 mutex_lock(&the_lnet.ln_api_mutex);
404 if (value == *drop_asym_route) {
405 mutex_unlock(&the_lnet.ln_api_mutex);
409 *drop_asym_route = value;
411 mutex_unlock(&the_lnet.ln_api_mutex);
417 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
420 unsigned *transaction_to = (unsigned *)kp->arg;
423 rc = kstrtoul(val, 0, &value);
425 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
430 * The purpose of locking the api_mutex here is to ensure that
431 * the correct value ends up stored properly.
433 mutex_lock(&the_lnet.ln_api_mutex);
435 if (value < lnet_retry_count || value == 0) {
436 mutex_unlock(&the_lnet.ln_api_mutex);
437 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
438 "Has to be greater than lnet_retry_count (%u)\n",
439 value, lnet_retry_count);
443 if (value == *transaction_to) {
444 mutex_unlock(&the_lnet.ln_api_mutex);
448 *transaction_to = value;
449 if (lnet_retry_count == 0)
450 lnet_lnd_timeout = value;
452 lnet_lnd_timeout = value / lnet_retry_count;
454 mutex_unlock(&the_lnet.ln_api_mutex);
460 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
463 unsigned *retry_count = (unsigned *)kp->arg;
466 rc = kstrtoul(val, 0, &value);
468 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
473 * The purpose of locking the api_mutex here is to ensure that
474 * the correct value ends up stored properly.
476 mutex_lock(&the_lnet.ln_api_mutex);
478 if (lnet_health_sensitivity == 0) {
479 mutex_unlock(&the_lnet.ln_api_mutex);
480 CERROR("Can not set retry_count when health feature is turned off\n");
484 if (value > lnet_transaction_timeout) {
485 mutex_unlock(&the_lnet.ln_api_mutex);
486 CERROR("Invalid value for lnet_retry_count (%lu). "
487 "Has to be smaller than lnet_transaction_timeout (%u)\n",
488 value, lnet_transaction_timeout);
492 *retry_count = value;
495 lnet_lnd_timeout = lnet_transaction_timeout;
497 lnet_lnd_timeout = lnet_transaction_timeout / value;
499 mutex_unlock(&the_lnet.ln_api_mutex);
505 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
509 rc = kstrtoint(val, 0, &value);
511 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
515 if (value < LNET_INTERFACES_MIN) {
516 CWARN("max interfaces provided are too small, setting to %d\n",
517 LNET_INTERFACES_MAX_DEFAULT);
518 value = LNET_INTERFACES_MAX_DEFAULT;
521 *(int *)kp->arg = value;
527 lnet_get_routes(void)
533 lnet_get_networks(void)
538 if (*networks != 0 && *ip2nets != 0) {
539 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
540 "'ip2nets' but not both at once\n");
545 rc = lnet_parse_ip2nets(&nets, ip2nets);
546 return (rc == 0) ? nets : NULL;
556 lnet_init_locks(void)
558 spin_lock_init(&the_lnet.ln_eq_wait_lock);
559 spin_lock_init(&the_lnet.ln_msg_resend_lock);
560 init_completion(&the_lnet.ln_mt_wait_complete);
561 mutex_init(&the_lnet.ln_lnd_mutex);
564 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
565 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
567 struct kmem_cache *lnet_rspt_cachep; /* response tracker cache */
568 struct kmem_cache *lnet_msg_cachep;
571 lnet_slab_setup(void)
573 /* create specific kmem_cache for MEs and small MDs (i.e., originally
574 * allocated in <size-xxx> kmem_cache).
576 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
578 if (!lnet_mes_cachep)
581 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
582 LNET_SMALL_MD_SIZE, 0, 0,
584 if (!lnet_small_mds_cachep)
587 lnet_rspt_cachep = kmem_cache_create("lnet_rspt", sizeof(struct lnet_rsp_tracker),
589 if (!lnet_rspt_cachep)
592 lnet_msg_cachep = kmem_cache_create("lnet_msg", sizeof(struct lnet_msg),
594 if (!lnet_msg_cachep)
601 lnet_slab_cleanup(void)
603 if (lnet_msg_cachep) {
604 kmem_cache_destroy(lnet_msg_cachep);
605 lnet_msg_cachep = NULL;
609 if (lnet_rspt_cachep) {
610 kmem_cache_destroy(lnet_rspt_cachep);
611 lnet_rspt_cachep = NULL;
614 if (lnet_small_mds_cachep) {
615 kmem_cache_destroy(lnet_small_mds_cachep);
616 lnet_small_mds_cachep = NULL;
619 if (lnet_mes_cachep) {
620 kmem_cache_destroy(lnet_mes_cachep);
621 lnet_mes_cachep = NULL;
626 lnet_create_remote_nets_table(void)
629 struct list_head *hash;
631 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
632 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
633 CFS_ALLOC_PTR_ARRAY(hash, LNET_REMOTE_NETS_HASH_SIZE);
635 CERROR("Failed to create remote nets hash table\n");
639 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
640 INIT_LIST_HEAD(&hash[i]);
641 the_lnet.ln_remote_nets_hash = hash;
646 lnet_destroy_remote_nets_table(void)
650 if (the_lnet.ln_remote_nets_hash == NULL)
653 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
654 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
656 CFS_FREE_PTR_ARRAY(the_lnet.ln_remote_nets_hash,
657 LNET_REMOTE_NETS_HASH_SIZE);
658 the_lnet.ln_remote_nets_hash = NULL;
662 lnet_destroy_locks(void)
664 if (the_lnet.ln_res_lock != NULL) {
665 cfs_percpt_lock_free(the_lnet.ln_res_lock);
666 the_lnet.ln_res_lock = NULL;
669 if (the_lnet.ln_net_lock != NULL) {
670 cfs_percpt_lock_free(the_lnet.ln_net_lock);
671 the_lnet.ln_net_lock = NULL;
676 lnet_create_locks(void)
680 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
681 if (the_lnet.ln_res_lock == NULL)
684 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
685 if (the_lnet.ln_net_lock == NULL)
691 lnet_destroy_locks();
695 static void lnet_assert_wire_constants(void)
697 /* Wire protocol assertions generated by 'wirecheck'
698 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
699 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
700 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
703 BUILD_BUG_ON(LNET_PROTO_TCP_MAGIC != 0xeebc0ded);
704 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MAJOR != 1);
705 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MINOR != 0);
706 BUILD_BUG_ON(LNET_MSG_ACK != 0);
707 BUILD_BUG_ON(LNET_MSG_PUT != 1);
708 BUILD_BUG_ON(LNET_MSG_GET != 2);
709 BUILD_BUG_ON(LNET_MSG_REPLY != 3);
710 BUILD_BUG_ON(LNET_MSG_HELLO != 4);
712 /* Checks for struct lnet_handle_wire */
713 BUILD_BUG_ON((int)sizeof(struct lnet_handle_wire) != 16);
714 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
715 wh_interface_cookie) != 0);
716 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) != 8);
717 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
718 wh_object_cookie) != 8);
719 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) != 8);
721 /* Checks for struct struct lnet_magicversion */
722 BUILD_BUG_ON((int)sizeof(struct lnet_magicversion) != 8);
723 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, magic) != 0);
724 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->magic) != 4);
725 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, version_major) != 4);
726 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_major) != 2);
727 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion,
728 version_minor) != 6);
729 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_minor) != 2);
731 /* Checks for struct struct lnet_hdr */
732 BUILD_BUG_ON((int)sizeof(struct lnet_hdr) != 72);
733 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_nid) != 0);
734 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_nid) != 8);
735 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_nid) != 8);
736 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_nid) != 8);
737 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_pid) != 16);
738 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_pid) != 4);
739 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_pid) != 20);
740 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_pid) != 4);
741 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, type) != 24);
742 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->type) != 4);
743 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, payload_length) != 28);
744 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->payload_length) != 4);
745 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg) != 32);
746 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg) != 40);
749 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) != 32);
750 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) != 16);
751 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.match_bits) != 48);
752 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) != 8);
753 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.mlength) != 56);
754 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) != 4);
757 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) != 32);
758 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) != 16);
759 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.match_bits) != 48);
760 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) != 8);
761 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.hdr_data) != 56);
762 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) != 8);
763 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ptl_index) != 64);
764 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) != 4);
765 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.offset) != 68);
766 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) != 4);
769 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.return_wmd) != 32);
770 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) != 16);
771 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.match_bits) != 48);
772 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) != 8);
773 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.ptl_index) != 56);
774 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) != 4);
775 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.src_offset) != 60);
776 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) != 4);
777 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.sink_length) != 64);
778 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) != 4);
781 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) != 32);
782 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) != 16);
785 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.incarnation) != 32);
786 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) != 8);
787 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.type) != 40);
788 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) != 4);
790 /* Checks for struct lnet_ni_status and related constants */
791 BUILD_BUG_ON(LNET_NI_STATUS_INVALID != 0x00000000);
792 BUILD_BUG_ON(LNET_NI_STATUS_UP != 0x15aac0de);
793 BUILD_BUG_ON(LNET_NI_STATUS_DOWN != 0xdeadface);
795 /* Checks for struct lnet_ni_status */
796 BUILD_BUG_ON((int)sizeof(struct lnet_ni_status) != 16);
797 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_nid) != 0);
798 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) != 8);
799 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_status) != 8);
800 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_status) != 4);
801 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_unused) != 12);
802 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) != 4);
804 /* Checks for struct lnet_ping_info and related constants */
805 BUILD_BUG_ON(LNET_PROTO_PING_MAGIC != 0x70696E67);
806 BUILD_BUG_ON(LNET_PING_FEAT_INVAL != 0);
807 BUILD_BUG_ON(LNET_PING_FEAT_BASE != 1);
808 BUILD_BUG_ON(LNET_PING_FEAT_NI_STATUS != 2);
809 BUILD_BUG_ON(LNET_PING_FEAT_RTE_DISABLED != 4);
810 BUILD_BUG_ON(LNET_PING_FEAT_MULTI_RAIL != 8);
811 BUILD_BUG_ON(LNET_PING_FEAT_DISCOVERY != 16);
812 BUILD_BUG_ON(LNET_PING_FEAT_BITS != 31);
814 /* Checks for struct lnet_ping_info */
815 BUILD_BUG_ON((int)sizeof(struct lnet_ping_info) != 16);
816 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_magic) != 0);
817 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) != 4);
818 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_features) != 4);
819 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_features) != 4);
820 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_pid) != 8);
821 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) != 4);
822 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_nnis) != 12);
823 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) != 4);
824 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_ni) != 16);
825 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) != 0);
828 static const struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
830 const struct lnet_lnd *lnd;
832 /* holding lnd mutex */
833 if (type >= NUM_LNDS)
835 lnd = the_lnet.ln_lnds[type];
836 LASSERT(!lnd || lnd->lnd_type == type);
842 lnet_get_lnd_timeout(void)
844 return lnet_lnd_timeout;
846 EXPORT_SYMBOL(lnet_get_lnd_timeout);
849 lnet_register_lnd(const struct lnet_lnd *lnd)
851 mutex_lock(&the_lnet.ln_lnd_mutex);
853 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
854 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
856 the_lnet.ln_lnds[lnd->lnd_type] = lnd;
858 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
860 mutex_unlock(&the_lnet.ln_lnd_mutex);
862 EXPORT_SYMBOL(lnet_register_lnd);
865 lnet_unregister_lnd(const struct lnet_lnd *lnd)
867 mutex_lock(&the_lnet.ln_lnd_mutex);
869 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
871 the_lnet.ln_lnds[lnd->lnd_type] = NULL;
872 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
874 mutex_unlock(&the_lnet.ln_lnd_mutex);
876 EXPORT_SYMBOL(lnet_unregister_lnd);
879 lnet_counters_get_common(struct lnet_counters_common *common)
881 struct lnet_counters *ctr;
884 memset(common, 0, sizeof(*common));
886 lnet_net_lock(LNET_LOCK_EX);
888 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
889 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
890 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
891 common->lcc_errors += ctr->lct_common.lcc_errors;
892 common->lcc_send_count += ctr->lct_common.lcc_send_count;
893 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
894 common->lcc_route_count += ctr->lct_common.lcc_route_count;
895 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
896 common->lcc_send_length += ctr->lct_common.lcc_send_length;
897 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
898 common->lcc_route_length += ctr->lct_common.lcc_route_length;
899 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
901 lnet_net_unlock(LNET_LOCK_EX);
903 EXPORT_SYMBOL(lnet_counters_get_common);
906 lnet_counters_get(struct lnet_counters *counters)
908 struct lnet_counters *ctr;
909 struct lnet_counters_health *health = &counters->lct_health;
912 memset(counters, 0, sizeof(*counters));
914 lnet_counters_get_common(&counters->lct_common);
916 lnet_net_lock(LNET_LOCK_EX);
918 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
919 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
920 health->lch_resend_count += ctr->lct_health.lch_resend_count;
921 health->lch_response_timeout_count +=
922 ctr->lct_health.lch_response_timeout_count;
923 health->lch_local_interrupt_count +=
924 ctr->lct_health.lch_local_interrupt_count;
925 health->lch_local_dropped_count +=
926 ctr->lct_health.lch_local_dropped_count;
927 health->lch_local_aborted_count +=
928 ctr->lct_health.lch_local_aborted_count;
929 health->lch_local_no_route_count +=
930 ctr->lct_health.lch_local_no_route_count;
931 health->lch_local_timeout_count +=
932 ctr->lct_health.lch_local_timeout_count;
933 health->lch_local_error_count +=
934 ctr->lct_health.lch_local_error_count;
935 health->lch_remote_dropped_count +=
936 ctr->lct_health.lch_remote_dropped_count;
937 health->lch_remote_error_count +=
938 ctr->lct_health.lch_remote_error_count;
939 health->lch_remote_timeout_count +=
940 ctr->lct_health.lch_remote_timeout_count;
941 health->lch_network_timeout_count +=
942 ctr->lct_health.lch_network_timeout_count;
944 lnet_net_unlock(LNET_LOCK_EX);
946 EXPORT_SYMBOL(lnet_counters_get);
949 lnet_counters_reset(void)
951 struct lnet_counters *counters;
954 lnet_net_lock(LNET_LOCK_EX);
956 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
957 memset(counters, 0, sizeof(struct lnet_counters));
959 lnet_net_unlock(LNET_LOCK_EX);
963 lnet_res_type2str(int type)
968 case LNET_COOKIE_TYPE_MD:
970 case LNET_COOKIE_TYPE_ME:
972 case LNET_COOKIE_TYPE_EQ:
978 lnet_res_container_cleanup(struct lnet_res_container *rec)
982 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
985 while (!list_empty(&rec->rec_active)) {
986 struct list_head *e = rec->rec_active.next;
989 if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
990 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
992 } else { /* NB: Active MEs should be attached on portals */
999 /* Found alive MD/ME/EQ, user really should unlink/free
1000 * all of them before finalize LNet, but if someone didn't,
1001 * we have to recycle garbage for him */
1002 CERROR("%d active elements on exit of %s container\n",
1003 count, lnet_res_type2str(rec->rec_type));
1006 if (rec->rec_lh_hash != NULL) {
1007 CFS_FREE_PTR_ARRAY(rec->rec_lh_hash, LNET_LH_HASH_SIZE);
1008 rec->rec_lh_hash = NULL;
1011 rec->rec_type = 0; /* mark it as finalized */
1015 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1020 LASSERT(rec->rec_type == 0);
1022 rec->rec_type = type;
1023 INIT_LIST_HEAD(&rec->rec_active);
1025 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1027 /* Arbitrary choice of hash table size */
1028 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1029 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1030 if (rec->rec_lh_hash == NULL) {
1035 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1036 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1041 CERROR("Failed to setup %s resource container\n",
1042 lnet_res_type2str(type));
1043 lnet_res_container_cleanup(rec);
1048 lnet_res_containers_destroy(struct lnet_res_container **recs)
1050 struct lnet_res_container *rec;
1053 cfs_percpt_for_each(rec, i, recs)
1054 lnet_res_container_cleanup(rec);
1056 cfs_percpt_free(recs);
1059 static struct lnet_res_container **
1060 lnet_res_containers_create(int type)
1062 struct lnet_res_container **recs;
1063 struct lnet_res_container *rec;
1067 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1069 CERROR("Failed to allocate %s resource containers\n",
1070 lnet_res_type2str(type));
1074 cfs_percpt_for_each(rec, i, recs) {
1075 rc = lnet_res_container_setup(rec, i, type);
1077 lnet_res_containers_destroy(recs);
1085 struct lnet_libhandle *
1086 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1088 /* ALWAYS called with lnet_res_lock held */
1089 struct list_head *head;
1090 struct lnet_libhandle *lh;
1093 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1096 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1097 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1099 list_for_each_entry(lh, head, lh_hash_chain) {
1100 if (lh->lh_cookie == cookie)
1108 lnet_res_lh_initialize(struct lnet_res_container *rec,
1109 struct lnet_libhandle *lh)
1111 /* ALWAYS called with lnet_res_lock held */
1112 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1115 lh->lh_cookie = rec->rec_lh_cookie;
1116 rec->rec_lh_cookie += 1 << ibits;
1118 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1120 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1124 lnet_create_array_of_queues(void)
1126 struct list_head **qs;
1127 struct list_head *q;
1130 qs = cfs_percpt_alloc(lnet_cpt_table(),
1131 sizeof(struct list_head));
1133 CERROR("Failed to allocate queues\n");
1137 cfs_percpt_for_each(q, i, qs)
1143 static int lnet_unprepare(void);
1146 lnet_prepare(lnet_pid_t requested_pid)
1148 /* Prepare to bring up the network */
1149 struct lnet_res_container **recs;
1152 if (requested_pid == LNET_PID_ANY) {
1153 /* Don't instantiate LNET just for me */
1157 LASSERT(the_lnet.ln_refcount == 0);
1159 the_lnet.ln_routing = 0;
1161 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1162 the_lnet.ln_pid = requested_pid;
1164 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1165 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1166 INIT_LIST_HEAD(&the_lnet.ln_nets);
1167 INIT_LIST_HEAD(&the_lnet.ln_routers);
1168 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1169 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1170 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1171 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1172 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1173 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1174 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1175 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1176 the_lnet.ln_mt_handler = NULL;
1177 init_completion(&the_lnet.ln_started);
1179 rc = lnet_slab_setup();
1183 rc = lnet_create_remote_nets_table();
1188 * NB the interface cookie in wire handles guards against delayed
1189 * replies and ACKs appearing valid after reboot.
1191 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1193 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1194 sizeof(struct lnet_counters));
1195 if (the_lnet.ln_counters == NULL) {
1196 CERROR("Failed to allocate counters for LNet\n");
1201 rc = lnet_peer_tables_create();
1205 rc = lnet_msg_containers_create();
1209 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1210 LNET_COOKIE_TYPE_EQ);
1214 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1220 the_lnet.ln_md_containers = recs;
1222 rc = lnet_portals_create();
1224 CERROR("Failed to create portals for LNet: %d\n", rc);
1228 the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1229 if (!the_lnet.ln_mt_zombie_rstqs) {
1242 lnet_unprepare (void)
1244 /* NB no LNET_LOCK since this is the last reference. All LND instances
1245 * have shut down already, so it is safe to unlink and free all
1246 * descriptors, even those that appear committed to a network op (eg MD
1247 * with non-zero pending count) */
1249 lnet_fail_nid(LNET_NID_ANY, 0);
1251 LASSERT(the_lnet.ln_refcount == 0);
1252 LASSERT(list_empty(&the_lnet.ln_test_peers));
1253 LASSERT(list_empty(&the_lnet.ln_nets));
1255 if (the_lnet.ln_mt_zombie_rstqs) {
1256 lnet_clean_zombie_rstqs();
1257 the_lnet.ln_mt_zombie_rstqs = NULL;
1260 lnet_assert_handler_unused(the_lnet.ln_mt_handler);
1261 the_lnet.ln_mt_handler = NULL;
1263 lnet_portals_destroy();
1265 if (the_lnet.ln_md_containers != NULL) {
1266 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1267 the_lnet.ln_md_containers = NULL;
1270 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1272 lnet_msg_containers_destroy();
1274 lnet_rtrpools_free(0);
1276 if (the_lnet.ln_counters != NULL) {
1277 cfs_percpt_free(the_lnet.ln_counters);
1278 the_lnet.ln_counters = NULL;
1280 lnet_destroy_remote_nets_table();
1281 lnet_slab_cleanup();
1287 lnet_net2ni_locked(__u32 net_id, int cpt)
1290 struct lnet_net *net;
1292 LASSERT(cpt != LNET_LOCK_EX);
1294 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1295 if (net->net_id == net_id) {
1296 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1306 lnet_net2ni_addref(__u32 net)
1311 ni = lnet_net2ni_locked(net, 0);
1313 lnet_ni_addref_locked(ni, 0);
1318 EXPORT_SYMBOL(lnet_net2ni_addref);
1321 lnet_get_net_locked(__u32 net_id)
1323 struct lnet_net *net;
1325 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1326 if (net->net_id == net_id)
1334 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1339 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1344 val = hash_long(key, LNET_CPT_BITS);
1345 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1349 return (unsigned int)(key + val + (val >> 1)) % number;
1353 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1355 struct lnet_net *net;
1357 /* must called with hold of lnet_net_lock */
1358 if (LNET_CPT_NUMBER == 1)
1359 return 0; /* the only one */
1362 * If NI is provided then use the CPT identified in the NI cpt
1363 * list if one exists. If one doesn't exist, then that NI is
1364 * associated with all CPTs and it follows that the net it belongs
1365 * to is implicitly associated with all CPTs, so just hash the nid
1369 if (ni->ni_cpts != NULL)
1370 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1373 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1376 /* no NI provided so look at the net */
1377 net = lnet_get_net_locked(LNET_NIDNET(nid));
1379 if (net != NULL && net->net_cpts != NULL) {
1380 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1383 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1387 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1392 if (LNET_CPT_NUMBER == 1)
1393 return 0; /* the only one */
1395 cpt = lnet_net_lock_current();
1397 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1399 lnet_net_unlock(cpt);
1403 EXPORT_SYMBOL(lnet_cpt_of_nid);
1406 lnet_islocalnet_locked(__u32 net_id)
1408 struct lnet_net *net;
1411 net = lnet_get_net_locked(net_id);
1413 local = net != NULL;
1419 lnet_islocalnet(__u32 net_id)
1424 cpt = lnet_net_lock_current();
1426 local = lnet_islocalnet_locked(net_id);
1428 lnet_net_unlock(cpt);
1434 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1436 struct lnet_net *net;
1439 LASSERT(cpt != LNET_LOCK_EX);
1441 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1442 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1443 if (ni->ni_nid == nid)
1452 lnet_nid2ni_addref(lnet_nid_t nid)
1457 ni = lnet_nid2ni_locked(nid, 0);
1459 lnet_ni_addref_locked(ni, 0);
1464 EXPORT_SYMBOL(lnet_nid2ni_addref);
1467 lnet_islocalnid(lnet_nid_t nid)
1472 cpt = lnet_net_lock_current();
1473 ni = lnet_nid2ni_locked(nid, cpt);
1474 lnet_net_unlock(cpt);
1480 lnet_count_acceptor_nets(void)
1482 /* Return the # of NIs that need the acceptor. */
1484 struct lnet_net *net;
1487 cpt = lnet_net_lock_current();
1488 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1489 /* all socklnd type networks should have the acceptor
1491 if (net->net_lnd->lnd_accept != NULL)
1495 lnet_net_unlock(cpt);
1500 struct lnet_ping_buffer *
1501 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1503 struct lnet_ping_buffer *pbuf;
1505 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1507 pbuf->pb_nnis = nnis;
1508 pbuf->pb_needs_post = false;
1509 atomic_set(&pbuf->pb_refcnt, 1);
1516 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1518 LASSERT(atomic_read(&pbuf->pb_refcnt) == 0);
1519 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1522 static struct lnet_ping_buffer *
1523 lnet_ping_target_create(int nnis)
1525 struct lnet_ping_buffer *pbuf;
1527 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1529 CERROR("Can't allocate ping source [%d]\n", nnis);
1533 pbuf->pb_info.pi_nnis = nnis;
1534 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1535 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1536 pbuf->pb_info.pi_features =
1537 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1543 lnet_get_net_ni_count_locked(struct lnet_net *net)
1548 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1555 lnet_get_net_ni_count_pre(struct lnet_net *net)
1560 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1567 lnet_get_ni_count(void)
1570 struct lnet_net *net;
1575 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1576 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1586 lnet_get_net_count(void)
1588 struct lnet_net *net;
1593 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1603 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1605 struct lnet_ni_status *stat;
1609 __swab32s(&pbuf->pb_info.pi_magic);
1610 __swab32s(&pbuf->pb_info.pi_features);
1611 __swab32s(&pbuf->pb_info.pi_pid);
1612 __swab32s(&pbuf->pb_info.pi_nnis);
1613 nnis = pbuf->pb_info.pi_nnis;
1614 if (nnis > pbuf->pb_nnis)
1615 nnis = pbuf->pb_nnis;
1616 for (i = 0; i < nnis; i++) {
1617 stat = &pbuf->pb_info.pi_ni[i];
1618 __swab64s(&stat->ns_nid);
1619 __swab32s(&stat->ns_status);
1624 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1628 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1630 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1632 /* Loopback is guaranteed to be present */
1633 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1635 if (LNET_PING_INFO_LONI(pinfo) != LNET_NID_LO_0)
1641 lnet_ping_target_destroy(void)
1643 struct lnet_net *net;
1646 lnet_net_lock(LNET_LOCK_EX);
1648 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1649 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1651 ni->ni_status = NULL;
1656 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1657 the_lnet.ln_ping_target = NULL;
1659 lnet_net_unlock(LNET_LOCK_EX);
1663 lnet_ping_target_event_handler(struct lnet_event *event)
1665 struct lnet_ping_buffer *pbuf = event->md_user_ptr;
1667 if (event->unlinked)
1668 lnet_ping_buffer_decref(pbuf);
1672 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1673 struct lnet_handle_md *ping_mdh,
1674 int ni_count, bool set_eq)
1676 struct lnet_process_id id = {
1677 .nid = LNET_NID_ANY,
1681 struct lnet_md md = { NULL };
1685 the_lnet.ln_ping_target_handler =
1686 lnet_ping_target_event_handler;
1688 *ppbuf = lnet_ping_target_create(ni_count);
1689 if (*ppbuf == NULL) {
1694 /* Ping target ME/MD */
1695 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1696 LNET_PROTO_PING_MATCHBITS, 0,
1697 LNET_UNLINK, LNET_INS_AFTER);
1700 CERROR("Can't create ping target ME: %d\n", rc);
1701 goto fail_decref_ping_buffer;
1704 /* initialize md content */
1705 md.start = &(*ppbuf)->pb_info;
1706 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1707 md.threshold = LNET_MD_THRESH_INF;
1709 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1710 LNET_MD_MANAGE_REMOTE;
1711 md.handler = the_lnet.ln_ping_target_handler;
1712 md.user_ptr = *ppbuf;
1714 rc = LNetMDAttach(me, &md, LNET_RETAIN, ping_mdh);
1716 CERROR("Can't attach ping target MD: %d\n", rc);
1717 goto fail_unlink_ping_me;
1719 lnet_ping_buffer_addref(*ppbuf);
1723 fail_unlink_ping_me:
1725 fail_decref_ping_buffer:
1726 LASSERT(atomic_read(&(*ppbuf)->pb_refcnt) == 1);
1727 lnet_ping_buffer_decref(*ppbuf);
1734 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1735 struct lnet_handle_md *ping_mdh)
1737 LNetMDUnlink(*ping_mdh);
1738 LNetInvalidateMDHandle(ping_mdh);
1740 /* NB the MD could be busy; this just starts the unlink */
1741 wait_var_event_warning(&pbuf->pb_refcnt,
1742 atomic_read(&pbuf->pb_refcnt) <= 1,
1743 "Still waiting for ping data MD to unlink\n");
1747 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1750 struct lnet_net *net;
1751 struct lnet_ni_status *ns;
1756 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1757 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1758 LASSERT(i < pbuf->pb_nnis);
1760 ns = &pbuf->pb_info.pi_ni[i];
1762 ns->ns_nid = ni->ni_nid;
1765 ns->ns_status = (ni->ni_status != NULL) ?
1766 ni->ni_status->ns_status :
1775 * We (ab)use the ns_status of the loopback interface to
1776 * transmit the sequence number. The first interface listed
1777 * must be the loopback interface.
1779 rc = lnet_ping_info_validate(&pbuf->pb_info);
1781 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1784 LNET_PING_BUFFER_SEQNO(pbuf) =
1785 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1789 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1790 struct lnet_handle_md ping_mdh)
1792 struct lnet_ping_buffer *old_pbuf = NULL;
1793 struct lnet_handle_md old_ping_md;
1795 /* switch the NIs to point to the new ping info created */
1796 lnet_net_lock(LNET_LOCK_EX);
1798 if (!the_lnet.ln_routing)
1799 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1800 if (!lnet_peer_discovery_disabled)
1801 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1803 /* Ensure only known feature bits have been set. */
1804 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1805 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1807 lnet_ping_target_install_locked(pbuf);
1809 if (the_lnet.ln_ping_target) {
1810 old_pbuf = the_lnet.ln_ping_target;
1811 old_ping_md = the_lnet.ln_ping_target_md;
1813 the_lnet.ln_ping_target_md = ping_mdh;
1814 the_lnet.ln_ping_target = pbuf;
1816 lnet_net_unlock(LNET_LOCK_EX);
1819 /* unlink and free the old ping info */
1820 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1821 lnet_ping_buffer_decref(old_pbuf);
1824 lnet_push_update_to_peers(0);
1828 lnet_ping_target_fini(void)
1830 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1831 &the_lnet.ln_ping_target_md);
1833 lnet_assert_handler_unused(the_lnet.ln_ping_target_handler);
1834 lnet_ping_target_destroy();
1837 /* Resize the push target. */
1838 int lnet_push_target_resize(void)
1840 struct lnet_handle_md mdh;
1841 struct lnet_handle_md old_mdh;
1842 struct lnet_ping_buffer *pbuf;
1843 struct lnet_ping_buffer *old_pbuf;
1848 nnis = the_lnet.ln_push_target_nnis;
1850 CDEBUG(D_NET, "Invalid nnis %d\n", nnis);
1854 /* NB: lnet_ping_buffer_alloc() sets pbuf refcount to 1. That ref is
1855 * dropped when we need to resize again (see "old_pbuf" below) or when
1856 * LNet is shutdown (see lnet_push_target_fini())
1858 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1860 CDEBUG(D_NET, "Can't allocate pbuf for nnis %d\n", nnis);
1864 rc = lnet_push_target_post(pbuf, &mdh);
1866 CDEBUG(D_NET, "Failed to post push target: %d\n", rc);
1867 lnet_ping_buffer_decref(pbuf);
1871 lnet_net_lock(LNET_LOCK_EX);
1872 old_pbuf = the_lnet.ln_push_target;
1873 old_mdh = the_lnet.ln_push_target_md;
1874 the_lnet.ln_push_target = pbuf;
1875 the_lnet.ln_push_target_md = mdh;
1876 lnet_net_unlock(LNET_LOCK_EX);
1879 LNetMDUnlink(old_mdh);
1880 /* Drop ref set by lnet_ping_buffer_alloc() */
1881 lnet_ping_buffer_decref(old_pbuf);
1884 /* Received another push or reply that requires a larger buffer */
1885 if (nnis < the_lnet.ln_push_target_nnis)
1888 CDEBUG(D_NET, "nnis %d success\n", nnis);
1892 int lnet_push_target_post(struct lnet_ping_buffer *pbuf,
1893 struct lnet_handle_md *mdhp)
1895 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1896 struct lnet_md md = { NULL };
1900 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1901 LNET_PROTO_PING_MATCHBITS, 0,
1902 LNET_UNLINK, LNET_INS_AFTER);
1905 CERROR("Can't create push target ME: %d\n", rc);
1909 pbuf->pb_needs_post = false;
1911 /* This reference is dropped by lnet_push_target_event_handler() */
1912 lnet_ping_buffer_addref(pbuf);
1914 /* initialize md content */
1915 md.start = &pbuf->pb_info;
1916 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
1919 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE;
1921 md.handler = the_lnet.ln_push_target_handler;
1923 rc = LNetMDAttach(me, &md, LNET_UNLINK, mdhp);
1925 CERROR("Can't attach push MD: %d\n", rc);
1927 lnet_ping_buffer_decref(pbuf);
1928 pbuf->pb_needs_post = true;
1932 CDEBUG(D_NET, "posted push target %p\n", pbuf);
1937 static void lnet_push_target_event_handler(struct lnet_event *ev)
1939 struct lnet_ping_buffer *pbuf = ev->md_user_ptr;
1941 CDEBUG(D_NET, "type %d status %d unlinked %d\n", ev->type, ev->status,
1944 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1945 lnet_swap_pinginfo(pbuf);
1947 if (ev->type == LNET_EVENT_UNLINK) {
1948 /* Drop ref added by lnet_push_target_post() */
1949 lnet_ping_buffer_decref(pbuf);
1953 lnet_peer_push_event(ev);
1955 /* Drop ref added by lnet_push_target_post */
1956 lnet_ping_buffer_decref(pbuf);
1959 /* Initialize the push target. */
1960 static int lnet_push_target_init(void)
1964 if (the_lnet.ln_push_target)
1967 the_lnet.ln_push_target_handler =
1968 lnet_push_target_event_handler;
1970 rc = LNetSetLazyPortal(LNET_RESERVED_PORTAL);
1973 /* Start at the required minimum, we'll enlarge if required. */
1974 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1976 rc = lnet_push_target_resize();
1979 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
1980 the_lnet.ln_push_target_handler = NULL;
1986 /* Clean up the push target. */
1987 static void lnet_push_target_fini(void)
1989 if (!the_lnet.ln_push_target)
1992 /* Unlink and invalidate to prevent new references. */
1993 LNetMDUnlink(the_lnet.ln_push_target_md);
1994 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1996 /* Wait for the unlink to complete. */
1997 wait_var_event_warning(&the_lnet.ln_push_target->pb_refcnt,
1998 atomic_read(&the_lnet.ln_push_target->pb_refcnt) <= 1,
1999 "Still waiting for ping data MD to unlink\n");
2001 /* Drop ref set by lnet_ping_buffer_alloc() */
2002 lnet_ping_buffer_decref(the_lnet.ln_push_target);
2003 the_lnet.ln_push_target = NULL;
2004 the_lnet.ln_push_target_nnis = 0;
2006 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2007 lnet_assert_handler_unused(the_lnet.ln_push_target_handler);
2008 the_lnet.ln_push_target_handler = NULL;
2012 lnet_ni_tq_credits(struct lnet_ni *ni)
2016 LASSERT(ni->ni_ncpts >= 1);
2018 if (ni->ni_ncpts == 1)
2019 return ni->ni_net->net_tunables.lct_max_tx_credits;
2021 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2022 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2023 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2029 lnet_ni_unlink_locked(struct lnet_ni *ni)
2031 /* move it to zombie list and nobody can find it anymore */
2032 LASSERT(!list_empty(&ni->ni_netlist));
2033 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2034 lnet_ni_decref_locked(ni, 0);
2038 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2043 struct list_head *zombie_list = &net->net_ni_zombie;
2046 * Now wait for the NIs I just nuked to show up on the zombie
2047 * list and shut them down in guaranteed thread context
2050 while (!list_empty(zombie_list)) {
2054 ni = list_entry(zombie_list->next,
2055 struct lnet_ni, ni_netlist);
2056 list_del_init(&ni->ni_netlist);
2057 /* the ni should be in deleting state. If it's not it's
2059 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2060 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2063 /* still busy, add it back to zombie list */
2064 list_add(&ni->ni_netlist, zombie_list);
2068 if (!list_empty(&ni->ni_netlist)) {
2069 lnet_net_unlock(LNET_LOCK_EX);
2071 if ((i & (-i)) == i) {
2073 "Waiting for zombie LNI %s\n",
2074 libcfs_nid2str(ni->ni_nid));
2076 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2077 lnet_net_lock(LNET_LOCK_EX);
2081 lnet_net_unlock(LNET_LOCK_EX);
2083 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2085 LASSERT(!in_interrupt());
2086 /* Holding the mutex makes it safe for lnd_shutdown
2087 * to call module_put(). Module unload cannot finish
2088 * until lnet_unregister_lnd() completes, and that
2089 * requires the mutex.
2091 mutex_lock(&the_lnet.ln_lnd_mutex);
2092 (net->net_lnd->lnd_shutdown)(ni);
2093 mutex_unlock(&the_lnet.ln_lnd_mutex);
2096 CDEBUG(D_LNI, "Removed LNI %s\n",
2097 libcfs_nid2str(ni->ni_nid));
2101 lnet_net_lock(LNET_LOCK_EX);
2105 /* shutdown down the NI and release refcount */
2107 lnet_shutdown_lndni(struct lnet_ni *ni)
2110 struct lnet_net *net = ni->ni_net;
2112 lnet_net_lock(LNET_LOCK_EX);
2114 ni->ni_state = LNET_NI_STATE_DELETING;
2116 lnet_ni_unlink_locked(ni);
2117 lnet_incr_dlc_seq();
2118 lnet_net_unlock(LNET_LOCK_EX);
2120 /* clear messages for this NI on the lazy portal */
2121 for (i = 0; i < the_lnet.ln_nportals; i++)
2122 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2124 lnet_net_lock(LNET_LOCK_EX);
2125 lnet_clear_zombies_nis_locked(net);
2126 lnet_net_unlock(LNET_LOCK_EX);
2130 lnet_shutdown_lndnet(struct lnet_net *net)
2134 lnet_net_lock(LNET_LOCK_EX);
2136 list_del_init(&net->net_list);
2138 while (!list_empty(&net->net_ni_list)) {
2139 ni = list_entry(net->net_ni_list.next,
2140 struct lnet_ni, ni_netlist);
2141 lnet_net_unlock(LNET_LOCK_EX);
2142 lnet_shutdown_lndni(ni);
2143 lnet_net_lock(LNET_LOCK_EX);
2146 lnet_net_unlock(LNET_LOCK_EX);
2148 /* Do peer table cleanup for this net */
2149 lnet_peer_tables_cleanup(net);
2155 lnet_shutdown_lndnets(void)
2157 struct lnet_net *net;
2159 struct lnet_msg *msg, *tmp;
2161 /* NB called holding the global mutex */
2163 /* All quiet on the API front */
2164 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2165 LASSERT(the_lnet.ln_refcount == 0);
2167 lnet_net_lock(LNET_LOCK_EX);
2168 the_lnet.ln_state = LNET_STATE_STOPPING;
2171 * move the nets to the zombie list to avoid them being
2172 * picked up for new work. LONET is also included in the
2173 * Nets that will be moved to the zombie list
2175 list_splice_init(&the_lnet.ln_nets, &the_lnet.ln_net_zombie);
2177 /* Drop the cached loopback Net. */
2178 if (the_lnet.ln_loni != NULL) {
2179 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2180 the_lnet.ln_loni = NULL;
2182 lnet_net_unlock(LNET_LOCK_EX);
2184 /* iterate through the net zombie list and delete each net */
2185 while (!list_empty(&the_lnet.ln_net_zombie)) {
2186 net = list_entry(the_lnet.ln_net_zombie.next,
2187 struct lnet_net, net_list);
2188 lnet_shutdown_lndnet(net);
2191 spin_lock(&the_lnet.ln_msg_resend_lock);
2192 list_splice(&the_lnet.ln_msg_resend, &resend);
2193 spin_unlock(&the_lnet.ln_msg_resend_lock);
2195 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2196 list_del_init(&msg->msg_list);
2197 msg->msg_no_resend = true;
2198 lnet_finalize(msg, -ECANCELED);
2201 lnet_net_lock(LNET_LOCK_EX);
2202 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2203 lnet_net_unlock(LNET_LOCK_EX);
2207 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2210 struct lnet_tx_queue *tq;
2212 struct lnet_net *net = ni->ni_net;
2214 mutex_lock(&the_lnet.ln_lnd_mutex);
2217 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2218 ni->ni_lnd_tunables_set = true;
2221 rc = (net->net_lnd->lnd_startup)(ni);
2223 mutex_unlock(&the_lnet.ln_lnd_mutex);
2226 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2227 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2232 ni->ni_state = LNET_NI_STATE_ACTIVE;
2235 /* We keep a reference on the loopback net through the loopback NI */
2236 if (net->net_lnd->lnd_type == LOLND) {
2238 LASSERT(the_lnet.ln_loni == NULL);
2239 the_lnet.ln_loni = ni;
2240 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2241 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2242 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2243 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2247 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2248 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2249 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2250 libcfs_lnd2str(net->net_lnd->lnd_type),
2251 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2253 /* shutdown the NI since if we get here then it must've already
2256 lnet_shutdown_lndni(ni);
2260 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2261 tq->tq_credits_min =
2262 tq->tq_credits_max =
2263 tq->tq_credits = lnet_ni_tq_credits(ni);
2266 atomic_set(&ni->ni_tx_credits,
2267 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2268 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2270 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2271 libcfs_nid2str(ni->ni_nid),
2272 ni->ni_net->net_tunables.lct_peer_tx_credits,
2273 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2274 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2275 ni->ni_net->net_tunables.lct_peer_timeout);
2284 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2287 struct lnet_net *net_l = NULL;
2288 LIST_HEAD(local_ni_list);
2292 const struct lnet_lnd *lnd;
2294 net->net_tunables.lct_peer_timeout;
2296 net->net_tunables.lct_max_tx_credits;
2297 int peerrtrcredits =
2298 net->net_tunables.lct_peer_rtr_credits;
2301 * make sure that this net is unique. If it isn't then
2302 * we are adding interfaces to an already existing network, and
2303 * 'net' is just a convenient way to pass in the list.
2304 * if it is unique we need to find the LND and load it if
2307 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2308 lnd_type = LNET_NETTYP(net->net_id);
2310 mutex_lock(&the_lnet.ln_lnd_mutex);
2311 lnd = lnet_find_lnd_by_type(lnd_type);
2314 mutex_unlock(&the_lnet.ln_lnd_mutex);
2315 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2316 mutex_lock(&the_lnet.ln_lnd_mutex);
2318 lnd = lnet_find_lnd_by_type(lnd_type);
2320 mutex_unlock(&the_lnet.ln_lnd_mutex);
2321 CERROR("Can't load LND %s, module %s, rc=%d\n",
2322 libcfs_lnd2str(lnd_type),
2323 libcfs_lnd2modname(lnd_type), rc);
2324 #ifndef HAVE_MODULE_LOADING_SUPPORT
2325 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2326 "compiled with kernel module "
2327 "loading support.");
2336 mutex_unlock(&the_lnet.ln_lnd_mutex);
2342 * net_l: if the network being added is unique then net_l
2343 * will point to that network
2344 * if the network being added is not unique then
2345 * net_l points to the existing network.
2347 * When we enter the loop below, we'll pick NIs off he
2348 * network beign added and start them up, then add them to
2349 * a local ni list. Once we've successfully started all
2350 * the NIs then we join the local NI list (of started up
2351 * networks) with the net_l->net_ni_list, which should
2352 * point to the correct network to add the new ni list to
2354 * If any of the new NIs fail to start up, then we want to
2355 * iterate through the local ni list, which should include
2356 * any NIs which were successfully started up, and shut
2359 * After than we want to delete the network being added,
2360 * to avoid a memory leak.
2364 * When a network uses TCP bonding then all its interfaces
2365 * must be specified when the network is first defined: the
2366 * TCP bonding code doesn't allow for interfaces to be added
2369 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2370 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2375 while (!list_empty(&net->net_ni_added)) {
2376 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2378 list_del_init(&ni->ni_netlist);
2380 /* make sure that the the NI we're about to start
2381 * up is actually unique. if it's not fail. */
2382 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2383 ni->ni_interfaces[0])) {
2388 /* adjust the pointer the parent network, just in case it
2389 * the net is a duplicate */
2392 rc = lnet_startup_lndni(ni, tun);
2398 list_add_tail(&ni->ni_netlist, &local_ni_list);
2403 lnet_net_lock(LNET_LOCK_EX);
2404 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2405 lnet_incr_dlc_seq();
2406 lnet_net_unlock(LNET_LOCK_EX);
2408 /* if the network is not unique then we don't want to keep
2409 * it around after we're done. Free it. Otherwise add that
2410 * net to the global the_lnet.ln_nets */
2411 if (net_l != net && net_l != NULL) {
2413 * TODO - note. currently the tunables can not be updated
2419 * restore tunables after it has been overwitten by the
2422 if (peer_timeout != -1)
2423 net->net_tunables.lct_peer_timeout = peer_timeout;
2424 if (maxtxcredits != -1)
2425 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2426 if (peerrtrcredits != -1)
2427 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2429 lnet_net_lock(LNET_LOCK_EX);
2430 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2431 lnet_net_unlock(LNET_LOCK_EX);
2434 /* update net count */
2435 lnet_current_net_count = lnet_get_net_count();
2441 * shutdown the new NIs that are being started up
2442 * free the NET being started
2444 while (!list_empty(&local_ni_list)) {
2445 ni = list_entry(local_ni_list.next, struct lnet_ni,
2448 lnet_shutdown_lndni(ni);
2458 lnet_startup_lndnets(struct list_head *netlist)
2460 struct lnet_net *net;
2465 * Change to running state before bringing up the LNDs. This
2466 * allows lnet_shutdown_lndnets() to assert that we've passed
2469 lnet_net_lock(LNET_LOCK_EX);
2470 the_lnet.ln_state = LNET_STATE_RUNNING;
2471 lnet_net_unlock(LNET_LOCK_EX);
2473 while (!list_empty(netlist)) {
2474 net = list_entry(netlist->next, struct lnet_net, net_list);
2475 list_del_init(&net->net_list);
2477 rc = lnet_startup_lndnet(net, NULL);
2487 lnet_shutdown_lndnets();
2493 * Initialize LNet library.
2495 * Automatically called at module loading time. Caller has to call
2496 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2497 * latter returned 0. It must be called exactly once.
2499 * \retval 0 on success
2500 * \retval -ve on failures.
2502 int lnet_lib_init(void)
2506 lnet_assert_wire_constants();
2508 /* refer to global cfs_cpt_table for now */
2509 the_lnet.ln_cpt_table = cfs_cpt_tab;
2510 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_tab);
2512 LASSERT(the_lnet.ln_cpt_number > 0);
2513 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2514 /* we are under risk of consuming all lh_cookie */
2515 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2516 "please change setting of CPT-table and retry\n",
2517 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2521 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2522 the_lnet.ln_cpt_bits++;
2524 rc = lnet_create_locks();
2526 CERROR("Can't create LNet global locks: %d\n", rc);
2530 the_lnet.ln_refcount = 0;
2531 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2532 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2534 /* The hash table size is the number of bits it takes to express the set
2535 * ln_num_routes, minus 1 (better to under estimate than over so we
2536 * don't waste memory). */
2537 if (rnet_htable_size <= 0)
2538 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2539 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2540 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2541 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2542 order_base_2(rnet_htable_size) - 1);
2544 /* All LNDs apart from the LOLND are in separate modules. They
2545 * register themselves when their module loads, and unregister
2546 * themselves when their module is unloaded. */
2547 lnet_register_lnd(&the_lolnd);
2552 * Finalize LNet library.
2554 * \pre lnet_lib_init() called with success.
2555 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2557 * As this happens at module-unload, all lnds must already be unloaded,
2558 * so they must already be unregistered.
2560 void lnet_lib_exit(void)
2564 LASSERT(the_lnet.ln_refcount == 0);
2565 lnet_unregister_lnd(&the_lolnd);
2566 for (i = 0; i < NUM_LNDS; i++)
2567 LASSERT(!the_lnet.ln_lnds[i]);
2568 lnet_destroy_locks();
2572 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2574 * Users must call this function at least once before any other functions.
2575 * For each successful call there must be a corresponding call to
2576 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2579 * The PID used by LNet may be different from the one requested.
2582 * \param requested_pid PID requested by the caller.
2584 * \return >= 0 on success, and < 0 error code on failures.
2587 LNetNIInit(lnet_pid_t requested_pid)
2589 int im_a_router = 0;
2592 struct lnet_ping_buffer *pbuf;
2593 struct lnet_handle_md ping_mdh;
2594 LIST_HEAD(net_head);
2595 struct lnet_net *net;
2597 mutex_lock(&the_lnet.ln_api_mutex);
2599 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2601 if (the_lnet.ln_refcount > 0) {
2602 rc = the_lnet.ln_refcount++;
2603 mutex_unlock(&the_lnet.ln_api_mutex);
2607 rc = lnet_prepare(requested_pid);
2609 mutex_unlock(&the_lnet.ln_api_mutex);
2613 /* create a network for Loopback network */
2614 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2617 goto err_empty_list;
2620 /* Add in the loopback NI */
2621 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2623 goto err_empty_list;
2626 /* If LNet is being initialized via DLC it is possible
2627 * that the user requests not to load module parameters (ones which
2628 * are supported by DLC) on initialization. Therefore, make sure not
2629 * to load networks, routes and forwarding from module parameters
2630 * in this case. On cleanup in case of failure only clean up
2631 * routes if it has been loaded */
2632 if (!the_lnet.ln_nis_from_mod_params) {
2633 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2636 goto err_empty_list;
2639 ni_count = lnet_startup_lndnets(&net_head);
2642 goto err_empty_list;
2645 if (!the_lnet.ln_nis_from_mod_params) {
2646 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2648 goto err_shutdown_lndnis;
2650 rc = lnet_rtrpools_alloc(im_a_router);
2652 goto err_destroy_routes;
2655 rc = lnet_acceptor_start();
2657 goto err_destroy_routes;
2659 the_lnet.ln_refcount = 1;
2660 /* Now I may use my own API functions... */
2662 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2664 goto err_acceptor_stop;
2666 lnet_ping_target_update(pbuf, ping_mdh);
2668 the_lnet.ln_mt_handler = lnet_mt_event_handler;
2670 rc = lnet_push_target_init();
2674 rc = lnet_peer_discovery_start();
2676 goto err_destroy_push_target;
2678 rc = lnet_monitor_thr_start();
2680 goto err_stop_discovery_thr;
2683 lnet_router_debugfs_init();
2685 mutex_unlock(&the_lnet.ln_api_mutex);
2687 complete_all(&the_lnet.ln_started);
2689 /* wait for all routers to start */
2690 lnet_wait_router_start();
2694 err_stop_discovery_thr:
2695 lnet_peer_discovery_stop();
2696 err_destroy_push_target:
2697 lnet_push_target_fini();
2699 lnet_ping_target_fini();
2701 the_lnet.ln_refcount = 0;
2702 lnet_acceptor_stop();
2704 if (!the_lnet.ln_nis_from_mod_params)
2705 lnet_destroy_routes();
2706 err_shutdown_lndnis:
2707 lnet_shutdown_lndnets();
2711 mutex_unlock(&the_lnet.ln_api_mutex);
2712 while (!list_empty(&net_head)) {
2713 struct lnet_net *net;
2715 net = list_entry(net_head.next, struct lnet_net, net_list);
2716 list_del_init(&net->net_list);
2721 EXPORT_SYMBOL(LNetNIInit);
2724 * Stop LNet interfaces, routing, and forwarding.
2726 * Users must call this function once for each successful call to LNetNIInit().
2727 * Once the LNetNIFini() operation has been started, the results of pending
2728 * API operations are undefined.
2730 * \return always 0 for current implementation.
2735 mutex_lock(&the_lnet.ln_api_mutex);
2737 LASSERT(the_lnet.ln_refcount > 0);
2739 if (the_lnet.ln_refcount != 1) {
2740 the_lnet.ln_refcount--;
2742 LASSERT(!the_lnet.ln_niinit_self);
2746 lnet_router_debugfs_fini();
2747 lnet_monitor_thr_stop();
2748 lnet_peer_discovery_stop();
2749 lnet_push_target_fini();
2750 lnet_ping_target_fini();
2752 /* Teardown fns that use my own API functions BEFORE here */
2753 the_lnet.ln_refcount = 0;
2755 lnet_acceptor_stop();
2756 lnet_destroy_routes();
2757 lnet_shutdown_lndnets();
2761 mutex_unlock(&the_lnet.ln_api_mutex);
2764 EXPORT_SYMBOL(LNetNIFini);
2767 * Grabs the ni data from the ni structure and fills the out
2770 * \param[in] ni network interface structure
2771 * \param[out] cfg_ni NI config information
2772 * \param[out] tun network and LND tunables
2775 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2776 struct lnet_ioctl_config_lnd_tunables *tun,
2777 struct lnet_ioctl_element_stats *stats,
2780 size_t min_size = 0;
2783 if (!ni || !cfg_ni || !tun)
2786 if (ni->ni_interfaces[0] != NULL) {
2787 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2788 if (ni->ni_interfaces[i] != NULL) {
2789 strncpy(cfg_ni->lic_ni_intf[i],
2790 ni->ni_interfaces[i],
2791 sizeof(cfg_ni->lic_ni_intf[i]));
2796 cfg_ni->lic_nid = ni->ni_nid;
2797 if (ni->ni_nid == LNET_NID_LO_0)
2798 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2800 cfg_ni->lic_status = ni->ni_status->ns_status;
2801 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2802 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2804 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2807 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2808 LNET_STATS_TYPE_SEND);
2809 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2810 LNET_STATS_TYPE_RECV);
2811 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2812 LNET_STATS_TYPE_DROP);
2816 * tun->lt_tun will always be present, but in order to be
2817 * backwards compatible, we need to deal with the cases when
2818 * tun->lt_tun is smaller than what the kernel has, because it
2819 * comes from an older version of a userspace program, then we'll
2820 * need to copy as much information as we have available space.
2822 min_size = tun_size - sizeof(tun->lt_cmn);
2823 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2825 /* copy over the cpts */
2826 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2827 ni->ni_cpts == NULL) {
2828 for (i = 0; i < ni->ni_ncpts; i++)
2829 cfg_ni->lic_cpts[i] = i;
2832 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2833 i < LNET_MAX_SHOW_NUM_CPT;
2835 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2837 cfg_ni->lic_ncpts = ni->ni_ncpts;
2841 * NOTE: This is a legacy function left in the code to be backwards
2842 * compatible with older userspace programs. It should eventually be
2845 * Grabs the ni data from the ni structure and fills the out
2848 * \param[in] ni network interface structure
2849 * \param[out] config config information
2852 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2853 struct lnet_ioctl_config_data *config)
2855 struct lnet_ioctl_net_config *net_config;
2856 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2857 size_t min_size, tunable_size = 0;
2863 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2867 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2868 ARRAY_SIZE(net_config->ni_interfaces));
2870 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2871 if (!ni->ni_interfaces[i])
2874 strncpy(net_config->ni_interfaces[i],
2875 ni->ni_interfaces[i],
2876 sizeof(net_config->ni_interfaces[i]));
2879 config->cfg_nid = ni->ni_nid;
2880 config->cfg_config_u.cfg_net.net_peer_timeout =
2881 ni->ni_net->net_tunables.lct_peer_timeout;
2882 config->cfg_config_u.cfg_net.net_max_tx_credits =
2883 ni->ni_net->net_tunables.lct_max_tx_credits;
2884 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2885 ni->ni_net->net_tunables.lct_peer_tx_credits;
2886 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2887 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2889 if (ni->ni_nid == LNET_NID_LO_0)
2890 net_config->ni_status = LNET_NI_STATUS_UP;
2892 net_config->ni_status = ni->ni_status->ns_status;
2895 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2897 for (i = 0; i < num_cpts; i++)
2898 net_config->ni_cpts[i] = ni->ni_cpts[i];
2900 config->cfg_ncpts = num_cpts;
2904 * See if user land tools sent in a newer and larger version
2905 * of struct lnet_tunables than what the kernel uses.
2907 min_size = sizeof(*config) + sizeof(*net_config);
2909 if (config->cfg_hdr.ioc_len > min_size)
2910 tunable_size = config->cfg_hdr.ioc_len - min_size;
2912 /* Don't copy too much data to user space */
2913 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2914 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2916 if (lnd_cfg && min_size) {
2917 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2918 config->cfg_config_u.cfg_net.net_interface_count = 1;
2920 /* Tell user land that kernel side has less data */
2921 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2922 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2923 config->cfg_hdr.ioc_len -= min_size;
2929 lnet_get_ni_idx_locked(int idx)
2932 struct lnet_net *net;
2934 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2935 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2945 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2948 struct lnet_net *net = mynet;
2951 * It is possible that the net has been cleaned out while there is
2952 * a message being sent. This function accessed the net without
2953 * checking if the list is empty
2957 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2959 if (list_empty(&net->net_ni_list))
2961 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2967 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2968 /* if you reached the end of the ni list and the net is
2969 * specified, then there are no more nis in that net */
2973 /* we reached the end of this net ni list. move to the
2975 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2976 /* no more nets and no more NIs. */
2979 /* get the next net */
2980 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2982 if (list_empty(&net->net_ni_list))
2984 /* get the ni on it */
2985 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2991 if (list_empty(&prev->ni_netlist))
2994 /* there are more nis left */
2995 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3001 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3006 int idx = config->cfg_count;
3008 cpt = lnet_net_lock_current();
3010 ni = lnet_get_ni_idx_locked(idx);
3015 lnet_fill_ni_info_legacy(ni, config);
3019 lnet_net_unlock(cpt);
3024 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3025 struct lnet_ioctl_config_lnd_tunables *tun,
3026 struct lnet_ioctl_element_stats *stats,
3033 if (!cfg_ni || !tun || !stats)
3036 cpt = lnet_net_lock_current();
3038 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3043 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3047 lnet_net_unlock(cpt);
3051 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3060 cpt = lnet_net_lock_current();
3062 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3065 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3069 lnet_net_unlock(cpt);
3074 static int lnet_add_net_common(struct lnet_net *net,
3075 struct lnet_ioctl_config_lnd_tunables *tun)
3078 struct lnet_ping_buffer *pbuf;
3079 struct lnet_handle_md ping_mdh;
3081 struct lnet_remotenet *rnet;
3084 lnet_net_lock(LNET_LOCK_EX);
3085 rnet = lnet_find_rnet_locked(net->net_id);
3086 lnet_net_unlock(LNET_LOCK_EX);
3088 * make sure that the net added doesn't invalidate the current
3089 * configuration LNet is keeping
3092 CERROR("Adding net %s will invalidate routing configuration\n",
3093 libcfs_net2str(net->net_id));
3099 * make sure you calculate the correct number of slots in the ping
3100 * buffer. Since the ping info is a flattened list of all the NIs,
3101 * we should allocate enough slots to accomodate the number of NIs
3102 * which will be added.
3104 * since ni hasn't been configured yet, use
3105 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3107 net_ni_count = lnet_get_net_ni_count_pre(net);
3109 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3110 net_ni_count + lnet_get_ni_count(),
3118 memcpy(&net->net_tunables,
3119 &tun->lt_cmn, sizeof(net->net_tunables));
3121 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3123 net_id = net->net_id;
3125 rc = lnet_startup_lndnet(net,
3126 (tun) ? &tun->lt_tun : NULL);
3130 lnet_net_lock(LNET_LOCK_EX);
3131 net = lnet_get_net_locked(net_id);
3132 lnet_net_unlock(LNET_LOCK_EX);
3137 * Start the acceptor thread if this is the first network
3138 * being added that requires the thread.
3140 if (net->net_lnd->lnd_accept) {
3141 rc = lnet_acceptor_start();
3143 /* shutdown the net that we just started */
3144 CERROR("Failed to start up acceptor thread\n");
3145 lnet_shutdown_lndnet(net);
3150 lnet_net_lock(LNET_LOCK_EX);
3151 lnet_peer_net_added(net);
3152 lnet_net_unlock(LNET_LOCK_EX);
3154 lnet_ping_target_update(pbuf, ping_mdh);
3159 lnet_ping_md_unlink(pbuf, &ping_mdh);
3160 lnet_ping_buffer_decref(pbuf);
3164 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3165 struct lnet_ioctl_config_lnd_tunables *tun)
3167 struct lnet_net *net;
3170 LIST_HEAD(net_head);
3172 rc = lnet_parse_ip2nets(&nets, ip2nets);
3176 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3180 mutex_lock(&the_lnet.ln_api_mutex);
3181 while (!list_empty(&net_head)) {
3182 net = list_entry(net_head.next, struct lnet_net, net_list);
3183 list_del_init(&net->net_list);
3184 rc = lnet_add_net_common(net, tun);
3190 mutex_unlock(&the_lnet.ln_api_mutex);
3192 while (!list_empty(&net_head)) {
3193 net = list_entry(net_head.next, struct lnet_net, net_list);
3194 list_del_init(&net->net_list);
3200 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3202 struct lnet_net *net;
3204 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3206 __u32 net_id, lnd_type;
3208 /* get the tunables if they are available */
3209 if (conf->lic_cfg_hdr.ioc_len >=
3210 sizeof(*conf) + sizeof(*tun))
3211 tun = (struct lnet_ioctl_config_lnd_tunables *)
3214 /* handle legacy ip2nets from DLC */
3215 if (conf->lic_legacy_ip2nets[0] != '\0')
3216 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3219 net_id = LNET_NIDNET(conf->lic_nid);
3220 lnd_type = LNET_NETTYP(net_id);
3222 if (!libcfs_isknown_lnd(lnd_type)) {
3223 CERROR("No valid net and lnd information provided\n");
3227 net = lnet_net_alloc(net_id, NULL);
3231 for (i = 0; i < conf->lic_ncpts; i++) {
3232 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3236 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3237 conf->lic_ni_intf[0]);
3241 mutex_lock(&the_lnet.ln_api_mutex);
3243 rc = lnet_add_net_common(net, tun);
3245 mutex_unlock(&the_lnet.ln_api_mutex);
3250 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3252 struct lnet_net *net;
3254 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3255 struct lnet_ping_buffer *pbuf;
3256 struct lnet_handle_md ping_mdh;
3261 /* don't allow userspace to shutdown the LOLND */
3262 if (LNET_NETTYP(net_id) == LOLND)
3265 mutex_lock(&the_lnet.ln_api_mutex);
3269 net = lnet_get_net_locked(net_id);
3271 CERROR("net %s not found\n",
3272 libcfs_net2str(net_id));
3277 addr = LNET_NIDADDR(conf->lic_nid);
3279 /* remove the entire net */
3280 net_count = lnet_get_net_ni_count_locked(net);
3284 /* create and link a new ping info, before removing the old one */
3285 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3286 lnet_get_ni_count() - net_count,
3289 goto unlock_api_mutex;
3291 lnet_shutdown_lndnet(net);
3293 lnet_acceptor_stop();
3295 lnet_ping_target_update(pbuf, ping_mdh);
3297 goto unlock_api_mutex;
3300 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3302 CERROR("nid %s not found\n",
3303 libcfs_nid2str(conf->lic_nid));
3308 net_count = lnet_get_net_ni_count_locked(net);
3312 /* create and link a new ping info, before removing the old one */
3313 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3314 lnet_get_ni_count() - 1, false);
3316 goto unlock_api_mutex;
3318 lnet_shutdown_lndni(ni);
3320 lnet_acceptor_stop();
3322 lnet_ping_target_update(pbuf, ping_mdh);
3324 /* check if the net is empty and remove it if it is */
3326 lnet_shutdown_lndnet(net);
3328 goto unlock_api_mutex;
3333 mutex_unlock(&the_lnet.ln_api_mutex);
3339 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3340 * They are only expected to be called for unique networks.
3341 * That can be as a result of older DLC library
3342 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3345 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3347 struct lnet_net *net;
3348 LIST_HEAD(net_head);
3350 struct lnet_ioctl_config_lnd_tunables tun;
3351 const char *nets = conf->cfg_config_u.cfg_net.net_intf;
3353 /* Create a net/ni structures for the network string */
3354 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3356 return rc == 0 ? -EINVAL : rc;
3358 mutex_lock(&the_lnet.ln_api_mutex);
3361 rc = -EINVAL; /* only add one network per call */
3362 goto out_unlock_clean;
3365 net = list_entry(net_head.next, struct lnet_net, net_list);
3366 list_del_init(&net->net_list);
3368 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3370 memset(&tun, 0, sizeof(tun));
3372 tun.lt_cmn.lct_peer_timeout =
3373 conf->cfg_config_u.cfg_net.net_peer_timeout;
3374 tun.lt_cmn.lct_peer_tx_credits =
3375 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3376 tun.lt_cmn.lct_peer_rtr_credits =
3377 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3378 tun.lt_cmn.lct_max_tx_credits =
3379 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3381 rc = lnet_add_net_common(net, &tun);
3384 mutex_unlock(&the_lnet.ln_api_mutex);
3385 while (!list_empty(&net_head)) {
3386 /* net_head list is empty in success case */
3387 net = list_entry(net_head.next, struct lnet_net, net_list);
3388 list_del_init(&net->net_list);
3395 lnet_dyn_del_net(__u32 net_id)
3397 struct lnet_net *net;
3398 struct lnet_ping_buffer *pbuf;
3399 struct lnet_handle_md ping_mdh;
3403 /* don't allow userspace to shutdown the LOLND */
3404 if (LNET_NETTYP(net_id) == LOLND)
3407 mutex_lock(&the_lnet.ln_api_mutex);
3411 net = lnet_get_net_locked(net_id);
3418 net_ni_count = lnet_get_net_ni_count_locked(net);
3422 /* create and link a new ping info, before removing the old one */
3423 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3424 lnet_get_ni_count() - net_ni_count, false);
3428 lnet_shutdown_lndnet(net);
3430 lnet_acceptor_stop();
3432 lnet_ping_target_update(pbuf, ping_mdh);
3435 mutex_unlock(&the_lnet.ln_api_mutex);
3440 void lnet_incr_dlc_seq(void)
3442 atomic_inc(&lnet_dlc_seq_no);
3445 __u32 lnet_get_dlc_seq_locked(void)
3447 return atomic_read(&lnet_dlc_seq_no);
3451 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3453 struct lnet_net *net;
3456 lnet_net_lock(LNET_LOCK_EX);
3457 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3458 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3459 if (ni->ni_nid == nid || all) {
3460 atomic_set(&ni->ni_healthv, value);
3461 if (list_empty(&ni->ni_recovery) &&
3462 value < LNET_MAX_HEALTH_VALUE) {
3463 CERROR("manually adding local NI %s to recovery\n",
3464 libcfs_nid2str(ni->ni_nid));
3465 list_add_tail(&ni->ni_recovery,
3466 &the_lnet.ln_mt_localNIRecovq);
3467 lnet_ni_addref_locked(ni, 0);
3470 lnet_net_unlock(LNET_LOCK_EX);
3476 lnet_net_unlock(LNET_LOCK_EX);
3480 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3484 lnet_nid_t nid = stats->hlni_nid;
3486 cpt = lnet_net_lock_current();
3487 ni = lnet_nid2ni_locked(nid, cpt);
3494 stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3495 stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3496 stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3497 stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3498 stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3499 stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3500 stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3503 lnet_net_unlock(cpt);
3509 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3514 lnet_net_lock(LNET_LOCK_EX);
3515 list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3516 list->rlst_nid_array[i] = ni->ni_nid;
3518 if (i >= LNET_MAX_SHOW_NUM_NID)
3521 lnet_net_unlock(LNET_LOCK_EX);
3522 list->rlst_num_nids = i;
3528 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3530 struct lnet_peer_ni *lpni;
3533 lnet_net_lock(LNET_LOCK_EX);
3534 list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3535 list->rlst_nid_array[i] = lpni->lpni_nid;
3537 if (i >= LNET_MAX_SHOW_NUM_NID)
3540 lnet_net_unlock(LNET_LOCK_EX);
3541 list->rlst_num_nids = i;
3547 * LNet ioctl handler.
3551 LNetCtl(unsigned int cmd, void *arg)
3553 struct libcfs_ioctl_data *data = arg;
3554 struct lnet_ioctl_config_data *config;
3555 struct lnet_process_id id = {0};
3559 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3560 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3563 case IOC_LIBCFS_GET_NI:
3564 rc = LNetGetId(data->ioc_count, &id);
3565 data->ioc_nid = id.nid;
3568 case IOC_LIBCFS_FAIL_NID:
3569 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3571 case IOC_LIBCFS_ADD_ROUTE: {
3572 /* default router sensitivity to 1 */
3573 unsigned int sensitivity = 1;
3576 if (config->cfg_hdr.ioc_len < sizeof(*config))
3579 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3581 config->cfg_config_u.cfg_route.rtr_sensitivity;
3584 mutex_lock(&the_lnet.ln_api_mutex);
3585 rc = lnet_add_route(config->cfg_net,
3586 config->cfg_config_u.cfg_route.rtr_hop,
3588 config->cfg_config_u.cfg_route.
3589 rtr_priority, sensitivity);
3590 mutex_unlock(&the_lnet.ln_api_mutex);
3594 case IOC_LIBCFS_DEL_ROUTE:
3597 if (config->cfg_hdr.ioc_len < sizeof(*config))
3600 mutex_lock(&the_lnet.ln_api_mutex);
3601 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3602 mutex_unlock(&the_lnet.ln_api_mutex);
3605 case IOC_LIBCFS_GET_ROUTE:
3608 if (config->cfg_hdr.ioc_len < sizeof(*config))
3611 mutex_lock(&the_lnet.ln_api_mutex);
3612 rc = lnet_get_route(config->cfg_count,
3614 &config->cfg_config_u.cfg_route.rtr_hop,
3616 &config->cfg_config_u.cfg_route.rtr_flags,
3617 &config->cfg_config_u.cfg_route.
3619 &config->cfg_config_u.cfg_route.
3621 mutex_unlock(&the_lnet.ln_api_mutex);
3624 case IOC_LIBCFS_GET_LOCAL_NI: {
3625 struct lnet_ioctl_config_ni *cfg_ni;
3626 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3627 struct lnet_ioctl_element_stats *stats;
3632 /* get the tunables if they are available */
3633 if (cfg_ni->lic_cfg_hdr.ioc_len <
3634 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3637 stats = (struct lnet_ioctl_element_stats *)
3639 tun = (struct lnet_ioctl_config_lnd_tunables *)
3640 (cfg_ni->lic_bulk + sizeof(*stats));
3642 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3645 mutex_lock(&the_lnet.ln_api_mutex);
3646 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3647 mutex_unlock(&the_lnet.ln_api_mutex);
3651 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3652 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3654 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3657 mutex_lock(&the_lnet.ln_api_mutex);
3658 rc = lnet_get_ni_stats(msg_stats);
3659 mutex_unlock(&the_lnet.ln_api_mutex);
3664 case IOC_LIBCFS_GET_NET: {
3665 size_t total = sizeof(*config) +
3666 sizeof(struct lnet_ioctl_net_config);
3669 if (config->cfg_hdr.ioc_len < total)
3672 mutex_lock(&the_lnet.ln_api_mutex);
3673 rc = lnet_get_net_config(config);
3674 mutex_unlock(&the_lnet.ln_api_mutex);
3678 case IOC_LIBCFS_GET_LNET_STATS:
3680 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3682 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3685 mutex_lock(&the_lnet.ln_api_mutex);
3686 lnet_counters_get(&lnet_stats->st_cntrs);
3687 mutex_unlock(&the_lnet.ln_api_mutex);
3691 case IOC_LIBCFS_CONFIG_RTR:
3694 if (config->cfg_hdr.ioc_len < sizeof(*config))
3697 mutex_lock(&the_lnet.ln_api_mutex);
3698 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3699 rc = lnet_rtrpools_enable();
3700 mutex_unlock(&the_lnet.ln_api_mutex);
3703 lnet_rtrpools_disable();
3704 mutex_unlock(&the_lnet.ln_api_mutex);
3707 case IOC_LIBCFS_ADD_BUF:
3710 if (config->cfg_hdr.ioc_len < sizeof(*config))
3713 mutex_lock(&the_lnet.ln_api_mutex);
3714 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3716 config->cfg_config_u.cfg_buffers.
3718 config->cfg_config_u.cfg_buffers.
3720 mutex_unlock(&the_lnet.ln_api_mutex);
3723 case IOC_LIBCFS_SET_NUMA_RANGE: {
3724 struct lnet_ioctl_set_value *numa;
3726 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3728 lnet_net_lock(LNET_LOCK_EX);
3729 lnet_numa_range = numa->sv_value;
3730 lnet_net_unlock(LNET_LOCK_EX);
3734 case IOC_LIBCFS_GET_NUMA_RANGE: {
3735 struct lnet_ioctl_set_value *numa;
3737 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3739 numa->sv_value = lnet_numa_range;
3743 case IOC_LIBCFS_GET_BUF: {
3744 struct lnet_ioctl_pool_cfg *pool_cfg;
3745 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3749 if (config->cfg_hdr.ioc_len < total)
3752 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3754 mutex_lock(&the_lnet.ln_api_mutex);
3755 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3756 mutex_unlock(&the_lnet.ln_api_mutex);
3760 case IOC_LIBCFS_GET_LOCAL_HSTATS: {
3761 struct lnet_ioctl_local_ni_hstats *stats = arg;
3763 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
3766 mutex_lock(&the_lnet.ln_api_mutex);
3767 rc = lnet_get_local_ni_hstats(stats);
3768 mutex_unlock(&the_lnet.ln_api_mutex);
3773 case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
3774 struct lnet_ioctl_recovery_list *list = arg;
3775 if (list->rlst_hdr.ioc_len < sizeof(*list))
3778 mutex_lock(&the_lnet.ln_api_mutex);
3779 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
3780 rc = lnet_get_local_ni_recovery_list(list);
3782 rc = lnet_get_peer_ni_recovery_list(list);
3783 mutex_unlock(&the_lnet.ln_api_mutex);
3787 case IOC_LIBCFS_ADD_PEER_NI: {
3788 struct lnet_ioctl_peer_cfg *cfg = arg;
3790 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3793 mutex_lock(&the_lnet.ln_api_mutex);
3794 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3797 mutex_unlock(&the_lnet.ln_api_mutex);
3801 case IOC_LIBCFS_DEL_PEER_NI: {
3802 struct lnet_ioctl_peer_cfg *cfg = arg;
3804 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3807 mutex_lock(&the_lnet.ln_api_mutex);
3808 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3809 cfg->prcfg_cfg_nid);
3810 mutex_unlock(&the_lnet.ln_api_mutex);
3814 case IOC_LIBCFS_GET_PEER_INFO: {
3815 struct lnet_ioctl_peer *peer_info = arg;
3817 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3820 mutex_lock(&the_lnet.ln_api_mutex);
3821 rc = lnet_get_peer_ni_info(
3822 peer_info->pr_count,
3824 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3825 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3826 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3827 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3828 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3829 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3830 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3831 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3832 mutex_unlock(&the_lnet.ln_api_mutex);
3836 case IOC_LIBCFS_GET_PEER_NI: {
3837 struct lnet_ioctl_peer_cfg *cfg = arg;
3839 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3842 mutex_lock(&the_lnet.ln_api_mutex);
3843 rc = lnet_get_peer_info(cfg,
3844 (void __user *)cfg->prcfg_bulk);
3845 mutex_unlock(&the_lnet.ln_api_mutex);
3849 case IOC_LIBCFS_GET_PEER_LIST: {
3850 struct lnet_ioctl_peer_cfg *cfg = arg;
3852 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3855 mutex_lock(&the_lnet.ln_api_mutex);
3856 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3857 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3858 mutex_unlock(&the_lnet.ln_api_mutex);
3862 case IOC_LIBCFS_SET_HEALHV: {
3863 struct lnet_ioctl_reset_health_cfg *cfg = arg;
3865 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
3867 if (cfg->rh_value < 0 ||
3868 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
3869 value = LNET_MAX_HEALTH_VALUE;
3871 value = cfg->rh_value;
3872 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
3873 value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
3874 "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
3875 mutex_lock(&the_lnet.ln_api_mutex);
3876 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
3877 lnet_ni_set_healthv(cfg->rh_nid, value,
3880 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
3882 mutex_unlock(&the_lnet.ln_api_mutex);
3886 case IOC_LIBCFS_NOTIFY_ROUTER: {
3887 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3889 /* The deadline passed in by the user should be some time in
3890 * seconds in the future since the UNIX epoch. We have to map
3891 * that deadline to the wall clock.
3893 deadline += ktime_get_seconds();
3894 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
3898 case IOC_LIBCFS_LNET_DIST:
3899 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3900 if (rc < 0 && rc != -EHOSTUNREACH)
3903 data->ioc_u32[0] = rc;
3906 case IOC_LIBCFS_TESTPROTOCOMPAT:
3907 the_lnet.ln_testprotocompat = data->ioc_flags;
3910 case IOC_LIBCFS_LNET_FAULT:
3911 return lnet_fault_ctl(data->ioc_flags, data);
3913 case IOC_LIBCFS_PING: {
3914 signed long timeout;
3916 id.nid = data->ioc_nid;
3917 id.pid = data->ioc_u32[0];
3919 /* If timeout is negative then set default of 3 minutes */
3920 if (((s32)data->ioc_u32[1] <= 0) ||
3921 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3922 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3924 timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
3926 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3927 data->ioc_plen1 / sizeof(struct lnet_process_id));
3932 data->ioc_count = rc;
3936 case IOC_LIBCFS_PING_PEER: {
3937 struct lnet_ioctl_ping_data *ping = arg;
3938 struct lnet_peer *lp;
3939 signed long timeout;
3941 /* If timeout is negative then set default of 3 minutes */
3942 if (((s32)ping->op_param) <= 0 ||
3943 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3944 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3946 timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
3948 rc = lnet_ping(ping->ping_id, timeout,
3954 mutex_lock(&the_lnet.ln_api_mutex);
3955 lp = lnet_find_peer(ping->ping_id.nid);
3957 ping->ping_id.nid = lp->lp_primary_nid;
3958 ping->mr_info = lnet_peer_is_multi_rail(lp);
3959 lnet_peer_decref_locked(lp);
3961 mutex_unlock(&the_lnet.ln_api_mutex);
3963 ping->ping_count = rc;
3967 case IOC_LIBCFS_DISCOVER: {
3968 struct lnet_ioctl_ping_data *discover = arg;
3969 struct lnet_peer *lp;
3971 rc = lnet_discover(discover->ping_id, discover->op_param,
3973 discover->ping_count);
3977 mutex_lock(&the_lnet.ln_api_mutex);
3978 lp = lnet_find_peer(discover->ping_id.nid);
3980 discover->ping_id.nid = lp->lp_primary_nid;
3981 discover->mr_info = lnet_peer_is_multi_rail(lp);
3982 lnet_peer_decref_locked(lp);
3984 mutex_unlock(&the_lnet.ln_api_mutex);
3986 discover->ping_count = rc;
3991 ni = lnet_net2ni_addref(data->ioc_net);
3995 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3998 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4005 EXPORT_SYMBOL(LNetCtl);
4007 void LNetDebugPeer(struct lnet_process_id id)
4009 lnet_debug_peer(id.nid);
4011 EXPORT_SYMBOL(LNetDebugPeer);
4014 * Determine if the specified peer \a nid is on the local node.
4016 * \param nid peer nid to check
4018 * \retval true If peer NID is on the local node.
4019 * \retval false If peer NID is not on the local node.
4021 bool LNetIsPeerLocal(lnet_nid_t nid)
4023 struct lnet_net *net;
4027 cpt = lnet_net_lock_current();
4028 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4029 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4030 if (ni->ni_nid == nid) {
4031 lnet_net_unlock(cpt);
4036 lnet_net_unlock(cpt);
4040 EXPORT_SYMBOL(LNetIsPeerLocal);
4043 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4044 * Note that all interfaces share a same PID, as requested by LNetNIInit().
4046 * \param index Index of the interface to look up.
4047 * \param id On successful return, this location will hold the
4048 * struct lnet_process_id ID of the interface.
4050 * \retval 0 If an interface exists at \a index.
4051 * \retval -ENOENT If no interface has been found.
4054 LNetGetId(unsigned int index, struct lnet_process_id *id)
4057 struct lnet_net *net;
4061 LASSERT(the_lnet.ln_refcount > 0);
4063 cpt = lnet_net_lock_current();
4065 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4066 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4070 id->nid = ni->ni_nid;
4071 id->pid = the_lnet.ln_pid;
4077 lnet_net_unlock(cpt);
4080 EXPORT_SYMBOL(LNetGetId);
4085 struct lnet_handle_md mdh;
4086 struct completion completion;
4090 lnet_ping_event_handler(struct lnet_event *event)
4092 struct ping_data *pd = event->md_user_ptr;
4094 CDEBUG(D_NET, "ping event (%d %d)%s\n",
4095 event->type, event->status,
4096 event->unlinked ? " unlinked" : "");
4098 if (event->status) {
4100 pd->rc = event->status;
4101 } else if (event->type == LNET_EVENT_REPLY) {
4103 pd->rc = event->mlength;
4105 if (event->unlinked)
4106 complete(&pd->completion);
4109 static int lnet_ping(struct lnet_process_id id, signed long timeout,
4110 struct lnet_process_id __user *ids, int n_ids)
4112 struct lnet_md md = { NULL };
4113 struct ping_data pd = { 0 };
4114 struct lnet_ping_buffer *pbuf;
4115 struct lnet_process_id tmpid;
4121 /* n_ids limit is arbitrary */
4122 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4126 * if the user buffer has more space than the lnet_interfaces_max
4127 * then only fill it up to lnet_interfaces_max
4129 if (n_ids > lnet_interfaces_max)
4130 n_ids = lnet_interfaces_max;
4132 if (id.pid == LNET_PID_ANY)
4133 id.pid = LNET_PID_LUSTRE;
4135 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4139 /* initialize md content */
4140 md.start = &pbuf->pb_info;
4141 md.length = LNET_PING_INFO_SIZE(n_ids);
4142 md.threshold = 2; /* GET/REPLY */
4144 md.options = LNET_MD_TRUNCATE;
4146 md.handler = lnet_ping_event_handler;
4148 init_completion(&pd.completion);
4150 rc = LNetMDBind(&md, LNET_UNLINK, &pd.mdh);
4152 CERROR("Can't bind MD: %d\n", rc);
4153 goto fail_ping_buffer_decref;
4156 rc = LNetGet(LNET_NID_ANY, pd.mdh, id,
4157 LNET_RESERVED_PORTAL,
4158 LNET_PROTO_PING_MATCHBITS, 0, false);
4161 /* Don't CERROR; this could be deliberate! */
4162 rc2 = LNetMDUnlink(pd.mdh);
4165 /* NB must wait for the UNLINK event below... */
4168 if (wait_for_completion_timeout(&pd.completion, timeout) == 0) {
4169 /* Ensure completion in finite time... */
4170 LNetMDUnlink(pd.mdh);
4171 wait_for_completion(&pd.completion);
4175 goto fail_ping_buffer_decref;
4179 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4181 rc = -EPROTO; /* if I can't parse... */
4184 CERROR("%s: ping info too short %d\n",
4185 libcfs_id2str(id), nob);
4186 goto fail_ping_buffer_decref;
4189 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4190 lnet_swap_pinginfo(pbuf);
4191 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4192 CERROR("%s: Unexpected magic %08x\n",
4193 libcfs_id2str(id), pbuf->pb_info.pi_magic);
4194 goto fail_ping_buffer_decref;
4197 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4198 CERROR("%s: ping w/o NI status: 0x%x\n",
4199 libcfs_id2str(id), pbuf->pb_info.pi_features);
4200 goto fail_ping_buffer_decref;
4203 if (nob < LNET_PING_INFO_SIZE(0)) {
4204 CERROR("%s: Short reply %d(%d min)\n",
4206 nob, (int)LNET_PING_INFO_SIZE(0));
4207 goto fail_ping_buffer_decref;
4210 if (pbuf->pb_info.pi_nnis < n_ids)
4211 n_ids = pbuf->pb_info.pi_nnis;
4213 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4214 CERROR("%s: Short reply %d(%d expected)\n",
4216 nob, (int)LNET_PING_INFO_SIZE(n_ids));
4217 goto fail_ping_buffer_decref;
4220 rc = -EFAULT; /* if I segv in copy_to_user()... */
4222 memset(&tmpid, 0, sizeof(tmpid));
4223 for (i = 0; i < n_ids; i++) {
4224 tmpid.pid = pbuf->pb_info.pi_pid;
4225 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4226 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4227 goto fail_ping_buffer_decref;
4229 rc = pbuf->pb_info.pi_nnis;
4231 fail_ping_buffer_decref:
4232 lnet_ping_buffer_decref(pbuf);
4237 lnet_discover(struct lnet_process_id id, __u32 force,
4238 struct lnet_process_id __user *ids, int n_ids)
4240 struct lnet_peer_ni *lpni;
4241 struct lnet_peer_ni *p;
4242 struct lnet_peer *lp;
4243 struct lnet_process_id *buf;
4249 id.nid == LNET_NID_ANY)
4252 if (id.pid == LNET_PID_ANY)
4253 id.pid = LNET_PID_LUSTRE;
4256 * If the user buffer has more space than the lnet_interfaces_max,
4257 * then only fill it up to lnet_interfaces_max.
4259 if (n_ids > lnet_interfaces_max)
4260 n_ids = lnet_interfaces_max;
4262 CFS_ALLOC_PTR_ARRAY(buf, n_ids);
4266 cpt = lnet_net_lock_current();
4267 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4274 * Clearing the NIDS_UPTODATE flag ensures the peer will
4275 * be discovered, provided discovery has not been disabled.
4277 lp = lpni->lpni_peer_net->lpn_peer;
4278 spin_lock(&lp->lp_lock);
4279 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4280 /* If the force flag is set, force a PING and PUSH as well. */
4282 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4283 spin_unlock(&lp->lp_lock);
4284 rc = lnet_discover_peer_locked(lpni, cpt, true);
4290 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4291 buf[i].pid = id.pid;
4292 buf[i].nid = p->lpni_nid;
4299 lnet_peer_ni_decref_locked(lpni);
4301 lnet_net_unlock(cpt);
4304 if (copy_to_user(ids, buf, rc * sizeof(*buf)))
4306 CFS_FREE_PTR_ARRAY(buf, n_ids);
4312 * Retrieve peer discovery status.
4314 * \retval 1 if lnet_peer_discovery_disabled is 0
4315 * \retval 0 if lnet_peer_discovery_disabled is 1
4318 LNetGetPeerDiscoveryStatus(void)
4320 return !lnet_peer_discovery_disabled;
4322 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);