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 unsigned int lnet_recovery_limit;
128 module_param(lnet_recovery_limit, uint, 0644);
129 MODULE_PARM_DESC(lnet_recovery_limit,
130 "How long to attempt recovery of unhealthy peer interfaces in seconds. Set to 0 to allow indefinite recovery");
132 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
133 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
135 static struct kernel_param_ops param_ops_interfaces_max = {
137 .get = param_get_int,
140 #define param_check_interfaces_max(name, p) \
141 __param_check(name, p, int)
143 #ifdef HAVE_KERNEL_PARAM_OPS
144 module_param(lnet_interfaces_max, interfaces_max, 0644);
146 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
147 ¶m_ops_interfaces_max, 0644);
149 MODULE_PARM_DESC(lnet_interfaces_max,
150 "Maximum number of interfaces in a node.");
152 unsigned lnet_peer_discovery_disabled = 0;
153 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
155 static struct kernel_param_ops param_ops_discovery_disabled = {
156 .set = discovery_set,
157 .get = param_get_int,
160 #define param_check_discovery_disabled(name, p) \
161 __param_check(name, p, int)
162 #ifdef HAVE_KERNEL_PARAM_OPS
163 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
165 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
166 ¶m_ops_discovery_disabled, 0644);
168 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
169 "Set to 1 to disable peer discovery on this node.");
171 unsigned int lnet_drop_asym_route;
172 static int drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp);
174 static struct kernel_param_ops param_ops_drop_asym_route = {
175 .set = drop_asym_route_set,
176 .get = param_get_int,
179 #define param_check_drop_asym_route(name, p) \
180 __param_check(name, p, int)
181 #ifdef HAVE_KERNEL_PARAM_OPS
182 module_param(lnet_drop_asym_route, drop_asym_route, 0644);
184 module_param_call(lnet_drop_asym_route, drop_asym_route_set, param_get_int,
185 ¶m_ops_drop_asym_route, 0644);
187 MODULE_PARM_DESC(lnet_drop_asym_route,
188 "Set to 1 to drop asymmetrical route messages.");
190 #define LNET_TRANSACTION_TIMEOUT_DEFAULT 50
191 unsigned int lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_DEFAULT;
192 static int transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp);
193 #ifdef HAVE_KERNEL_PARAM_OPS
194 static struct kernel_param_ops param_ops_transaction_timeout = {
195 .set = transaction_to_set,
196 .get = param_get_int,
199 #define param_check_transaction_timeout(name, p) \
200 __param_check(name, p, int)
201 module_param(lnet_transaction_timeout, transaction_timeout, S_IRUGO|S_IWUSR);
203 module_param_call(lnet_transaction_timeout, transaction_to_set, param_get_int,
204 &lnet_transaction_timeout, S_IRUGO|S_IWUSR);
206 MODULE_PARM_DESC(lnet_transaction_timeout,
207 "Maximum number of seconds to wait for a peer response.");
209 #define LNET_RETRY_COUNT_DEFAULT 2
210 unsigned int lnet_retry_count = LNET_RETRY_COUNT_DEFAULT;
211 static int retry_count_set(const char *val, cfs_kernel_param_arg_t *kp);
212 #ifdef HAVE_KERNEL_PARAM_OPS
213 static struct kernel_param_ops param_ops_retry_count = {
214 .set = retry_count_set,
215 .get = param_get_int,
218 #define param_check_retry_count(name, p) \
219 __param_check(name, p, int)
220 module_param(lnet_retry_count, retry_count, S_IRUGO|S_IWUSR);
222 module_param_call(lnet_retry_count, retry_count_set, param_get_int,
223 &lnet_retry_count, S_IRUGO|S_IWUSR);
225 MODULE_PARM_DESC(lnet_retry_count,
226 "Maximum number of times to retry transmitting a message");
228 unsigned int lnet_response_tracking = 3;
229 static int response_tracking_set(const char *val, cfs_kernel_param_arg_t *kp);
231 #ifdef HAVE_KERNEL_PARAM_OPS
232 static struct kernel_param_ops param_ops_response_tracking = {
233 .set = response_tracking_set,
234 .get = param_get_int,
237 #define param_check_response_tracking(name, p) \
238 __param_check(name, p, int)
239 module_param(lnet_response_tracking, response_tracking, 0644);
241 module_param_call(lnet_response_tracking, response_tracking_set, param_get_int,
242 &lnet_response_tracking, 0644);
244 MODULE_PARM_DESC(lnet_response_tracking,
245 "(0|1|2|3) LNet Internal Only|GET Reply only|PUT ACK only|Full Tracking (default)");
247 #define LNET_LND_TIMEOUT_DEFAULT ((LNET_TRANSACTION_TIMEOUT_DEFAULT - 1) / \
248 (LNET_RETRY_COUNT_DEFAULT + 1))
249 unsigned int lnet_lnd_timeout = LNET_LND_TIMEOUT_DEFAULT;
250 static void lnet_set_lnd_timeout(void)
252 lnet_lnd_timeout = (lnet_transaction_timeout - 1) /
253 (lnet_retry_count + 1);
256 unsigned int lnet_current_net_count;
259 * This sequence number keeps track of how many times DLC was used to
260 * update the local NIs. It is incremented when a NI is added or
261 * removed and checked when sending a message to determine if there is
262 * a need to re-run the selection algorithm. See lnet_select_pathway()
263 * for more details on its usage.
265 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
267 static int lnet_ping(struct lnet_process_id id, signed long timeout,
268 struct lnet_process_id __user *ids, int n_ids);
270 static int lnet_discover(struct lnet_process_id id, __u32 force,
271 struct lnet_process_id __user *ids, int n_ids);
274 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
277 unsigned *sensitivity = (unsigned *)kp->arg;
280 rc = kstrtoul(val, 0, &value);
282 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
287 * The purpose of locking the api_mutex here is to ensure that
288 * the correct value ends up stored properly.
290 mutex_lock(&the_lnet.ln_api_mutex);
292 if (value > LNET_MAX_HEALTH_VALUE) {
293 mutex_unlock(&the_lnet.ln_api_mutex);
294 CERROR("Invalid health value. Maximum: %d value = %lu\n",
295 LNET_MAX_HEALTH_VALUE, value);
299 if (*sensitivity != 0 && value == 0 && lnet_retry_count != 0) {
300 lnet_retry_count = 0;
301 lnet_set_lnd_timeout();
304 *sensitivity = value;
306 mutex_unlock(&the_lnet.ln_api_mutex);
312 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
315 unsigned *interval = (unsigned *)kp->arg;
318 rc = kstrtoul(val, 0, &value);
320 CERROR("Invalid module parameter value for 'lnet_recovery_interval'\n");
325 CERROR("lnet_recovery_interval must be at least 1 second\n");
330 * The purpose of locking the api_mutex here is to ensure that
331 * the correct value ends up stored properly.
333 mutex_lock(&the_lnet.ln_api_mutex);
337 mutex_unlock(&the_lnet.ln_api_mutex);
343 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
346 unsigned *discovery_off = (unsigned *)kp->arg;
348 struct lnet_ping_buffer *pbuf;
350 rc = kstrtoul(val, 0, &value);
352 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
356 value = (value) ? 1 : 0;
359 * The purpose of locking the api_mutex here is to ensure that
360 * the correct value ends up stored properly.
362 mutex_lock(&the_lnet.ln_api_mutex);
364 if (value == *discovery_off) {
365 mutex_unlock(&the_lnet.ln_api_mutex);
370 * We still want to set the discovery value even when LNet is not
371 * running. This is the case when LNet is being loaded and we want
372 * the module parameters to take effect. Otherwise if we're
373 * changing the value dynamically, we want to set it after
376 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
377 *discovery_off = value;
378 mutex_unlock(&the_lnet.ln_api_mutex);
382 /* tell peers that discovery setting has changed */
383 lnet_net_lock(LNET_LOCK_EX);
384 pbuf = the_lnet.ln_ping_target;
386 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
388 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
389 lnet_net_unlock(LNET_LOCK_EX);
391 /* only send a push when we're turning off discovery */
392 if (*discovery_off <= 0 && value > 0)
393 lnet_push_update_to_peers(1);
394 *discovery_off = value;
396 mutex_unlock(&the_lnet.ln_api_mutex);
402 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
405 unsigned int *drop_asym_route = (unsigned int *)kp->arg;
408 rc = kstrtoul(val, 0, &value);
410 CERROR("Invalid module parameter value for "
411 "'lnet_drop_asym_route'\n");
416 * The purpose of locking the api_mutex here is to ensure that
417 * the correct value ends up stored properly.
419 mutex_lock(&the_lnet.ln_api_mutex);
421 if (value == *drop_asym_route) {
422 mutex_unlock(&the_lnet.ln_api_mutex);
426 *drop_asym_route = value;
428 mutex_unlock(&the_lnet.ln_api_mutex);
434 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
437 unsigned *transaction_to = (unsigned *)kp->arg;
440 rc = kstrtoul(val, 0, &value);
442 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
447 * The purpose of locking the api_mutex here is to ensure that
448 * the correct value ends up stored properly.
450 mutex_lock(&the_lnet.ln_api_mutex);
452 if (value <= lnet_retry_count || value == 0) {
453 mutex_unlock(&the_lnet.ln_api_mutex);
454 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
455 "Has to be greater than lnet_retry_count (%u)\n",
456 value, lnet_retry_count);
460 if (value == *transaction_to) {
461 mutex_unlock(&the_lnet.ln_api_mutex);
465 *transaction_to = value;
466 /* Update the lnet_lnd_timeout now that we've modified the
467 * transaction timeout
469 lnet_set_lnd_timeout();
471 mutex_unlock(&the_lnet.ln_api_mutex);
477 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
480 unsigned *retry_count = (unsigned *)kp->arg;
483 rc = kstrtoul(val, 0, &value);
485 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
490 * The purpose of locking the api_mutex here is to ensure that
491 * the correct value ends up stored properly.
493 mutex_lock(&the_lnet.ln_api_mutex);
495 if (lnet_health_sensitivity == 0 && value > 0) {
496 mutex_unlock(&the_lnet.ln_api_mutex);
497 CERROR("Can not set lnet_retry_count when health feature is turned off\n");
501 if (value > lnet_transaction_timeout) {
502 mutex_unlock(&the_lnet.ln_api_mutex);
503 CERROR("Invalid value for lnet_retry_count (%lu). "
504 "Has to be smaller than lnet_transaction_timeout (%u)\n",
505 value, lnet_transaction_timeout);
509 *retry_count = value;
511 /* Update the lnet_lnd_timeout now that we've modified the
514 lnet_set_lnd_timeout();
516 mutex_unlock(&the_lnet.ln_api_mutex);
522 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
526 rc = kstrtoint(val, 0, &value);
528 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
532 if (value < LNET_INTERFACES_MIN) {
533 CWARN("max interfaces provided are too small, setting to %d\n",
534 LNET_INTERFACES_MAX_DEFAULT);
535 value = LNET_INTERFACES_MAX_DEFAULT;
538 *(int *)kp->arg = value;
544 response_tracking_set(const char *val, cfs_kernel_param_arg_t *kp)
547 unsigned long new_value;
549 rc = kstrtoul(val, 0, &new_value);
551 CERROR("Invalid value for 'lnet_response_tracking'\n");
555 if (new_value < 0 || new_value > 3) {
556 CWARN("Invalid value (%lu) for 'lnet_response_tracking'\n",
561 lnet_response_tracking = new_value;
567 lnet_get_routes(void)
573 lnet_get_networks(void)
578 if (*networks != 0 && *ip2nets != 0) {
579 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
580 "'ip2nets' but not both at once\n");
585 rc = lnet_parse_ip2nets(&nets, ip2nets);
586 return (rc == 0) ? nets : NULL;
596 lnet_init_locks(void)
598 spin_lock_init(&the_lnet.ln_eq_wait_lock);
599 spin_lock_init(&the_lnet.ln_msg_resend_lock);
600 init_completion(&the_lnet.ln_mt_wait_complete);
601 mutex_init(&the_lnet.ln_lnd_mutex);
604 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
605 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
607 struct kmem_cache *lnet_rspt_cachep; /* response tracker cache */
608 struct kmem_cache *lnet_msg_cachep;
611 lnet_slab_setup(void)
613 /* create specific kmem_cache for MEs and small MDs (i.e., originally
614 * allocated in <size-xxx> kmem_cache).
616 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
618 if (!lnet_mes_cachep)
621 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
622 LNET_SMALL_MD_SIZE, 0, 0,
624 if (!lnet_small_mds_cachep)
627 lnet_rspt_cachep = kmem_cache_create("lnet_rspt", sizeof(struct lnet_rsp_tracker),
629 if (!lnet_rspt_cachep)
632 lnet_msg_cachep = kmem_cache_create("lnet_msg", sizeof(struct lnet_msg),
634 if (!lnet_msg_cachep)
641 lnet_slab_cleanup(void)
643 if (lnet_msg_cachep) {
644 kmem_cache_destroy(lnet_msg_cachep);
645 lnet_msg_cachep = NULL;
649 if (lnet_rspt_cachep) {
650 kmem_cache_destroy(lnet_rspt_cachep);
651 lnet_rspt_cachep = NULL;
654 if (lnet_small_mds_cachep) {
655 kmem_cache_destroy(lnet_small_mds_cachep);
656 lnet_small_mds_cachep = NULL;
659 if (lnet_mes_cachep) {
660 kmem_cache_destroy(lnet_mes_cachep);
661 lnet_mes_cachep = NULL;
666 lnet_create_remote_nets_table(void)
669 struct list_head *hash;
671 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
672 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
673 CFS_ALLOC_PTR_ARRAY(hash, LNET_REMOTE_NETS_HASH_SIZE);
675 CERROR("Failed to create remote nets hash table\n");
679 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
680 INIT_LIST_HEAD(&hash[i]);
681 the_lnet.ln_remote_nets_hash = hash;
686 lnet_destroy_remote_nets_table(void)
690 if (the_lnet.ln_remote_nets_hash == NULL)
693 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
694 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
696 CFS_FREE_PTR_ARRAY(the_lnet.ln_remote_nets_hash,
697 LNET_REMOTE_NETS_HASH_SIZE);
698 the_lnet.ln_remote_nets_hash = NULL;
702 lnet_destroy_locks(void)
704 if (the_lnet.ln_res_lock != NULL) {
705 cfs_percpt_lock_free(the_lnet.ln_res_lock);
706 the_lnet.ln_res_lock = NULL;
709 if (the_lnet.ln_net_lock != NULL) {
710 cfs_percpt_lock_free(the_lnet.ln_net_lock);
711 the_lnet.ln_net_lock = NULL;
716 lnet_create_locks(void)
720 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
721 if (the_lnet.ln_res_lock == NULL)
724 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
725 if (the_lnet.ln_net_lock == NULL)
731 lnet_destroy_locks();
735 static void lnet_assert_wire_constants(void)
737 /* Wire protocol assertions generated by 'wirecheck'
738 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
739 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
740 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
743 BUILD_BUG_ON(LNET_PROTO_TCP_MAGIC != 0xeebc0ded);
744 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MAJOR != 1);
745 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MINOR != 0);
746 BUILD_BUG_ON(LNET_MSG_ACK != 0);
747 BUILD_BUG_ON(LNET_MSG_PUT != 1);
748 BUILD_BUG_ON(LNET_MSG_GET != 2);
749 BUILD_BUG_ON(LNET_MSG_REPLY != 3);
750 BUILD_BUG_ON(LNET_MSG_HELLO != 4);
752 /* Checks for struct lnet_handle_wire */
753 BUILD_BUG_ON((int)sizeof(struct lnet_handle_wire) != 16);
754 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
755 wh_interface_cookie) != 0);
756 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) != 8);
757 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
758 wh_object_cookie) != 8);
759 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) != 8);
761 /* Checks for struct struct lnet_magicversion */
762 BUILD_BUG_ON((int)sizeof(struct lnet_magicversion) != 8);
763 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, magic) != 0);
764 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->magic) != 4);
765 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, version_major) != 4);
766 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_major) != 2);
767 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion,
768 version_minor) != 6);
769 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_minor) != 2);
771 /* Checks for struct struct lnet_hdr */
772 BUILD_BUG_ON((int)sizeof(struct lnet_hdr) != 72);
773 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_nid) != 0);
774 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_nid) != 8);
775 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_nid) != 8);
776 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_nid) != 8);
777 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_pid) != 16);
778 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_pid) != 4);
779 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_pid) != 20);
780 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_pid) != 4);
781 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, type) != 24);
782 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->type) != 4);
783 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, payload_length) != 28);
784 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->payload_length) != 4);
785 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg) != 32);
786 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg) != 40);
789 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) != 32);
790 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) != 16);
791 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.match_bits) != 48);
792 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) != 8);
793 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.mlength) != 56);
794 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) != 4);
797 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) != 32);
798 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) != 16);
799 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.match_bits) != 48);
800 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) != 8);
801 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.hdr_data) != 56);
802 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) != 8);
803 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ptl_index) != 64);
804 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) != 4);
805 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.offset) != 68);
806 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) != 4);
809 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.return_wmd) != 32);
810 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) != 16);
811 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.match_bits) != 48);
812 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) != 8);
813 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.ptl_index) != 56);
814 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) != 4);
815 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.src_offset) != 60);
816 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) != 4);
817 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.sink_length) != 64);
818 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) != 4);
821 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) != 32);
822 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) != 16);
825 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.incarnation) != 32);
826 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) != 8);
827 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.type) != 40);
828 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) != 4);
830 /* Checks for struct lnet_ni_status and related constants */
831 BUILD_BUG_ON(LNET_NI_STATUS_INVALID != 0x00000000);
832 BUILD_BUG_ON(LNET_NI_STATUS_UP != 0x15aac0de);
833 BUILD_BUG_ON(LNET_NI_STATUS_DOWN != 0xdeadface);
835 /* Checks for struct lnet_ni_status */
836 BUILD_BUG_ON((int)sizeof(struct lnet_ni_status) != 16);
837 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_nid) != 0);
838 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) != 8);
839 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_status) != 8);
840 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_status) != 4);
841 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_unused) != 12);
842 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) != 4);
844 /* Checks for struct lnet_ping_info and related constants */
845 BUILD_BUG_ON(LNET_PROTO_PING_MAGIC != 0x70696E67);
846 BUILD_BUG_ON(LNET_PING_FEAT_INVAL != 0);
847 BUILD_BUG_ON(LNET_PING_FEAT_BASE != 1);
848 BUILD_BUG_ON(LNET_PING_FEAT_NI_STATUS != 2);
849 BUILD_BUG_ON(LNET_PING_FEAT_RTE_DISABLED != 4);
850 BUILD_BUG_ON(LNET_PING_FEAT_MULTI_RAIL != 8);
851 BUILD_BUG_ON(LNET_PING_FEAT_DISCOVERY != 16);
852 BUILD_BUG_ON(LNET_PING_FEAT_BITS != 31);
854 /* Checks for struct lnet_ping_info */
855 BUILD_BUG_ON((int)sizeof(struct lnet_ping_info) != 16);
856 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_magic) != 0);
857 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) != 4);
858 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_features) != 4);
859 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_features) != 4);
860 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_pid) != 8);
861 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) != 4);
862 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_nnis) != 12);
863 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) != 4);
864 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_ni) != 16);
865 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) != 0);
868 static const struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
870 const struct lnet_lnd *lnd;
872 /* holding lnd mutex */
873 if (type >= NUM_LNDS)
875 lnd = the_lnet.ln_lnds[type];
876 LASSERT(!lnd || lnd->lnd_type == type);
882 lnet_get_lnd_timeout(void)
884 return lnet_lnd_timeout;
886 EXPORT_SYMBOL(lnet_get_lnd_timeout);
889 lnet_register_lnd(const struct lnet_lnd *lnd)
891 mutex_lock(&the_lnet.ln_lnd_mutex);
893 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
894 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
896 the_lnet.ln_lnds[lnd->lnd_type] = lnd;
898 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
900 mutex_unlock(&the_lnet.ln_lnd_mutex);
902 EXPORT_SYMBOL(lnet_register_lnd);
905 lnet_unregister_lnd(const struct lnet_lnd *lnd)
907 mutex_lock(&the_lnet.ln_lnd_mutex);
909 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
911 the_lnet.ln_lnds[lnd->lnd_type] = NULL;
912 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
914 mutex_unlock(&the_lnet.ln_lnd_mutex);
916 EXPORT_SYMBOL(lnet_unregister_lnd);
919 lnet_counters_get_common_locked(struct lnet_counters_common *common)
921 struct lnet_counters *ctr;
924 /* FIXME !!! Their is no assert_lnet_net_locked() to ensure this
925 * actually called under the protection of the lnet_net_lock.
927 memset(common, 0, sizeof(*common));
929 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
930 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
931 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
932 common->lcc_errors += ctr->lct_common.lcc_errors;
933 common->lcc_send_count += ctr->lct_common.lcc_send_count;
934 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
935 common->lcc_route_count += ctr->lct_common.lcc_route_count;
936 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
937 common->lcc_send_length += ctr->lct_common.lcc_send_length;
938 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
939 common->lcc_route_length += ctr->lct_common.lcc_route_length;
940 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
945 lnet_counters_get_common(struct lnet_counters_common *common)
947 lnet_net_lock(LNET_LOCK_EX);
948 lnet_counters_get_common_locked(common);
949 lnet_net_unlock(LNET_LOCK_EX);
951 EXPORT_SYMBOL(lnet_counters_get_common);
954 lnet_counters_get(struct lnet_counters *counters)
956 struct lnet_counters *ctr;
957 struct lnet_counters_health *health = &counters->lct_health;
960 memset(counters, 0, sizeof(*counters));
962 lnet_net_lock(LNET_LOCK_EX);
964 if (the_lnet.ln_state != LNET_STATE_RUNNING)
965 GOTO(out_unlock, rc = -ENODEV);
967 lnet_counters_get_common_locked(&counters->lct_common);
969 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
970 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
971 health->lch_resend_count += ctr->lct_health.lch_resend_count;
972 health->lch_response_timeout_count +=
973 ctr->lct_health.lch_response_timeout_count;
974 health->lch_local_interrupt_count +=
975 ctr->lct_health.lch_local_interrupt_count;
976 health->lch_local_dropped_count +=
977 ctr->lct_health.lch_local_dropped_count;
978 health->lch_local_aborted_count +=
979 ctr->lct_health.lch_local_aborted_count;
980 health->lch_local_no_route_count +=
981 ctr->lct_health.lch_local_no_route_count;
982 health->lch_local_timeout_count +=
983 ctr->lct_health.lch_local_timeout_count;
984 health->lch_local_error_count +=
985 ctr->lct_health.lch_local_error_count;
986 health->lch_remote_dropped_count +=
987 ctr->lct_health.lch_remote_dropped_count;
988 health->lch_remote_error_count +=
989 ctr->lct_health.lch_remote_error_count;
990 health->lch_remote_timeout_count +=
991 ctr->lct_health.lch_remote_timeout_count;
992 health->lch_network_timeout_count +=
993 ctr->lct_health.lch_network_timeout_count;
996 lnet_net_unlock(LNET_LOCK_EX);
999 EXPORT_SYMBOL(lnet_counters_get);
1002 lnet_counters_reset(void)
1004 struct lnet_counters *counters;
1007 lnet_net_lock(LNET_LOCK_EX);
1009 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1012 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
1013 memset(counters, 0, sizeof(struct lnet_counters));
1015 lnet_net_unlock(LNET_LOCK_EX);
1019 lnet_res_type2str(int type)
1024 case LNET_COOKIE_TYPE_MD:
1026 case LNET_COOKIE_TYPE_ME:
1028 case LNET_COOKIE_TYPE_EQ:
1034 lnet_res_container_cleanup(struct lnet_res_container *rec)
1038 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
1041 while (!list_empty(&rec->rec_active)) {
1042 struct list_head *e = rec->rec_active.next;
1045 if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
1046 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
1048 } else { /* NB: Active MEs should be attached on portals */
1055 /* Found alive MD/ME/EQ, user really should unlink/free
1056 * all of them before finalize LNet, but if someone didn't,
1057 * we have to recycle garbage for him */
1058 CERROR("%d active elements on exit of %s container\n",
1059 count, lnet_res_type2str(rec->rec_type));
1062 if (rec->rec_lh_hash != NULL) {
1063 CFS_FREE_PTR_ARRAY(rec->rec_lh_hash, LNET_LH_HASH_SIZE);
1064 rec->rec_lh_hash = NULL;
1067 rec->rec_type = 0; /* mark it as finalized */
1071 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1076 LASSERT(rec->rec_type == 0);
1078 rec->rec_type = type;
1079 INIT_LIST_HEAD(&rec->rec_active);
1081 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1083 /* Arbitrary choice of hash table size */
1084 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1085 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1086 if (rec->rec_lh_hash == NULL) {
1091 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1092 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1097 CERROR("Failed to setup %s resource container\n",
1098 lnet_res_type2str(type));
1099 lnet_res_container_cleanup(rec);
1104 lnet_res_containers_destroy(struct lnet_res_container **recs)
1106 struct lnet_res_container *rec;
1109 cfs_percpt_for_each(rec, i, recs)
1110 lnet_res_container_cleanup(rec);
1112 cfs_percpt_free(recs);
1115 static struct lnet_res_container **
1116 lnet_res_containers_create(int type)
1118 struct lnet_res_container **recs;
1119 struct lnet_res_container *rec;
1123 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1125 CERROR("Failed to allocate %s resource containers\n",
1126 lnet_res_type2str(type));
1130 cfs_percpt_for_each(rec, i, recs) {
1131 rc = lnet_res_container_setup(rec, i, type);
1133 lnet_res_containers_destroy(recs);
1141 struct lnet_libhandle *
1142 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1144 /* ALWAYS called with lnet_res_lock held */
1145 struct list_head *head;
1146 struct lnet_libhandle *lh;
1149 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1152 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1153 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1155 list_for_each_entry(lh, head, lh_hash_chain) {
1156 if (lh->lh_cookie == cookie)
1164 lnet_res_lh_initialize(struct lnet_res_container *rec,
1165 struct lnet_libhandle *lh)
1167 /* ALWAYS called with lnet_res_lock held */
1168 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1171 lh->lh_cookie = rec->rec_lh_cookie;
1172 rec->rec_lh_cookie += 1 << ibits;
1174 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1176 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1180 lnet_create_array_of_queues(void)
1182 struct list_head **qs;
1183 struct list_head *q;
1186 qs = cfs_percpt_alloc(lnet_cpt_table(),
1187 sizeof(struct list_head));
1189 CERROR("Failed to allocate queues\n");
1193 cfs_percpt_for_each(q, i, qs)
1199 static int lnet_unprepare(void);
1202 lnet_prepare(lnet_pid_t requested_pid)
1204 /* Prepare to bring up the network */
1205 struct lnet_res_container **recs;
1208 if (requested_pid == LNET_PID_ANY) {
1209 /* Don't instantiate LNET just for me */
1213 LASSERT(the_lnet.ln_refcount == 0);
1215 the_lnet.ln_routing = 0;
1217 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1218 the_lnet.ln_pid = requested_pid;
1220 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1221 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1222 INIT_LIST_HEAD(&the_lnet.ln_nets);
1223 INIT_LIST_HEAD(&the_lnet.ln_routers);
1224 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1225 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1226 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1227 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1228 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1229 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1230 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1231 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1232 the_lnet.ln_mt_handler = NULL;
1233 init_completion(&the_lnet.ln_started);
1235 rc = lnet_slab_setup();
1239 rc = lnet_create_remote_nets_table();
1244 * NB the interface cookie in wire handles guards against delayed
1245 * replies and ACKs appearing valid after reboot.
1247 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1249 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1250 sizeof(struct lnet_counters));
1251 if (the_lnet.ln_counters == NULL) {
1252 CERROR("Failed to allocate counters for LNet\n");
1257 rc = lnet_peer_tables_create();
1261 rc = lnet_msg_containers_create();
1265 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1266 LNET_COOKIE_TYPE_EQ);
1270 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1276 the_lnet.ln_md_containers = recs;
1278 rc = lnet_portals_create();
1280 CERROR("Failed to create portals for LNet: %d\n", rc);
1284 the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1285 if (!the_lnet.ln_mt_zombie_rstqs) {
1298 lnet_unprepare (void)
1300 /* NB no LNET_LOCK since this is the last reference. All LND instances
1301 * have shut down already, so it is safe to unlink and free all
1302 * descriptors, even those that appear committed to a network op (eg MD
1303 * with non-zero pending count) */
1305 lnet_fail_nid(LNET_NID_ANY, 0);
1307 LASSERT(the_lnet.ln_refcount == 0);
1308 LASSERT(list_empty(&the_lnet.ln_test_peers));
1309 LASSERT(list_empty(&the_lnet.ln_nets));
1311 if (the_lnet.ln_mt_zombie_rstqs) {
1312 lnet_clean_zombie_rstqs();
1313 the_lnet.ln_mt_zombie_rstqs = NULL;
1316 lnet_assert_handler_unused(the_lnet.ln_mt_handler);
1317 the_lnet.ln_mt_handler = NULL;
1319 lnet_portals_destroy();
1321 if (the_lnet.ln_md_containers != NULL) {
1322 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1323 the_lnet.ln_md_containers = NULL;
1326 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1328 lnet_msg_containers_destroy();
1330 lnet_rtrpools_free(0);
1332 if (the_lnet.ln_counters != NULL) {
1333 cfs_percpt_free(the_lnet.ln_counters);
1334 the_lnet.ln_counters = NULL;
1336 lnet_destroy_remote_nets_table();
1337 lnet_slab_cleanup();
1343 lnet_net2ni_locked(__u32 net_id, int cpt)
1346 struct lnet_net *net;
1348 LASSERT(cpt != LNET_LOCK_EX);
1350 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1351 if (net->net_id == net_id) {
1352 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1362 lnet_net2ni_addref(__u32 net)
1367 ni = lnet_net2ni_locked(net, 0);
1369 lnet_ni_addref_locked(ni, 0);
1374 EXPORT_SYMBOL(lnet_net2ni_addref);
1377 lnet_get_net_locked(__u32 net_id)
1379 struct lnet_net *net;
1381 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1382 if (net->net_id == net_id)
1390 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1395 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1400 val = hash_long(key, LNET_CPT_BITS);
1401 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1405 return (unsigned int)(key + val + (val >> 1)) % number;
1409 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1411 struct lnet_net *net;
1413 /* must called with hold of lnet_net_lock */
1414 if (LNET_CPT_NUMBER == 1)
1415 return 0; /* the only one */
1418 * If NI is provided then use the CPT identified in the NI cpt
1419 * list if one exists. If one doesn't exist, then that NI is
1420 * associated with all CPTs and it follows that the net it belongs
1421 * to is implicitly associated with all CPTs, so just hash the nid
1425 if (ni->ni_cpts != NULL)
1426 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1429 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1432 /* no NI provided so look at the net */
1433 net = lnet_get_net_locked(LNET_NIDNET(nid));
1435 if (net != NULL && net->net_cpts != NULL) {
1436 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1439 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1443 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1448 if (LNET_CPT_NUMBER == 1)
1449 return 0; /* the only one */
1451 cpt = lnet_net_lock_current();
1453 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1455 lnet_net_unlock(cpt);
1459 EXPORT_SYMBOL(lnet_cpt_of_nid);
1462 lnet_islocalnet_locked(__u32 net_id)
1464 struct lnet_net *net;
1467 net = lnet_get_net_locked(net_id);
1469 local = net != NULL;
1475 lnet_islocalnet(__u32 net_id)
1480 cpt = lnet_net_lock_current();
1482 local = lnet_islocalnet_locked(net_id);
1484 lnet_net_unlock(cpt);
1490 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1492 struct lnet_net *net;
1495 LASSERT(cpt != LNET_LOCK_EX);
1497 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1498 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1499 if (ni->ni_nid == nid)
1508 lnet_nid2ni_addref(lnet_nid_t nid)
1513 ni = lnet_nid2ni_locked(nid, 0);
1515 lnet_ni_addref_locked(ni, 0);
1520 EXPORT_SYMBOL(lnet_nid2ni_addref);
1523 lnet_islocalnid(lnet_nid_t nid)
1528 cpt = lnet_net_lock_current();
1529 ni = lnet_nid2ni_locked(nid, cpt);
1530 lnet_net_unlock(cpt);
1536 lnet_count_acceptor_nets(void)
1538 /* Return the # of NIs that need the acceptor. */
1540 struct lnet_net *net;
1543 cpt = lnet_net_lock_current();
1544 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1545 /* all socklnd type networks should have the acceptor
1547 if (net->net_lnd->lnd_accept != NULL)
1551 lnet_net_unlock(cpt);
1556 struct lnet_ping_buffer *
1557 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1559 struct lnet_ping_buffer *pbuf;
1561 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1563 pbuf->pb_nnis = nnis;
1564 pbuf->pb_needs_post = false;
1565 atomic_set(&pbuf->pb_refcnt, 1);
1572 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1574 LASSERT(atomic_read(&pbuf->pb_refcnt) == 0);
1575 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1578 static struct lnet_ping_buffer *
1579 lnet_ping_target_create(int nnis)
1581 struct lnet_ping_buffer *pbuf;
1583 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1585 CERROR("Can't allocate ping source [%d]\n", nnis);
1589 pbuf->pb_info.pi_nnis = nnis;
1590 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1591 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1592 pbuf->pb_info.pi_features =
1593 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1599 lnet_get_net_ni_count_locked(struct lnet_net *net)
1604 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1611 lnet_get_net_ni_count_pre(struct lnet_net *net)
1616 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1623 lnet_get_ni_count(void)
1626 struct lnet_net *net;
1631 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1632 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1642 lnet_get_net_count(void)
1644 struct lnet_net *net;
1649 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1659 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1661 struct lnet_ni_status *stat;
1665 __swab32s(&pbuf->pb_info.pi_magic);
1666 __swab32s(&pbuf->pb_info.pi_features);
1667 __swab32s(&pbuf->pb_info.pi_pid);
1668 __swab32s(&pbuf->pb_info.pi_nnis);
1669 nnis = pbuf->pb_info.pi_nnis;
1670 if (nnis > pbuf->pb_nnis)
1671 nnis = pbuf->pb_nnis;
1672 for (i = 0; i < nnis; i++) {
1673 stat = &pbuf->pb_info.pi_ni[i];
1674 __swab64s(&stat->ns_nid);
1675 __swab32s(&stat->ns_status);
1680 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1684 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1686 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1688 /* Loopback is guaranteed to be present */
1689 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1691 if (LNET_PING_INFO_LONI(pinfo) != LNET_NID_LO_0)
1697 lnet_ping_target_destroy(void)
1699 struct lnet_net *net;
1702 lnet_net_lock(LNET_LOCK_EX);
1704 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1705 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1707 ni->ni_status = NULL;
1712 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1713 the_lnet.ln_ping_target = NULL;
1715 lnet_net_unlock(LNET_LOCK_EX);
1719 lnet_ping_target_event_handler(struct lnet_event *event)
1721 struct lnet_ping_buffer *pbuf = event->md_user_ptr;
1723 if (event->unlinked)
1724 lnet_ping_buffer_decref(pbuf);
1728 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1729 struct lnet_handle_md *ping_mdh,
1730 int ni_count, bool set_eq)
1732 struct lnet_process_id id = {
1733 .nid = LNET_NID_ANY,
1737 struct lnet_md md = { NULL };
1741 the_lnet.ln_ping_target_handler =
1742 lnet_ping_target_event_handler;
1744 *ppbuf = lnet_ping_target_create(ni_count);
1745 if (*ppbuf == NULL) {
1750 /* Ping target ME/MD */
1751 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1752 LNET_PROTO_PING_MATCHBITS, 0,
1753 LNET_UNLINK, LNET_INS_AFTER);
1756 CERROR("Can't create ping target ME: %d\n", rc);
1757 goto fail_decref_ping_buffer;
1760 /* initialize md content */
1761 md.start = &(*ppbuf)->pb_info;
1762 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1763 md.threshold = LNET_MD_THRESH_INF;
1765 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1766 LNET_MD_MANAGE_REMOTE;
1767 md.handler = the_lnet.ln_ping_target_handler;
1768 md.user_ptr = *ppbuf;
1770 rc = LNetMDAttach(me, &md, LNET_RETAIN, ping_mdh);
1772 CERROR("Can't attach ping target MD: %d\n", rc);
1773 goto fail_decref_ping_buffer;
1775 lnet_ping_buffer_addref(*ppbuf);
1779 fail_decref_ping_buffer:
1780 LASSERT(atomic_read(&(*ppbuf)->pb_refcnt) == 1);
1781 lnet_ping_buffer_decref(*ppbuf);
1788 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1789 struct lnet_handle_md *ping_mdh)
1791 LNetMDUnlink(*ping_mdh);
1792 LNetInvalidateMDHandle(ping_mdh);
1794 /* NB the MD could be busy; this just starts the unlink */
1795 wait_var_event_warning(&pbuf->pb_refcnt,
1796 atomic_read(&pbuf->pb_refcnt) <= 1,
1797 "Still waiting for ping data MD to unlink\n");
1801 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1804 struct lnet_net *net;
1805 struct lnet_ni_status *ns;
1810 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1811 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1812 LASSERT(i < pbuf->pb_nnis);
1814 ns = &pbuf->pb_info.pi_ni[i];
1816 ns->ns_nid = ni->ni_nid;
1819 ns->ns_status = (ni->ni_status != NULL) ?
1820 ni->ni_status->ns_status :
1829 * We (ab)use the ns_status of the loopback interface to
1830 * transmit the sequence number. The first interface listed
1831 * must be the loopback interface.
1833 rc = lnet_ping_info_validate(&pbuf->pb_info);
1835 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1838 LNET_PING_BUFFER_SEQNO(pbuf) =
1839 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1843 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1844 struct lnet_handle_md ping_mdh)
1846 struct lnet_ping_buffer *old_pbuf = NULL;
1847 struct lnet_handle_md old_ping_md;
1849 /* switch the NIs to point to the new ping info created */
1850 lnet_net_lock(LNET_LOCK_EX);
1852 if (!the_lnet.ln_routing)
1853 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1854 if (!lnet_peer_discovery_disabled)
1855 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1857 /* Ensure only known feature bits have been set. */
1858 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1859 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1861 lnet_ping_target_install_locked(pbuf);
1863 if (the_lnet.ln_ping_target) {
1864 old_pbuf = the_lnet.ln_ping_target;
1865 old_ping_md = the_lnet.ln_ping_target_md;
1867 the_lnet.ln_ping_target_md = ping_mdh;
1868 the_lnet.ln_ping_target = pbuf;
1870 lnet_net_unlock(LNET_LOCK_EX);
1873 /* unlink and free the old ping info */
1874 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1875 lnet_ping_buffer_decref(old_pbuf);
1878 lnet_push_update_to_peers(0);
1882 lnet_ping_target_fini(void)
1884 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1885 &the_lnet.ln_ping_target_md);
1887 lnet_assert_handler_unused(the_lnet.ln_ping_target_handler);
1888 lnet_ping_target_destroy();
1891 /* Resize the push target. */
1892 int lnet_push_target_resize(void)
1894 struct lnet_handle_md mdh;
1895 struct lnet_handle_md old_mdh;
1896 struct lnet_ping_buffer *pbuf;
1897 struct lnet_ping_buffer *old_pbuf;
1902 nnis = the_lnet.ln_push_target_nnis;
1904 CDEBUG(D_NET, "Invalid nnis %d\n", nnis);
1908 /* NB: lnet_ping_buffer_alloc() sets pbuf refcount to 1. That ref is
1909 * dropped when we need to resize again (see "old_pbuf" below) or when
1910 * LNet is shutdown (see lnet_push_target_fini())
1912 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1914 CDEBUG(D_NET, "Can't allocate pbuf for nnis %d\n", nnis);
1918 rc = lnet_push_target_post(pbuf, &mdh);
1920 CDEBUG(D_NET, "Failed to post push target: %d\n", rc);
1921 lnet_ping_buffer_decref(pbuf);
1925 lnet_net_lock(LNET_LOCK_EX);
1926 old_pbuf = the_lnet.ln_push_target;
1927 old_mdh = the_lnet.ln_push_target_md;
1928 the_lnet.ln_push_target = pbuf;
1929 the_lnet.ln_push_target_md = mdh;
1930 lnet_net_unlock(LNET_LOCK_EX);
1933 LNetMDUnlink(old_mdh);
1934 /* Drop ref set by lnet_ping_buffer_alloc() */
1935 lnet_ping_buffer_decref(old_pbuf);
1938 /* Received another push or reply that requires a larger buffer */
1939 if (nnis < the_lnet.ln_push_target_nnis)
1942 CDEBUG(D_NET, "nnis %d success\n", nnis);
1946 int lnet_push_target_post(struct lnet_ping_buffer *pbuf,
1947 struct lnet_handle_md *mdhp)
1949 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1950 struct lnet_md md = { NULL };
1954 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1955 LNET_PROTO_PING_MATCHBITS, 0,
1956 LNET_UNLINK, LNET_INS_AFTER);
1959 CERROR("Can't create push target ME: %d\n", rc);
1963 pbuf->pb_needs_post = false;
1965 /* This reference is dropped by lnet_push_target_event_handler() */
1966 lnet_ping_buffer_addref(pbuf);
1968 /* initialize md content */
1969 md.start = &pbuf->pb_info;
1970 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
1973 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE;
1975 md.handler = the_lnet.ln_push_target_handler;
1977 rc = LNetMDAttach(me, &md, LNET_UNLINK, mdhp);
1979 CERROR("Can't attach push MD: %d\n", rc);
1980 lnet_ping_buffer_decref(pbuf);
1981 pbuf->pb_needs_post = true;
1985 CDEBUG(D_NET, "posted push target %p\n", pbuf);
1990 static void lnet_push_target_event_handler(struct lnet_event *ev)
1992 struct lnet_ping_buffer *pbuf = ev->md_user_ptr;
1994 CDEBUG(D_NET, "type %d status %d unlinked %d\n", ev->type, ev->status,
1997 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1998 lnet_swap_pinginfo(pbuf);
2000 if (ev->type == LNET_EVENT_UNLINK) {
2001 /* Drop ref added by lnet_push_target_post() */
2002 lnet_ping_buffer_decref(pbuf);
2006 lnet_peer_push_event(ev);
2008 /* Drop ref added by lnet_push_target_post */
2009 lnet_ping_buffer_decref(pbuf);
2012 /* Initialize the push target. */
2013 static int lnet_push_target_init(void)
2017 if (the_lnet.ln_push_target)
2020 the_lnet.ln_push_target_handler =
2021 lnet_push_target_event_handler;
2023 rc = LNetSetLazyPortal(LNET_RESERVED_PORTAL);
2026 /* Start at the required minimum, we'll enlarge if required. */
2027 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
2029 rc = lnet_push_target_resize();
2032 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2033 the_lnet.ln_push_target_handler = NULL;
2039 /* Clean up the push target. */
2040 static void lnet_push_target_fini(void)
2042 if (!the_lnet.ln_push_target)
2045 /* Unlink and invalidate to prevent new references. */
2046 LNetMDUnlink(the_lnet.ln_push_target_md);
2047 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
2049 /* Wait for the unlink to complete. */
2050 wait_var_event_warning(&the_lnet.ln_push_target->pb_refcnt,
2051 atomic_read(&the_lnet.ln_push_target->pb_refcnt) <= 1,
2052 "Still waiting for ping data MD to unlink\n");
2054 /* Drop ref set by lnet_ping_buffer_alloc() */
2055 lnet_ping_buffer_decref(the_lnet.ln_push_target);
2056 the_lnet.ln_push_target = NULL;
2057 the_lnet.ln_push_target_nnis = 0;
2059 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2060 lnet_assert_handler_unused(the_lnet.ln_push_target_handler);
2061 the_lnet.ln_push_target_handler = NULL;
2065 lnet_ni_tq_credits(struct lnet_ni *ni)
2069 LASSERT(ni->ni_ncpts >= 1);
2071 if (ni->ni_ncpts == 1)
2072 return ni->ni_net->net_tunables.lct_max_tx_credits;
2074 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2075 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2076 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2082 lnet_ni_unlink_locked(struct lnet_ni *ni)
2084 /* move it to zombie list and nobody can find it anymore */
2085 LASSERT(!list_empty(&ni->ni_netlist));
2086 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2087 lnet_ni_decref_locked(ni, 0);
2091 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2096 struct list_head *zombie_list = &net->net_ni_zombie;
2099 * Now wait for the NIs I just nuked to show up on the zombie
2100 * list and shut them down in guaranteed thread context
2103 while (!list_empty(zombie_list)) {
2107 ni = list_entry(zombie_list->next,
2108 struct lnet_ni, ni_netlist);
2109 list_del_init(&ni->ni_netlist);
2110 /* the ni should be in deleting state. If it's not it's
2112 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2113 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2116 /* still busy, add it back to zombie list */
2117 list_add(&ni->ni_netlist, zombie_list);
2121 if (!list_empty(&ni->ni_netlist)) {
2122 /* Unlock mutex while waiting to allow other
2123 * threads to read the LNet state and fall through
2126 lnet_net_unlock(LNET_LOCK_EX);
2127 mutex_unlock(&the_lnet.ln_api_mutex);
2130 if ((i & (-i)) == i) {
2132 "Waiting for zombie LNI %s\n",
2133 libcfs_nid2str(ni->ni_nid));
2135 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2137 mutex_lock(&the_lnet.ln_api_mutex);
2138 lnet_net_lock(LNET_LOCK_EX);
2142 lnet_net_unlock(LNET_LOCK_EX);
2144 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2146 LASSERT(!in_interrupt());
2147 /* Holding the mutex makes it safe for lnd_shutdown
2148 * to call module_put(). Module unload cannot finish
2149 * until lnet_unregister_lnd() completes, and that
2150 * requires the mutex.
2152 mutex_lock(&the_lnet.ln_lnd_mutex);
2153 (net->net_lnd->lnd_shutdown)(ni);
2154 mutex_unlock(&the_lnet.ln_lnd_mutex);
2157 CDEBUG(D_LNI, "Removed LNI %s\n",
2158 libcfs_nid2str(ni->ni_nid));
2162 lnet_net_lock(LNET_LOCK_EX);
2166 /* shutdown down the NI and release refcount */
2168 lnet_shutdown_lndni(struct lnet_ni *ni)
2171 struct lnet_net *net = ni->ni_net;
2173 lnet_net_lock(LNET_LOCK_EX);
2175 ni->ni_state = LNET_NI_STATE_DELETING;
2177 lnet_ni_unlink_locked(ni);
2178 lnet_incr_dlc_seq();
2179 lnet_net_unlock(LNET_LOCK_EX);
2181 /* clear messages for this NI on the lazy portal */
2182 for (i = 0; i < the_lnet.ln_nportals; i++)
2183 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2185 lnet_net_lock(LNET_LOCK_EX);
2186 lnet_clear_zombies_nis_locked(net);
2187 lnet_net_unlock(LNET_LOCK_EX);
2191 lnet_shutdown_lndnet(struct lnet_net *net)
2195 lnet_net_lock(LNET_LOCK_EX);
2197 list_del_init(&net->net_list);
2199 while (!list_empty(&net->net_ni_list)) {
2200 ni = list_entry(net->net_ni_list.next,
2201 struct lnet_ni, ni_netlist);
2202 lnet_net_unlock(LNET_LOCK_EX);
2203 lnet_shutdown_lndni(ni);
2204 lnet_net_lock(LNET_LOCK_EX);
2207 lnet_net_unlock(LNET_LOCK_EX);
2209 /* Do peer table cleanup for this net */
2210 lnet_peer_tables_cleanup(net);
2216 lnet_shutdown_lndnets(void)
2218 struct lnet_net *net;
2220 struct lnet_msg *msg, *tmp;
2222 /* NB called holding the global mutex */
2224 /* All quiet on the API front */
2225 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2226 LASSERT(the_lnet.ln_refcount == 0);
2228 lnet_net_lock(LNET_LOCK_EX);
2229 the_lnet.ln_state = LNET_STATE_STOPPING;
2232 * move the nets to the zombie list to avoid them being
2233 * picked up for new work. LONET is also included in the
2234 * Nets that will be moved to the zombie list
2236 list_splice_init(&the_lnet.ln_nets, &the_lnet.ln_net_zombie);
2238 /* Drop the cached loopback Net. */
2239 if (the_lnet.ln_loni != NULL) {
2240 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2241 the_lnet.ln_loni = NULL;
2243 lnet_net_unlock(LNET_LOCK_EX);
2245 /* iterate through the net zombie list and delete each net */
2246 while (!list_empty(&the_lnet.ln_net_zombie)) {
2247 net = list_entry(the_lnet.ln_net_zombie.next,
2248 struct lnet_net, net_list);
2249 lnet_shutdown_lndnet(net);
2252 spin_lock(&the_lnet.ln_msg_resend_lock);
2253 list_splice(&the_lnet.ln_msg_resend, &resend);
2254 spin_unlock(&the_lnet.ln_msg_resend_lock);
2256 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2257 list_del_init(&msg->msg_list);
2258 msg->msg_no_resend = true;
2259 lnet_finalize(msg, -ECANCELED);
2262 lnet_net_lock(LNET_LOCK_EX);
2263 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2264 lnet_net_unlock(LNET_LOCK_EX);
2268 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2271 struct lnet_tx_queue *tq;
2273 struct lnet_net *net = ni->ni_net;
2275 mutex_lock(&the_lnet.ln_lnd_mutex);
2278 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2279 ni->ni_lnd_tunables_set = true;
2282 rc = (net->net_lnd->lnd_startup)(ni);
2284 mutex_unlock(&the_lnet.ln_lnd_mutex);
2287 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2288 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2293 ni->ni_state = LNET_NI_STATE_ACTIVE;
2296 /* We keep a reference on the loopback net through the loopback NI */
2297 if (net->net_lnd->lnd_type == LOLND) {
2299 LASSERT(the_lnet.ln_loni == NULL);
2300 the_lnet.ln_loni = ni;
2301 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2302 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2303 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2304 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2308 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2309 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2310 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2311 libcfs_lnd2str(net->net_lnd->lnd_type),
2312 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2314 /* shutdown the NI since if we get here then it must've already
2317 lnet_shutdown_lndni(ni);
2321 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2322 tq->tq_credits_min =
2323 tq->tq_credits_max =
2324 tq->tq_credits = lnet_ni_tq_credits(ni);
2327 atomic_set(&ni->ni_tx_credits,
2328 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2329 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2331 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2332 libcfs_nid2str(ni->ni_nid),
2333 ni->ni_net->net_tunables.lct_peer_tx_credits,
2334 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2335 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2336 ni->ni_net->net_tunables.lct_peer_timeout);
2345 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2348 struct lnet_net *net_l = NULL;
2349 LIST_HEAD(local_ni_list);
2353 const struct lnet_lnd *lnd;
2355 net->net_tunables.lct_peer_timeout;
2357 net->net_tunables.lct_max_tx_credits;
2358 int peerrtrcredits =
2359 net->net_tunables.lct_peer_rtr_credits;
2362 * make sure that this net is unique. If it isn't then
2363 * we are adding interfaces to an already existing network, and
2364 * 'net' is just a convenient way to pass in the list.
2365 * if it is unique we need to find the LND and load it if
2368 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2369 lnd_type = LNET_NETTYP(net->net_id);
2371 mutex_lock(&the_lnet.ln_lnd_mutex);
2372 lnd = lnet_find_lnd_by_type(lnd_type);
2375 mutex_unlock(&the_lnet.ln_lnd_mutex);
2376 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2377 mutex_lock(&the_lnet.ln_lnd_mutex);
2379 lnd = lnet_find_lnd_by_type(lnd_type);
2381 mutex_unlock(&the_lnet.ln_lnd_mutex);
2382 CERROR("Can't load LND %s, module %s, rc=%d\n",
2383 libcfs_lnd2str(lnd_type),
2384 libcfs_lnd2modname(lnd_type), rc);
2385 #ifndef HAVE_MODULE_LOADING_SUPPORT
2386 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2387 "compiled with kernel module "
2388 "loading support.");
2397 mutex_unlock(&the_lnet.ln_lnd_mutex);
2403 * net_l: if the network being added is unique then net_l
2404 * will point to that network
2405 * if the network being added is not unique then
2406 * net_l points to the existing network.
2408 * When we enter the loop below, we'll pick NIs off he
2409 * network beign added and start them up, then add them to
2410 * a local ni list. Once we've successfully started all
2411 * the NIs then we join the local NI list (of started up
2412 * networks) with the net_l->net_ni_list, which should
2413 * point to the correct network to add the new ni list to
2415 * If any of the new NIs fail to start up, then we want to
2416 * iterate through the local ni list, which should include
2417 * any NIs which were successfully started up, and shut
2420 * After than we want to delete the network being added,
2421 * to avoid a memory leak.
2425 * When a network uses TCP bonding then all its interfaces
2426 * must be specified when the network is first defined: the
2427 * TCP bonding code doesn't allow for interfaces to be added
2430 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2431 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2436 while (!list_empty(&net->net_ni_added)) {
2437 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2439 list_del_init(&ni->ni_netlist);
2441 /* make sure that the the NI we're about to start
2442 * up is actually unique. if it's not fail. */
2443 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2444 ni->ni_interfaces[0])) {
2449 /* adjust the pointer the parent network, just in case it
2450 * the net is a duplicate */
2453 rc = lnet_startup_lndni(ni, tun);
2459 list_add_tail(&ni->ni_netlist, &local_ni_list);
2464 lnet_net_lock(LNET_LOCK_EX);
2465 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2466 lnet_incr_dlc_seq();
2467 lnet_net_unlock(LNET_LOCK_EX);
2469 /* if the network is not unique then we don't want to keep
2470 * it around after we're done. Free it. Otherwise add that
2471 * net to the global the_lnet.ln_nets */
2472 if (net_l != net && net_l != NULL) {
2474 * TODO - note. currently the tunables can not be updated
2480 * restore tunables after it has been overwitten by the
2483 if (peer_timeout != -1)
2484 net->net_tunables.lct_peer_timeout = peer_timeout;
2485 if (maxtxcredits != -1)
2486 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2487 if (peerrtrcredits != -1)
2488 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2490 lnet_net_lock(LNET_LOCK_EX);
2491 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2492 lnet_net_unlock(LNET_LOCK_EX);
2495 /* update net count */
2496 lnet_current_net_count = lnet_get_net_count();
2502 * shutdown the new NIs that are being started up
2503 * free the NET being started
2505 while (!list_empty(&local_ni_list)) {
2506 ni = list_entry(local_ni_list.next, struct lnet_ni,
2509 lnet_shutdown_lndni(ni);
2519 lnet_startup_lndnets(struct list_head *netlist)
2521 struct lnet_net *net;
2526 * Change to running state before bringing up the LNDs. This
2527 * allows lnet_shutdown_lndnets() to assert that we've passed
2530 lnet_net_lock(LNET_LOCK_EX);
2531 the_lnet.ln_state = LNET_STATE_RUNNING;
2532 lnet_net_unlock(LNET_LOCK_EX);
2534 while (!list_empty(netlist)) {
2535 net = list_entry(netlist->next, struct lnet_net, net_list);
2536 list_del_init(&net->net_list);
2538 rc = lnet_startup_lndnet(net, NULL);
2548 lnet_shutdown_lndnets();
2554 * Initialize LNet library.
2556 * Automatically called at module loading time. Caller has to call
2557 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2558 * latter returned 0. It must be called exactly once.
2560 * \retval 0 on success
2561 * \retval -ve on failures.
2563 int lnet_lib_init(void)
2567 lnet_assert_wire_constants();
2569 /* refer to global cfs_cpt_table for now */
2570 the_lnet.ln_cpt_table = cfs_cpt_tab;
2571 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_tab);
2573 LASSERT(the_lnet.ln_cpt_number > 0);
2574 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2575 /* we are under risk of consuming all lh_cookie */
2576 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2577 "please change setting of CPT-table and retry\n",
2578 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2582 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2583 the_lnet.ln_cpt_bits++;
2585 rc = lnet_create_locks();
2587 CERROR("Can't create LNet global locks: %d\n", rc);
2591 the_lnet.ln_refcount = 0;
2592 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2593 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2595 /* The hash table size is the number of bits it takes to express the set
2596 * ln_num_routes, minus 1 (better to under estimate than over so we
2597 * don't waste memory). */
2598 if (rnet_htable_size <= 0)
2599 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2600 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2601 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2602 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2603 order_base_2(rnet_htable_size) - 1);
2605 /* All LNDs apart from the LOLND are in separate modules. They
2606 * register themselves when their module loads, and unregister
2607 * themselves when their module is unloaded. */
2608 lnet_register_lnd(&the_lolnd);
2613 * Finalize LNet library.
2615 * \pre lnet_lib_init() called with success.
2616 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2618 * As this happens at module-unload, all lnds must already be unloaded,
2619 * so they must already be unregistered.
2621 void lnet_lib_exit(void)
2625 LASSERT(the_lnet.ln_refcount == 0);
2626 lnet_unregister_lnd(&the_lolnd);
2627 for (i = 0; i < NUM_LNDS; i++)
2628 LASSERT(!the_lnet.ln_lnds[i]);
2629 lnet_destroy_locks();
2633 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2635 * Users must call this function at least once before any other functions.
2636 * For each successful call there must be a corresponding call to
2637 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2640 * The PID used by LNet may be different from the one requested.
2643 * \param requested_pid PID requested by the caller.
2645 * \return >= 0 on success, and < 0 error code on failures.
2648 LNetNIInit(lnet_pid_t requested_pid)
2650 int im_a_router = 0;
2653 struct lnet_ping_buffer *pbuf;
2654 struct lnet_handle_md ping_mdh;
2655 LIST_HEAD(net_head);
2656 struct lnet_net *net;
2658 mutex_lock(&the_lnet.ln_api_mutex);
2660 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2662 if (the_lnet.ln_refcount > 0) {
2663 rc = the_lnet.ln_refcount++;
2664 mutex_unlock(&the_lnet.ln_api_mutex);
2668 rc = lnet_prepare(requested_pid);
2670 mutex_unlock(&the_lnet.ln_api_mutex);
2674 /* create a network for Loopback network */
2675 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2678 goto err_empty_list;
2681 /* Add in the loopback NI */
2682 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2684 goto err_empty_list;
2687 /* If LNet is being initialized via DLC it is possible
2688 * that the user requests not to load module parameters (ones which
2689 * are supported by DLC) on initialization. Therefore, make sure not
2690 * to load networks, routes and forwarding from module parameters
2691 * in this case. On cleanup in case of failure only clean up
2692 * routes if it has been loaded */
2693 if (!the_lnet.ln_nis_from_mod_params) {
2694 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2697 goto err_empty_list;
2700 ni_count = lnet_startup_lndnets(&net_head);
2703 goto err_empty_list;
2706 if (!the_lnet.ln_nis_from_mod_params) {
2707 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2709 goto err_shutdown_lndnis;
2711 rc = lnet_rtrpools_alloc(im_a_router);
2713 goto err_destroy_routes;
2716 rc = lnet_acceptor_start();
2718 goto err_destroy_routes;
2720 the_lnet.ln_refcount = 1;
2721 /* Now I may use my own API functions... */
2723 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2725 goto err_acceptor_stop;
2727 lnet_ping_target_update(pbuf, ping_mdh);
2729 the_lnet.ln_mt_handler = lnet_mt_event_handler;
2731 rc = lnet_push_target_init();
2735 rc = lnet_peer_discovery_start();
2737 goto err_destroy_push_target;
2739 rc = lnet_monitor_thr_start();
2741 goto err_stop_discovery_thr;
2744 lnet_router_debugfs_init();
2746 mutex_unlock(&the_lnet.ln_api_mutex);
2748 complete_all(&the_lnet.ln_started);
2750 /* wait for all routers to start */
2751 lnet_wait_router_start();
2755 err_stop_discovery_thr:
2756 lnet_peer_discovery_stop();
2757 err_destroy_push_target:
2758 lnet_push_target_fini();
2760 lnet_ping_target_fini();
2762 the_lnet.ln_refcount = 0;
2763 lnet_acceptor_stop();
2765 if (!the_lnet.ln_nis_from_mod_params)
2766 lnet_destroy_routes();
2767 err_shutdown_lndnis:
2768 lnet_shutdown_lndnets();
2772 mutex_unlock(&the_lnet.ln_api_mutex);
2773 while (!list_empty(&net_head)) {
2774 struct lnet_net *net;
2776 net = list_entry(net_head.next, struct lnet_net, net_list);
2777 list_del_init(&net->net_list);
2782 EXPORT_SYMBOL(LNetNIInit);
2785 * Stop LNet interfaces, routing, and forwarding.
2787 * Users must call this function once for each successful call to LNetNIInit().
2788 * Once the LNetNIFini() operation has been started, the results of pending
2789 * API operations are undefined.
2791 * \return always 0 for current implementation.
2796 mutex_lock(&the_lnet.ln_api_mutex);
2798 LASSERT(the_lnet.ln_refcount > 0);
2800 if (the_lnet.ln_refcount != 1) {
2801 the_lnet.ln_refcount--;
2803 LASSERT(!the_lnet.ln_niinit_self);
2807 lnet_router_debugfs_fini();
2808 lnet_monitor_thr_stop();
2809 lnet_peer_discovery_stop();
2810 lnet_push_target_fini();
2811 lnet_ping_target_fini();
2813 /* Teardown fns that use my own API functions BEFORE here */
2814 the_lnet.ln_refcount = 0;
2816 lnet_acceptor_stop();
2817 lnet_destroy_routes();
2818 lnet_shutdown_lndnets();
2822 mutex_unlock(&the_lnet.ln_api_mutex);
2825 EXPORT_SYMBOL(LNetNIFini);
2828 * Grabs the ni data from the ni structure and fills the out
2831 * \param[in] ni network interface structure
2832 * \param[out] cfg_ni NI config information
2833 * \param[out] tun network and LND tunables
2836 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2837 struct lnet_ioctl_config_lnd_tunables *tun,
2838 struct lnet_ioctl_element_stats *stats,
2841 size_t min_size = 0;
2844 if (!ni || !cfg_ni || !tun)
2847 if (ni->ni_interfaces[0] != NULL) {
2848 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2849 if (ni->ni_interfaces[i] != NULL) {
2850 strncpy(cfg_ni->lic_ni_intf[i],
2851 ni->ni_interfaces[i],
2852 sizeof(cfg_ni->lic_ni_intf[i]));
2857 cfg_ni->lic_nid = ni->ni_nid;
2858 if (ni->ni_nid == LNET_NID_LO_0)
2859 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2861 cfg_ni->lic_status = ni->ni_status->ns_status;
2862 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2863 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2865 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2868 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2869 LNET_STATS_TYPE_SEND);
2870 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2871 LNET_STATS_TYPE_RECV);
2872 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2873 LNET_STATS_TYPE_DROP);
2877 * tun->lt_tun will always be present, but in order to be
2878 * backwards compatible, we need to deal with the cases when
2879 * tun->lt_tun is smaller than what the kernel has, because it
2880 * comes from an older version of a userspace program, then we'll
2881 * need to copy as much information as we have available space.
2883 min_size = tun_size - sizeof(tun->lt_cmn);
2884 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2886 /* copy over the cpts */
2887 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2888 ni->ni_cpts == NULL) {
2889 for (i = 0; i < ni->ni_ncpts; i++)
2890 cfg_ni->lic_cpts[i] = i;
2893 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2894 i < LNET_MAX_SHOW_NUM_CPT;
2896 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2898 cfg_ni->lic_ncpts = ni->ni_ncpts;
2902 * NOTE: This is a legacy function left in the code to be backwards
2903 * compatible with older userspace programs. It should eventually be
2906 * Grabs the ni data from the ni structure and fills the out
2909 * \param[in] ni network interface structure
2910 * \param[out] config config information
2913 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2914 struct lnet_ioctl_config_data *config)
2916 struct lnet_ioctl_net_config *net_config;
2917 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2918 size_t min_size, tunable_size = 0;
2924 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2928 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2929 ARRAY_SIZE(net_config->ni_interfaces));
2931 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2932 if (!ni->ni_interfaces[i])
2935 strncpy(net_config->ni_interfaces[i],
2936 ni->ni_interfaces[i],
2937 sizeof(net_config->ni_interfaces[i]));
2940 config->cfg_nid = ni->ni_nid;
2941 config->cfg_config_u.cfg_net.net_peer_timeout =
2942 ni->ni_net->net_tunables.lct_peer_timeout;
2943 config->cfg_config_u.cfg_net.net_max_tx_credits =
2944 ni->ni_net->net_tunables.lct_max_tx_credits;
2945 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2946 ni->ni_net->net_tunables.lct_peer_tx_credits;
2947 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2948 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2950 if (ni->ni_nid == LNET_NID_LO_0)
2951 net_config->ni_status = LNET_NI_STATUS_UP;
2953 net_config->ni_status = ni->ni_status->ns_status;
2956 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2958 for (i = 0; i < num_cpts; i++)
2959 net_config->ni_cpts[i] = ni->ni_cpts[i];
2961 config->cfg_ncpts = num_cpts;
2965 * See if user land tools sent in a newer and larger version
2966 * of struct lnet_tunables than what the kernel uses.
2968 min_size = sizeof(*config) + sizeof(*net_config);
2970 if (config->cfg_hdr.ioc_len > min_size)
2971 tunable_size = config->cfg_hdr.ioc_len - min_size;
2973 /* Don't copy too much data to user space */
2974 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2975 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2977 if (lnd_cfg && min_size) {
2978 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2979 config->cfg_config_u.cfg_net.net_interface_count = 1;
2981 /* Tell user land that kernel side has less data */
2982 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2983 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2984 config->cfg_hdr.ioc_len -= min_size;
2990 lnet_get_ni_idx_locked(int idx)
2993 struct lnet_net *net;
2995 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2996 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3006 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
3009 struct lnet_net *net = mynet;
3012 * It is possible that the net has been cleaned out while there is
3013 * a message being sent. This function accessed the net without
3014 * checking if the list is empty
3018 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
3020 if (list_empty(&net->net_ni_list))
3022 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3028 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
3029 /* if you reached the end of the ni list and the net is
3030 * specified, then there are no more nis in that net */
3034 /* we reached the end of this net ni list. move to the
3036 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
3037 /* no more nets and no more NIs. */
3040 /* get the next net */
3041 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
3043 if (list_empty(&net->net_ni_list))
3045 /* get the ni on it */
3046 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3052 if (list_empty(&prev->ni_netlist))
3055 /* there are more nis left */
3056 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3062 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3067 int idx = config->cfg_count;
3069 cpt = lnet_net_lock_current();
3071 ni = lnet_get_ni_idx_locked(idx);
3076 lnet_fill_ni_info_legacy(ni, config);
3080 lnet_net_unlock(cpt);
3085 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3086 struct lnet_ioctl_config_lnd_tunables *tun,
3087 struct lnet_ioctl_element_stats *stats,
3094 if (!cfg_ni || !tun || !stats)
3097 cpt = lnet_net_lock_current();
3099 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3104 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3108 lnet_net_unlock(cpt);
3112 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3121 cpt = lnet_net_lock_current();
3123 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3126 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3130 lnet_net_unlock(cpt);
3135 static int lnet_add_net_common(struct lnet_net *net,
3136 struct lnet_ioctl_config_lnd_tunables *tun)
3139 struct lnet_ping_buffer *pbuf;
3140 struct lnet_handle_md ping_mdh;
3142 struct lnet_remotenet *rnet;
3145 lnet_net_lock(LNET_LOCK_EX);
3146 rnet = lnet_find_rnet_locked(net->net_id);
3147 lnet_net_unlock(LNET_LOCK_EX);
3149 * make sure that the net added doesn't invalidate the current
3150 * configuration LNet is keeping
3153 CERROR("Adding net %s will invalidate routing configuration\n",
3154 libcfs_net2str(net->net_id));
3160 * make sure you calculate the correct number of slots in the ping
3161 * buffer. Since the ping info is a flattened list of all the NIs,
3162 * we should allocate enough slots to accomodate the number of NIs
3163 * which will be added.
3165 * since ni hasn't been configured yet, use
3166 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3168 net_ni_count = lnet_get_net_ni_count_pre(net);
3170 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3171 net_ni_count + lnet_get_ni_count(),
3179 memcpy(&net->net_tunables,
3180 &tun->lt_cmn, sizeof(net->net_tunables));
3182 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3184 net_id = net->net_id;
3186 rc = lnet_startup_lndnet(net,
3187 (tun) ? &tun->lt_tun : NULL);
3191 lnet_net_lock(LNET_LOCK_EX);
3192 net = lnet_get_net_locked(net_id);
3193 lnet_net_unlock(LNET_LOCK_EX);
3198 * Start the acceptor thread if this is the first network
3199 * being added that requires the thread.
3201 if (net->net_lnd->lnd_accept) {
3202 rc = lnet_acceptor_start();
3204 /* shutdown the net that we just started */
3205 CERROR("Failed to start up acceptor thread\n");
3206 lnet_shutdown_lndnet(net);
3211 lnet_net_lock(LNET_LOCK_EX);
3212 lnet_peer_net_added(net);
3213 lnet_net_unlock(LNET_LOCK_EX);
3215 lnet_ping_target_update(pbuf, ping_mdh);
3220 lnet_ping_md_unlink(pbuf, &ping_mdh);
3221 lnet_ping_buffer_decref(pbuf);
3226 lnet_set_tune_defaults(struct lnet_ioctl_config_lnd_tunables *tun)
3229 if (!tun->lt_cmn.lct_peer_timeout)
3230 tun->lt_cmn.lct_peer_timeout = DEFAULT_PEER_TIMEOUT;
3231 if (!tun->lt_cmn.lct_peer_tx_credits)
3232 tun->lt_cmn.lct_peer_tx_credits = DEFAULT_PEER_CREDITS;
3233 if (!tun->lt_cmn.lct_max_tx_credits)
3234 tun->lt_cmn.lct_max_tx_credits = DEFAULT_CREDITS;
3238 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3239 struct lnet_ioctl_config_lnd_tunables *tun)
3241 struct lnet_net *net;
3244 LIST_HEAD(net_head);
3246 rc = lnet_parse_ip2nets(&nets, ip2nets);
3250 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3254 lnet_set_tune_defaults(tun);
3256 mutex_lock(&the_lnet.ln_api_mutex);
3257 while (!list_empty(&net_head)) {
3258 net = list_entry(net_head.next, struct lnet_net, net_list);
3259 list_del_init(&net->net_list);
3260 rc = lnet_add_net_common(net, tun);
3266 mutex_unlock(&the_lnet.ln_api_mutex);
3268 while (!list_empty(&net_head)) {
3269 net = list_entry(net_head.next, struct lnet_net, net_list);
3270 list_del_init(&net->net_list);
3276 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3278 struct lnet_net *net;
3280 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3282 __u32 net_id, lnd_type;
3284 /* get the tunables if they are available */
3285 if (conf->lic_cfg_hdr.ioc_len >=
3286 sizeof(*conf) + sizeof(*tun))
3287 tun = (struct lnet_ioctl_config_lnd_tunables *)
3290 /* handle legacy ip2nets from DLC */
3291 if (conf->lic_legacy_ip2nets[0] != '\0')
3292 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3295 net_id = LNET_NIDNET(conf->lic_nid);
3296 lnd_type = LNET_NETTYP(net_id);
3298 if (!libcfs_isknown_lnd(lnd_type)) {
3299 CERROR("No valid net and lnd information provided\n");
3303 net = lnet_net_alloc(net_id, NULL);
3307 for (i = 0; i < conf->lic_ncpts; i++) {
3308 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3312 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3313 conf->lic_ni_intf[0]);
3317 lnet_set_tune_defaults(tun);
3319 mutex_lock(&the_lnet.ln_api_mutex);
3321 rc = lnet_add_net_common(net, tun);
3323 mutex_unlock(&the_lnet.ln_api_mutex);
3328 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3330 struct lnet_net *net;
3332 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3333 struct lnet_ping_buffer *pbuf;
3334 struct lnet_handle_md ping_mdh;
3339 /* don't allow userspace to shutdown the LOLND */
3340 if (LNET_NETTYP(net_id) == LOLND)
3343 mutex_lock(&the_lnet.ln_api_mutex);
3347 net = lnet_get_net_locked(net_id);
3349 CERROR("net %s not found\n",
3350 libcfs_net2str(net_id));
3355 addr = LNET_NIDADDR(conf->lic_nid);
3357 /* remove the entire net */
3358 net_count = lnet_get_net_ni_count_locked(net);
3362 /* create and link a new ping info, before removing the old one */
3363 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3364 lnet_get_ni_count() - net_count,
3367 goto unlock_api_mutex;
3369 lnet_shutdown_lndnet(net);
3371 lnet_acceptor_stop();
3373 lnet_ping_target_update(pbuf, ping_mdh);
3375 goto unlock_api_mutex;
3378 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3380 CERROR("nid %s not found\n",
3381 libcfs_nid2str(conf->lic_nid));
3386 net_count = lnet_get_net_ni_count_locked(net);
3390 /* create and link a new ping info, before removing the old one */
3391 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3392 lnet_get_ni_count() - 1, false);
3394 goto unlock_api_mutex;
3396 lnet_shutdown_lndni(ni);
3398 lnet_acceptor_stop();
3400 lnet_ping_target_update(pbuf, ping_mdh);
3402 /* check if the net is empty and remove it if it is */
3404 lnet_shutdown_lndnet(net);
3406 goto unlock_api_mutex;
3411 mutex_unlock(&the_lnet.ln_api_mutex);
3417 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3418 * They are only expected to be called for unique networks.
3419 * That can be as a result of older DLC library
3420 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3423 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3425 struct lnet_net *net;
3426 LIST_HEAD(net_head);
3428 struct lnet_ioctl_config_lnd_tunables tun;
3429 const char *nets = conf->cfg_config_u.cfg_net.net_intf;
3431 /* Create a net/ni structures for the network string */
3432 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3434 return rc == 0 ? -EINVAL : rc;
3436 mutex_lock(&the_lnet.ln_api_mutex);
3439 rc = -EINVAL; /* only add one network per call */
3440 goto out_unlock_clean;
3443 net = list_entry(net_head.next, struct lnet_net, net_list);
3444 list_del_init(&net->net_list);
3446 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3448 memset(&tun, 0, sizeof(tun));
3450 tun.lt_cmn.lct_peer_timeout =
3451 (!conf->cfg_config_u.cfg_net.net_peer_timeout) ? DEFAULT_PEER_TIMEOUT :
3452 conf->cfg_config_u.cfg_net.net_peer_timeout;
3453 tun.lt_cmn.lct_peer_tx_credits =
3454 (!conf->cfg_config_u.cfg_net.net_peer_tx_credits) ? DEFAULT_PEER_CREDITS :
3455 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3456 tun.lt_cmn.lct_peer_rtr_credits =
3457 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3458 tun.lt_cmn.lct_max_tx_credits =
3459 (!conf->cfg_config_u.cfg_net.net_max_tx_credits) ? DEFAULT_CREDITS :
3460 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3462 rc = lnet_add_net_common(net, &tun);
3465 mutex_unlock(&the_lnet.ln_api_mutex);
3466 while (!list_empty(&net_head)) {
3467 /* net_head list is empty in success case */
3468 net = list_entry(net_head.next, struct lnet_net, net_list);
3469 list_del_init(&net->net_list);
3476 lnet_dyn_del_net(__u32 net_id)
3478 struct lnet_net *net;
3479 struct lnet_ping_buffer *pbuf;
3480 struct lnet_handle_md ping_mdh;
3484 /* don't allow userspace to shutdown the LOLND */
3485 if (LNET_NETTYP(net_id) == LOLND)
3488 mutex_lock(&the_lnet.ln_api_mutex);
3492 net = lnet_get_net_locked(net_id);
3499 net_ni_count = lnet_get_net_ni_count_locked(net);
3503 /* create and link a new ping info, before removing the old one */
3504 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3505 lnet_get_ni_count() - net_ni_count, false);
3509 lnet_shutdown_lndnet(net);
3511 lnet_acceptor_stop();
3513 lnet_ping_target_update(pbuf, ping_mdh);
3516 mutex_unlock(&the_lnet.ln_api_mutex);
3521 void lnet_incr_dlc_seq(void)
3523 atomic_inc(&lnet_dlc_seq_no);
3526 __u32 lnet_get_dlc_seq_locked(void)
3528 return atomic_read(&lnet_dlc_seq_no);
3532 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3534 struct lnet_net *net;
3537 lnet_net_lock(LNET_LOCK_EX);
3538 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3539 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3540 if (ni->ni_nid == nid || all) {
3541 atomic_set(&ni->ni_healthv, value);
3542 if (list_empty(&ni->ni_recovery) &&
3543 value < LNET_MAX_HEALTH_VALUE) {
3544 CERROR("manually adding local NI %s to recovery\n",
3545 libcfs_nid2str(ni->ni_nid));
3546 list_add_tail(&ni->ni_recovery,
3547 &the_lnet.ln_mt_localNIRecovq);
3548 lnet_ni_addref_locked(ni, 0);
3551 lnet_net_unlock(LNET_LOCK_EX);
3557 lnet_net_unlock(LNET_LOCK_EX);
3561 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3565 lnet_nid_t nid = stats->hlni_nid;
3567 cpt = lnet_net_lock_current();
3568 ni = lnet_nid2ni_locked(nid, cpt);
3575 stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3576 stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3577 stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3578 stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3579 stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3580 stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3581 stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3584 lnet_net_unlock(cpt);
3590 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3595 lnet_net_lock(LNET_LOCK_EX);
3596 list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3597 list->rlst_nid_array[i] = ni->ni_nid;
3599 if (i >= LNET_MAX_SHOW_NUM_NID)
3602 lnet_net_unlock(LNET_LOCK_EX);
3603 list->rlst_num_nids = i;
3609 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3611 struct lnet_peer_ni *lpni;
3614 lnet_net_lock(LNET_LOCK_EX);
3615 list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3616 list->rlst_nid_array[i] = lpni->lpni_nid;
3618 if (i >= LNET_MAX_SHOW_NUM_NID)
3621 lnet_net_unlock(LNET_LOCK_EX);
3622 list->rlst_num_nids = i;
3628 * LNet ioctl handler.
3632 LNetCtl(unsigned int cmd, void *arg)
3634 struct libcfs_ioctl_data *data = arg;
3635 struct lnet_ioctl_config_data *config;
3636 struct lnet_process_id id = {0};
3640 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3641 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3644 case IOC_LIBCFS_GET_NI:
3645 rc = LNetGetId(data->ioc_count, &id);
3646 data->ioc_nid = id.nid;
3649 case IOC_LIBCFS_FAIL_NID:
3650 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3652 case IOC_LIBCFS_ADD_ROUTE: {
3653 /* default router sensitivity to 1 */
3654 unsigned int sensitivity = 1;
3657 if (config->cfg_hdr.ioc_len < sizeof(*config))
3660 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3662 config->cfg_config_u.cfg_route.rtr_sensitivity;
3665 mutex_lock(&the_lnet.ln_api_mutex);
3666 rc = lnet_add_route(config->cfg_net,
3667 config->cfg_config_u.cfg_route.rtr_hop,
3669 config->cfg_config_u.cfg_route.
3670 rtr_priority, sensitivity);
3671 mutex_unlock(&the_lnet.ln_api_mutex);
3675 case IOC_LIBCFS_DEL_ROUTE:
3678 if (config->cfg_hdr.ioc_len < sizeof(*config))
3681 mutex_lock(&the_lnet.ln_api_mutex);
3682 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3683 mutex_unlock(&the_lnet.ln_api_mutex);
3686 case IOC_LIBCFS_GET_ROUTE:
3689 if (config->cfg_hdr.ioc_len < sizeof(*config))
3692 mutex_lock(&the_lnet.ln_api_mutex);
3693 rc = lnet_get_route(config->cfg_count,
3695 &config->cfg_config_u.cfg_route.rtr_hop,
3697 &config->cfg_config_u.cfg_route.rtr_flags,
3698 &config->cfg_config_u.cfg_route.
3700 &config->cfg_config_u.cfg_route.
3702 mutex_unlock(&the_lnet.ln_api_mutex);
3705 case IOC_LIBCFS_GET_LOCAL_NI: {
3706 struct lnet_ioctl_config_ni *cfg_ni;
3707 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3708 struct lnet_ioctl_element_stats *stats;
3713 /* get the tunables if they are available */
3714 if (cfg_ni->lic_cfg_hdr.ioc_len <
3715 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3718 stats = (struct lnet_ioctl_element_stats *)
3720 tun = (struct lnet_ioctl_config_lnd_tunables *)
3721 (cfg_ni->lic_bulk + sizeof(*stats));
3723 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3726 mutex_lock(&the_lnet.ln_api_mutex);
3727 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3728 mutex_unlock(&the_lnet.ln_api_mutex);
3732 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3733 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3735 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3738 mutex_lock(&the_lnet.ln_api_mutex);
3739 rc = lnet_get_ni_stats(msg_stats);
3740 mutex_unlock(&the_lnet.ln_api_mutex);
3745 case IOC_LIBCFS_GET_NET: {
3746 size_t total = sizeof(*config) +
3747 sizeof(struct lnet_ioctl_net_config);
3750 if (config->cfg_hdr.ioc_len < total)
3753 mutex_lock(&the_lnet.ln_api_mutex);
3754 rc = lnet_get_net_config(config);
3755 mutex_unlock(&the_lnet.ln_api_mutex);
3759 case IOC_LIBCFS_GET_LNET_STATS:
3761 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3763 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3766 mutex_lock(&the_lnet.ln_api_mutex);
3767 rc = lnet_counters_get(&lnet_stats->st_cntrs);
3768 mutex_unlock(&the_lnet.ln_api_mutex);
3772 case IOC_LIBCFS_CONFIG_RTR:
3775 if (config->cfg_hdr.ioc_len < sizeof(*config))
3778 mutex_lock(&the_lnet.ln_api_mutex);
3779 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3780 rc = lnet_rtrpools_enable();
3781 mutex_unlock(&the_lnet.ln_api_mutex);
3784 lnet_rtrpools_disable();
3785 mutex_unlock(&the_lnet.ln_api_mutex);
3788 case IOC_LIBCFS_ADD_BUF:
3791 if (config->cfg_hdr.ioc_len < sizeof(*config))
3794 mutex_lock(&the_lnet.ln_api_mutex);
3795 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3797 config->cfg_config_u.cfg_buffers.
3799 config->cfg_config_u.cfg_buffers.
3801 mutex_unlock(&the_lnet.ln_api_mutex);
3804 case IOC_LIBCFS_SET_NUMA_RANGE: {
3805 struct lnet_ioctl_set_value *numa;
3807 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3809 lnet_net_lock(LNET_LOCK_EX);
3810 lnet_numa_range = numa->sv_value;
3811 lnet_net_unlock(LNET_LOCK_EX);
3815 case IOC_LIBCFS_GET_NUMA_RANGE: {
3816 struct lnet_ioctl_set_value *numa;
3818 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3820 numa->sv_value = lnet_numa_range;
3824 case IOC_LIBCFS_GET_BUF: {
3825 struct lnet_ioctl_pool_cfg *pool_cfg;
3826 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3830 if (config->cfg_hdr.ioc_len < total)
3833 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3835 mutex_lock(&the_lnet.ln_api_mutex);
3836 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3837 mutex_unlock(&the_lnet.ln_api_mutex);
3841 case IOC_LIBCFS_GET_LOCAL_HSTATS: {
3842 struct lnet_ioctl_local_ni_hstats *stats = arg;
3844 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
3847 mutex_lock(&the_lnet.ln_api_mutex);
3848 rc = lnet_get_local_ni_hstats(stats);
3849 mutex_unlock(&the_lnet.ln_api_mutex);
3854 case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
3855 struct lnet_ioctl_recovery_list *list = arg;
3856 if (list->rlst_hdr.ioc_len < sizeof(*list))
3859 mutex_lock(&the_lnet.ln_api_mutex);
3860 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
3861 rc = lnet_get_local_ni_recovery_list(list);
3863 rc = lnet_get_peer_ni_recovery_list(list);
3864 mutex_unlock(&the_lnet.ln_api_mutex);
3868 case IOC_LIBCFS_ADD_PEER_NI: {
3869 struct lnet_ioctl_peer_cfg *cfg = arg;
3871 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3874 mutex_lock(&the_lnet.ln_api_mutex);
3875 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3878 mutex_unlock(&the_lnet.ln_api_mutex);
3882 case IOC_LIBCFS_DEL_PEER_NI: {
3883 struct lnet_ioctl_peer_cfg *cfg = arg;
3885 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3888 mutex_lock(&the_lnet.ln_api_mutex);
3889 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3890 cfg->prcfg_cfg_nid);
3891 mutex_unlock(&the_lnet.ln_api_mutex);
3895 case IOC_LIBCFS_GET_PEER_INFO: {
3896 struct lnet_ioctl_peer *peer_info = arg;
3898 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3901 mutex_lock(&the_lnet.ln_api_mutex);
3902 rc = lnet_get_peer_ni_info(
3903 peer_info->pr_count,
3905 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3906 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3907 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3908 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3909 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3910 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3911 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3912 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3913 mutex_unlock(&the_lnet.ln_api_mutex);
3917 case IOC_LIBCFS_GET_PEER_NI: {
3918 struct lnet_ioctl_peer_cfg *cfg = arg;
3920 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3923 mutex_lock(&the_lnet.ln_api_mutex);
3924 rc = lnet_get_peer_info(cfg,
3925 (void __user *)cfg->prcfg_bulk);
3926 mutex_unlock(&the_lnet.ln_api_mutex);
3930 case IOC_LIBCFS_GET_PEER_LIST: {
3931 struct lnet_ioctl_peer_cfg *cfg = arg;
3933 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3936 mutex_lock(&the_lnet.ln_api_mutex);
3937 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3938 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3939 mutex_unlock(&the_lnet.ln_api_mutex);
3943 case IOC_LIBCFS_SET_HEALHV: {
3944 struct lnet_ioctl_reset_health_cfg *cfg = arg;
3946 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
3948 if (cfg->rh_value < 0 ||
3949 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
3950 value = LNET_MAX_HEALTH_VALUE;
3952 value = cfg->rh_value;
3953 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
3954 value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
3955 "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
3956 mutex_lock(&the_lnet.ln_api_mutex);
3957 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
3958 lnet_ni_set_healthv(cfg->rh_nid, value,
3961 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
3963 mutex_unlock(&the_lnet.ln_api_mutex);
3967 case IOC_LIBCFS_NOTIFY_ROUTER: {
3968 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3970 /* The deadline passed in by the user should be some time in
3971 * seconds in the future since the UNIX epoch. We have to map
3972 * that deadline to the wall clock.
3974 deadline += ktime_get_seconds();
3975 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
3979 case IOC_LIBCFS_LNET_DIST:
3980 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3981 if (rc < 0 && rc != -EHOSTUNREACH)
3984 data->ioc_u32[0] = rc;
3987 case IOC_LIBCFS_TESTPROTOCOMPAT:
3988 the_lnet.ln_testprotocompat = data->ioc_flags;
3991 case IOC_LIBCFS_LNET_FAULT:
3992 return lnet_fault_ctl(data->ioc_flags, data);
3994 case IOC_LIBCFS_PING: {
3995 signed long timeout;
3997 id.nid = data->ioc_nid;
3998 id.pid = data->ioc_u32[0];
4000 /* If timeout is negative then set default of 3 minutes */
4001 if (((s32)data->ioc_u32[1] <= 0) ||
4002 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
4003 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
4005 timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
4007 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
4008 data->ioc_plen1 / sizeof(struct lnet_process_id));
4013 data->ioc_count = rc;
4017 case IOC_LIBCFS_PING_PEER: {
4018 struct lnet_ioctl_ping_data *ping = arg;
4019 struct lnet_peer *lp;
4020 signed long timeout;
4022 /* If timeout is negative then set default of 3 minutes */
4023 if (((s32)ping->op_param) <= 0 ||
4024 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
4025 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
4027 timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
4029 rc = lnet_ping(ping->ping_id, timeout,
4035 mutex_lock(&the_lnet.ln_api_mutex);
4036 lp = lnet_find_peer(ping->ping_id.nid);
4038 ping->ping_id.nid = lp->lp_primary_nid;
4039 ping->mr_info = lnet_peer_is_multi_rail(lp);
4040 lnet_peer_decref_locked(lp);
4042 mutex_unlock(&the_lnet.ln_api_mutex);
4044 ping->ping_count = rc;
4048 case IOC_LIBCFS_DISCOVER: {
4049 struct lnet_ioctl_ping_data *discover = arg;
4050 struct lnet_peer *lp;
4052 rc = lnet_discover(discover->ping_id, discover->op_param,
4054 discover->ping_count);
4058 mutex_lock(&the_lnet.ln_api_mutex);
4059 lp = lnet_find_peer(discover->ping_id.nid);
4061 discover->ping_id.nid = lp->lp_primary_nid;
4062 discover->mr_info = lnet_peer_is_multi_rail(lp);
4063 lnet_peer_decref_locked(lp);
4065 mutex_unlock(&the_lnet.ln_api_mutex);
4067 discover->ping_count = rc;
4072 ni = lnet_net2ni_addref(data->ioc_net);
4076 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
4079 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4086 EXPORT_SYMBOL(LNetCtl);
4088 void LNetDebugPeer(struct lnet_process_id id)
4090 lnet_debug_peer(id.nid);
4092 EXPORT_SYMBOL(LNetDebugPeer);
4095 * Determine if the specified peer \a nid is on the local node.
4097 * \param nid peer nid to check
4099 * \retval true If peer NID is on the local node.
4100 * \retval false If peer NID is not on the local node.
4102 bool LNetIsPeerLocal(lnet_nid_t nid)
4104 struct lnet_net *net;
4108 cpt = lnet_net_lock_current();
4109 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4110 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4111 if (ni->ni_nid == nid) {
4112 lnet_net_unlock(cpt);
4117 lnet_net_unlock(cpt);
4121 EXPORT_SYMBOL(LNetIsPeerLocal);
4124 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4125 * Note that all interfaces share a same PID, as requested by LNetNIInit().
4127 * \param index Index of the interface to look up.
4128 * \param id On successful return, this location will hold the
4129 * struct lnet_process_id ID of the interface.
4131 * \retval 0 If an interface exists at \a index.
4132 * \retval -ENOENT If no interface has been found.
4135 LNetGetId(unsigned int index, struct lnet_process_id *id)
4138 struct lnet_net *net;
4142 LASSERT(the_lnet.ln_refcount > 0);
4144 cpt = lnet_net_lock_current();
4146 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4147 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4151 id->nid = ni->ni_nid;
4152 id->pid = the_lnet.ln_pid;
4158 lnet_net_unlock(cpt);
4161 EXPORT_SYMBOL(LNetGetId);
4166 struct lnet_handle_md mdh;
4167 struct completion completion;
4171 lnet_ping_event_handler(struct lnet_event *event)
4173 struct ping_data *pd = event->md_user_ptr;
4175 CDEBUG(D_NET, "ping event (%d %d)%s\n",
4176 event->type, event->status,
4177 event->unlinked ? " unlinked" : "");
4179 if (event->status) {
4181 pd->rc = event->status;
4182 } else if (event->type == LNET_EVENT_REPLY) {
4184 pd->rc = event->mlength;
4186 if (event->unlinked)
4187 complete(&pd->completion);
4190 static int lnet_ping(struct lnet_process_id id, signed long timeout,
4191 struct lnet_process_id __user *ids, int n_ids)
4193 struct lnet_md md = { NULL };
4194 struct ping_data pd = { 0 };
4195 struct lnet_ping_buffer *pbuf;
4196 struct lnet_process_id tmpid;
4202 /* n_ids limit is arbitrary */
4203 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4207 * if the user buffer has more space than the lnet_interfaces_max
4208 * then only fill it up to lnet_interfaces_max
4210 if (n_ids > lnet_interfaces_max)
4211 n_ids = lnet_interfaces_max;
4213 if (id.pid == LNET_PID_ANY)
4214 id.pid = LNET_PID_LUSTRE;
4216 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4220 /* initialize md content */
4221 md.start = &pbuf->pb_info;
4222 md.length = LNET_PING_INFO_SIZE(n_ids);
4223 md.threshold = 2; /* GET/REPLY */
4225 md.options = LNET_MD_TRUNCATE;
4227 md.handler = lnet_ping_event_handler;
4229 init_completion(&pd.completion);
4231 rc = LNetMDBind(&md, LNET_UNLINK, &pd.mdh);
4233 CERROR("Can't bind MD: %d\n", rc);
4234 goto fail_ping_buffer_decref;
4237 rc = LNetGet(LNET_NID_ANY, pd.mdh, id,
4238 LNET_RESERVED_PORTAL,
4239 LNET_PROTO_PING_MATCHBITS, 0, false);
4242 /* Don't CERROR; this could be deliberate! */
4243 rc2 = LNetMDUnlink(pd.mdh);
4246 /* NB must wait for the UNLINK event below... */
4249 if (wait_for_completion_timeout(&pd.completion, timeout) == 0) {
4250 /* Ensure completion in finite time... */
4251 LNetMDUnlink(pd.mdh);
4252 wait_for_completion(&pd.completion);
4256 goto fail_ping_buffer_decref;
4260 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4262 rc = -EPROTO; /* if I can't parse... */
4265 CERROR("%s: ping info too short %d\n",
4266 libcfs_id2str(id), nob);
4267 goto fail_ping_buffer_decref;
4270 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4271 lnet_swap_pinginfo(pbuf);
4272 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4273 CERROR("%s: Unexpected magic %08x\n",
4274 libcfs_id2str(id), pbuf->pb_info.pi_magic);
4275 goto fail_ping_buffer_decref;
4278 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4279 CERROR("%s: ping w/o NI status: 0x%x\n",
4280 libcfs_id2str(id), pbuf->pb_info.pi_features);
4281 goto fail_ping_buffer_decref;
4284 if (nob < LNET_PING_INFO_SIZE(0)) {
4285 CERROR("%s: Short reply %d(%d min)\n",
4287 nob, (int)LNET_PING_INFO_SIZE(0));
4288 goto fail_ping_buffer_decref;
4291 if (pbuf->pb_info.pi_nnis < n_ids)
4292 n_ids = pbuf->pb_info.pi_nnis;
4294 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4295 CERROR("%s: Short reply %d(%d expected)\n",
4297 nob, (int)LNET_PING_INFO_SIZE(n_ids));
4298 goto fail_ping_buffer_decref;
4301 rc = -EFAULT; /* if I segv in copy_to_user()... */
4303 memset(&tmpid, 0, sizeof(tmpid));
4304 for (i = 0; i < n_ids; i++) {
4305 tmpid.pid = pbuf->pb_info.pi_pid;
4306 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4307 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4308 goto fail_ping_buffer_decref;
4310 rc = pbuf->pb_info.pi_nnis;
4312 fail_ping_buffer_decref:
4313 lnet_ping_buffer_decref(pbuf);
4318 lnet_discover(struct lnet_process_id id, __u32 force,
4319 struct lnet_process_id __user *ids, int n_ids)
4321 struct lnet_peer_ni *lpni;
4322 struct lnet_peer_ni *p;
4323 struct lnet_peer *lp;
4324 struct lnet_process_id *buf;
4330 id.nid == LNET_NID_ANY)
4333 if (id.pid == LNET_PID_ANY)
4334 id.pid = LNET_PID_LUSTRE;
4337 * If the user buffer has more space than the lnet_interfaces_max,
4338 * then only fill it up to lnet_interfaces_max.
4340 if (n_ids > lnet_interfaces_max)
4341 n_ids = lnet_interfaces_max;
4343 CFS_ALLOC_PTR_ARRAY(buf, n_ids);
4347 cpt = lnet_net_lock_current();
4348 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4355 * Clearing the NIDS_UPTODATE flag ensures the peer will
4356 * be discovered, provided discovery has not been disabled.
4358 lp = lpni->lpni_peer_net->lpn_peer;
4359 spin_lock(&lp->lp_lock);
4360 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4361 /* If the force flag is set, force a PING and PUSH as well. */
4363 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4364 spin_unlock(&lp->lp_lock);
4365 rc = lnet_discover_peer_locked(lpni, cpt, true);
4371 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4372 buf[i].pid = id.pid;
4373 buf[i].nid = p->lpni_nid;
4380 lnet_peer_ni_decref_locked(lpni);
4382 lnet_net_unlock(cpt);
4385 if (copy_to_user(ids, buf, rc * sizeof(*buf)))
4387 CFS_FREE_PTR_ARRAY(buf, n_ids);
4393 * Retrieve peer discovery status.
4395 * \retval 1 if lnet_peer_discovery_disabled is 0
4396 * \retval 0 if lnet_peer_discovery_disabled is 1
4399 LNetGetPeerDiscoveryStatus(void)
4401 return !lnet_peer_discovery_disabled;
4403 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);