4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LNET
35 #include <linux/ctype.h>
36 #include <linux/log2.h>
37 #include <linux/ktime.h>
38 #include <linux/moduleparam.h>
39 #include <linux/uaccess.h>
40 #ifdef HAVE_SCHED_HEADERS
41 #include <linux/sched/signal.h>
44 #include <lnet/lib-lnet.h>
46 #define D_LNI D_CONSOLE
49 * initialize ln_api_mutex statically, since it needs to be used in
50 * discovery_set callback. That module parameter callback can be called
51 * before module init completes. The mutex needs to be ready for use then.
53 struct lnet the_lnet = {
54 .ln_api_mutex = __MUTEX_INITIALIZER(the_lnet.ln_api_mutex),
55 }; /* THE state of the network */
56 EXPORT_SYMBOL(the_lnet);
58 static char *ip2nets = "";
59 module_param(ip2nets, charp, 0444);
60 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
62 static char *networks = "";
63 module_param(networks, charp, 0444);
64 MODULE_PARM_DESC(networks, "local networks");
66 static char *routes = "";
67 module_param(routes, charp, 0444);
68 MODULE_PARM_DESC(routes, "routes to non-local networks");
70 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
71 module_param(rnet_htable_size, int, 0444);
72 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
74 static int use_tcp_bonding = false;
75 module_param(use_tcp_bonding, int, 0444);
76 MODULE_PARM_DESC(use_tcp_bonding,
77 "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
79 unsigned int lnet_numa_range = 0;
80 module_param(lnet_numa_range, uint, 0444);
81 MODULE_PARM_DESC(lnet_numa_range,
82 "NUMA range to consider during Multi-Rail selection");
85 * lnet_health_sensitivity determines by how much we decrement the health
86 * value on sending error. The value defaults to 100, which means health
87 * interface health is decremented by 100 points every failure.
89 unsigned int lnet_health_sensitivity = 100;
90 static int sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
91 #ifdef HAVE_KERNEL_PARAM_OPS
92 static struct kernel_param_ops param_ops_health_sensitivity = {
93 .set = sensitivity_set,
96 #define param_check_health_sensitivity(name, p) \
97 __param_check(name, p, int)
98 module_param(lnet_health_sensitivity, health_sensitivity, S_IRUGO|S_IWUSR);
100 module_param_call(lnet_health_sensitivity, sensitivity_set, param_get_int,
101 &lnet_health_sensitivity, S_IRUGO|S_IWUSR);
103 MODULE_PARM_DESC(lnet_health_sensitivity,
104 "Value to decrement the health value by on error");
107 * lnet_recovery_interval determines how often we should perform recovery
108 * on unhealthy interfaces.
110 unsigned int lnet_recovery_interval = 1;
111 static int recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp);
112 #ifdef HAVE_KERNEL_PARAM_OPS
113 static struct kernel_param_ops param_ops_recovery_interval = {
114 .set = recovery_interval_set,
115 .get = param_get_int,
117 #define param_check_recovery_interval(name, p) \
118 __param_check(name, p, int)
119 module_param(lnet_recovery_interval, recovery_interval, S_IRUGO|S_IWUSR);
121 module_param_call(lnet_recovery_interval, recovery_interval_set, param_get_int,
122 &lnet_recovery_interval, S_IRUGO|S_IWUSR);
124 MODULE_PARM_DESC(lnet_recovery_interval,
125 "Interval to recover unhealthy interfaces in seconds");
127 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
128 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
130 static struct kernel_param_ops param_ops_interfaces_max = {
132 .get = param_get_int,
135 #define param_check_interfaces_max(name, p) \
136 __param_check(name, p, int)
138 #ifdef HAVE_KERNEL_PARAM_OPS
139 module_param(lnet_interfaces_max, interfaces_max, 0644);
141 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
142 ¶m_ops_interfaces_max, 0644);
144 MODULE_PARM_DESC(lnet_interfaces_max,
145 "Maximum number of interfaces in a node.");
147 unsigned lnet_peer_discovery_disabled = 0;
148 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
150 static struct kernel_param_ops param_ops_discovery_disabled = {
151 .set = discovery_set,
152 .get = param_get_int,
155 #define param_check_discovery_disabled(name, p) \
156 __param_check(name, p, int)
157 #ifdef HAVE_KERNEL_PARAM_OPS
158 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
160 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
161 ¶m_ops_discovery_disabled, 0644);
163 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
164 "Set to 1 to disable peer discovery on this node.");
166 unsigned int lnet_drop_asym_route;
167 static int drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp);
169 static struct kernel_param_ops param_ops_drop_asym_route = {
170 .set = drop_asym_route_set,
171 .get = param_get_int,
174 #define param_check_drop_asym_route(name, p) \
175 __param_check(name, p, int)
176 #ifdef HAVE_KERNEL_PARAM_OPS
177 module_param(lnet_drop_asym_route, drop_asym_route, 0644);
179 module_param_call(lnet_drop_asym_route, drop_asym_route_set, param_get_int,
180 ¶m_ops_drop_asym_route, 0644);
182 MODULE_PARM_DESC(lnet_drop_asym_route,
183 "Set to 1 to drop asymmetrical route messages.");
185 #define LNET_TRANSACTION_TIMEOUT_NO_HEALTH_DEFAULT 50
186 #define LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT 50
188 unsigned lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT;
189 static int transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp);
190 #ifdef HAVE_KERNEL_PARAM_OPS
191 static struct kernel_param_ops param_ops_transaction_timeout = {
192 .set = transaction_to_set,
193 .get = param_get_int,
196 #define param_check_transaction_timeout(name, p) \
197 __param_check(name, p, int)
198 module_param(lnet_transaction_timeout, transaction_timeout, S_IRUGO|S_IWUSR);
200 module_param_call(lnet_transaction_timeout, transaction_to_set, param_get_int,
201 &lnet_transaction_timeout, S_IRUGO|S_IWUSR);
203 MODULE_PARM_DESC(lnet_transaction_timeout,
204 "Maximum number of seconds to wait for a peer response.");
206 #define LNET_RETRY_COUNT_HEALTH_DEFAULT 2
207 unsigned lnet_retry_count = LNET_RETRY_COUNT_HEALTH_DEFAULT;
208 static int retry_count_set(const char *val, cfs_kernel_param_arg_t *kp);
209 #ifdef HAVE_KERNEL_PARAM_OPS
210 static struct kernel_param_ops param_ops_retry_count = {
211 .set = retry_count_set,
212 .get = param_get_int,
215 #define param_check_retry_count(name, p) \
216 __param_check(name, p, int)
217 module_param(lnet_retry_count, retry_count, S_IRUGO|S_IWUSR);
219 module_param_call(lnet_retry_count, retry_count_set, param_get_int,
220 &lnet_retry_count, S_IRUGO|S_IWUSR);
222 MODULE_PARM_DESC(lnet_retry_count,
223 "Maximum number of times to retry transmitting a message");
226 unsigned lnet_lnd_timeout = LNET_LND_DEFAULT_TIMEOUT;
227 unsigned int lnet_current_net_count;
230 * This sequence number keeps track of how many times DLC was used to
231 * update the local NIs. It is incremented when a NI is added or
232 * removed and checked when sending a message to determine if there is
233 * a need to re-run the selection algorithm. See lnet_select_pathway()
234 * for more details on its usage.
236 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
238 static int lnet_ping(struct lnet_process_id id, signed long timeout,
239 struct lnet_process_id __user *ids, int n_ids);
241 static int lnet_discover(struct lnet_process_id id, __u32 force,
242 struct lnet_process_id __user *ids, int n_ids);
245 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
248 unsigned *sensitivity = (unsigned *)kp->arg;
251 rc = kstrtoul(val, 0, &value);
253 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
258 * The purpose of locking the api_mutex here is to ensure that
259 * the correct value ends up stored properly.
261 mutex_lock(&the_lnet.ln_api_mutex);
263 if (value > LNET_MAX_HEALTH_VALUE) {
264 mutex_unlock(&the_lnet.ln_api_mutex);
265 CERROR("Invalid health value. Maximum: %d value = %lu\n",
266 LNET_MAX_HEALTH_VALUE, value);
271 * if we're turning on health then use the health timeout
274 if (*sensitivity == 0 && value != 0) {
275 lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT;
276 lnet_retry_count = LNET_RETRY_COUNT_HEALTH_DEFAULT;
278 * if we're turning off health then use the no health timeout
281 } else if (*sensitivity != 0 && value == 0) {
282 lnet_transaction_timeout =
283 LNET_TRANSACTION_TIMEOUT_NO_HEALTH_DEFAULT;
284 lnet_retry_count = 0;
287 *sensitivity = value;
289 mutex_unlock(&the_lnet.ln_api_mutex);
295 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
298 unsigned *interval = (unsigned *)kp->arg;
301 rc = kstrtoul(val, 0, &value);
303 CERROR("Invalid module parameter value for 'lnet_recovery_interval'\n");
308 CERROR("lnet_recovery_interval must be at least 1 second\n");
313 * The purpose of locking the api_mutex here is to ensure that
314 * the correct value ends up stored properly.
316 mutex_lock(&the_lnet.ln_api_mutex);
320 mutex_unlock(&the_lnet.ln_api_mutex);
326 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
329 unsigned *discovery = (unsigned *)kp->arg;
331 struct lnet_ping_buffer *pbuf;
333 rc = kstrtoul(val, 0, &value);
335 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
339 value = (value) ? 1 : 0;
342 * The purpose of locking the api_mutex here is to ensure that
343 * the correct value ends up stored properly.
345 mutex_lock(&the_lnet.ln_api_mutex);
347 if (value == *discovery) {
348 mutex_unlock(&the_lnet.ln_api_mutex);
354 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
355 mutex_unlock(&the_lnet.ln_api_mutex);
359 /* tell peers that discovery setting has changed */
360 lnet_net_lock(LNET_LOCK_EX);
361 pbuf = the_lnet.ln_ping_target;
363 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
365 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
366 lnet_net_unlock(LNET_LOCK_EX);
368 lnet_push_update_to_peers(1);
370 mutex_unlock(&the_lnet.ln_api_mutex);
376 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
379 unsigned int *drop_asym_route = (unsigned int *)kp->arg;
382 rc = kstrtoul(val, 0, &value);
384 CERROR("Invalid module parameter value for "
385 "'lnet_drop_asym_route'\n");
390 * The purpose of locking the api_mutex here is to ensure that
391 * the correct value ends up stored properly.
393 mutex_lock(&the_lnet.ln_api_mutex);
395 if (value == *drop_asym_route) {
396 mutex_unlock(&the_lnet.ln_api_mutex);
400 *drop_asym_route = value;
402 mutex_unlock(&the_lnet.ln_api_mutex);
408 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
411 unsigned *transaction_to = (unsigned *)kp->arg;
414 rc = kstrtoul(val, 0, &value);
416 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
421 * The purpose of locking the api_mutex here is to ensure that
422 * the correct value ends up stored properly.
424 mutex_lock(&the_lnet.ln_api_mutex);
426 if (value < lnet_retry_count || value == 0) {
427 mutex_unlock(&the_lnet.ln_api_mutex);
428 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
429 "Has to be greater than lnet_retry_count (%u)\n",
430 value, lnet_retry_count);
434 if (value == *transaction_to) {
435 mutex_unlock(&the_lnet.ln_api_mutex);
439 *transaction_to = value;
440 if (lnet_retry_count == 0)
441 lnet_lnd_timeout = value;
443 lnet_lnd_timeout = value / lnet_retry_count;
445 mutex_unlock(&the_lnet.ln_api_mutex);
451 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
454 unsigned *retry_count = (unsigned *)kp->arg;
457 rc = kstrtoul(val, 0, &value);
459 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
464 * The purpose of locking the api_mutex here is to ensure that
465 * the correct value ends up stored properly.
467 mutex_lock(&the_lnet.ln_api_mutex);
469 if (lnet_health_sensitivity == 0) {
470 mutex_unlock(&the_lnet.ln_api_mutex);
471 CERROR("Can not set retry_count when health feature is turned off\n");
475 if (value > lnet_transaction_timeout) {
476 mutex_unlock(&the_lnet.ln_api_mutex);
477 CERROR("Invalid value for lnet_retry_count (%lu). "
478 "Has to be smaller than lnet_transaction_timeout (%u)\n",
479 value, lnet_transaction_timeout);
483 *retry_count = value;
486 lnet_lnd_timeout = lnet_transaction_timeout;
488 lnet_lnd_timeout = lnet_transaction_timeout / value;
490 mutex_unlock(&the_lnet.ln_api_mutex);
496 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
500 rc = kstrtoint(val, 0, &value);
502 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
506 if (value < LNET_INTERFACES_MIN) {
507 CWARN("max interfaces provided are too small, setting to %d\n",
508 LNET_INTERFACES_MAX_DEFAULT);
509 value = LNET_INTERFACES_MAX_DEFAULT;
512 *(int *)kp->arg = value;
518 lnet_get_routes(void)
524 lnet_get_networks(void)
529 if (*networks != 0 && *ip2nets != 0) {
530 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
531 "'ip2nets' but not both at once\n");
536 rc = lnet_parse_ip2nets(&nets, ip2nets);
537 return (rc == 0) ? nets : NULL;
547 lnet_init_locks(void)
549 spin_lock_init(&the_lnet.ln_eq_wait_lock);
550 spin_lock_init(&the_lnet.ln_msg_resend_lock);
551 init_completion(&the_lnet.ln_mt_wait_complete);
552 mutex_init(&the_lnet.ln_lnd_mutex);
555 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
556 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
558 struct kmem_cache *lnet_rspt_cachep; /* response tracker cache */
559 struct kmem_cache *lnet_msg_cachep;
562 lnet_slab_setup(void)
564 /* create specific kmem_cache for MEs and small MDs (i.e., originally
565 * allocated in <size-xxx> kmem_cache).
567 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
569 if (!lnet_mes_cachep)
572 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
573 LNET_SMALL_MD_SIZE, 0, 0,
575 if (!lnet_small_mds_cachep)
578 lnet_rspt_cachep = kmem_cache_create("lnet_rspt", sizeof(struct lnet_rsp_tracker),
580 if (!lnet_rspt_cachep)
583 lnet_msg_cachep = kmem_cache_create("lnet_msg", sizeof(struct lnet_msg),
585 if (!lnet_msg_cachep)
592 lnet_slab_cleanup(void)
594 if (lnet_msg_cachep) {
595 kmem_cache_destroy(lnet_msg_cachep);
596 lnet_msg_cachep = NULL;
600 if (lnet_rspt_cachep) {
601 kmem_cache_destroy(lnet_rspt_cachep);
602 lnet_rspt_cachep = NULL;
605 if (lnet_small_mds_cachep) {
606 kmem_cache_destroy(lnet_small_mds_cachep);
607 lnet_small_mds_cachep = NULL;
610 if (lnet_mes_cachep) {
611 kmem_cache_destroy(lnet_mes_cachep);
612 lnet_mes_cachep = NULL;
617 lnet_create_remote_nets_table(void)
620 struct list_head *hash;
622 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
623 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
624 CFS_ALLOC_PTR_ARRAY(hash, LNET_REMOTE_NETS_HASH_SIZE);
626 CERROR("Failed to create remote nets hash table\n");
630 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
631 INIT_LIST_HEAD(&hash[i]);
632 the_lnet.ln_remote_nets_hash = hash;
637 lnet_destroy_remote_nets_table(void)
641 if (the_lnet.ln_remote_nets_hash == NULL)
644 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
645 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
647 CFS_FREE_PTR_ARRAY(the_lnet.ln_remote_nets_hash,
648 LNET_REMOTE_NETS_HASH_SIZE);
649 the_lnet.ln_remote_nets_hash = NULL;
653 lnet_destroy_locks(void)
655 if (the_lnet.ln_res_lock != NULL) {
656 cfs_percpt_lock_free(the_lnet.ln_res_lock);
657 the_lnet.ln_res_lock = NULL;
660 if (the_lnet.ln_net_lock != NULL) {
661 cfs_percpt_lock_free(the_lnet.ln_net_lock);
662 the_lnet.ln_net_lock = NULL;
667 lnet_create_locks(void)
671 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
672 if (the_lnet.ln_res_lock == NULL)
675 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
676 if (the_lnet.ln_net_lock == NULL)
682 lnet_destroy_locks();
686 static void lnet_assert_wire_constants(void)
688 /* Wire protocol assertions generated by 'wirecheck'
689 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
690 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
691 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
694 BUILD_BUG_ON(LNET_PROTO_TCP_MAGIC != 0xeebc0ded);
695 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MAJOR != 1);
696 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MINOR != 0);
697 BUILD_BUG_ON(LNET_MSG_ACK != 0);
698 BUILD_BUG_ON(LNET_MSG_PUT != 1);
699 BUILD_BUG_ON(LNET_MSG_GET != 2);
700 BUILD_BUG_ON(LNET_MSG_REPLY != 3);
701 BUILD_BUG_ON(LNET_MSG_HELLO != 4);
703 /* Checks for struct lnet_handle_wire */
704 BUILD_BUG_ON((int)sizeof(struct lnet_handle_wire) != 16);
705 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
706 wh_interface_cookie) != 0);
707 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) != 8);
708 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
709 wh_object_cookie) != 8);
710 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) != 8);
712 /* Checks for struct struct lnet_magicversion */
713 BUILD_BUG_ON((int)sizeof(struct lnet_magicversion) != 8);
714 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, magic) != 0);
715 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->magic) != 4);
716 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, version_major) != 4);
717 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_major) != 2);
718 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion,
719 version_minor) != 6);
720 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_minor) != 2);
722 /* Checks for struct struct lnet_hdr */
723 BUILD_BUG_ON((int)sizeof(struct lnet_hdr) != 72);
724 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_nid) != 0);
725 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_nid) != 8);
726 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_nid) != 8);
727 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_nid) != 8);
728 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_pid) != 16);
729 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_pid) != 4);
730 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_pid) != 20);
731 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_pid) != 4);
732 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, type) != 24);
733 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->type) != 4);
734 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, payload_length) != 28);
735 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->payload_length) != 4);
736 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg) != 32);
737 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg) != 40);
740 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) != 32);
741 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) != 16);
742 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.match_bits) != 48);
743 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) != 8);
744 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.mlength) != 56);
745 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) != 4);
748 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) != 32);
749 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) != 16);
750 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.match_bits) != 48);
751 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) != 8);
752 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.hdr_data) != 56);
753 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) != 8);
754 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ptl_index) != 64);
755 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) != 4);
756 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.offset) != 68);
757 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) != 4);
760 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.return_wmd) != 32);
761 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) != 16);
762 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.match_bits) != 48);
763 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) != 8);
764 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.ptl_index) != 56);
765 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) != 4);
766 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.src_offset) != 60);
767 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) != 4);
768 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.sink_length) != 64);
769 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) != 4);
772 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) != 32);
773 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) != 16);
776 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.incarnation) != 32);
777 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) != 8);
778 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.type) != 40);
779 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) != 4);
781 /* Checks for struct lnet_ni_status and related constants */
782 BUILD_BUG_ON(LNET_NI_STATUS_INVALID != 0x00000000);
783 BUILD_BUG_ON(LNET_NI_STATUS_UP != 0x15aac0de);
784 BUILD_BUG_ON(LNET_NI_STATUS_DOWN != 0xdeadface);
786 /* Checks for struct lnet_ni_status */
787 BUILD_BUG_ON((int)sizeof(struct lnet_ni_status) != 16);
788 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_nid) != 0);
789 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) != 8);
790 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_status) != 8);
791 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_status) != 4);
792 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_unused) != 12);
793 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) != 4);
795 /* Checks for struct lnet_ping_info and related constants */
796 BUILD_BUG_ON(LNET_PROTO_PING_MAGIC != 0x70696E67);
797 BUILD_BUG_ON(LNET_PING_FEAT_INVAL != 0);
798 BUILD_BUG_ON(LNET_PING_FEAT_BASE != 1);
799 BUILD_BUG_ON(LNET_PING_FEAT_NI_STATUS != 2);
800 BUILD_BUG_ON(LNET_PING_FEAT_RTE_DISABLED != 4);
801 BUILD_BUG_ON(LNET_PING_FEAT_MULTI_RAIL != 8);
802 BUILD_BUG_ON(LNET_PING_FEAT_DISCOVERY != 16);
803 BUILD_BUG_ON(LNET_PING_FEAT_BITS != 31);
805 /* Checks for struct lnet_ping_info */
806 BUILD_BUG_ON((int)sizeof(struct lnet_ping_info) != 16);
807 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_magic) != 0);
808 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) != 4);
809 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_features) != 4);
810 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_features) != 4);
811 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_pid) != 8);
812 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) != 4);
813 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_nnis) != 12);
814 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) != 4);
815 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_ni) != 16);
816 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) != 0);
819 static const struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
821 const struct lnet_lnd *lnd;
823 /* holding lnd mutex */
824 if (type >= NUM_LNDS)
826 lnd = the_lnet.ln_lnds[type];
827 LASSERT(!lnd || lnd->lnd_type == type);
833 lnet_get_lnd_timeout(void)
835 return lnet_lnd_timeout;
837 EXPORT_SYMBOL(lnet_get_lnd_timeout);
840 lnet_register_lnd(const struct lnet_lnd *lnd)
842 mutex_lock(&the_lnet.ln_lnd_mutex);
844 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
845 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
847 the_lnet.ln_lnds[lnd->lnd_type] = lnd;
849 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
851 mutex_unlock(&the_lnet.ln_lnd_mutex);
853 EXPORT_SYMBOL(lnet_register_lnd);
856 lnet_unregister_lnd(const struct lnet_lnd *lnd)
858 mutex_lock(&the_lnet.ln_lnd_mutex);
860 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
862 the_lnet.ln_lnds[lnd->lnd_type] = NULL;
863 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
865 mutex_unlock(&the_lnet.ln_lnd_mutex);
867 EXPORT_SYMBOL(lnet_unregister_lnd);
870 lnet_counters_get_common(struct lnet_counters_common *common)
872 struct lnet_counters *ctr;
875 memset(common, 0, sizeof(*common));
877 lnet_net_lock(LNET_LOCK_EX);
879 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
880 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
881 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
882 common->lcc_errors += ctr->lct_common.lcc_errors;
883 common->lcc_send_count += ctr->lct_common.lcc_send_count;
884 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
885 common->lcc_route_count += ctr->lct_common.lcc_route_count;
886 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
887 common->lcc_send_length += ctr->lct_common.lcc_send_length;
888 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
889 common->lcc_route_length += ctr->lct_common.lcc_route_length;
890 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
892 lnet_net_unlock(LNET_LOCK_EX);
894 EXPORT_SYMBOL(lnet_counters_get_common);
897 lnet_counters_get(struct lnet_counters *counters)
899 struct lnet_counters *ctr;
900 struct lnet_counters_health *health = &counters->lct_health;
903 memset(counters, 0, sizeof(*counters));
905 lnet_counters_get_common(&counters->lct_common);
907 lnet_net_lock(LNET_LOCK_EX);
909 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
910 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
911 health->lch_resend_count += ctr->lct_health.lch_resend_count;
912 health->lch_response_timeout_count +=
913 ctr->lct_health.lch_response_timeout_count;
914 health->lch_local_interrupt_count +=
915 ctr->lct_health.lch_local_interrupt_count;
916 health->lch_local_dropped_count +=
917 ctr->lct_health.lch_local_dropped_count;
918 health->lch_local_aborted_count +=
919 ctr->lct_health.lch_local_aborted_count;
920 health->lch_local_no_route_count +=
921 ctr->lct_health.lch_local_no_route_count;
922 health->lch_local_timeout_count +=
923 ctr->lct_health.lch_local_timeout_count;
924 health->lch_local_error_count +=
925 ctr->lct_health.lch_local_error_count;
926 health->lch_remote_dropped_count +=
927 ctr->lct_health.lch_remote_dropped_count;
928 health->lch_remote_error_count +=
929 ctr->lct_health.lch_remote_error_count;
930 health->lch_remote_timeout_count +=
931 ctr->lct_health.lch_remote_timeout_count;
932 health->lch_network_timeout_count +=
933 ctr->lct_health.lch_network_timeout_count;
935 lnet_net_unlock(LNET_LOCK_EX);
937 EXPORT_SYMBOL(lnet_counters_get);
940 lnet_counters_reset(void)
942 struct lnet_counters *counters;
945 lnet_net_lock(LNET_LOCK_EX);
947 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
948 memset(counters, 0, sizeof(struct lnet_counters));
950 lnet_net_unlock(LNET_LOCK_EX);
954 lnet_res_type2str(int type)
959 case LNET_COOKIE_TYPE_MD:
961 case LNET_COOKIE_TYPE_ME:
963 case LNET_COOKIE_TYPE_EQ:
969 lnet_res_container_cleanup(struct lnet_res_container *rec)
973 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
976 while (!list_empty(&rec->rec_active)) {
977 struct list_head *e = rec->rec_active.next;
980 if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
981 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
983 } else { /* NB: Active MEs should be attached on portals */
990 /* Found alive MD/ME/EQ, user really should unlink/free
991 * all of them before finalize LNet, but if someone didn't,
992 * we have to recycle garbage for him */
993 CERROR("%d active elements on exit of %s container\n",
994 count, lnet_res_type2str(rec->rec_type));
997 if (rec->rec_lh_hash != NULL) {
998 CFS_FREE_PTR_ARRAY(rec->rec_lh_hash, LNET_LH_HASH_SIZE);
999 rec->rec_lh_hash = NULL;
1002 rec->rec_type = 0; /* mark it as finalized */
1006 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1011 LASSERT(rec->rec_type == 0);
1013 rec->rec_type = type;
1014 INIT_LIST_HEAD(&rec->rec_active);
1016 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1018 /* Arbitrary choice of hash table size */
1019 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1020 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1021 if (rec->rec_lh_hash == NULL) {
1026 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1027 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1032 CERROR("Failed to setup %s resource container\n",
1033 lnet_res_type2str(type));
1034 lnet_res_container_cleanup(rec);
1039 lnet_res_containers_destroy(struct lnet_res_container **recs)
1041 struct lnet_res_container *rec;
1044 cfs_percpt_for_each(rec, i, recs)
1045 lnet_res_container_cleanup(rec);
1047 cfs_percpt_free(recs);
1050 static struct lnet_res_container **
1051 lnet_res_containers_create(int type)
1053 struct lnet_res_container **recs;
1054 struct lnet_res_container *rec;
1058 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1060 CERROR("Failed to allocate %s resource containers\n",
1061 lnet_res_type2str(type));
1065 cfs_percpt_for_each(rec, i, recs) {
1066 rc = lnet_res_container_setup(rec, i, type);
1068 lnet_res_containers_destroy(recs);
1076 struct lnet_libhandle *
1077 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1079 /* ALWAYS called with lnet_res_lock held */
1080 struct list_head *head;
1081 struct lnet_libhandle *lh;
1084 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1087 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1088 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1090 list_for_each_entry(lh, head, lh_hash_chain) {
1091 if (lh->lh_cookie == cookie)
1099 lnet_res_lh_initialize(struct lnet_res_container *rec,
1100 struct lnet_libhandle *lh)
1102 /* ALWAYS called with lnet_res_lock held */
1103 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1106 lh->lh_cookie = rec->rec_lh_cookie;
1107 rec->rec_lh_cookie += 1 << ibits;
1109 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1111 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1115 lnet_create_array_of_queues(void)
1117 struct list_head **qs;
1118 struct list_head *q;
1121 qs = cfs_percpt_alloc(lnet_cpt_table(),
1122 sizeof(struct list_head));
1124 CERROR("Failed to allocate queues\n");
1128 cfs_percpt_for_each(q, i, qs)
1134 static int lnet_unprepare(void);
1137 lnet_prepare(lnet_pid_t requested_pid)
1139 /* Prepare to bring up the network */
1140 struct lnet_res_container **recs;
1143 if (requested_pid == LNET_PID_ANY) {
1144 /* Don't instantiate LNET just for me */
1148 LASSERT(the_lnet.ln_refcount == 0);
1150 the_lnet.ln_routing = 0;
1152 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1153 the_lnet.ln_pid = requested_pid;
1155 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1156 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1157 INIT_LIST_HEAD(&the_lnet.ln_nets);
1158 INIT_LIST_HEAD(&the_lnet.ln_routers);
1159 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1160 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1161 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1162 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1163 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1164 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1165 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1166 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1167 the_lnet.ln_mt_eq = NULL;
1168 init_completion(&the_lnet.ln_started);
1170 rc = lnet_slab_setup();
1174 rc = lnet_create_remote_nets_table();
1179 * NB the interface cookie in wire handles guards against delayed
1180 * replies and ACKs appearing valid after reboot.
1182 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1184 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1185 sizeof(struct lnet_counters));
1186 if (the_lnet.ln_counters == NULL) {
1187 CERROR("Failed to allocate counters for LNet\n");
1192 rc = lnet_peer_tables_create();
1196 rc = lnet_msg_containers_create();
1200 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1201 LNET_COOKIE_TYPE_EQ);
1205 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1211 the_lnet.ln_md_containers = recs;
1213 rc = lnet_portals_create();
1215 CERROR("Failed to create portals for LNet: %d\n", rc);
1219 the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1220 if (!the_lnet.ln_mt_zombie_rstqs) {
1233 lnet_unprepare (void)
1237 /* NB no LNET_LOCK since this is the last reference. All LND instances
1238 * have shut down already, so it is safe to unlink and free all
1239 * descriptors, even those that appear committed to a network op (eg MD
1240 * with non-zero pending count) */
1242 lnet_fail_nid(LNET_NID_ANY, 0);
1244 LASSERT(the_lnet.ln_refcount == 0);
1245 LASSERT(list_empty(&the_lnet.ln_test_peers));
1246 LASSERT(list_empty(&the_lnet.ln_nets));
1248 if (the_lnet.ln_mt_zombie_rstqs) {
1249 lnet_clean_zombie_rstqs();
1250 the_lnet.ln_mt_zombie_rstqs = NULL;
1253 if (the_lnet.ln_mt_eq) {
1254 rc = LNetEQFree(the_lnet.ln_mt_eq);
1255 the_lnet.ln_mt_eq = NULL;
1259 lnet_portals_destroy();
1261 if (the_lnet.ln_md_containers != NULL) {
1262 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1263 the_lnet.ln_md_containers = NULL;
1266 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1268 lnet_msg_containers_destroy();
1270 lnet_rtrpools_free(0);
1272 if (the_lnet.ln_counters != NULL) {
1273 cfs_percpt_free(the_lnet.ln_counters);
1274 the_lnet.ln_counters = NULL;
1276 lnet_destroy_remote_nets_table();
1277 lnet_slab_cleanup();
1283 lnet_net2ni_locked(__u32 net_id, int cpt)
1286 struct lnet_net *net;
1288 LASSERT(cpt != LNET_LOCK_EX);
1290 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1291 if (net->net_id == net_id) {
1292 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1302 lnet_net2ni_addref(__u32 net)
1307 ni = lnet_net2ni_locked(net, 0);
1309 lnet_ni_addref_locked(ni, 0);
1314 EXPORT_SYMBOL(lnet_net2ni_addref);
1317 lnet_get_net_locked(__u32 net_id)
1319 struct lnet_net *net;
1321 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1322 if (net->net_id == net_id)
1330 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1335 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1340 val = hash_long(key, LNET_CPT_BITS);
1341 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1345 return (unsigned int)(key + val + (val >> 1)) % number;
1349 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1351 struct lnet_net *net;
1353 /* must called with hold of lnet_net_lock */
1354 if (LNET_CPT_NUMBER == 1)
1355 return 0; /* the only one */
1358 * If NI is provided then use the CPT identified in the NI cpt
1359 * list if one exists. If one doesn't exist, then that NI is
1360 * associated with all CPTs and it follows that the net it belongs
1361 * to is implicitly associated with all CPTs, so just hash the nid
1365 if (ni->ni_cpts != NULL)
1366 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1369 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1372 /* no NI provided so look at the net */
1373 net = lnet_get_net_locked(LNET_NIDNET(nid));
1375 if (net != NULL && net->net_cpts != NULL) {
1376 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1379 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1383 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1388 if (LNET_CPT_NUMBER == 1)
1389 return 0; /* the only one */
1391 cpt = lnet_net_lock_current();
1393 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1395 lnet_net_unlock(cpt);
1399 EXPORT_SYMBOL(lnet_cpt_of_nid);
1402 lnet_islocalnet_locked(__u32 net_id)
1404 struct lnet_net *net;
1407 net = lnet_get_net_locked(net_id);
1409 local = net != NULL;
1415 lnet_islocalnet(__u32 net_id)
1420 cpt = lnet_net_lock_current();
1422 local = lnet_islocalnet_locked(net_id);
1424 lnet_net_unlock(cpt);
1430 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1432 struct lnet_net *net;
1435 LASSERT(cpt != LNET_LOCK_EX);
1437 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1438 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1439 if (ni->ni_nid == nid)
1448 lnet_nid2ni_addref(lnet_nid_t nid)
1453 ni = lnet_nid2ni_locked(nid, 0);
1455 lnet_ni_addref_locked(ni, 0);
1460 EXPORT_SYMBOL(lnet_nid2ni_addref);
1463 lnet_islocalnid(lnet_nid_t nid)
1468 cpt = lnet_net_lock_current();
1469 ni = lnet_nid2ni_locked(nid, cpt);
1470 lnet_net_unlock(cpt);
1476 lnet_count_acceptor_nets(void)
1478 /* Return the # of NIs that need the acceptor. */
1480 struct lnet_net *net;
1483 cpt = lnet_net_lock_current();
1484 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1485 /* all socklnd type networks should have the acceptor
1487 if (net->net_lnd->lnd_accept != NULL)
1491 lnet_net_unlock(cpt);
1496 struct lnet_ping_buffer *
1497 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1499 struct lnet_ping_buffer *pbuf;
1501 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1503 pbuf->pb_nnis = nnis;
1504 atomic_set(&pbuf->pb_refcnt, 1);
1511 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1513 LASSERT(atomic_read(&pbuf->pb_refcnt) == 0);
1514 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1517 static struct lnet_ping_buffer *
1518 lnet_ping_target_create(int nnis)
1520 struct lnet_ping_buffer *pbuf;
1522 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1524 CERROR("Can't allocate ping source [%d]\n", nnis);
1528 pbuf->pb_info.pi_nnis = nnis;
1529 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1530 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1531 pbuf->pb_info.pi_features =
1532 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1538 lnet_get_net_ni_count_locked(struct lnet_net *net)
1543 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1550 lnet_get_net_ni_count_pre(struct lnet_net *net)
1555 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1562 lnet_get_ni_count(void)
1565 struct lnet_net *net;
1570 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1571 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1581 lnet_get_net_count(void)
1583 struct lnet_net *net;
1588 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1598 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1600 struct lnet_ni_status *stat;
1604 __swab32s(&pbuf->pb_info.pi_magic);
1605 __swab32s(&pbuf->pb_info.pi_features);
1606 __swab32s(&pbuf->pb_info.pi_pid);
1607 __swab32s(&pbuf->pb_info.pi_nnis);
1608 nnis = pbuf->pb_info.pi_nnis;
1609 if (nnis > pbuf->pb_nnis)
1610 nnis = pbuf->pb_nnis;
1611 for (i = 0; i < nnis; i++) {
1612 stat = &pbuf->pb_info.pi_ni[i];
1613 __swab64s(&stat->ns_nid);
1614 __swab32s(&stat->ns_status);
1619 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1623 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1625 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1627 /* Loopback is guaranteed to be present */
1628 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1630 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1636 lnet_ping_target_destroy(void)
1638 struct lnet_net *net;
1641 lnet_net_lock(LNET_LOCK_EX);
1643 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1644 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1646 ni->ni_status = NULL;
1651 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1652 the_lnet.ln_ping_target = NULL;
1654 lnet_net_unlock(LNET_LOCK_EX);
1658 lnet_ping_target_event_handler(struct lnet_event *event)
1660 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1662 if (event->unlinked)
1663 lnet_ping_buffer_decref(pbuf);
1667 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1668 struct lnet_handle_md *ping_mdh,
1669 int ni_count, bool set_eq)
1671 struct lnet_process_id id = {
1672 .nid = LNET_NID_ANY,
1676 struct lnet_md md = { NULL };
1680 the_lnet.ln_ping_target_eq =
1681 LNetEQAlloc(lnet_ping_target_event_handler);
1682 if (IS_ERR(the_lnet.ln_ping_target_eq)) {
1683 rc = PTR_ERR(the_lnet.ln_ping_target_eq);
1684 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1689 *ppbuf = lnet_ping_target_create(ni_count);
1690 if (*ppbuf == NULL) {
1695 /* Ping target ME/MD */
1696 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1697 LNET_PROTO_PING_MATCHBITS, 0,
1698 LNET_UNLINK, LNET_INS_AFTER);
1701 CERROR("Can't create ping target ME: %d\n", rc);
1702 goto fail_decref_ping_buffer;
1705 /* initialize md content */
1706 md.start = &(*ppbuf)->pb_info;
1707 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1708 md.threshold = LNET_MD_THRESH_INF;
1710 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1711 LNET_MD_MANAGE_REMOTE;
1712 md.eq_handle = the_lnet.ln_ping_target_eq;
1713 md.user_ptr = *ppbuf;
1715 rc = LNetMDAttach(me, md, LNET_RETAIN, ping_mdh);
1717 CERROR("Can't attach ping target MD: %d\n", rc);
1718 goto fail_unlink_ping_me;
1720 lnet_ping_buffer_addref(*ppbuf);
1724 fail_unlink_ping_me:
1726 fail_decref_ping_buffer:
1727 LASSERT(atomic_read(&(*ppbuf)->pb_refcnt) == 1);
1728 lnet_ping_buffer_decref(*ppbuf);
1732 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1739 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1740 struct lnet_handle_md *ping_mdh)
1742 LNetMDUnlink(*ping_mdh);
1743 LNetInvalidateMDHandle(ping_mdh);
1745 /* NB the MD could be busy; this just starts the unlink */
1746 while (atomic_read(&pbuf->pb_refcnt) > 1) {
1747 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1748 schedule_timeout_uninterruptible(cfs_time_seconds(1));
1753 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1756 struct lnet_net *net;
1757 struct lnet_ni_status *ns;
1762 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1763 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1764 LASSERT(i < pbuf->pb_nnis);
1766 ns = &pbuf->pb_info.pi_ni[i];
1768 ns->ns_nid = ni->ni_nid;
1771 ns->ns_status = (ni->ni_status != NULL) ?
1772 ni->ni_status->ns_status :
1781 * We (ab)use the ns_status of the loopback interface to
1782 * transmit the sequence number. The first interface listed
1783 * must be the loopback interface.
1785 rc = lnet_ping_info_validate(&pbuf->pb_info);
1787 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1790 LNET_PING_BUFFER_SEQNO(pbuf) =
1791 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1795 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1796 struct lnet_handle_md ping_mdh)
1798 struct lnet_ping_buffer *old_pbuf = NULL;
1799 struct lnet_handle_md old_ping_md;
1801 /* switch the NIs to point to the new ping info created */
1802 lnet_net_lock(LNET_LOCK_EX);
1804 if (!the_lnet.ln_routing)
1805 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1806 if (!lnet_peer_discovery_disabled)
1807 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1809 /* Ensure only known feature bits have been set. */
1810 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1811 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1813 lnet_ping_target_install_locked(pbuf);
1815 if (the_lnet.ln_ping_target) {
1816 old_pbuf = the_lnet.ln_ping_target;
1817 old_ping_md = the_lnet.ln_ping_target_md;
1819 the_lnet.ln_ping_target_md = ping_mdh;
1820 the_lnet.ln_ping_target = pbuf;
1822 lnet_net_unlock(LNET_LOCK_EX);
1825 /* unlink and free the old ping info */
1826 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1827 lnet_ping_buffer_decref(old_pbuf);
1830 lnet_push_update_to_peers(0);
1834 lnet_ping_target_fini(void)
1838 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1839 &the_lnet.ln_ping_target_md);
1841 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1844 lnet_ping_target_destroy();
1847 /* Resize the push target. */
1848 int lnet_push_target_resize(void)
1850 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1851 struct lnet_md md = { NULL };
1853 struct lnet_handle_md mdh;
1854 struct lnet_handle_md old_mdh;
1855 struct lnet_ping_buffer *pbuf;
1856 struct lnet_ping_buffer *old_pbuf;
1861 nnis = the_lnet.ln_push_target_nnis;
1867 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1873 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1874 LNET_PROTO_PING_MATCHBITS, 0,
1875 LNET_UNLINK, LNET_INS_AFTER);
1879 CERROR("Can't create push target ME: %d\n", rc);
1880 goto fail_decref_pbuf;
1883 /* initialize md content */
1884 md.start = &pbuf->pb_info;
1885 md.length = LNET_PING_INFO_SIZE(nnis);
1886 md.threshold = LNET_MD_THRESH_INF;
1888 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1889 LNET_MD_MANAGE_REMOTE;
1891 md.eq_handle = the_lnet.ln_push_target_eq;
1893 rc = LNetMDAttach(me, md, LNET_RETAIN, &mdh);
1895 CERROR("Can't attach push MD: %d\n", rc);
1896 goto fail_unlink_me;
1898 lnet_ping_buffer_addref(pbuf);
1900 lnet_net_lock(LNET_LOCK_EX);
1901 old_pbuf = the_lnet.ln_push_target;
1902 old_mdh = the_lnet.ln_push_target_md;
1903 the_lnet.ln_push_target = pbuf;
1904 the_lnet.ln_push_target_md = mdh;
1905 lnet_net_unlock(LNET_LOCK_EX);
1908 LNetMDUnlink(old_mdh);
1909 lnet_ping_buffer_decref(old_pbuf);
1912 if (nnis < the_lnet.ln_push_target_nnis)
1915 CDEBUG(D_NET, "nnis %d success\n", nnis);
1922 lnet_ping_buffer_decref(pbuf);
1924 CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1928 static void lnet_push_target_event_handler(struct lnet_event *ev)
1930 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1932 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1933 lnet_swap_pinginfo(pbuf);
1935 lnet_peer_push_event(ev);
1937 lnet_ping_buffer_decref(pbuf);
1940 /* Initialize the push target. */
1941 static int lnet_push_target_init(void)
1945 if (the_lnet.ln_push_target)
1948 the_lnet.ln_push_target_eq =
1949 LNetEQAlloc(lnet_push_target_event_handler);
1950 if (IS_ERR(the_lnet.ln_push_target_eq)) {
1951 rc = PTR_ERR(the_lnet.ln_push_target_eq);
1952 CERROR("Can't allocated push target EQ: %d\n", rc);
1956 /* Start at the required minimum, we'll enlarge if required. */
1957 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1959 rc = lnet_push_target_resize();
1962 LNetEQFree(the_lnet.ln_push_target_eq);
1963 the_lnet.ln_push_target_eq = NULL;
1969 /* Clean up the push target. */
1970 static void lnet_push_target_fini(void)
1972 if (!the_lnet.ln_push_target)
1975 /* Unlink and invalidate to prevent new references. */
1976 LNetMDUnlink(the_lnet.ln_push_target_md);
1977 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1979 /* Wait for the unlink to complete. */
1980 while (atomic_read(&the_lnet.ln_push_target->pb_refcnt) > 1) {
1981 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1982 schedule_timeout_uninterruptible(cfs_time_seconds(1));
1985 lnet_ping_buffer_decref(the_lnet.ln_push_target);
1986 the_lnet.ln_push_target = NULL;
1987 the_lnet.ln_push_target_nnis = 0;
1989 LNetEQFree(the_lnet.ln_push_target_eq);
1990 the_lnet.ln_push_target_eq = NULL;
1994 lnet_ni_tq_credits(struct lnet_ni *ni)
1998 LASSERT(ni->ni_ncpts >= 1);
2000 if (ni->ni_ncpts == 1)
2001 return ni->ni_net->net_tunables.lct_max_tx_credits;
2003 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2004 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2005 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2011 lnet_ni_unlink_locked(struct lnet_ni *ni)
2013 /* move it to zombie list and nobody can find it anymore */
2014 LASSERT(!list_empty(&ni->ni_netlist));
2015 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2016 lnet_ni_decref_locked(ni, 0);
2020 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2025 struct list_head *zombie_list = &net->net_ni_zombie;
2028 * Now wait for the NIs I just nuked to show up on the zombie
2029 * list and shut them down in guaranteed thread context
2032 while (!list_empty(zombie_list)) {
2036 ni = list_entry(zombie_list->next,
2037 struct lnet_ni, ni_netlist);
2038 list_del_init(&ni->ni_netlist);
2039 /* the ni should be in deleting state. If it's not it's
2041 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2042 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2045 /* still busy, add it back to zombie list */
2046 list_add(&ni->ni_netlist, zombie_list);
2050 if (!list_empty(&ni->ni_netlist)) {
2051 lnet_net_unlock(LNET_LOCK_EX);
2053 if ((i & (-i)) == i) {
2055 "Waiting for zombie LNI %s\n",
2056 libcfs_nid2str(ni->ni_nid));
2058 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2059 lnet_net_lock(LNET_LOCK_EX);
2063 lnet_net_unlock(LNET_LOCK_EX);
2065 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2067 LASSERT(!in_interrupt());
2068 /* Holding the mutex makes it safe for lnd_shutdown
2069 * to call module_put(). Module unload cannot finish
2070 * until lnet_unregister_lnd() completes, and that
2071 * requires the mutex.
2073 mutex_lock(&the_lnet.ln_lnd_mutex);
2074 (net->net_lnd->lnd_shutdown)(ni);
2075 mutex_unlock(&the_lnet.ln_lnd_mutex);
2078 CDEBUG(D_LNI, "Removed LNI %s\n",
2079 libcfs_nid2str(ni->ni_nid));
2083 lnet_net_lock(LNET_LOCK_EX);
2087 /* shutdown down the NI and release refcount */
2089 lnet_shutdown_lndni(struct lnet_ni *ni)
2092 struct lnet_net *net = ni->ni_net;
2094 lnet_net_lock(LNET_LOCK_EX);
2096 ni->ni_state = LNET_NI_STATE_DELETING;
2098 lnet_ni_unlink_locked(ni);
2099 lnet_incr_dlc_seq();
2100 lnet_net_unlock(LNET_LOCK_EX);
2102 /* clear messages for this NI on the lazy portal */
2103 for (i = 0; i < the_lnet.ln_nportals; i++)
2104 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2106 lnet_net_lock(LNET_LOCK_EX);
2107 lnet_clear_zombies_nis_locked(net);
2108 lnet_net_unlock(LNET_LOCK_EX);
2112 lnet_shutdown_lndnet(struct lnet_net *net)
2116 lnet_net_lock(LNET_LOCK_EX);
2118 list_del_init(&net->net_list);
2120 while (!list_empty(&net->net_ni_list)) {
2121 ni = list_entry(net->net_ni_list.next,
2122 struct lnet_ni, ni_netlist);
2123 lnet_net_unlock(LNET_LOCK_EX);
2124 lnet_shutdown_lndni(ni);
2125 lnet_net_lock(LNET_LOCK_EX);
2128 lnet_net_unlock(LNET_LOCK_EX);
2130 /* Do peer table cleanup for this net */
2131 lnet_peer_tables_cleanup(net);
2137 lnet_shutdown_lndnets(void)
2139 struct lnet_net *net;
2141 struct lnet_msg *msg, *tmp;
2143 /* NB called holding the global mutex */
2145 /* All quiet on the API front */
2146 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2147 LASSERT(the_lnet.ln_refcount == 0);
2149 lnet_net_lock(LNET_LOCK_EX);
2150 the_lnet.ln_state = LNET_STATE_STOPPING;
2153 * move the nets to the zombie list to avoid them being
2154 * picked up for new work. LONET is also included in the
2155 * Nets that will be moved to the zombie list
2157 list_splice_init(&the_lnet.ln_nets, &the_lnet.ln_net_zombie);
2159 /* Drop the cached loopback Net. */
2160 if (the_lnet.ln_loni != NULL) {
2161 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2162 the_lnet.ln_loni = NULL;
2164 lnet_net_unlock(LNET_LOCK_EX);
2166 /* iterate through the net zombie list and delete each net */
2167 while (!list_empty(&the_lnet.ln_net_zombie)) {
2168 net = list_entry(the_lnet.ln_net_zombie.next,
2169 struct lnet_net, net_list);
2170 lnet_shutdown_lndnet(net);
2173 spin_lock(&the_lnet.ln_msg_resend_lock);
2174 list_splice(&the_lnet.ln_msg_resend, &resend);
2175 spin_unlock(&the_lnet.ln_msg_resend_lock);
2177 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2178 list_del_init(&msg->msg_list);
2179 msg->msg_no_resend = true;
2180 lnet_finalize(msg, -ECANCELED);
2183 lnet_net_lock(LNET_LOCK_EX);
2184 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2185 lnet_net_unlock(LNET_LOCK_EX);
2189 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2192 struct lnet_tx_queue *tq;
2194 struct lnet_net *net = ni->ni_net;
2196 mutex_lock(&the_lnet.ln_lnd_mutex);
2199 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2200 ni->ni_lnd_tunables_set = true;
2203 rc = (net->net_lnd->lnd_startup)(ni);
2205 mutex_unlock(&the_lnet.ln_lnd_mutex);
2208 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2209 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2214 ni->ni_state = LNET_NI_STATE_ACTIVE;
2217 /* We keep a reference on the loopback net through the loopback NI */
2218 if (net->net_lnd->lnd_type == LOLND) {
2220 LASSERT(the_lnet.ln_loni == NULL);
2221 the_lnet.ln_loni = ni;
2222 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2223 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2224 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2225 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2229 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2230 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2231 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2232 libcfs_lnd2str(net->net_lnd->lnd_type),
2233 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2235 /* shutdown the NI since if we get here then it must've already
2238 lnet_shutdown_lndni(ni);
2242 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2243 tq->tq_credits_min =
2244 tq->tq_credits_max =
2245 tq->tq_credits = lnet_ni_tq_credits(ni);
2248 atomic_set(&ni->ni_tx_credits,
2249 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2250 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2252 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2253 libcfs_nid2str(ni->ni_nid),
2254 ni->ni_net->net_tunables.lct_peer_tx_credits,
2255 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2256 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2257 ni->ni_net->net_tunables.lct_peer_timeout);
2266 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2269 struct lnet_net *net_l = NULL;
2270 LIST_HEAD(local_ni_list);
2274 const struct lnet_lnd *lnd;
2276 net->net_tunables.lct_peer_timeout;
2278 net->net_tunables.lct_max_tx_credits;
2279 int peerrtrcredits =
2280 net->net_tunables.lct_peer_rtr_credits;
2283 * make sure that this net is unique. If it isn't then
2284 * we are adding interfaces to an already existing network, and
2285 * 'net' is just a convenient way to pass in the list.
2286 * if it is unique we need to find the LND and load it if
2289 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2290 lnd_type = LNET_NETTYP(net->net_id);
2292 mutex_lock(&the_lnet.ln_lnd_mutex);
2293 lnd = lnet_find_lnd_by_type(lnd_type);
2296 mutex_unlock(&the_lnet.ln_lnd_mutex);
2297 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2298 mutex_lock(&the_lnet.ln_lnd_mutex);
2300 lnd = lnet_find_lnd_by_type(lnd_type);
2302 mutex_unlock(&the_lnet.ln_lnd_mutex);
2303 CERROR("Can't load LND %s, module %s, rc=%d\n",
2304 libcfs_lnd2str(lnd_type),
2305 libcfs_lnd2modname(lnd_type), rc);
2306 #ifndef HAVE_MODULE_LOADING_SUPPORT
2307 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2308 "compiled with kernel module "
2309 "loading support.");
2318 mutex_unlock(&the_lnet.ln_lnd_mutex);
2324 * net_l: if the network being added is unique then net_l
2325 * will point to that network
2326 * if the network being added is not unique then
2327 * net_l points to the existing network.
2329 * When we enter the loop below, we'll pick NIs off he
2330 * network beign added and start them up, then add them to
2331 * a local ni list. Once we've successfully started all
2332 * the NIs then we join the local NI list (of started up
2333 * networks) with the net_l->net_ni_list, which should
2334 * point to the correct network to add the new ni list to
2336 * If any of the new NIs fail to start up, then we want to
2337 * iterate through the local ni list, which should include
2338 * any NIs which were successfully started up, and shut
2341 * After than we want to delete the network being added,
2342 * to avoid a memory leak.
2346 * When a network uses TCP bonding then all its interfaces
2347 * must be specified when the network is first defined: the
2348 * TCP bonding code doesn't allow for interfaces to be added
2351 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2352 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2357 while (!list_empty(&net->net_ni_added)) {
2358 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2360 list_del_init(&ni->ni_netlist);
2362 /* make sure that the the NI we're about to start
2363 * up is actually unique. if it's not fail. */
2364 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2365 ni->ni_interfaces[0])) {
2370 /* adjust the pointer the parent network, just in case it
2371 * the net is a duplicate */
2374 rc = lnet_startup_lndni(ni, tun);
2380 list_add_tail(&ni->ni_netlist, &local_ni_list);
2385 lnet_net_lock(LNET_LOCK_EX);
2386 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2387 lnet_incr_dlc_seq();
2388 lnet_net_unlock(LNET_LOCK_EX);
2390 /* if the network is not unique then we don't want to keep
2391 * it around after we're done. Free it. Otherwise add that
2392 * net to the global the_lnet.ln_nets */
2393 if (net_l != net && net_l != NULL) {
2395 * TODO - note. currently the tunables can not be updated
2401 * restore tunables after it has been overwitten by the
2404 if (peer_timeout != -1)
2405 net->net_tunables.lct_peer_timeout = peer_timeout;
2406 if (maxtxcredits != -1)
2407 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2408 if (peerrtrcredits != -1)
2409 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2411 lnet_net_lock(LNET_LOCK_EX);
2412 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2413 lnet_net_unlock(LNET_LOCK_EX);
2416 /* update net count */
2417 lnet_current_net_count = lnet_get_net_count();
2423 * shutdown the new NIs that are being started up
2424 * free the NET being started
2426 while (!list_empty(&local_ni_list)) {
2427 ni = list_entry(local_ni_list.next, struct lnet_ni,
2430 lnet_shutdown_lndni(ni);
2440 lnet_startup_lndnets(struct list_head *netlist)
2442 struct lnet_net *net;
2447 * Change to running state before bringing up the LNDs. This
2448 * allows lnet_shutdown_lndnets() to assert that we've passed
2451 lnet_net_lock(LNET_LOCK_EX);
2452 the_lnet.ln_state = LNET_STATE_RUNNING;
2453 lnet_net_unlock(LNET_LOCK_EX);
2455 while (!list_empty(netlist)) {
2456 net = list_entry(netlist->next, struct lnet_net, net_list);
2457 list_del_init(&net->net_list);
2459 rc = lnet_startup_lndnet(net, NULL);
2469 lnet_shutdown_lndnets();
2475 * Initialize LNet library.
2477 * Automatically called at module loading time. Caller has to call
2478 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2479 * latter returned 0. It must be called exactly once.
2481 * \retval 0 on success
2482 * \retval -ve on failures.
2484 int lnet_lib_init(void)
2488 lnet_assert_wire_constants();
2490 /* refer to global cfs_cpt_table for now */
2491 the_lnet.ln_cpt_table = cfs_cpt_tab;
2492 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_tab);
2494 LASSERT(the_lnet.ln_cpt_number > 0);
2495 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2496 /* we are under risk of consuming all lh_cookie */
2497 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2498 "please change setting of CPT-table and retry\n",
2499 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2503 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2504 the_lnet.ln_cpt_bits++;
2506 rc = lnet_create_locks();
2508 CERROR("Can't create LNet global locks: %d\n", rc);
2512 the_lnet.ln_refcount = 0;
2513 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2514 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2516 /* The hash table size is the number of bits it takes to express the set
2517 * ln_num_routes, minus 1 (better to under estimate than over so we
2518 * don't waste memory). */
2519 if (rnet_htable_size <= 0)
2520 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2521 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2522 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2523 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2524 order_base_2(rnet_htable_size) - 1);
2526 /* All LNDs apart from the LOLND are in separate modules. They
2527 * register themselves when their module loads, and unregister
2528 * themselves when their module is unloaded. */
2529 lnet_register_lnd(&the_lolnd);
2534 * Finalize LNet library.
2536 * \pre lnet_lib_init() called with success.
2537 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2539 * As this happens at module-unload, all lnds must already be unloaded,
2540 * so they must already be unregistered.
2542 void lnet_lib_exit(void)
2546 LASSERT(the_lnet.ln_refcount == 0);
2547 lnet_unregister_lnd(&the_lolnd);
2548 for (i = 0; i < NUM_LNDS; i++)
2549 LASSERT(!the_lnet.ln_lnds[i]);
2550 lnet_destroy_locks();
2554 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2556 * Users must call this function at least once before any other functions.
2557 * For each successful call there must be a corresponding call to
2558 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2561 * The PID used by LNet may be different from the one requested.
2564 * \param requested_pid PID requested by the caller.
2566 * \return >= 0 on success, and < 0 error code on failures.
2569 LNetNIInit(lnet_pid_t requested_pid)
2571 int im_a_router = 0;
2574 struct lnet_ping_buffer *pbuf;
2575 struct lnet_handle_md ping_mdh;
2576 LIST_HEAD(net_head);
2577 struct lnet_net *net;
2579 mutex_lock(&the_lnet.ln_api_mutex);
2581 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2583 if (the_lnet.ln_refcount > 0) {
2584 rc = the_lnet.ln_refcount++;
2585 mutex_unlock(&the_lnet.ln_api_mutex);
2589 rc = lnet_prepare(requested_pid);
2591 mutex_unlock(&the_lnet.ln_api_mutex);
2595 /* create a network for Loopback network */
2596 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2599 goto err_empty_list;
2602 /* Add in the loopback NI */
2603 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2605 goto err_empty_list;
2608 /* If LNet is being initialized via DLC it is possible
2609 * that the user requests not to load module parameters (ones which
2610 * are supported by DLC) on initialization. Therefore, make sure not
2611 * to load networks, routes and forwarding from module parameters
2612 * in this case. On cleanup in case of failure only clean up
2613 * routes if it has been loaded */
2614 if (!the_lnet.ln_nis_from_mod_params) {
2615 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2618 goto err_empty_list;
2621 ni_count = lnet_startup_lndnets(&net_head);
2624 goto err_empty_list;
2627 if (!the_lnet.ln_nis_from_mod_params) {
2628 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2630 goto err_shutdown_lndnis;
2632 rc = lnet_rtrpools_alloc(im_a_router);
2634 goto err_destroy_routes;
2637 rc = lnet_acceptor_start();
2639 goto err_destroy_routes;
2641 the_lnet.ln_refcount = 1;
2642 /* Now I may use my own API functions... */
2644 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2646 goto err_acceptor_stop;
2648 lnet_ping_target_update(pbuf, ping_mdh);
2650 the_lnet.ln_mt_eq = LNetEQAlloc(lnet_mt_event_handler);
2651 if (IS_ERR(the_lnet.ln_mt_eq)) {
2652 rc = PTR_ERR(the_lnet.ln_mt_eq);
2653 CERROR("Can't allocate monitor thread EQ: %d\n", rc);
2657 rc = lnet_push_target_init();
2661 rc = lnet_peer_discovery_start();
2663 goto err_destroy_push_target;
2665 rc = lnet_monitor_thr_start();
2667 goto err_stop_discovery_thr;
2670 lnet_router_debugfs_init();
2672 mutex_unlock(&the_lnet.ln_api_mutex);
2674 complete_all(&the_lnet.ln_started);
2676 /* wait for all routers to start */
2677 lnet_wait_router_start();
2681 err_stop_discovery_thr:
2682 lnet_peer_discovery_stop();
2683 err_destroy_push_target:
2684 lnet_push_target_fini();
2686 lnet_ping_target_fini();
2688 the_lnet.ln_refcount = 0;
2689 lnet_acceptor_stop();
2691 if (!the_lnet.ln_nis_from_mod_params)
2692 lnet_destroy_routes();
2693 err_shutdown_lndnis:
2694 lnet_shutdown_lndnets();
2698 mutex_unlock(&the_lnet.ln_api_mutex);
2699 while (!list_empty(&net_head)) {
2700 struct lnet_net *net;
2702 net = list_entry(net_head.next, struct lnet_net, net_list);
2703 list_del_init(&net->net_list);
2708 EXPORT_SYMBOL(LNetNIInit);
2711 * Stop LNet interfaces, routing, and forwarding.
2713 * Users must call this function once for each successful call to LNetNIInit().
2714 * Once the LNetNIFini() operation has been started, the results of pending
2715 * API operations are undefined.
2717 * \return always 0 for current implementation.
2722 mutex_lock(&the_lnet.ln_api_mutex);
2724 LASSERT(the_lnet.ln_refcount > 0);
2726 if (the_lnet.ln_refcount != 1) {
2727 the_lnet.ln_refcount--;
2729 LASSERT(!the_lnet.ln_niinit_self);
2733 lnet_router_debugfs_fini();
2734 lnet_monitor_thr_stop();
2735 lnet_peer_discovery_stop();
2736 lnet_push_target_fini();
2737 lnet_ping_target_fini();
2739 /* Teardown fns that use my own API functions BEFORE here */
2740 the_lnet.ln_refcount = 0;
2742 lnet_acceptor_stop();
2743 lnet_destroy_routes();
2744 lnet_shutdown_lndnets();
2748 mutex_unlock(&the_lnet.ln_api_mutex);
2751 EXPORT_SYMBOL(LNetNIFini);
2754 * Grabs the ni data from the ni structure and fills the out
2757 * \param[in] ni network interface structure
2758 * \param[out] cfg_ni NI config information
2759 * \param[out] tun network and LND tunables
2762 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2763 struct lnet_ioctl_config_lnd_tunables *tun,
2764 struct lnet_ioctl_element_stats *stats,
2767 size_t min_size = 0;
2770 if (!ni || !cfg_ni || !tun)
2773 if (ni->ni_interfaces[0] != NULL) {
2774 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2775 if (ni->ni_interfaces[i] != NULL) {
2776 strncpy(cfg_ni->lic_ni_intf[i],
2777 ni->ni_interfaces[i],
2778 sizeof(cfg_ni->lic_ni_intf[i]));
2783 cfg_ni->lic_nid = ni->ni_nid;
2784 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2785 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2787 cfg_ni->lic_status = ni->ni_status->ns_status;
2788 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2789 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2791 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2794 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2795 LNET_STATS_TYPE_SEND);
2796 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2797 LNET_STATS_TYPE_RECV);
2798 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2799 LNET_STATS_TYPE_DROP);
2803 * tun->lt_tun will always be present, but in order to be
2804 * backwards compatible, we need to deal with the cases when
2805 * tun->lt_tun is smaller than what the kernel has, because it
2806 * comes from an older version of a userspace program, then we'll
2807 * need to copy as much information as we have available space.
2809 min_size = tun_size - sizeof(tun->lt_cmn);
2810 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2812 /* copy over the cpts */
2813 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2814 ni->ni_cpts == NULL) {
2815 for (i = 0; i < ni->ni_ncpts; i++)
2816 cfg_ni->lic_cpts[i] = i;
2819 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2820 i < LNET_MAX_SHOW_NUM_CPT;
2822 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2824 cfg_ni->lic_ncpts = ni->ni_ncpts;
2828 * NOTE: This is a legacy function left in the code to be backwards
2829 * compatible with older userspace programs. It should eventually be
2832 * Grabs the ni data from the ni structure and fills the out
2835 * \param[in] ni network interface structure
2836 * \param[out] config config information
2839 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2840 struct lnet_ioctl_config_data *config)
2842 struct lnet_ioctl_net_config *net_config;
2843 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2844 size_t min_size, tunable_size = 0;
2850 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2854 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2855 ARRAY_SIZE(net_config->ni_interfaces));
2857 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2858 if (!ni->ni_interfaces[i])
2861 strncpy(net_config->ni_interfaces[i],
2862 ni->ni_interfaces[i],
2863 sizeof(net_config->ni_interfaces[i]));
2866 config->cfg_nid = ni->ni_nid;
2867 config->cfg_config_u.cfg_net.net_peer_timeout =
2868 ni->ni_net->net_tunables.lct_peer_timeout;
2869 config->cfg_config_u.cfg_net.net_max_tx_credits =
2870 ni->ni_net->net_tunables.lct_max_tx_credits;
2871 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2872 ni->ni_net->net_tunables.lct_peer_tx_credits;
2873 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2874 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2876 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2877 net_config->ni_status = LNET_NI_STATUS_UP;
2879 net_config->ni_status = ni->ni_status->ns_status;
2882 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2884 for (i = 0; i < num_cpts; i++)
2885 net_config->ni_cpts[i] = ni->ni_cpts[i];
2887 config->cfg_ncpts = num_cpts;
2891 * See if user land tools sent in a newer and larger version
2892 * of struct lnet_tunables than what the kernel uses.
2894 min_size = sizeof(*config) + sizeof(*net_config);
2896 if (config->cfg_hdr.ioc_len > min_size)
2897 tunable_size = config->cfg_hdr.ioc_len - min_size;
2899 /* Don't copy too much data to user space */
2900 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2901 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2903 if (lnd_cfg && min_size) {
2904 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2905 config->cfg_config_u.cfg_net.net_interface_count = 1;
2907 /* Tell user land that kernel side has less data */
2908 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2909 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2910 config->cfg_hdr.ioc_len -= min_size;
2916 lnet_get_ni_idx_locked(int idx)
2919 struct lnet_net *net;
2921 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2922 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2932 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2935 struct lnet_net *net = mynet;
2938 * It is possible that the net has been cleaned out while there is
2939 * a message being sent. This function accessed the net without
2940 * checking if the list is empty
2944 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2946 if (list_empty(&net->net_ni_list))
2948 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2954 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2955 /* if you reached the end of the ni list and the net is
2956 * specified, then there are no more nis in that net */
2960 /* we reached the end of this net ni list. move to the
2962 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2963 /* no more nets and no more NIs. */
2966 /* get the next net */
2967 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2969 if (list_empty(&net->net_ni_list))
2971 /* get the ni on it */
2972 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2978 if (list_empty(&prev->ni_netlist))
2981 /* there are more nis left */
2982 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2988 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2993 int idx = config->cfg_count;
2995 cpt = lnet_net_lock_current();
2997 ni = lnet_get_ni_idx_locked(idx);
3002 lnet_fill_ni_info_legacy(ni, config);
3006 lnet_net_unlock(cpt);
3011 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3012 struct lnet_ioctl_config_lnd_tunables *tun,
3013 struct lnet_ioctl_element_stats *stats,
3020 if (!cfg_ni || !tun || !stats)
3023 cpt = lnet_net_lock_current();
3025 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3030 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3034 lnet_net_unlock(cpt);
3038 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3047 cpt = lnet_net_lock_current();
3049 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3052 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3056 lnet_net_unlock(cpt);
3061 static int lnet_add_net_common(struct lnet_net *net,
3062 struct lnet_ioctl_config_lnd_tunables *tun)
3065 struct lnet_ping_buffer *pbuf;
3066 struct lnet_handle_md ping_mdh;
3068 struct lnet_remotenet *rnet;
3070 int num_acceptor_nets;
3072 lnet_net_lock(LNET_LOCK_EX);
3073 rnet = lnet_find_rnet_locked(net->net_id);
3074 lnet_net_unlock(LNET_LOCK_EX);
3076 * make sure that the net added doesn't invalidate the current
3077 * configuration LNet is keeping
3080 CERROR("Adding net %s will invalidate routing configuration\n",
3081 libcfs_net2str(net->net_id));
3087 * make sure you calculate the correct number of slots in the ping
3088 * buffer. Since the ping info is a flattened list of all the NIs,
3089 * we should allocate enough slots to accomodate the number of NIs
3090 * which will be added.
3092 * since ni hasn't been configured yet, use
3093 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3095 net_ni_count = lnet_get_net_ni_count_pre(net);
3097 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3098 net_ni_count + lnet_get_ni_count(),
3106 memcpy(&net->net_tunables,
3107 &tun->lt_cmn, sizeof(net->net_tunables));
3109 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3112 * before starting this network get a count of the current TCP
3113 * networks which require the acceptor thread running. If that
3114 * count is == 0 before we start up this network, then we'd want to
3115 * start up the acceptor thread after starting up this network
3117 num_acceptor_nets = lnet_count_acceptor_nets();
3119 net_id = net->net_id;
3121 rc = lnet_startup_lndnet(net,
3122 (tun) ? &tun->lt_tun : NULL);
3126 lnet_net_lock(LNET_LOCK_EX);
3127 net = lnet_get_net_locked(net_id);
3128 lnet_net_unlock(LNET_LOCK_EX);
3133 * Start the acceptor thread if this is the first network
3134 * being added that requires the thread.
3136 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
3137 rc = lnet_acceptor_start();
3139 /* shutdown the net that we just started */
3140 CERROR("Failed to start up acceptor thread\n");
3141 lnet_shutdown_lndnet(net);
3146 lnet_net_lock(LNET_LOCK_EX);
3147 lnet_peer_net_added(net);
3148 lnet_net_unlock(LNET_LOCK_EX);
3150 lnet_ping_target_update(pbuf, ping_mdh);
3155 lnet_ping_md_unlink(pbuf, &ping_mdh);
3156 lnet_ping_buffer_decref(pbuf);
3160 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3161 struct lnet_ioctl_config_lnd_tunables *tun)
3163 struct lnet_net *net;
3166 LIST_HEAD(net_head);
3168 rc = lnet_parse_ip2nets(&nets, ip2nets);
3172 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3176 mutex_lock(&the_lnet.ln_api_mutex);
3177 while (!list_empty(&net_head)) {
3178 net = list_entry(net_head.next, struct lnet_net, net_list);
3179 list_del_init(&net->net_list);
3180 rc = lnet_add_net_common(net, tun);
3186 mutex_unlock(&the_lnet.ln_api_mutex);
3188 while (!list_empty(&net_head)) {
3189 net = list_entry(net_head.next, struct lnet_net, net_list);
3190 list_del_init(&net->net_list);
3196 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3198 struct lnet_net *net;
3200 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3202 __u32 net_id, lnd_type;
3204 /* get the tunables if they are available */
3205 if (conf->lic_cfg_hdr.ioc_len >=
3206 sizeof(*conf) + sizeof(*tun))
3207 tun = (struct lnet_ioctl_config_lnd_tunables *)
3210 /* handle legacy ip2nets from DLC */
3211 if (conf->lic_legacy_ip2nets[0] != '\0')
3212 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3215 net_id = LNET_NIDNET(conf->lic_nid);
3216 lnd_type = LNET_NETTYP(net_id);
3218 if (!libcfs_isknown_lnd(lnd_type)) {
3219 CERROR("No valid net and lnd information provided\n");
3223 net = lnet_net_alloc(net_id, NULL);
3227 for (i = 0; i < conf->lic_ncpts; i++) {
3228 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3232 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3233 conf->lic_ni_intf[0]);
3237 mutex_lock(&the_lnet.ln_api_mutex);
3239 rc = lnet_add_net_common(net, tun);
3241 mutex_unlock(&the_lnet.ln_api_mutex);
3246 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3248 struct lnet_net *net;
3250 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3251 struct lnet_ping_buffer *pbuf;
3252 struct lnet_handle_md ping_mdh;
3257 /* don't allow userspace to shutdown the LOLND */
3258 if (LNET_NETTYP(net_id) == LOLND)
3261 mutex_lock(&the_lnet.ln_api_mutex);
3265 net = lnet_get_net_locked(net_id);
3267 CERROR("net %s not found\n",
3268 libcfs_net2str(net_id));
3273 addr = LNET_NIDADDR(conf->lic_nid);
3275 /* remove the entire net */
3276 net_count = lnet_get_net_ni_count_locked(net);
3280 /* create and link a new ping info, before removing the old one */
3281 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3282 lnet_get_ni_count() - net_count,
3285 goto unlock_api_mutex;
3287 lnet_shutdown_lndnet(net);
3289 if (lnet_count_acceptor_nets() == 0)
3290 lnet_acceptor_stop();
3292 lnet_ping_target_update(pbuf, ping_mdh);
3294 goto unlock_api_mutex;
3297 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3299 CERROR("nid %s not found\n",
3300 libcfs_nid2str(conf->lic_nid));
3305 net_count = lnet_get_net_ni_count_locked(net);
3309 /* create and link a new ping info, before removing the old one */
3310 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3311 lnet_get_ni_count() - 1, false);
3313 goto unlock_api_mutex;
3315 lnet_shutdown_lndni(ni);
3317 if (lnet_count_acceptor_nets() == 0)
3318 lnet_acceptor_stop();
3320 lnet_ping_target_update(pbuf, ping_mdh);
3322 /* check if the net is empty and remove it if it is */
3324 lnet_shutdown_lndnet(net);
3326 goto unlock_api_mutex;
3331 mutex_unlock(&the_lnet.ln_api_mutex);
3337 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3338 * They are only expected to be called for unique networks.
3339 * That can be as a result of older DLC library
3340 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3343 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3345 struct lnet_net *net;
3346 LIST_HEAD(net_head);
3348 struct lnet_ioctl_config_lnd_tunables tun;
3349 char *nets = conf->cfg_config_u.cfg_net.net_intf;
3351 /* Create a net/ni structures for the network string */
3352 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3354 return rc == 0 ? -EINVAL : rc;
3356 mutex_lock(&the_lnet.ln_api_mutex);
3359 rc = -EINVAL; /* only add one network per call */
3360 goto out_unlock_clean;
3363 net = list_entry(net_head.next, struct lnet_net, net_list);
3364 list_del_init(&net->net_list);
3366 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3368 memset(&tun, 0, sizeof(tun));
3370 tun.lt_cmn.lct_peer_timeout =
3371 conf->cfg_config_u.cfg_net.net_peer_timeout;
3372 tun.lt_cmn.lct_peer_tx_credits =
3373 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3374 tun.lt_cmn.lct_peer_rtr_credits =
3375 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3376 tun.lt_cmn.lct_max_tx_credits =
3377 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3379 rc = lnet_add_net_common(net, &tun);
3382 mutex_unlock(&the_lnet.ln_api_mutex);
3383 while (!list_empty(&net_head)) {
3384 /* net_head list is empty in success case */
3385 net = list_entry(net_head.next, struct lnet_net, net_list);
3386 list_del_init(&net->net_list);
3393 lnet_dyn_del_net(__u32 net_id)
3395 struct lnet_net *net;
3396 struct lnet_ping_buffer *pbuf;
3397 struct lnet_handle_md ping_mdh;
3401 /* don't allow userspace to shutdown the LOLND */
3402 if (LNET_NETTYP(net_id) == LOLND)
3405 mutex_lock(&the_lnet.ln_api_mutex);
3409 net = lnet_get_net_locked(net_id);
3416 net_ni_count = lnet_get_net_ni_count_locked(net);
3420 /* create and link a new ping info, before removing the old one */
3421 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3422 lnet_get_ni_count() - net_ni_count, false);
3426 lnet_shutdown_lndnet(net);
3428 if (lnet_count_acceptor_nets() == 0)
3429 lnet_acceptor_stop();
3431 lnet_ping_target_update(pbuf, ping_mdh);
3434 mutex_unlock(&the_lnet.ln_api_mutex);
3439 void lnet_incr_dlc_seq(void)
3441 atomic_inc(&lnet_dlc_seq_no);
3444 __u32 lnet_get_dlc_seq_locked(void)
3446 return atomic_read(&lnet_dlc_seq_no);
3450 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3452 struct lnet_net *net;
3455 lnet_net_lock(LNET_LOCK_EX);
3456 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3457 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3458 if (ni->ni_nid == nid || all) {
3459 atomic_set(&ni->ni_healthv, value);
3460 if (list_empty(&ni->ni_recovery) &&
3461 value < LNET_MAX_HEALTH_VALUE) {
3462 CERROR("manually adding local NI %s to recovery\n",
3463 libcfs_nid2str(ni->ni_nid));
3464 list_add_tail(&ni->ni_recovery,
3465 &the_lnet.ln_mt_localNIRecovq);
3466 lnet_ni_addref_locked(ni, 0);
3469 lnet_net_unlock(LNET_LOCK_EX);
3475 lnet_net_unlock(LNET_LOCK_EX);
3479 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3483 lnet_nid_t nid = stats->hlni_nid;
3485 cpt = lnet_net_lock_current();
3486 ni = lnet_nid2ni_locked(nid, cpt);
3493 stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3494 stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3495 stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3496 stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3497 stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3498 stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3499 stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3502 lnet_net_unlock(cpt);
3508 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3513 lnet_net_lock(LNET_LOCK_EX);
3514 list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3515 list->rlst_nid_array[i] = ni->ni_nid;
3517 if (i >= LNET_MAX_SHOW_NUM_NID)
3520 lnet_net_unlock(LNET_LOCK_EX);
3521 list->rlst_num_nids = i;
3527 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3529 struct lnet_peer_ni *lpni;
3532 lnet_net_lock(LNET_LOCK_EX);
3533 list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3534 list->rlst_nid_array[i] = lpni->lpni_nid;
3536 if (i >= LNET_MAX_SHOW_NUM_NID)
3539 lnet_net_unlock(LNET_LOCK_EX);
3540 list->rlst_num_nids = i;
3546 * LNet ioctl handler.
3550 LNetCtl(unsigned int cmd, void *arg)
3552 struct libcfs_ioctl_data *data = arg;
3553 struct lnet_ioctl_config_data *config;
3554 struct lnet_process_id id = {0};
3558 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3559 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3562 case IOC_LIBCFS_GET_NI:
3563 rc = LNetGetId(data->ioc_count, &id);
3564 data->ioc_nid = id.nid;
3567 case IOC_LIBCFS_FAIL_NID:
3568 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3570 case IOC_LIBCFS_ADD_ROUTE: {
3571 /* default router sensitivity to 1 */
3572 unsigned int sensitivity = 1;
3575 if (config->cfg_hdr.ioc_len < sizeof(*config))
3578 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3580 config->cfg_config_u.cfg_route.rtr_sensitivity;
3583 mutex_lock(&the_lnet.ln_api_mutex);
3584 rc = lnet_add_route(config->cfg_net,
3585 config->cfg_config_u.cfg_route.rtr_hop,
3587 config->cfg_config_u.cfg_route.
3588 rtr_priority, sensitivity);
3589 mutex_unlock(&the_lnet.ln_api_mutex);
3593 case IOC_LIBCFS_DEL_ROUTE:
3596 if (config->cfg_hdr.ioc_len < sizeof(*config))
3599 mutex_lock(&the_lnet.ln_api_mutex);
3600 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3601 mutex_unlock(&the_lnet.ln_api_mutex);
3604 case IOC_LIBCFS_GET_ROUTE:
3607 if (config->cfg_hdr.ioc_len < sizeof(*config))
3610 mutex_lock(&the_lnet.ln_api_mutex);
3611 rc = lnet_get_route(config->cfg_count,
3613 &config->cfg_config_u.cfg_route.rtr_hop,
3615 &config->cfg_config_u.cfg_route.rtr_flags,
3616 &config->cfg_config_u.cfg_route.
3618 &config->cfg_config_u.cfg_route.
3620 mutex_unlock(&the_lnet.ln_api_mutex);
3623 case IOC_LIBCFS_GET_LOCAL_NI: {
3624 struct lnet_ioctl_config_ni *cfg_ni;
3625 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3626 struct lnet_ioctl_element_stats *stats;
3631 /* get the tunables if they are available */
3632 if (cfg_ni->lic_cfg_hdr.ioc_len <
3633 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3636 stats = (struct lnet_ioctl_element_stats *)
3638 tun = (struct lnet_ioctl_config_lnd_tunables *)
3639 (cfg_ni->lic_bulk + sizeof(*stats));
3641 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3644 mutex_lock(&the_lnet.ln_api_mutex);
3645 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3646 mutex_unlock(&the_lnet.ln_api_mutex);
3650 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3651 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3653 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3656 mutex_lock(&the_lnet.ln_api_mutex);
3657 rc = lnet_get_ni_stats(msg_stats);
3658 mutex_unlock(&the_lnet.ln_api_mutex);
3663 case IOC_LIBCFS_GET_NET: {
3664 size_t total = sizeof(*config) +
3665 sizeof(struct lnet_ioctl_net_config);
3668 if (config->cfg_hdr.ioc_len < total)
3671 mutex_lock(&the_lnet.ln_api_mutex);
3672 rc = lnet_get_net_config(config);
3673 mutex_unlock(&the_lnet.ln_api_mutex);
3677 case IOC_LIBCFS_GET_LNET_STATS:
3679 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3681 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3684 mutex_lock(&the_lnet.ln_api_mutex);
3685 lnet_counters_get(&lnet_stats->st_cntrs);
3686 mutex_unlock(&the_lnet.ln_api_mutex);
3690 case IOC_LIBCFS_CONFIG_RTR:
3693 if (config->cfg_hdr.ioc_len < sizeof(*config))
3696 mutex_lock(&the_lnet.ln_api_mutex);
3697 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3698 rc = lnet_rtrpools_enable();
3699 mutex_unlock(&the_lnet.ln_api_mutex);
3702 lnet_rtrpools_disable();
3703 mutex_unlock(&the_lnet.ln_api_mutex);
3706 case IOC_LIBCFS_ADD_BUF:
3709 if (config->cfg_hdr.ioc_len < sizeof(*config))
3712 mutex_lock(&the_lnet.ln_api_mutex);
3713 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3715 config->cfg_config_u.cfg_buffers.
3717 config->cfg_config_u.cfg_buffers.
3719 mutex_unlock(&the_lnet.ln_api_mutex);
3722 case IOC_LIBCFS_SET_NUMA_RANGE: {
3723 struct lnet_ioctl_set_value *numa;
3725 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3727 lnet_net_lock(LNET_LOCK_EX);
3728 lnet_numa_range = numa->sv_value;
3729 lnet_net_unlock(LNET_LOCK_EX);
3733 case IOC_LIBCFS_GET_NUMA_RANGE: {
3734 struct lnet_ioctl_set_value *numa;
3736 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3738 numa->sv_value = lnet_numa_range;
3742 case IOC_LIBCFS_GET_BUF: {
3743 struct lnet_ioctl_pool_cfg *pool_cfg;
3744 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3748 if (config->cfg_hdr.ioc_len < total)
3751 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3753 mutex_lock(&the_lnet.ln_api_mutex);
3754 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3755 mutex_unlock(&the_lnet.ln_api_mutex);
3759 case IOC_LIBCFS_GET_LOCAL_HSTATS: {
3760 struct lnet_ioctl_local_ni_hstats *stats = arg;
3762 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
3765 mutex_lock(&the_lnet.ln_api_mutex);
3766 rc = lnet_get_local_ni_hstats(stats);
3767 mutex_unlock(&the_lnet.ln_api_mutex);
3772 case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
3773 struct lnet_ioctl_recovery_list *list = arg;
3774 if (list->rlst_hdr.ioc_len < sizeof(*list))
3777 mutex_lock(&the_lnet.ln_api_mutex);
3778 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
3779 rc = lnet_get_local_ni_recovery_list(list);
3781 rc = lnet_get_peer_ni_recovery_list(list);
3782 mutex_unlock(&the_lnet.ln_api_mutex);
3786 case IOC_LIBCFS_ADD_PEER_NI: {
3787 struct lnet_ioctl_peer_cfg *cfg = arg;
3789 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3792 mutex_lock(&the_lnet.ln_api_mutex);
3793 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3796 mutex_unlock(&the_lnet.ln_api_mutex);
3800 case IOC_LIBCFS_DEL_PEER_NI: {
3801 struct lnet_ioctl_peer_cfg *cfg = arg;
3803 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3806 mutex_lock(&the_lnet.ln_api_mutex);
3807 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3808 cfg->prcfg_cfg_nid);
3809 mutex_unlock(&the_lnet.ln_api_mutex);
3813 case IOC_LIBCFS_GET_PEER_INFO: {
3814 struct lnet_ioctl_peer *peer_info = arg;
3816 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3819 mutex_lock(&the_lnet.ln_api_mutex);
3820 rc = lnet_get_peer_ni_info(
3821 peer_info->pr_count,
3823 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3824 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3825 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3826 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3827 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3828 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3829 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3830 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3831 mutex_unlock(&the_lnet.ln_api_mutex);
3835 case IOC_LIBCFS_GET_PEER_NI: {
3836 struct lnet_ioctl_peer_cfg *cfg = arg;
3838 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3841 mutex_lock(&the_lnet.ln_api_mutex);
3842 rc = lnet_get_peer_info(cfg,
3843 (void __user *)cfg->prcfg_bulk);
3844 mutex_unlock(&the_lnet.ln_api_mutex);
3848 case IOC_LIBCFS_GET_PEER_LIST: {
3849 struct lnet_ioctl_peer_cfg *cfg = arg;
3851 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3854 mutex_lock(&the_lnet.ln_api_mutex);
3855 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3856 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3857 mutex_unlock(&the_lnet.ln_api_mutex);
3861 case IOC_LIBCFS_SET_HEALHV: {
3862 struct lnet_ioctl_reset_health_cfg *cfg = arg;
3864 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
3866 if (cfg->rh_value < 0 ||
3867 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
3868 value = LNET_MAX_HEALTH_VALUE;
3870 value = cfg->rh_value;
3871 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
3872 value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
3873 "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
3874 mutex_lock(&the_lnet.ln_api_mutex);
3875 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
3876 lnet_ni_set_healthv(cfg->rh_nid, value,
3879 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
3881 mutex_unlock(&the_lnet.ln_api_mutex);
3885 case IOC_LIBCFS_NOTIFY_ROUTER: {
3886 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3888 /* The deadline passed in by the user should be some time in
3889 * seconds in the future since the UNIX epoch. We have to map
3890 * that deadline to the wall clock.
3892 deadline += ktime_get_seconds();
3893 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
3897 case IOC_LIBCFS_LNET_DIST:
3898 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3899 if (rc < 0 && rc != -EHOSTUNREACH)
3902 data->ioc_u32[0] = rc;
3905 case IOC_LIBCFS_TESTPROTOCOMPAT:
3906 the_lnet.ln_testprotocompat = data->ioc_flags;
3909 case IOC_LIBCFS_LNET_FAULT:
3910 return lnet_fault_ctl(data->ioc_flags, data);
3912 case IOC_LIBCFS_PING: {
3913 signed long timeout;
3915 id.nid = data->ioc_nid;
3916 id.pid = data->ioc_u32[0];
3918 /* If timeout is negative then set default of 3 minutes */
3919 if (((s32)data->ioc_u32[1] <= 0) ||
3920 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3921 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3923 timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
3925 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3926 data->ioc_plen1 / sizeof(struct lnet_process_id));
3931 data->ioc_count = rc;
3935 case IOC_LIBCFS_PING_PEER: {
3936 struct lnet_ioctl_ping_data *ping = arg;
3937 struct lnet_peer *lp;
3938 signed long timeout;
3940 /* If timeout is negative then set default of 3 minutes */
3941 if (((s32)ping->op_param) <= 0 ||
3942 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3943 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3945 timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
3947 rc = lnet_ping(ping->ping_id, timeout,
3953 mutex_lock(&the_lnet.ln_api_mutex);
3954 lp = lnet_find_peer(ping->ping_id.nid);
3956 ping->ping_id.nid = lp->lp_primary_nid;
3957 ping->mr_info = lnet_peer_is_multi_rail(lp);
3958 lnet_peer_decref_locked(lp);
3960 mutex_unlock(&the_lnet.ln_api_mutex);
3962 ping->ping_count = rc;
3966 case IOC_LIBCFS_DISCOVER: {
3967 struct lnet_ioctl_ping_data *discover = arg;
3968 struct lnet_peer *lp;
3970 rc = lnet_discover(discover->ping_id, discover->op_param,
3972 discover->ping_count);
3976 mutex_lock(&the_lnet.ln_api_mutex);
3977 lp = lnet_find_peer(discover->ping_id.nid);
3979 discover->ping_id.nid = lp->lp_primary_nid;
3980 discover->mr_info = lnet_peer_is_multi_rail(lp);
3981 lnet_peer_decref_locked(lp);
3983 mutex_unlock(&the_lnet.ln_api_mutex);
3985 discover->ping_count = rc;
3990 ni = lnet_net2ni_addref(data->ioc_net);
3994 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3997 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4004 EXPORT_SYMBOL(LNetCtl);
4006 void LNetDebugPeer(struct lnet_process_id id)
4008 lnet_debug_peer(id.nid);
4010 EXPORT_SYMBOL(LNetDebugPeer);
4013 * Determine if the specified peer \a nid is on the local node.
4015 * \param nid peer nid to check
4017 * \retval true If peer NID is on the local node.
4018 * \retval false If peer NID is not on the local node.
4020 bool LNetIsPeerLocal(lnet_nid_t nid)
4022 struct lnet_net *net;
4026 cpt = lnet_net_lock_current();
4027 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4028 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4029 if (ni->ni_nid == nid) {
4030 lnet_net_unlock(cpt);
4035 lnet_net_unlock(cpt);
4039 EXPORT_SYMBOL(LNetIsPeerLocal);
4042 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4043 * Note that all interfaces share a same PID, as requested by LNetNIInit().
4045 * \param index Index of the interface to look up.
4046 * \param id On successful return, this location will hold the
4047 * struct lnet_process_id ID of the interface.
4049 * \retval 0 If an interface exists at \a index.
4050 * \retval -ENOENT If no interface has been found.
4053 LNetGetId(unsigned int index, struct lnet_process_id *id)
4056 struct lnet_net *net;
4060 LASSERT(the_lnet.ln_refcount > 0);
4062 cpt = lnet_net_lock_current();
4064 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4065 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4069 id->nid = ni->ni_nid;
4070 id->pid = the_lnet.ln_pid;
4076 lnet_net_unlock(cpt);
4079 EXPORT_SYMBOL(LNetGetId);
4084 struct lnet_handle_md mdh;
4085 struct completion completion;
4089 lnet_ping_event_handler(struct lnet_event *event)
4091 struct ping_data *pd = event->md.user_ptr;
4093 CDEBUG(D_NET, "ping event (%d %d)%s\n",
4094 event->type, event->status,
4095 event->unlinked ? " unlinked" : "");
4097 if (event->status) {
4099 pd->rc = event->status;
4100 } else if (event->type == LNET_EVENT_REPLY) {
4102 pd->rc = event->mlength;
4104 if (event->unlinked)
4105 complete(&pd->completion);
4108 static int lnet_ping(struct lnet_process_id id, signed long timeout,
4109 struct lnet_process_id __user *ids, int n_ids)
4112 struct lnet_md md = { NULL };
4113 struct ping_data pd = { 0 };
4114 struct lnet_ping_buffer *pbuf;
4115 struct lnet_process_id tmpid;
4121 /* n_ids limit is arbitrary */
4122 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4126 * if the user buffer has more space than the lnet_interfaces_max
4127 * then only fill it up to lnet_interfaces_max
4129 if (n_ids > lnet_interfaces_max)
4130 n_ids = lnet_interfaces_max;
4132 if (id.pid == LNET_PID_ANY)
4133 id.pid = LNET_PID_LUSTRE;
4135 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4139 eq = LNetEQAlloc(lnet_ping_event_handler);
4142 CERROR("Can't allocate EQ: %d\n", rc);
4143 goto fail_ping_buffer_decref;
4146 /* initialize md content */
4147 md.start = &pbuf->pb_info;
4148 md.length = LNET_PING_INFO_SIZE(n_ids);
4149 md.threshold = 2; /* GET/REPLY */
4151 md.options = LNET_MD_TRUNCATE;
4155 init_completion(&pd.completion);
4157 rc = LNetMDBind(md, LNET_UNLINK, &pd.mdh);
4159 CERROR("Can't bind MD: %d\n", rc);
4163 rc = LNetGet(LNET_NID_ANY, pd.mdh, id,
4164 LNET_RESERVED_PORTAL,
4165 LNET_PROTO_PING_MATCHBITS, 0, false);
4168 /* Don't CERROR; this could be deliberate! */
4169 rc2 = LNetMDUnlink(pd.mdh);
4172 /* NB must wait for the UNLINK event below... */
4175 if (wait_for_completion_timeout(&pd.completion, timeout) == 0) {
4176 /* Ensure completion in finite time... */
4177 LNetMDUnlink(pd.mdh);
4178 wait_for_completion(&pd.completion);
4186 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4188 rc = -EPROTO; /* if I can't parse... */
4191 CERROR("%s: ping info too short %d\n",
4192 libcfs_id2str(id), nob);
4196 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4197 lnet_swap_pinginfo(pbuf);
4198 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4199 CERROR("%s: Unexpected magic %08x\n",
4200 libcfs_id2str(id), pbuf->pb_info.pi_magic);
4204 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4205 CERROR("%s: ping w/o NI status: 0x%x\n",
4206 libcfs_id2str(id), pbuf->pb_info.pi_features);
4210 if (nob < LNET_PING_INFO_SIZE(0)) {
4211 CERROR("%s: Short reply %d(%d min)\n",
4213 nob, (int)LNET_PING_INFO_SIZE(0));
4217 if (pbuf->pb_info.pi_nnis < n_ids)
4218 n_ids = pbuf->pb_info.pi_nnis;
4220 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4221 CERROR("%s: Short reply %d(%d expected)\n",
4223 nob, (int)LNET_PING_INFO_SIZE(n_ids));
4227 rc = -EFAULT; /* if I segv in copy_to_user()... */
4229 memset(&tmpid, 0, sizeof(tmpid));
4230 for (i = 0; i < n_ids; i++) {
4231 tmpid.pid = pbuf->pb_info.pi_pid;
4232 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4233 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4236 rc = pbuf->pb_info.pi_nnis;
4239 rc2 = LNetEQFree(eq);
4241 CERROR("rc2 %d\n", rc2);
4244 fail_ping_buffer_decref:
4245 lnet_ping_buffer_decref(pbuf);
4250 lnet_discover(struct lnet_process_id id, __u32 force,
4251 struct lnet_process_id __user *ids, int n_ids)
4253 struct lnet_peer_ni *lpni;
4254 struct lnet_peer_ni *p;
4255 struct lnet_peer *lp;
4256 struct lnet_process_id *buf;
4260 int max_intf = lnet_interfaces_max;
4263 id.nid == LNET_NID_ANY)
4266 if (id.pid == LNET_PID_ANY)
4267 id.pid = LNET_PID_LUSTRE;
4270 * if the user buffer has more space than the max_intf
4271 * then only fill it up to max_intf
4273 if (n_ids > max_intf)
4276 CFS_ALLOC_PTR_ARRAY(buf, n_ids);
4280 cpt = lnet_net_lock_current();
4281 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4288 * Clearing the NIDS_UPTODATE flag ensures the peer will
4289 * be discovered, provided discovery has not been disabled.
4291 lp = lpni->lpni_peer_net->lpn_peer;
4292 spin_lock(&lp->lp_lock);
4293 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4294 /* If the force flag is set, force a PING and PUSH as well. */
4296 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4297 spin_unlock(&lp->lp_lock);
4298 rc = lnet_discover_peer_locked(lpni, cpt, true);
4302 /* Peer may have changed. */
4303 lp = lpni->lpni_peer_net->lpn_peer;
4304 if (lp->lp_nnis < n_ids)
4305 n_ids = lp->lp_nnis;
4309 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4310 buf[i].pid = id.pid;
4311 buf[i].nid = p->lpni_nid;
4316 lnet_net_unlock(cpt);
4319 if (copy_to_user(ids, buf, n_ids * sizeof(*buf)))
4325 lnet_peer_ni_decref_locked(lpni);
4327 lnet_net_unlock(cpt);
4329 CFS_FREE_PTR_ARRAY(buf, n_ids);
4335 * Retrieve peer discovery status.
4337 * \retval 1 if lnet_peer_discovery_disabled is 0
4338 * \retval 0 if lnet_peer_discovery_disabled is 1
4341 LNetGetPeerDiscoveryStatus(void)
4343 return !lnet_peer_discovery_disabled;
4345 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);