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 #define LNET_LND_TIMEOUT_DEFAULT ((LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT - 1) / \
227 (LNET_RETRY_COUNT_HEALTH_DEFAULT + 1))
228 unsigned int lnet_lnd_timeout = LNET_LND_TIMEOUT_DEFAULT;
229 static void lnet_set_lnd_timeout(void)
231 lnet_lnd_timeout = (lnet_transaction_timeout - 1) /
232 (lnet_retry_count + 1);
235 unsigned int lnet_current_net_count;
238 * This sequence number keeps track of how many times DLC was used to
239 * update the local NIs. It is incremented when a NI is added or
240 * removed and checked when sending a message to determine if there is
241 * a need to re-run the selection algorithm. See lnet_select_pathway()
242 * for more details on its usage.
244 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
246 static int lnet_ping(struct lnet_process_id id, signed long timeout,
247 struct lnet_process_id __user *ids, int n_ids);
249 static int lnet_discover(struct lnet_process_id id, __u32 force,
250 struct lnet_process_id __user *ids, int n_ids);
253 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
256 unsigned *sensitivity = (unsigned *)kp->arg;
259 rc = kstrtoul(val, 0, &value);
261 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
266 * The purpose of locking the api_mutex here is to ensure that
267 * the correct value ends up stored properly.
269 mutex_lock(&the_lnet.ln_api_mutex);
271 if (value > LNET_MAX_HEALTH_VALUE) {
272 mutex_unlock(&the_lnet.ln_api_mutex);
273 CERROR("Invalid health value. Maximum: %d value = %lu\n",
274 LNET_MAX_HEALTH_VALUE, value);
279 * if we're turning on health then use the health timeout
282 if (*sensitivity == 0 && value != 0) {
283 lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT;
284 lnet_retry_count = LNET_RETRY_COUNT_HEALTH_DEFAULT;
285 lnet_set_lnd_timeout();
287 * if we're turning off health then use the no health timeout
290 } else if (*sensitivity != 0 && value == 0) {
291 lnet_transaction_timeout =
292 LNET_TRANSACTION_TIMEOUT_NO_HEALTH_DEFAULT;
293 lnet_retry_count = 0;
294 lnet_set_lnd_timeout();
297 *sensitivity = value;
299 mutex_unlock(&the_lnet.ln_api_mutex);
305 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
308 unsigned *interval = (unsigned *)kp->arg;
311 rc = kstrtoul(val, 0, &value);
313 CERROR("Invalid module parameter value for 'lnet_recovery_interval'\n");
318 CERROR("lnet_recovery_interval must be at least 1 second\n");
323 * The purpose of locking the api_mutex here is to ensure that
324 * the correct value ends up stored properly.
326 mutex_lock(&the_lnet.ln_api_mutex);
330 mutex_unlock(&the_lnet.ln_api_mutex);
336 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
339 unsigned *discovery_off = (unsigned *)kp->arg;
341 struct lnet_ping_buffer *pbuf;
343 rc = kstrtoul(val, 0, &value);
345 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
349 value = (value) ? 1 : 0;
352 * The purpose of locking the api_mutex here is to ensure that
353 * the correct value ends up stored properly.
355 mutex_lock(&the_lnet.ln_api_mutex);
357 if (value == *discovery_off) {
358 mutex_unlock(&the_lnet.ln_api_mutex);
363 * We still want to set the discovery value even when LNet is not
364 * running. This is the case when LNet is being loaded and we want
365 * the module parameters to take effect. Otherwise if we're
366 * changing the value dynamically, we want to set it after
369 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
370 *discovery_off = value;
371 mutex_unlock(&the_lnet.ln_api_mutex);
375 /* tell peers that discovery setting has changed */
376 lnet_net_lock(LNET_LOCK_EX);
377 pbuf = the_lnet.ln_ping_target;
379 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
381 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
382 lnet_net_unlock(LNET_LOCK_EX);
384 /* only send a push when we're turning off discovery */
385 if (*discovery_off <= 0 && value > 0)
386 lnet_push_update_to_peers(1);
387 *discovery_off = value;
389 mutex_unlock(&the_lnet.ln_api_mutex);
395 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
398 unsigned int *drop_asym_route = (unsigned int *)kp->arg;
401 rc = kstrtoul(val, 0, &value);
403 CERROR("Invalid module parameter value for "
404 "'lnet_drop_asym_route'\n");
409 * The purpose of locking the api_mutex here is to ensure that
410 * the correct value ends up stored properly.
412 mutex_lock(&the_lnet.ln_api_mutex);
414 if (value == *drop_asym_route) {
415 mutex_unlock(&the_lnet.ln_api_mutex);
419 *drop_asym_route = value;
421 mutex_unlock(&the_lnet.ln_api_mutex);
427 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
430 unsigned *transaction_to = (unsigned *)kp->arg;
433 rc = kstrtoul(val, 0, &value);
435 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
440 * The purpose of locking the api_mutex here is to ensure that
441 * the correct value ends up stored properly.
443 mutex_lock(&the_lnet.ln_api_mutex);
445 if (value < lnet_retry_count || value == 0) {
446 mutex_unlock(&the_lnet.ln_api_mutex);
447 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
448 "Has to be greater than lnet_retry_count (%u)\n",
449 value, lnet_retry_count);
453 if (value == *transaction_to) {
454 mutex_unlock(&the_lnet.ln_api_mutex);
458 *transaction_to = value;
459 /* Update the lnet_lnd_timeout now that we've modified the
460 * transaction timeout
462 lnet_set_lnd_timeout();
464 mutex_unlock(&the_lnet.ln_api_mutex);
470 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
473 unsigned *retry_count = (unsigned *)kp->arg;
476 rc = kstrtoul(val, 0, &value);
478 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
483 * The purpose of locking the api_mutex here is to ensure that
484 * the correct value ends up stored properly.
486 mutex_lock(&the_lnet.ln_api_mutex);
488 if (lnet_health_sensitivity == 0) {
489 mutex_unlock(&the_lnet.ln_api_mutex);
490 CERROR("Can not set retry_count when health feature is turned off\n");
494 if (value > lnet_transaction_timeout) {
495 mutex_unlock(&the_lnet.ln_api_mutex);
496 CERROR("Invalid value for lnet_retry_count (%lu). "
497 "Has to be smaller than lnet_transaction_timeout (%u)\n",
498 value, lnet_transaction_timeout);
502 *retry_count = value;
504 /* Update the lnet_lnd_timeout now that we've modified the
507 lnet_set_lnd_timeout();
509 mutex_unlock(&the_lnet.ln_api_mutex);
515 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
519 rc = kstrtoint(val, 0, &value);
521 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
525 if (value < LNET_INTERFACES_MIN) {
526 CWARN("max interfaces provided are too small, setting to %d\n",
527 LNET_INTERFACES_MAX_DEFAULT);
528 value = LNET_INTERFACES_MAX_DEFAULT;
531 *(int *)kp->arg = value;
537 lnet_get_routes(void)
543 lnet_get_networks(void)
548 if (*networks != 0 && *ip2nets != 0) {
549 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
550 "'ip2nets' but not both at once\n");
555 rc = lnet_parse_ip2nets(&nets, ip2nets);
556 return (rc == 0) ? nets : NULL;
566 lnet_init_locks(void)
568 spin_lock_init(&the_lnet.ln_eq_wait_lock);
569 spin_lock_init(&the_lnet.ln_msg_resend_lock);
570 init_completion(&the_lnet.ln_mt_wait_complete);
571 mutex_init(&the_lnet.ln_lnd_mutex);
574 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
575 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
577 struct kmem_cache *lnet_rspt_cachep; /* response tracker cache */
578 struct kmem_cache *lnet_msg_cachep;
581 lnet_slab_setup(void)
583 /* create specific kmem_cache for MEs and small MDs (i.e., originally
584 * allocated in <size-xxx> kmem_cache).
586 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
588 if (!lnet_mes_cachep)
591 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
592 LNET_SMALL_MD_SIZE, 0, 0,
594 if (!lnet_small_mds_cachep)
597 lnet_rspt_cachep = kmem_cache_create("lnet_rspt", sizeof(struct lnet_rsp_tracker),
599 if (!lnet_rspt_cachep)
602 lnet_msg_cachep = kmem_cache_create("lnet_msg", sizeof(struct lnet_msg),
604 if (!lnet_msg_cachep)
611 lnet_slab_cleanup(void)
613 if (lnet_msg_cachep) {
614 kmem_cache_destroy(lnet_msg_cachep);
615 lnet_msg_cachep = NULL;
619 if (lnet_rspt_cachep) {
620 kmem_cache_destroy(lnet_rspt_cachep);
621 lnet_rspt_cachep = NULL;
624 if (lnet_small_mds_cachep) {
625 kmem_cache_destroy(lnet_small_mds_cachep);
626 lnet_small_mds_cachep = NULL;
629 if (lnet_mes_cachep) {
630 kmem_cache_destroy(lnet_mes_cachep);
631 lnet_mes_cachep = NULL;
636 lnet_create_remote_nets_table(void)
639 struct list_head *hash;
641 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
642 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
643 CFS_ALLOC_PTR_ARRAY(hash, LNET_REMOTE_NETS_HASH_SIZE);
645 CERROR("Failed to create remote nets hash table\n");
649 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
650 INIT_LIST_HEAD(&hash[i]);
651 the_lnet.ln_remote_nets_hash = hash;
656 lnet_destroy_remote_nets_table(void)
660 if (the_lnet.ln_remote_nets_hash == NULL)
663 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
664 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
666 CFS_FREE_PTR_ARRAY(the_lnet.ln_remote_nets_hash,
667 LNET_REMOTE_NETS_HASH_SIZE);
668 the_lnet.ln_remote_nets_hash = NULL;
672 lnet_destroy_locks(void)
674 if (the_lnet.ln_res_lock != NULL) {
675 cfs_percpt_lock_free(the_lnet.ln_res_lock);
676 the_lnet.ln_res_lock = NULL;
679 if (the_lnet.ln_net_lock != NULL) {
680 cfs_percpt_lock_free(the_lnet.ln_net_lock);
681 the_lnet.ln_net_lock = NULL;
686 lnet_create_locks(void)
690 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
691 if (the_lnet.ln_res_lock == NULL)
694 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
695 if (the_lnet.ln_net_lock == NULL)
701 lnet_destroy_locks();
705 static void lnet_assert_wire_constants(void)
707 /* Wire protocol assertions generated by 'wirecheck'
708 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
709 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
710 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
713 BUILD_BUG_ON(LNET_PROTO_TCP_MAGIC != 0xeebc0ded);
714 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MAJOR != 1);
715 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MINOR != 0);
716 BUILD_BUG_ON(LNET_MSG_ACK != 0);
717 BUILD_BUG_ON(LNET_MSG_PUT != 1);
718 BUILD_BUG_ON(LNET_MSG_GET != 2);
719 BUILD_BUG_ON(LNET_MSG_REPLY != 3);
720 BUILD_BUG_ON(LNET_MSG_HELLO != 4);
722 /* Checks for struct lnet_handle_wire */
723 BUILD_BUG_ON((int)sizeof(struct lnet_handle_wire) != 16);
724 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
725 wh_interface_cookie) != 0);
726 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) != 8);
727 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
728 wh_object_cookie) != 8);
729 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) != 8);
731 /* Checks for struct struct lnet_magicversion */
732 BUILD_BUG_ON((int)sizeof(struct lnet_magicversion) != 8);
733 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, magic) != 0);
734 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->magic) != 4);
735 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, version_major) != 4);
736 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_major) != 2);
737 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion,
738 version_minor) != 6);
739 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_minor) != 2);
741 /* Checks for struct struct lnet_hdr */
742 BUILD_BUG_ON((int)sizeof(struct lnet_hdr) != 72);
743 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_nid) != 0);
744 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_nid) != 8);
745 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_nid) != 8);
746 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_nid) != 8);
747 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_pid) != 16);
748 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_pid) != 4);
749 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_pid) != 20);
750 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_pid) != 4);
751 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, type) != 24);
752 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->type) != 4);
753 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, payload_length) != 28);
754 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->payload_length) != 4);
755 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg) != 32);
756 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg) != 40);
759 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) != 32);
760 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) != 16);
761 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.match_bits) != 48);
762 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) != 8);
763 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.mlength) != 56);
764 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) != 4);
767 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) != 32);
768 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) != 16);
769 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.match_bits) != 48);
770 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) != 8);
771 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.hdr_data) != 56);
772 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) != 8);
773 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ptl_index) != 64);
774 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) != 4);
775 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.offset) != 68);
776 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) != 4);
779 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.return_wmd) != 32);
780 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) != 16);
781 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.match_bits) != 48);
782 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) != 8);
783 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.ptl_index) != 56);
784 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) != 4);
785 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.src_offset) != 60);
786 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) != 4);
787 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.sink_length) != 64);
788 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) != 4);
791 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) != 32);
792 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) != 16);
795 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.incarnation) != 32);
796 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) != 8);
797 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.type) != 40);
798 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) != 4);
800 /* Checks for struct lnet_ni_status and related constants */
801 BUILD_BUG_ON(LNET_NI_STATUS_INVALID != 0x00000000);
802 BUILD_BUG_ON(LNET_NI_STATUS_UP != 0x15aac0de);
803 BUILD_BUG_ON(LNET_NI_STATUS_DOWN != 0xdeadface);
805 /* Checks for struct lnet_ni_status */
806 BUILD_BUG_ON((int)sizeof(struct lnet_ni_status) != 16);
807 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_nid) != 0);
808 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) != 8);
809 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_status) != 8);
810 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_status) != 4);
811 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_unused) != 12);
812 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) != 4);
814 /* Checks for struct lnet_ping_info and related constants */
815 BUILD_BUG_ON(LNET_PROTO_PING_MAGIC != 0x70696E67);
816 BUILD_BUG_ON(LNET_PING_FEAT_INVAL != 0);
817 BUILD_BUG_ON(LNET_PING_FEAT_BASE != 1);
818 BUILD_BUG_ON(LNET_PING_FEAT_NI_STATUS != 2);
819 BUILD_BUG_ON(LNET_PING_FEAT_RTE_DISABLED != 4);
820 BUILD_BUG_ON(LNET_PING_FEAT_MULTI_RAIL != 8);
821 BUILD_BUG_ON(LNET_PING_FEAT_DISCOVERY != 16);
822 BUILD_BUG_ON(LNET_PING_FEAT_BITS != 31);
824 /* Checks for struct lnet_ping_info */
825 BUILD_BUG_ON((int)sizeof(struct lnet_ping_info) != 16);
826 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_magic) != 0);
827 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) != 4);
828 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_features) != 4);
829 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_features) != 4);
830 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_pid) != 8);
831 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) != 4);
832 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_nnis) != 12);
833 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) != 4);
834 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_ni) != 16);
835 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) != 0);
838 static const struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
840 const struct lnet_lnd *lnd;
842 /* holding lnd mutex */
843 if (type >= NUM_LNDS)
845 lnd = the_lnet.ln_lnds[type];
846 LASSERT(!lnd || lnd->lnd_type == type);
852 lnet_get_lnd_timeout(void)
854 return lnet_lnd_timeout;
856 EXPORT_SYMBOL(lnet_get_lnd_timeout);
859 lnet_register_lnd(const struct lnet_lnd *lnd)
861 mutex_lock(&the_lnet.ln_lnd_mutex);
863 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
864 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
866 the_lnet.ln_lnds[lnd->lnd_type] = lnd;
868 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
870 mutex_unlock(&the_lnet.ln_lnd_mutex);
872 EXPORT_SYMBOL(lnet_register_lnd);
875 lnet_unregister_lnd(const struct lnet_lnd *lnd)
877 mutex_lock(&the_lnet.ln_lnd_mutex);
879 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
881 the_lnet.ln_lnds[lnd->lnd_type] = NULL;
882 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
884 mutex_unlock(&the_lnet.ln_lnd_mutex);
886 EXPORT_SYMBOL(lnet_unregister_lnd);
889 lnet_counters_get_common(struct lnet_counters_common *common)
891 struct lnet_counters *ctr;
894 memset(common, 0, sizeof(*common));
896 lnet_net_lock(LNET_LOCK_EX);
898 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
899 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
900 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
901 common->lcc_errors += ctr->lct_common.lcc_errors;
902 common->lcc_send_count += ctr->lct_common.lcc_send_count;
903 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
904 common->lcc_route_count += ctr->lct_common.lcc_route_count;
905 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
906 common->lcc_send_length += ctr->lct_common.lcc_send_length;
907 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
908 common->lcc_route_length += ctr->lct_common.lcc_route_length;
909 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
911 lnet_net_unlock(LNET_LOCK_EX);
913 EXPORT_SYMBOL(lnet_counters_get_common);
916 lnet_counters_get(struct lnet_counters *counters)
918 struct lnet_counters *ctr;
919 struct lnet_counters_health *health = &counters->lct_health;
922 memset(counters, 0, sizeof(*counters));
924 lnet_counters_get_common(&counters->lct_common);
926 lnet_net_lock(LNET_LOCK_EX);
928 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
929 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
930 health->lch_resend_count += ctr->lct_health.lch_resend_count;
931 health->lch_response_timeout_count +=
932 ctr->lct_health.lch_response_timeout_count;
933 health->lch_local_interrupt_count +=
934 ctr->lct_health.lch_local_interrupt_count;
935 health->lch_local_dropped_count +=
936 ctr->lct_health.lch_local_dropped_count;
937 health->lch_local_aborted_count +=
938 ctr->lct_health.lch_local_aborted_count;
939 health->lch_local_no_route_count +=
940 ctr->lct_health.lch_local_no_route_count;
941 health->lch_local_timeout_count +=
942 ctr->lct_health.lch_local_timeout_count;
943 health->lch_local_error_count +=
944 ctr->lct_health.lch_local_error_count;
945 health->lch_remote_dropped_count +=
946 ctr->lct_health.lch_remote_dropped_count;
947 health->lch_remote_error_count +=
948 ctr->lct_health.lch_remote_error_count;
949 health->lch_remote_timeout_count +=
950 ctr->lct_health.lch_remote_timeout_count;
951 health->lch_network_timeout_count +=
952 ctr->lct_health.lch_network_timeout_count;
954 lnet_net_unlock(LNET_LOCK_EX);
956 EXPORT_SYMBOL(lnet_counters_get);
959 lnet_counters_reset(void)
961 struct lnet_counters *counters;
964 lnet_net_lock(LNET_LOCK_EX);
966 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
967 memset(counters, 0, sizeof(struct lnet_counters));
969 lnet_net_unlock(LNET_LOCK_EX);
973 lnet_res_type2str(int type)
978 case LNET_COOKIE_TYPE_MD:
980 case LNET_COOKIE_TYPE_ME:
982 case LNET_COOKIE_TYPE_EQ:
988 lnet_res_container_cleanup(struct lnet_res_container *rec)
992 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
995 while (!list_empty(&rec->rec_active)) {
996 struct list_head *e = rec->rec_active.next;
999 if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
1000 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
1002 } else { /* NB: Active MEs should be attached on portals */
1009 /* Found alive MD/ME/EQ, user really should unlink/free
1010 * all of them before finalize LNet, but if someone didn't,
1011 * we have to recycle garbage for him */
1012 CERROR("%d active elements on exit of %s container\n",
1013 count, lnet_res_type2str(rec->rec_type));
1016 if (rec->rec_lh_hash != NULL) {
1017 CFS_FREE_PTR_ARRAY(rec->rec_lh_hash, LNET_LH_HASH_SIZE);
1018 rec->rec_lh_hash = NULL;
1021 rec->rec_type = 0; /* mark it as finalized */
1025 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1030 LASSERT(rec->rec_type == 0);
1032 rec->rec_type = type;
1033 INIT_LIST_HEAD(&rec->rec_active);
1035 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1037 /* Arbitrary choice of hash table size */
1038 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1039 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1040 if (rec->rec_lh_hash == NULL) {
1045 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1046 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1051 CERROR("Failed to setup %s resource container\n",
1052 lnet_res_type2str(type));
1053 lnet_res_container_cleanup(rec);
1058 lnet_res_containers_destroy(struct lnet_res_container **recs)
1060 struct lnet_res_container *rec;
1063 cfs_percpt_for_each(rec, i, recs)
1064 lnet_res_container_cleanup(rec);
1066 cfs_percpt_free(recs);
1069 static struct lnet_res_container **
1070 lnet_res_containers_create(int type)
1072 struct lnet_res_container **recs;
1073 struct lnet_res_container *rec;
1077 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1079 CERROR("Failed to allocate %s resource containers\n",
1080 lnet_res_type2str(type));
1084 cfs_percpt_for_each(rec, i, recs) {
1085 rc = lnet_res_container_setup(rec, i, type);
1087 lnet_res_containers_destroy(recs);
1095 struct lnet_libhandle *
1096 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1098 /* ALWAYS called with lnet_res_lock held */
1099 struct list_head *head;
1100 struct lnet_libhandle *lh;
1103 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1106 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1107 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1109 list_for_each_entry(lh, head, lh_hash_chain) {
1110 if (lh->lh_cookie == cookie)
1118 lnet_res_lh_initialize(struct lnet_res_container *rec,
1119 struct lnet_libhandle *lh)
1121 /* ALWAYS called with lnet_res_lock held */
1122 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1125 lh->lh_cookie = rec->rec_lh_cookie;
1126 rec->rec_lh_cookie += 1 << ibits;
1128 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1130 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1134 lnet_create_array_of_queues(void)
1136 struct list_head **qs;
1137 struct list_head *q;
1140 qs = cfs_percpt_alloc(lnet_cpt_table(),
1141 sizeof(struct list_head));
1143 CERROR("Failed to allocate queues\n");
1147 cfs_percpt_for_each(q, i, qs)
1153 static int lnet_unprepare(void);
1156 lnet_prepare(lnet_pid_t requested_pid)
1158 /* Prepare to bring up the network */
1159 struct lnet_res_container **recs;
1162 if (requested_pid == LNET_PID_ANY) {
1163 /* Don't instantiate LNET just for me */
1167 LASSERT(the_lnet.ln_refcount == 0);
1169 the_lnet.ln_routing = 0;
1171 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1172 the_lnet.ln_pid = requested_pid;
1174 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1175 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1176 INIT_LIST_HEAD(&the_lnet.ln_nets);
1177 INIT_LIST_HEAD(&the_lnet.ln_routers);
1178 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1179 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1180 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1181 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1182 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1183 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1184 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1185 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1186 the_lnet.ln_mt_handler = NULL;
1187 init_completion(&the_lnet.ln_started);
1189 rc = lnet_slab_setup();
1193 rc = lnet_create_remote_nets_table();
1198 * NB the interface cookie in wire handles guards against delayed
1199 * replies and ACKs appearing valid after reboot.
1201 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1203 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1204 sizeof(struct lnet_counters));
1205 if (the_lnet.ln_counters == NULL) {
1206 CERROR("Failed to allocate counters for LNet\n");
1211 rc = lnet_peer_tables_create();
1215 rc = lnet_msg_containers_create();
1219 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1220 LNET_COOKIE_TYPE_EQ);
1224 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1230 the_lnet.ln_md_containers = recs;
1232 rc = lnet_portals_create();
1234 CERROR("Failed to create portals for LNet: %d\n", rc);
1238 the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1239 if (!the_lnet.ln_mt_zombie_rstqs) {
1252 lnet_unprepare (void)
1254 /* NB no LNET_LOCK since this is the last reference. All LND instances
1255 * have shut down already, so it is safe to unlink and free all
1256 * descriptors, even those that appear committed to a network op (eg MD
1257 * with non-zero pending count) */
1259 lnet_fail_nid(LNET_NID_ANY, 0);
1261 LASSERT(the_lnet.ln_refcount == 0);
1262 LASSERT(list_empty(&the_lnet.ln_test_peers));
1263 LASSERT(list_empty(&the_lnet.ln_nets));
1265 if (the_lnet.ln_mt_zombie_rstqs) {
1266 lnet_clean_zombie_rstqs();
1267 the_lnet.ln_mt_zombie_rstqs = NULL;
1270 lnet_assert_handler_unused(the_lnet.ln_mt_handler);
1271 the_lnet.ln_mt_handler = NULL;
1273 lnet_portals_destroy();
1275 if (the_lnet.ln_md_containers != NULL) {
1276 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1277 the_lnet.ln_md_containers = NULL;
1280 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1282 lnet_msg_containers_destroy();
1284 lnet_rtrpools_free(0);
1286 if (the_lnet.ln_counters != NULL) {
1287 cfs_percpt_free(the_lnet.ln_counters);
1288 the_lnet.ln_counters = NULL;
1290 lnet_destroy_remote_nets_table();
1291 lnet_slab_cleanup();
1297 lnet_net2ni_locked(__u32 net_id, int cpt)
1300 struct lnet_net *net;
1302 LASSERT(cpt != LNET_LOCK_EX);
1304 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1305 if (net->net_id == net_id) {
1306 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1316 lnet_net2ni_addref(__u32 net)
1321 ni = lnet_net2ni_locked(net, 0);
1323 lnet_ni_addref_locked(ni, 0);
1328 EXPORT_SYMBOL(lnet_net2ni_addref);
1331 lnet_get_net_locked(__u32 net_id)
1333 struct lnet_net *net;
1335 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1336 if (net->net_id == net_id)
1344 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1349 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1354 val = hash_long(key, LNET_CPT_BITS);
1355 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1359 return (unsigned int)(key + val + (val >> 1)) % number;
1363 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1365 struct lnet_net *net;
1367 /* must called with hold of lnet_net_lock */
1368 if (LNET_CPT_NUMBER == 1)
1369 return 0; /* the only one */
1372 * If NI is provided then use the CPT identified in the NI cpt
1373 * list if one exists. If one doesn't exist, then that NI is
1374 * associated with all CPTs and it follows that the net it belongs
1375 * to is implicitly associated with all CPTs, so just hash the nid
1379 if (ni->ni_cpts != NULL)
1380 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1383 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1386 /* no NI provided so look at the net */
1387 net = lnet_get_net_locked(LNET_NIDNET(nid));
1389 if (net != NULL && net->net_cpts != NULL) {
1390 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1393 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1397 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1402 if (LNET_CPT_NUMBER == 1)
1403 return 0; /* the only one */
1405 cpt = lnet_net_lock_current();
1407 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1409 lnet_net_unlock(cpt);
1413 EXPORT_SYMBOL(lnet_cpt_of_nid);
1416 lnet_islocalnet_locked(__u32 net_id)
1418 struct lnet_net *net;
1421 net = lnet_get_net_locked(net_id);
1423 local = net != NULL;
1429 lnet_islocalnet(__u32 net_id)
1434 cpt = lnet_net_lock_current();
1436 local = lnet_islocalnet_locked(net_id);
1438 lnet_net_unlock(cpt);
1444 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1446 struct lnet_net *net;
1449 LASSERT(cpt != LNET_LOCK_EX);
1451 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1452 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1453 if (ni->ni_nid == nid)
1462 lnet_nid2ni_addref(lnet_nid_t nid)
1467 ni = lnet_nid2ni_locked(nid, 0);
1469 lnet_ni_addref_locked(ni, 0);
1474 EXPORT_SYMBOL(lnet_nid2ni_addref);
1477 lnet_islocalnid(lnet_nid_t nid)
1482 cpt = lnet_net_lock_current();
1483 ni = lnet_nid2ni_locked(nid, cpt);
1484 lnet_net_unlock(cpt);
1490 lnet_count_acceptor_nets(void)
1492 /* Return the # of NIs that need the acceptor. */
1494 struct lnet_net *net;
1497 cpt = lnet_net_lock_current();
1498 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1499 /* all socklnd type networks should have the acceptor
1501 if (net->net_lnd->lnd_accept != NULL)
1505 lnet_net_unlock(cpt);
1510 struct lnet_ping_buffer *
1511 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1513 struct lnet_ping_buffer *pbuf;
1515 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1517 pbuf->pb_nnis = nnis;
1518 pbuf->pb_needs_post = false;
1519 atomic_set(&pbuf->pb_refcnt, 1);
1526 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1528 LASSERT(atomic_read(&pbuf->pb_refcnt) == 0);
1529 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1532 static struct lnet_ping_buffer *
1533 lnet_ping_target_create(int nnis)
1535 struct lnet_ping_buffer *pbuf;
1537 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1539 CERROR("Can't allocate ping source [%d]\n", nnis);
1543 pbuf->pb_info.pi_nnis = nnis;
1544 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1545 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1546 pbuf->pb_info.pi_features =
1547 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1553 lnet_get_net_ni_count_locked(struct lnet_net *net)
1558 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1565 lnet_get_net_ni_count_pre(struct lnet_net *net)
1570 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1577 lnet_get_ni_count(void)
1580 struct lnet_net *net;
1585 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1586 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1596 lnet_get_net_count(void)
1598 struct lnet_net *net;
1603 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1613 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1615 struct lnet_ni_status *stat;
1619 __swab32s(&pbuf->pb_info.pi_magic);
1620 __swab32s(&pbuf->pb_info.pi_features);
1621 __swab32s(&pbuf->pb_info.pi_pid);
1622 __swab32s(&pbuf->pb_info.pi_nnis);
1623 nnis = pbuf->pb_info.pi_nnis;
1624 if (nnis > pbuf->pb_nnis)
1625 nnis = pbuf->pb_nnis;
1626 for (i = 0; i < nnis; i++) {
1627 stat = &pbuf->pb_info.pi_ni[i];
1628 __swab64s(&stat->ns_nid);
1629 __swab32s(&stat->ns_status);
1634 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1638 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1640 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1642 /* Loopback is guaranteed to be present */
1643 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1645 if (LNET_PING_INFO_LONI(pinfo) != LNET_NID_LO_0)
1651 lnet_ping_target_destroy(void)
1653 struct lnet_net *net;
1656 lnet_net_lock(LNET_LOCK_EX);
1658 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1659 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1661 ni->ni_status = NULL;
1666 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1667 the_lnet.ln_ping_target = NULL;
1669 lnet_net_unlock(LNET_LOCK_EX);
1673 lnet_ping_target_event_handler(struct lnet_event *event)
1675 struct lnet_ping_buffer *pbuf = event->md_user_ptr;
1677 if (event->unlinked)
1678 lnet_ping_buffer_decref(pbuf);
1682 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1683 struct lnet_handle_md *ping_mdh,
1684 int ni_count, bool set_eq)
1686 struct lnet_process_id id = {
1687 .nid = LNET_NID_ANY,
1691 struct lnet_md md = { NULL };
1695 the_lnet.ln_ping_target_handler =
1696 lnet_ping_target_event_handler;
1698 *ppbuf = lnet_ping_target_create(ni_count);
1699 if (*ppbuf == NULL) {
1704 /* Ping target ME/MD */
1705 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1706 LNET_PROTO_PING_MATCHBITS, 0,
1707 LNET_UNLINK, LNET_INS_AFTER);
1710 CERROR("Can't create ping target ME: %d\n", rc);
1711 goto fail_decref_ping_buffer;
1714 /* initialize md content */
1715 md.start = &(*ppbuf)->pb_info;
1716 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1717 md.threshold = LNET_MD_THRESH_INF;
1719 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1720 LNET_MD_MANAGE_REMOTE;
1721 md.handler = the_lnet.ln_ping_target_handler;
1722 md.user_ptr = *ppbuf;
1724 rc = LNetMDAttach(me, &md, LNET_RETAIN, ping_mdh);
1726 CERROR("Can't attach ping target MD: %d\n", rc);
1727 goto fail_unlink_ping_me;
1729 lnet_ping_buffer_addref(*ppbuf);
1733 fail_unlink_ping_me:
1735 fail_decref_ping_buffer:
1736 LASSERT(atomic_read(&(*ppbuf)->pb_refcnt) == 1);
1737 lnet_ping_buffer_decref(*ppbuf);
1744 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1745 struct lnet_handle_md *ping_mdh)
1747 LNetMDUnlink(*ping_mdh);
1748 LNetInvalidateMDHandle(ping_mdh);
1750 /* NB the MD could be busy; this just starts the unlink */
1751 wait_var_event_warning(&pbuf->pb_refcnt,
1752 atomic_read(&pbuf->pb_refcnt) <= 1,
1753 "Still waiting for ping data MD to unlink\n");
1757 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1760 struct lnet_net *net;
1761 struct lnet_ni_status *ns;
1766 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1767 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1768 LASSERT(i < pbuf->pb_nnis);
1770 ns = &pbuf->pb_info.pi_ni[i];
1772 ns->ns_nid = ni->ni_nid;
1775 ns->ns_status = (ni->ni_status != NULL) ?
1776 ni->ni_status->ns_status :
1785 * We (ab)use the ns_status of the loopback interface to
1786 * transmit the sequence number. The first interface listed
1787 * must be the loopback interface.
1789 rc = lnet_ping_info_validate(&pbuf->pb_info);
1791 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1794 LNET_PING_BUFFER_SEQNO(pbuf) =
1795 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1799 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1800 struct lnet_handle_md ping_mdh)
1802 struct lnet_ping_buffer *old_pbuf = NULL;
1803 struct lnet_handle_md old_ping_md;
1805 /* switch the NIs to point to the new ping info created */
1806 lnet_net_lock(LNET_LOCK_EX);
1808 if (!the_lnet.ln_routing)
1809 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1810 if (!lnet_peer_discovery_disabled)
1811 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1813 /* Ensure only known feature bits have been set. */
1814 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1815 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1817 lnet_ping_target_install_locked(pbuf);
1819 if (the_lnet.ln_ping_target) {
1820 old_pbuf = the_lnet.ln_ping_target;
1821 old_ping_md = the_lnet.ln_ping_target_md;
1823 the_lnet.ln_ping_target_md = ping_mdh;
1824 the_lnet.ln_ping_target = pbuf;
1826 lnet_net_unlock(LNET_LOCK_EX);
1829 /* unlink and free the old ping info */
1830 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1831 lnet_ping_buffer_decref(old_pbuf);
1834 lnet_push_update_to_peers(0);
1838 lnet_ping_target_fini(void)
1840 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1841 &the_lnet.ln_ping_target_md);
1843 lnet_assert_handler_unused(the_lnet.ln_ping_target_handler);
1844 lnet_ping_target_destroy();
1847 /* Resize the push target. */
1848 int lnet_push_target_resize(void)
1850 struct lnet_handle_md mdh;
1851 struct lnet_handle_md old_mdh;
1852 struct lnet_ping_buffer *pbuf;
1853 struct lnet_ping_buffer *old_pbuf;
1858 nnis = the_lnet.ln_push_target_nnis;
1860 CDEBUG(D_NET, "Invalid nnis %d\n", nnis);
1864 /* NB: lnet_ping_buffer_alloc() sets pbuf refcount to 1. That ref is
1865 * dropped when we need to resize again (see "old_pbuf" below) or when
1866 * LNet is shutdown (see lnet_push_target_fini())
1868 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1870 CDEBUG(D_NET, "Can't allocate pbuf for nnis %d\n", nnis);
1874 rc = lnet_push_target_post(pbuf, &mdh);
1876 CDEBUG(D_NET, "Failed to post push target: %d\n", rc);
1877 lnet_ping_buffer_decref(pbuf);
1881 lnet_net_lock(LNET_LOCK_EX);
1882 old_pbuf = the_lnet.ln_push_target;
1883 old_mdh = the_lnet.ln_push_target_md;
1884 the_lnet.ln_push_target = pbuf;
1885 the_lnet.ln_push_target_md = mdh;
1886 lnet_net_unlock(LNET_LOCK_EX);
1889 LNetMDUnlink(old_mdh);
1890 /* Drop ref set by lnet_ping_buffer_alloc() */
1891 lnet_ping_buffer_decref(old_pbuf);
1894 /* Received another push or reply that requires a larger buffer */
1895 if (nnis < the_lnet.ln_push_target_nnis)
1898 CDEBUG(D_NET, "nnis %d success\n", nnis);
1902 int lnet_push_target_post(struct lnet_ping_buffer *pbuf,
1903 struct lnet_handle_md *mdhp)
1905 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1906 struct lnet_md md = { NULL };
1910 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1911 LNET_PROTO_PING_MATCHBITS, 0,
1912 LNET_UNLINK, LNET_INS_AFTER);
1915 CERROR("Can't create push target ME: %d\n", rc);
1919 pbuf->pb_needs_post = false;
1921 /* This reference is dropped by lnet_push_target_event_handler() */
1922 lnet_ping_buffer_addref(pbuf);
1924 /* initialize md content */
1925 md.start = &pbuf->pb_info;
1926 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
1929 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE;
1931 md.handler = the_lnet.ln_push_target_handler;
1933 rc = LNetMDAttach(me, &md, LNET_UNLINK, mdhp);
1935 CERROR("Can't attach push MD: %d\n", rc);
1937 lnet_ping_buffer_decref(pbuf);
1938 pbuf->pb_needs_post = true;
1942 CDEBUG(D_NET, "posted push target %p\n", pbuf);
1947 static void lnet_push_target_event_handler(struct lnet_event *ev)
1949 struct lnet_ping_buffer *pbuf = ev->md_user_ptr;
1951 CDEBUG(D_NET, "type %d status %d unlinked %d\n", ev->type, ev->status,
1954 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1955 lnet_swap_pinginfo(pbuf);
1957 if (ev->type == LNET_EVENT_UNLINK) {
1958 /* Drop ref added by lnet_push_target_post() */
1959 lnet_ping_buffer_decref(pbuf);
1963 lnet_peer_push_event(ev);
1965 /* Drop ref added by lnet_push_target_post */
1966 lnet_ping_buffer_decref(pbuf);
1969 /* Initialize the push target. */
1970 static int lnet_push_target_init(void)
1974 if (the_lnet.ln_push_target)
1977 the_lnet.ln_push_target_handler =
1978 lnet_push_target_event_handler;
1980 rc = LNetSetLazyPortal(LNET_RESERVED_PORTAL);
1983 /* Start at the required minimum, we'll enlarge if required. */
1984 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1986 rc = lnet_push_target_resize();
1989 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
1990 the_lnet.ln_push_target_handler = NULL;
1996 /* Clean up the push target. */
1997 static void lnet_push_target_fini(void)
1999 if (!the_lnet.ln_push_target)
2002 /* Unlink and invalidate to prevent new references. */
2003 LNetMDUnlink(the_lnet.ln_push_target_md);
2004 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
2006 /* Wait for the unlink to complete. */
2007 wait_var_event_warning(&the_lnet.ln_push_target->pb_refcnt,
2008 atomic_read(&the_lnet.ln_push_target->pb_refcnt) <= 1,
2009 "Still waiting for ping data MD to unlink\n");
2011 /* Drop ref set by lnet_ping_buffer_alloc() */
2012 lnet_ping_buffer_decref(the_lnet.ln_push_target);
2013 the_lnet.ln_push_target = NULL;
2014 the_lnet.ln_push_target_nnis = 0;
2016 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2017 lnet_assert_handler_unused(the_lnet.ln_push_target_handler);
2018 the_lnet.ln_push_target_handler = NULL;
2022 lnet_ni_tq_credits(struct lnet_ni *ni)
2026 LASSERT(ni->ni_ncpts >= 1);
2028 if (ni->ni_ncpts == 1)
2029 return ni->ni_net->net_tunables.lct_max_tx_credits;
2031 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2032 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2033 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2039 lnet_ni_unlink_locked(struct lnet_ni *ni)
2041 /* move it to zombie list and nobody can find it anymore */
2042 LASSERT(!list_empty(&ni->ni_netlist));
2043 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2044 lnet_ni_decref_locked(ni, 0);
2048 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2053 struct list_head *zombie_list = &net->net_ni_zombie;
2056 * Now wait for the NIs I just nuked to show up on the zombie
2057 * list and shut them down in guaranteed thread context
2060 while (!list_empty(zombie_list)) {
2064 ni = list_entry(zombie_list->next,
2065 struct lnet_ni, ni_netlist);
2066 list_del_init(&ni->ni_netlist);
2067 /* the ni should be in deleting state. If it's not it's
2069 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2070 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2073 /* still busy, add it back to zombie list */
2074 list_add(&ni->ni_netlist, zombie_list);
2078 if (!list_empty(&ni->ni_netlist)) {
2079 lnet_net_unlock(LNET_LOCK_EX);
2081 if ((i & (-i)) == i) {
2083 "Waiting for zombie LNI %s\n",
2084 libcfs_nid2str(ni->ni_nid));
2086 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2087 lnet_net_lock(LNET_LOCK_EX);
2091 lnet_net_unlock(LNET_LOCK_EX);
2093 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2095 LASSERT(!in_interrupt());
2096 /* Holding the mutex makes it safe for lnd_shutdown
2097 * to call module_put(). Module unload cannot finish
2098 * until lnet_unregister_lnd() completes, and that
2099 * requires the mutex.
2101 mutex_lock(&the_lnet.ln_lnd_mutex);
2102 (net->net_lnd->lnd_shutdown)(ni);
2103 mutex_unlock(&the_lnet.ln_lnd_mutex);
2106 CDEBUG(D_LNI, "Removed LNI %s\n",
2107 libcfs_nid2str(ni->ni_nid));
2111 lnet_net_lock(LNET_LOCK_EX);
2115 /* shutdown down the NI and release refcount */
2117 lnet_shutdown_lndni(struct lnet_ni *ni)
2120 struct lnet_net *net = ni->ni_net;
2122 lnet_net_lock(LNET_LOCK_EX);
2124 ni->ni_state = LNET_NI_STATE_DELETING;
2126 lnet_ni_unlink_locked(ni);
2127 lnet_incr_dlc_seq();
2128 lnet_net_unlock(LNET_LOCK_EX);
2130 /* clear messages for this NI on the lazy portal */
2131 for (i = 0; i < the_lnet.ln_nportals; i++)
2132 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2134 lnet_net_lock(LNET_LOCK_EX);
2135 lnet_clear_zombies_nis_locked(net);
2136 lnet_net_unlock(LNET_LOCK_EX);
2140 lnet_shutdown_lndnet(struct lnet_net *net)
2144 lnet_net_lock(LNET_LOCK_EX);
2146 list_del_init(&net->net_list);
2148 while (!list_empty(&net->net_ni_list)) {
2149 ni = list_entry(net->net_ni_list.next,
2150 struct lnet_ni, ni_netlist);
2151 lnet_net_unlock(LNET_LOCK_EX);
2152 lnet_shutdown_lndni(ni);
2153 lnet_net_lock(LNET_LOCK_EX);
2156 lnet_net_unlock(LNET_LOCK_EX);
2158 /* Do peer table cleanup for this net */
2159 lnet_peer_tables_cleanup(net);
2165 lnet_shutdown_lndnets(void)
2167 struct lnet_net *net;
2169 struct lnet_msg *msg, *tmp;
2171 /* NB called holding the global mutex */
2173 /* All quiet on the API front */
2174 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2175 LASSERT(the_lnet.ln_refcount == 0);
2177 lnet_net_lock(LNET_LOCK_EX);
2178 the_lnet.ln_state = LNET_STATE_STOPPING;
2181 * move the nets to the zombie list to avoid them being
2182 * picked up for new work. LONET is also included in the
2183 * Nets that will be moved to the zombie list
2185 list_splice_init(&the_lnet.ln_nets, &the_lnet.ln_net_zombie);
2187 /* Drop the cached loopback Net. */
2188 if (the_lnet.ln_loni != NULL) {
2189 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2190 the_lnet.ln_loni = NULL;
2192 lnet_net_unlock(LNET_LOCK_EX);
2194 /* iterate through the net zombie list and delete each net */
2195 while (!list_empty(&the_lnet.ln_net_zombie)) {
2196 net = list_entry(the_lnet.ln_net_zombie.next,
2197 struct lnet_net, net_list);
2198 lnet_shutdown_lndnet(net);
2201 spin_lock(&the_lnet.ln_msg_resend_lock);
2202 list_splice(&the_lnet.ln_msg_resend, &resend);
2203 spin_unlock(&the_lnet.ln_msg_resend_lock);
2205 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2206 list_del_init(&msg->msg_list);
2207 msg->msg_no_resend = true;
2208 lnet_finalize(msg, -ECANCELED);
2211 lnet_net_lock(LNET_LOCK_EX);
2212 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2213 lnet_net_unlock(LNET_LOCK_EX);
2217 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2220 struct lnet_tx_queue *tq;
2222 struct lnet_net *net = ni->ni_net;
2224 mutex_lock(&the_lnet.ln_lnd_mutex);
2227 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2228 ni->ni_lnd_tunables_set = true;
2231 rc = (net->net_lnd->lnd_startup)(ni);
2233 mutex_unlock(&the_lnet.ln_lnd_mutex);
2236 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2237 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2242 ni->ni_state = LNET_NI_STATE_ACTIVE;
2245 /* We keep a reference on the loopback net through the loopback NI */
2246 if (net->net_lnd->lnd_type == LOLND) {
2248 LASSERT(the_lnet.ln_loni == NULL);
2249 the_lnet.ln_loni = ni;
2250 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2251 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2252 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2253 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2257 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2258 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2259 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2260 libcfs_lnd2str(net->net_lnd->lnd_type),
2261 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2263 /* shutdown the NI since if we get here then it must've already
2266 lnet_shutdown_lndni(ni);
2270 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2271 tq->tq_credits_min =
2272 tq->tq_credits_max =
2273 tq->tq_credits = lnet_ni_tq_credits(ni);
2276 atomic_set(&ni->ni_tx_credits,
2277 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2278 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2280 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2281 libcfs_nid2str(ni->ni_nid),
2282 ni->ni_net->net_tunables.lct_peer_tx_credits,
2283 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2284 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2285 ni->ni_net->net_tunables.lct_peer_timeout);
2294 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2297 struct lnet_net *net_l = NULL;
2298 LIST_HEAD(local_ni_list);
2302 const struct lnet_lnd *lnd;
2304 net->net_tunables.lct_peer_timeout;
2306 net->net_tunables.lct_max_tx_credits;
2307 int peerrtrcredits =
2308 net->net_tunables.lct_peer_rtr_credits;
2311 * make sure that this net is unique. If it isn't then
2312 * we are adding interfaces to an already existing network, and
2313 * 'net' is just a convenient way to pass in the list.
2314 * if it is unique we need to find the LND and load it if
2317 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2318 lnd_type = LNET_NETTYP(net->net_id);
2320 mutex_lock(&the_lnet.ln_lnd_mutex);
2321 lnd = lnet_find_lnd_by_type(lnd_type);
2324 mutex_unlock(&the_lnet.ln_lnd_mutex);
2325 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2326 mutex_lock(&the_lnet.ln_lnd_mutex);
2328 lnd = lnet_find_lnd_by_type(lnd_type);
2330 mutex_unlock(&the_lnet.ln_lnd_mutex);
2331 CERROR("Can't load LND %s, module %s, rc=%d\n",
2332 libcfs_lnd2str(lnd_type),
2333 libcfs_lnd2modname(lnd_type), rc);
2334 #ifndef HAVE_MODULE_LOADING_SUPPORT
2335 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2336 "compiled with kernel module "
2337 "loading support.");
2346 mutex_unlock(&the_lnet.ln_lnd_mutex);
2352 * net_l: if the network being added is unique then net_l
2353 * will point to that network
2354 * if the network being added is not unique then
2355 * net_l points to the existing network.
2357 * When we enter the loop below, we'll pick NIs off he
2358 * network beign added and start them up, then add them to
2359 * a local ni list. Once we've successfully started all
2360 * the NIs then we join the local NI list (of started up
2361 * networks) with the net_l->net_ni_list, which should
2362 * point to the correct network to add the new ni list to
2364 * If any of the new NIs fail to start up, then we want to
2365 * iterate through the local ni list, which should include
2366 * any NIs which were successfully started up, and shut
2369 * After than we want to delete the network being added,
2370 * to avoid a memory leak.
2374 * When a network uses TCP bonding then all its interfaces
2375 * must be specified when the network is first defined: the
2376 * TCP bonding code doesn't allow for interfaces to be added
2379 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2380 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2385 while (!list_empty(&net->net_ni_added)) {
2386 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2388 list_del_init(&ni->ni_netlist);
2390 /* make sure that the the NI we're about to start
2391 * up is actually unique. if it's not fail. */
2392 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2393 ni->ni_interfaces[0])) {
2398 /* adjust the pointer the parent network, just in case it
2399 * the net is a duplicate */
2402 rc = lnet_startup_lndni(ni, tun);
2408 list_add_tail(&ni->ni_netlist, &local_ni_list);
2413 lnet_net_lock(LNET_LOCK_EX);
2414 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2415 lnet_incr_dlc_seq();
2416 lnet_net_unlock(LNET_LOCK_EX);
2418 /* if the network is not unique then we don't want to keep
2419 * it around after we're done. Free it. Otherwise add that
2420 * net to the global the_lnet.ln_nets */
2421 if (net_l != net && net_l != NULL) {
2423 * TODO - note. currently the tunables can not be updated
2429 * restore tunables after it has been overwitten by the
2432 if (peer_timeout != -1)
2433 net->net_tunables.lct_peer_timeout = peer_timeout;
2434 if (maxtxcredits != -1)
2435 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2436 if (peerrtrcredits != -1)
2437 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2439 lnet_net_lock(LNET_LOCK_EX);
2440 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2441 lnet_net_unlock(LNET_LOCK_EX);
2444 /* update net count */
2445 lnet_current_net_count = lnet_get_net_count();
2451 * shutdown the new NIs that are being started up
2452 * free the NET being started
2454 while (!list_empty(&local_ni_list)) {
2455 ni = list_entry(local_ni_list.next, struct lnet_ni,
2458 lnet_shutdown_lndni(ni);
2468 lnet_startup_lndnets(struct list_head *netlist)
2470 struct lnet_net *net;
2475 * Change to running state before bringing up the LNDs. This
2476 * allows lnet_shutdown_lndnets() to assert that we've passed
2479 lnet_net_lock(LNET_LOCK_EX);
2480 the_lnet.ln_state = LNET_STATE_RUNNING;
2481 lnet_net_unlock(LNET_LOCK_EX);
2483 while (!list_empty(netlist)) {
2484 net = list_entry(netlist->next, struct lnet_net, net_list);
2485 list_del_init(&net->net_list);
2487 rc = lnet_startup_lndnet(net, NULL);
2497 lnet_shutdown_lndnets();
2503 * Initialize LNet library.
2505 * Automatically called at module loading time. Caller has to call
2506 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2507 * latter returned 0. It must be called exactly once.
2509 * \retval 0 on success
2510 * \retval -ve on failures.
2512 int lnet_lib_init(void)
2516 lnet_assert_wire_constants();
2518 /* refer to global cfs_cpt_table for now */
2519 the_lnet.ln_cpt_table = cfs_cpt_tab;
2520 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_tab);
2522 LASSERT(the_lnet.ln_cpt_number > 0);
2523 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2524 /* we are under risk of consuming all lh_cookie */
2525 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2526 "please change setting of CPT-table and retry\n",
2527 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2531 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2532 the_lnet.ln_cpt_bits++;
2534 rc = lnet_create_locks();
2536 CERROR("Can't create LNet global locks: %d\n", rc);
2540 the_lnet.ln_refcount = 0;
2541 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2542 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2544 /* The hash table size is the number of bits it takes to express the set
2545 * ln_num_routes, minus 1 (better to under estimate than over so we
2546 * don't waste memory). */
2547 if (rnet_htable_size <= 0)
2548 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2549 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2550 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2551 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2552 order_base_2(rnet_htable_size) - 1);
2554 /* All LNDs apart from the LOLND are in separate modules. They
2555 * register themselves when their module loads, and unregister
2556 * themselves when their module is unloaded. */
2557 lnet_register_lnd(&the_lolnd);
2562 * Finalize LNet library.
2564 * \pre lnet_lib_init() called with success.
2565 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2567 * As this happens at module-unload, all lnds must already be unloaded,
2568 * so they must already be unregistered.
2570 void lnet_lib_exit(void)
2574 LASSERT(the_lnet.ln_refcount == 0);
2575 lnet_unregister_lnd(&the_lolnd);
2576 for (i = 0; i < NUM_LNDS; i++)
2577 LASSERT(!the_lnet.ln_lnds[i]);
2578 lnet_destroy_locks();
2582 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2584 * Users must call this function at least once before any other functions.
2585 * For each successful call there must be a corresponding call to
2586 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2589 * The PID used by LNet may be different from the one requested.
2592 * \param requested_pid PID requested by the caller.
2594 * \return >= 0 on success, and < 0 error code on failures.
2597 LNetNIInit(lnet_pid_t requested_pid)
2599 int im_a_router = 0;
2602 struct lnet_ping_buffer *pbuf;
2603 struct lnet_handle_md ping_mdh;
2604 LIST_HEAD(net_head);
2605 struct lnet_net *net;
2607 mutex_lock(&the_lnet.ln_api_mutex);
2609 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2611 if (the_lnet.ln_refcount > 0) {
2612 rc = the_lnet.ln_refcount++;
2613 mutex_unlock(&the_lnet.ln_api_mutex);
2617 rc = lnet_prepare(requested_pid);
2619 mutex_unlock(&the_lnet.ln_api_mutex);
2623 /* create a network for Loopback network */
2624 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2627 goto err_empty_list;
2630 /* Add in the loopback NI */
2631 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2633 goto err_empty_list;
2636 /* If LNet is being initialized via DLC it is possible
2637 * that the user requests not to load module parameters (ones which
2638 * are supported by DLC) on initialization. Therefore, make sure not
2639 * to load networks, routes and forwarding from module parameters
2640 * in this case. On cleanup in case of failure only clean up
2641 * routes if it has been loaded */
2642 if (!the_lnet.ln_nis_from_mod_params) {
2643 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2646 goto err_empty_list;
2649 ni_count = lnet_startup_lndnets(&net_head);
2652 goto err_empty_list;
2655 if (!the_lnet.ln_nis_from_mod_params) {
2656 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2658 goto err_shutdown_lndnis;
2660 rc = lnet_rtrpools_alloc(im_a_router);
2662 goto err_destroy_routes;
2665 rc = lnet_acceptor_start();
2667 goto err_destroy_routes;
2669 the_lnet.ln_refcount = 1;
2670 /* Now I may use my own API functions... */
2672 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2674 goto err_acceptor_stop;
2676 lnet_ping_target_update(pbuf, ping_mdh);
2678 the_lnet.ln_mt_handler = lnet_mt_event_handler;
2680 rc = lnet_push_target_init();
2684 rc = lnet_peer_discovery_start();
2686 goto err_destroy_push_target;
2688 rc = lnet_monitor_thr_start();
2690 goto err_stop_discovery_thr;
2693 lnet_router_debugfs_init();
2695 mutex_unlock(&the_lnet.ln_api_mutex);
2697 complete_all(&the_lnet.ln_started);
2699 /* wait for all routers to start */
2700 lnet_wait_router_start();
2704 err_stop_discovery_thr:
2705 lnet_peer_discovery_stop();
2706 err_destroy_push_target:
2707 lnet_push_target_fini();
2709 lnet_ping_target_fini();
2711 the_lnet.ln_refcount = 0;
2712 lnet_acceptor_stop();
2714 if (!the_lnet.ln_nis_from_mod_params)
2715 lnet_destroy_routes();
2716 err_shutdown_lndnis:
2717 lnet_shutdown_lndnets();
2721 mutex_unlock(&the_lnet.ln_api_mutex);
2722 while (!list_empty(&net_head)) {
2723 struct lnet_net *net;
2725 net = list_entry(net_head.next, struct lnet_net, net_list);
2726 list_del_init(&net->net_list);
2731 EXPORT_SYMBOL(LNetNIInit);
2734 * Stop LNet interfaces, routing, and forwarding.
2736 * Users must call this function once for each successful call to LNetNIInit().
2737 * Once the LNetNIFini() operation has been started, the results of pending
2738 * API operations are undefined.
2740 * \return always 0 for current implementation.
2745 mutex_lock(&the_lnet.ln_api_mutex);
2747 LASSERT(the_lnet.ln_refcount > 0);
2749 if (the_lnet.ln_refcount != 1) {
2750 the_lnet.ln_refcount--;
2752 LASSERT(!the_lnet.ln_niinit_self);
2756 lnet_router_debugfs_fini();
2757 lnet_monitor_thr_stop();
2758 lnet_peer_discovery_stop();
2759 lnet_push_target_fini();
2760 lnet_ping_target_fini();
2762 /* Teardown fns that use my own API functions BEFORE here */
2763 the_lnet.ln_refcount = 0;
2765 lnet_acceptor_stop();
2766 lnet_destroy_routes();
2767 lnet_shutdown_lndnets();
2771 mutex_unlock(&the_lnet.ln_api_mutex);
2774 EXPORT_SYMBOL(LNetNIFini);
2777 * Grabs the ni data from the ni structure and fills the out
2780 * \param[in] ni network interface structure
2781 * \param[out] cfg_ni NI config information
2782 * \param[out] tun network and LND tunables
2785 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2786 struct lnet_ioctl_config_lnd_tunables *tun,
2787 struct lnet_ioctl_element_stats *stats,
2790 size_t min_size = 0;
2793 if (!ni || !cfg_ni || !tun)
2796 if (ni->ni_interfaces[0] != NULL) {
2797 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2798 if (ni->ni_interfaces[i] != NULL) {
2799 strncpy(cfg_ni->lic_ni_intf[i],
2800 ni->ni_interfaces[i],
2801 sizeof(cfg_ni->lic_ni_intf[i]));
2806 cfg_ni->lic_nid = ni->ni_nid;
2807 if (ni->ni_nid == LNET_NID_LO_0)
2808 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2810 cfg_ni->lic_status = ni->ni_status->ns_status;
2811 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2812 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2814 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2817 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2818 LNET_STATS_TYPE_SEND);
2819 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2820 LNET_STATS_TYPE_RECV);
2821 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2822 LNET_STATS_TYPE_DROP);
2826 * tun->lt_tun will always be present, but in order to be
2827 * backwards compatible, we need to deal with the cases when
2828 * tun->lt_tun is smaller than what the kernel has, because it
2829 * comes from an older version of a userspace program, then we'll
2830 * need to copy as much information as we have available space.
2832 min_size = tun_size - sizeof(tun->lt_cmn);
2833 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2835 /* copy over the cpts */
2836 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2837 ni->ni_cpts == NULL) {
2838 for (i = 0; i < ni->ni_ncpts; i++)
2839 cfg_ni->lic_cpts[i] = i;
2842 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2843 i < LNET_MAX_SHOW_NUM_CPT;
2845 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2847 cfg_ni->lic_ncpts = ni->ni_ncpts;
2851 * NOTE: This is a legacy function left in the code to be backwards
2852 * compatible with older userspace programs. It should eventually be
2855 * Grabs the ni data from the ni structure and fills the out
2858 * \param[in] ni network interface structure
2859 * \param[out] config config information
2862 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2863 struct lnet_ioctl_config_data *config)
2865 struct lnet_ioctl_net_config *net_config;
2866 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2867 size_t min_size, tunable_size = 0;
2873 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2877 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2878 ARRAY_SIZE(net_config->ni_interfaces));
2880 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2881 if (!ni->ni_interfaces[i])
2884 strncpy(net_config->ni_interfaces[i],
2885 ni->ni_interfaces[i],
2886 sizeof(net_config->ni_interfaces[i]));
2889 config->cfg_nid = ni->ni_nid;
2890 config->cfg_config_u.cfg_net.net_peer_timeout =
2891 ni->ni_net->net_tunables.lct_peer_timeout;
2892 config->cfg_config_u.cfg_net.net_max_tx_credits =
2893 ni->ni_net->net_tunables.lct_max_tx_credits;
2894 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2895 ni->ni_net->net_tunables.lct_peer_tx_credits;
2896 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2897 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2899 if (ni->ni_nid == LNET_NID_LO_0)
2900 net_config->ni_status = LNET_NI_STATUS_UP;
2902 net_config->ni_status = ni->ni_status->ns_status;
2905 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2907 for (i = 0; i < num_cpts; i++)
2908 net_config->ni_cpts[i] = ni->ni_cpts[i];
2910 config->cfg_ncpts = num_cpts;
2914 * See if user land tools sent in a newer and larger version
2915 * of struct lnet_tunables than what the kernel uses.
2917 min_size = sizeof(*config) + sizeof(*net_config);
2919 if (config->cfg_hdr.ioc_len > min_size)
2920 tunable_size = config->cfg_hdr.ioc_len - min_size;
2922 /* Don't copy too much data to user space */
2923 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2924 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2926 if (lnd_cfg && min_size) {
2927 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2928 config->cfg_config_u.cfg_net.net_interface_count = 1;
2930 /* Tell user land that kernel side has less data */
2931 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2932 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2933 config->cfg_hdr.ioc_len -= min_size;
2939 lnet_get_ni_idx_locked(int idx)
2942 struct lnet_net *net;
2944 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2945 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2955 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2958 struct lnet_net *net = mynet;
2961 * It is possible that the net has been cleaned out while there is
2962 * a message being sent. This function accessed the net without
2963 * checking if the list is empty
2967 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2969 if (list_empty(&net->net_ni_list))
2971 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2977 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2978 /* if you reached the end of the ni list and the net is
2979 * specified, then there are no more nis in that net */
2983 /* we reached the end of this net ni list. move to the
2985 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2986 /* no more nets and no more NIs. */
2989 /* get the next net */
2990 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2992 if (list_empty(&net->net_ni_list))
2994 /* get the ni on it */
2995 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3001 if (list_empty(&prev->ni_netlist))
3004 /* there are more nis left */
3005 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3011 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3016 int idx = config->cfg_count;
3018 cpt = lnet_net_lock_current();
3020 ni = lnet_get_ni_idx_locked(idx);
3025 lnet_fill_ni_info_legacy(ni, config);
3029 lnet_net_unlock(cpt);
3034 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3035 struct lnet_ioctl_config_lnd_tunables *tun,
3036 struct lnet_ioctl_element_stats *stats,
3043 if (!cfg_ni || !tun || !stats)
3046 cpt = lnet_net_lock_current();
3048 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3053 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3057 lnet_net_unlock(cpt);
3061 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3070 cpt = lnet_net_lock_current();
3072 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3075 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3079 lnet_net_unlock(cpt);
3084 static int lnet_add_net_common(struct lnet_net *net,
3085 struct lnet_ioctl_config_lnd_tunables *tun)
3088 struct lnet_ping_buffer *pbuf;
3089 struct lnet_handle_md ping_mdh;
3091 struct lnet_remotenet *rnet;
3094 lnet_net_lock(LNET_LOCK_EX);
3095 rnet = lnet_find_rnet_locked(net->net_id);
3096 lnet_net_unlock(LNET_LOCK_EX);
3098 * make sure that the net added doesn't invalidate the current
3099 * configuration LNet is keeping
3102 CERROR("Adding net %s will invalidate routing configuration\n",
3103 libcfs_net2str(net->net_id));
3109 * make sure you calculate the correct number of slots in the ping
3110 * buffer. Since the ping info is a flattened list of all the NIs,
3111 * we should allocate enough slots to accomodate the number of NIs
3112 * which will be added.
3114 * since ni hasn't been configured yet, use
3115 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3117 net_ni_count = lnet_get_net_ni_count_pre(net);
3119 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3120 net_ni_count + lnet_get_ni_count(),
3128 memcpy(&net->net_tunables,
3129 &tun->lt_cmn, sizeof(net->net_tunables));
3131 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3133 net_id = net->net_id;
3135 rc = lnet_startup_lndnet(net,
3136 (tun) ? &tun->lt_tun : NULL);
3140 lnet_net_lock(LNET_LOCK_EX);
3141 net = lnet_get_net_locked(net_id);
3142 lnet_net_unlock(LNET_LOCK_EX);
3147 * Start the acceptor thread if this is the first network
3148 * being added that requires the thread.
3150 if (net->net_lnd->lnd_accept) {
3151 rc = lnet_acceptor_start();
3153 /* shutdown the net that we just started */
3154 CERROR("Failed to start up acceptor thread\n");
3155 lnet_shutdown_lndnet(net);
3160 lnet_net_lock(LNET_LOCK_EX);
3161 lnet_peer_net_added(net);
3162 lnet_net_unlock(LNET_LOCK_EX);
3164 lnet_ping_target_update(pbuf, ping_mdh);
3169 lnet_ping_md_unlink(pbuf, &ping_mdh);
3170 lnet_ping_buffer_decref(pbuf);
3174 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3175 struct lnet_ioctl_config_lnd_tunables *tun)
3177 struct lnet_net *net;
3180 LIST_HEAD(net_head);
3182 rc = lnet_parse_ip2nets(&nets, ip2nets);
3186 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3190 mutex_lock(&the_lnet.ln_api_mutex);
3191 while (!list_empty(&net_head)) {
3192 net = list_entry(net_head.next, struct lnet_net, net_list);
3193 list_del_init(&net->net_list);
3194 rc = lnet_add_net_common(net, tun);
3200 mutex_unlock(&the_lnet.ln_api_mutex);
3202 while (!list_empty(&net_head)) {
3203 net = list_entry(net_head.next, struct lnet_net, net_list);
3204 list_del_init(&net->net_list);
3210 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3212 struct lnet_net *net;
3214 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3216 __u32 net_id, lnd_type;
3218 /* get the tunables if they are available */
3219 if (conf->lic_cfg_hdr.ioc_len >=
3220 sizeof(*conf) + sizeof(*tun))
3221 tun = (struct lnet_ioctl_config_lnd_tunables *)
3224 /* handle legacy ip2nets from DLC */
3225 if (conf->lic_legacy_ip2nets[0] != '\0')
3226 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3229 net_id = LNET_NIDNET(conf->lic_nid);
3230 lnd_type = LNET_NETTYP(net_id);
3232 if (!libcfs_isknown_lnd(lnd_type)) {
3233 CERROR("No valid net and lnd information provided\n");
3237 net = lnet_net_alloc(net_id, NULL);
3241 for (i = 0; i < conf->lic_ncpts; i++) {
3242 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3246 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3247 conf->lic_ni_intf[0]);
3251 mutex_lock(&the_lnet.ln_api_mutex);
3253 rc = lnet_add_net_common(net, tun);
3255 mutex_unlock(&the_lnet.ln_api_mutex);
3260 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3262 struct lnet_net *net;
3264 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3265 struct lnet_ping_buffer *pbuf;
3266 struct lnet_handle_md ping_mdh;
3271 /* don't allow userspace to shutdown the LOLND */
3272 if (LNET_NETTYP(net_id) == LOLND)
3275 mutex_lock(&the_lnet.ln_api_mutex);
3279 net = lnet_get_net_locked(net_id);
3281 CERROR("net %s not found\n",
3282 libcfs_net2str(net_id));
3287 addr = LNET_NIDADDR(conf->lic_nid);
3289 /* remove the entire net */
3290 net_count = lnet_get_net_ni_count_locked(net);
3294 /* create and link a new ping info, before removing the old one */
3295 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3296 lnet_get_ni_count() - net_count,
3299 goto unlock_api_mutex;
3301 lnet_shutdown_lndnet(net);
3303 lnet_acceptor_stop();
3305 lnet_ping_target_update(pbuf, ping_mdh);
3307 goto unlock_api_mutex;
3310 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3312 CERROR("nid %s not found\n",
3313 libcfs_nid2str(conf->lic_nid));
3318 net_count = lnet_get_net_ni_count_locked(net);
3322 /* create and link a new ping info, before removing the old one */
3323 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3324 lnet_get_ni_count() - 1, false);
3326 goto unlock_api_mutex;
3328 lnet_shutdown_lndni(ni);
3330 lnet_acceptor_stop();
3332 lnet_ping_target_update(pbuf, ping_mdh);
3334 /* check if the net is empty and remove it if it is */
3336 lnet_shutdown_lndnet(net);
3338 goto unlock_api_mutex;
3343 mutex_unlock(&the_lnet.ln_api_mutex);
3349 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3350 * They are only expected to be called for unique networks.
3351 * That can be as a result of older DLC library
3352 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3355 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3357 struct lnet_net *net;
3358 LIST_HEAD(net_head);
3360 struct lnet_ioctl_config_lnd_tunables tun;
3361 const char *nets = conf->cfg_config_u.cfg_net.net_intf;
3363 /* Create a net/ni structures for the network string */
3364 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3366 return rc == 0 ? -EINVAL : rc;
3368 mutex_lock(&the_lnet.ln_api_mutex);
3371 rc = -EINVAL; /* only add one network per call */
3372 goto out_unlock_clean;
3375 net = list_entry(net_head.next, struct lnet_net, net_list);
3376 list_del_init(&net->net_list);
3378 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3380 memset(&tun, 0, sizeof(tun));
3382 tun.lt_cmn.lct_peer_timeout =
3383 conf->cfg_config_u.cfg_net.net_peer_timeout;
3384 tun.lt_cmn.lct_peer_tx_credits =
3385 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3386 tun.lt_cmn.lct_peer_rtr_credits =
3387 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3388 tun.lt_cmn.lct_max_tx_credits =
3389 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3391 rc = lnet_add_net_common(net, &tun);
3394 mutex_unlock(&the_lnet.ln_api_mutex);
3395 while (!list_empty(&net_head)) {
3396 /* net_head list is empty in success case */
3397 net = list_entry(net_head.next, struct lnet_net, net_list);
3398 list_del_init(&net->net_list);
3405 lnet_dyn_del_net(__u32 net_id)
3407 struct lnet_net *net;
3408 struct lnet_ping_buffer *pbuf;
3409 struct lnet_handle_md ping_mdh;
3413 /* don't allow userspace to shutdown the LOLND */
3414 if (LNET_NETTYP(net_id) == LOLND)
3417 mutex_lock(&the_lnet.ln_api_mutex);
3421 net = lnet_get_net_locked(net_id);
3428 net_ni_count = lnet_get_net_ni_count_locked(net);
3432 /* create and link a new ping info, before removing the old one */
3433 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3434 lnet_get_ni_count() - net_ni_count, false);
3438 lnet_shutdown_lndnet(net);
3440 lnet_acceptor_stop();
3442 lnet_ping_target_update(pbuf, ping_mdh);
3445 mutex_unlock(&the_lnet.ln_api_mutex);
3450 void lnet_incr_dlc_seq(void)
3452 atomic_inc(&lnet_dlc_seq_no);
3455 __u32 lnet_get_dlc_seq_locked(void)
3457 return atomic_read(&lnet_dlc_seq_no);
3461 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3463 struct lnet_net *net;
3466 lnet_net_lock(LNET_LOCK_EX);
3467 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3468 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3469 if (ni->ni_nid == nid || all) {
3470 atomic_set(&ni->ni_healthv, value);
3471 if (list_empty(&ni->ni_recovery) &&
3472 value < LNET_MAX_HEALTH_VALUE) {
3473 CERROR("manually adding local NI %s to recovery\n",
3474 libcfs_nid2str(ni->ni_nid));
3475 list_add_tail(&ni->ni_recovery,
3476 &the_lnet.ln_mt_localNIRecovq);
3477 lnet_ni_addref_locked(ni, 0);
3480 lnet_net_unlock(LNET_LOCK_EX);
3486 lnet_net_unlock(LNET_LOCK_EX);
3490 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3494 lnet_nid_t nid = stats->hlni_nid;
3496 cpt = lnet_net_lock_current();
3497 ni = lnet_nid2ni_locked(nid, cpt);
3504 stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3505 stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3506 stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3507 stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3508 stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3509 stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3510 stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3513 lnet_net_unlock(cpt);
3519 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3524 lnet_net_lock(LNET_LOCK_EX);
3525 list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3526 list->rlst_nid_array[i] = ni->ni_nid;
3528 if (i >= LNET_MAX_SHOW_NUM_NID)
3531 lnet_net_unlock(LNET_LOCK_EX);
3532 list->rlst_num_nids = i;
3538 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3540 struct lnet_peer_ni *lpni;
3543 lnet_net_lock(LNET_LOCK_EX);
3544 list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3545 list->rlst_nid_array[i] = lpni->lpni_nid;
3547 if (i >= LNET_MAX_SHOW_NUM_NID)
3550 lnet_net_unlock(LNET_LOCK_EX);
3551 list->rlst_num_nids = i;
3557 * LNet ioctl handler.
3561 LNetCtl(unsigned int cmd, void *arg)
3563 struct libcfs_ioctl_data *data = arg;
3564 struct lnet_ioctl_config_data *config;
3565 struct lnet_process_id id = {0};
3569 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3570 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3573 case IOC_LIBCFS_GET_NI:
3574 rc = LNetGetId(data->ioc_count, &id);
3575 data->ioc_nid = id.nid;
3578 case IOC_LIBCFS_FAIL_NID:
3579 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3581 case IOC_LIBCFS_ADD_ROUTE: {
3582 /* default router sensitivity to 1 */
3583 unsigned int sensitivity = 1;
3586 if (config->cfg_hdr.ioc_len < sizeof(*config))
3589 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3591 config->cfg_config_u.cfg_route.rtr_sensitivity;
3594 mutex_lock(&the_lnet.ln_api_mutex);
3595 rc = lnet_add_route(config->cfg_net,
3596 config->cfg_config_u.cfg_route.rtr_hop,
3598 config->cfg_config_u.cfg_route.
3599 rtr_priority, sensitivity);
3600 mutex_unlock(&the_lnet.ln_api_mutex);
3604 case IOC_LIBCFS_DEL_ROUTE:
3607 if (config->cfg_hdr.ioc_len < sizeof(*config))
3610 mutex_lock(&the_lnet.ln_api_mutex);
3611 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3612 mutex_unlock(&the_lnet.ln_api_mutex);
3615 case IOC_LIBCFS_GET_ROUTE:
3618 if (config->cfg_hdr.ioc_len < sizeof(*config))
3621 mutex_lock(&the_lnet.ln_api_mutex);
3622 rc = lnet_get_route(config->cfg_count,
3624 &config->cfg_config_u.cfg_route.rtr_hop,
3626 &config->cfg_config_u.cfg_route.rtr_flags,
3627 &config->cfg_config_u.cfg_route.
3629 &config->cfg_config_u.cfg_route.
3631 mutex_unlock(&the_lnet.ln_api_mutex);
3634 case IOC_LIBCFS_GET_LOCAL_NI: {
3635 struct lnet_ioctl_config_ni *cfg_ni;
3636 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3637 struct lnet_ioctl_element_stats *stats;
3642 /* get the tunables if they are available */
3643 if (cfg_ni->lic_cfg_hdr.ioc_len <
3644 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3647 stats = (struct lnet_ioctl_element_stats *)
3649 tun = (struct lnet_ioctl_config_lnd_tunables *)
3650 (cfg_ni->lic_bulk + sizeof(*stats));
3652 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3655 mutex_lock(&the_lnet.ln_api_mutex);
3656 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3657 mutex_unlock(&the_lnet.ln_api_mutex);
3661 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3662 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3664 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3667 mutex_lock(&the_lnet.ln_api_mutex);
3668 rc = lnet_get_ni_stats(msg_stats);
3669 mutex_unlock(&the_lnet.ln_api_mutex);
3674 case IOC_LIBCFS_GET_NET: {
3675 size_t total = sizeof(*config) +
3676 sizeof(struct lnet_ioctl_net_config);
3679 if (config->cfg_hdr.ioc_len < total)
3682 mutex_lock(&the_lnet.ln_api_mutex);
3683 rc = lnet_get_net_config(config);
3684 mutex_unlock(&the_lnet.ln_api_mutex);
3688 case IOC_LIBCFS_GET_LNET_STATS:
3690 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3692 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3695 mutex_lock(&the_lnet.ln_api_mutex);
3696 lnet_counters_get(&lnet_stats->st_cntrs);
3697 mutex_unlock(&the_lnet.ln_api_mutex);
3701 case IOC_LIBCFS_CONFIG_RTR:
3704 if (config->cfg_hdr.ioc_len < sizeof(*config))
3707 mutex_lock(&the_lnet.ln_api_mutex);
3708 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3709 rc = lnet_rtrpools_enable();
3710 mutex_unlock(&the_lnet.ln_api_mutex);
3713 lnet_rtrpools_disable();
3714 mutex_unlock(&the_lnet.ln_api_mutex);
3717 case IOC_LIBCFS_ADD_BUF:
3720 if (config->cfg_hdr.ioc_len < sizeof(*config))
3723 mutex_lock(&the_lnet.ln_api_mutex);
3724 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3726 config->cfg_config_u.cfg_buffers.
3728 config->cfg_config_u.cfg_buffers.
3730 mutex_unlock(&the_lnet.ln_api_mutex);
3733 case IOC_LIBCFS_SET_NUMA_RANGE: {
3734 struct lnet_ioctl_set_value *numa;
3736 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3738 lnet_net_lock(LNET_LOCK_EX);
3739 lnet_numa_range = numa->sv_value;
3740 lnet_net_unlock(LNET_LOCK_EX);
3744 case IOC_LIBCFS_GET_NUMA_RANGE: {
3745 struct lnet_ioctl_set_value *numa;
3747 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3749 numa->sv_value = lnet_numa_range;
3753 case IOC_LIBCFS_GET_BUF: {
3754 struct lnet_ioctl_pool_cfg *pool_cfg;
3755 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3759 if (config->cfg_hdr.ioc_len < total)
3762 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3764 mutex_lock(&the_lnet.ln_api_mutex);
3765 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3766 mutex_unlock(&the_lnet.ln_api_mutex);
3770 case IOC_LIBCFS_GET_LOCAL_HSTATS: {
3771 struct lnet_ioctl_local_ni_hstats *stats = arg;
3773 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
3776 mutex_lock(&the_lnet.ln_api_mutex);
3777 rc = lnet_get_local_ni_hstats(stats);
3778 mutex_unlock(&the_lnet.ln_api_mutex);
3783 case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
3784 struct lnet_ioctl_recovery_list *list = arg;
3785 if (list->rlst_hdr.ioc_len < sizeof(*list))
3788 mutex_lock(&the_lnet.ln_api_mutex);
3789 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
3790 rc = lnet_get_local_ni_recovery_list(list);
3792 rc = lnet_get_peer_ni_recovery_list(list);
3793 mutex_unlock(&the_lnet.ln_api_mutex);
3797 case IOC_LIBCFS_ADD_PEER_NI: {
3798 struct lnet_ioctl_peer_cfg *cfg = arg;
3800 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3803 mutex_lock(&the_lnet.ln_api_mutex);
3804 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3807 mutex_unlock(&the_lnet.ln_api_mutex);
3811 case IOC_LIBCFS_DEL_PEER_NI: {
3812 struct lnet_ioctl_peer_cfg *cfg = arg;
3814 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3817 mutex_lock(&the_lnet.ln_api_mutex);
3818 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3819 cfg->prcfg_cfg_nid);
3820 mutex_unlock(&the_lnet.ln_api_mutex);
3824 case IOC_LIBCFS_GET_PEER_INFO: {
3825 struct lnet_ioctl_peer *peer_info = arg;
3827 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3830 mutex_lock(&the_lnet.ln_api_mutex);
3831 rc = lnet_get_peer_ni_info(
3832 peer_info->pr_count,
3834 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3835 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3836 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3837 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3838 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3839 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3840 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3841 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3842 mutex_unlock(&the_lnet.ln_api_mutex);
3846 case IOC_LIBCFS_GET_PEER_NI: {
3847 struct lnet_ioctl_peer_cfg *cfg = arg;
3849 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3852 mutex_lock(&the_lnet.ln_api_mutex);
3853 rc = lnet_get_peer_info(cfg,
3854 (void __user *)cfg->prcfg_bulk);
3855 mutex_unlock(&the_lnet.ln_api_mutex);
3859 case IOC_LIBCFS_GET_PEER_LIST: {
3860 struct lnet_ioctl_peer_cfg *cfg = arg;
3862 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3865 mutex_lock(&the_lnet.ln_api_mutex);
3866 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3867 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3868 mutex_unlock(&the_lnet.ln_api_mutex);
3872 case IOC_LIBCFS_SET_HEALHV: {
3873 struct lnet_ioctl_reset_health_cfg *cfg = arg;
3875 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
3877 if (cfg->rh_value < 0 ||
3878 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
3879 value = LNET_MAX_HEALTH_VALUE;
3881 value = cfg->rh_value;
3882 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
3883 value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
3884 "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
3885 mutex_lock(&the_lnet.ln_api_mutex);
3886 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
3887 lnet_ni_set_healthv(cfg->rh_nid, value,
3890 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
3892 mutex_unlock(&the_lnet.ln_api_mutex);
3896 case IOC_LIBCFS_NOTIFY_ROUTER: {
3897 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3899 /* The deadline passed in by the user should be some time in
3900 * seconds in the future since the UNIX epoch. We have to map
3901 * that deadline to the wall clock.
3903 deadline += ktime_get_seconds();
3904 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
3908 case IOC_LIBCFS_LNET_DIST:
3909 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3910 if (rc < 0 && rc != -EHOSTUNREACH)
3913 data->ioc_u32[0] = rc;
3916 case IOC_LIBCFS_TESTPROTOCOMPAT:
3917 the_lnet.ln_testprotocompat = data->ioc_flags;
3920 case IOC_LIBCFS_LNET_FAULT:
3921 return lnet_fault_ctl(data->ioc_flags, data);
3923 case IOC_LIBCFS_PING: {
3924 signed long timeout;
3926 id.nid = data->ioc_nid;
3927 id.pid = data->ioc_u32[0];
3929 /* If timeout is negative then set default of 3 minutes */
3930 if (((s32)data->ioc_u32[1] <= 0) ||
3931 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3932 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3934 timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
3936 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3937 data->ioc_plen1 / sizeof(struct lnet_process_id));
3942 data->ioc_count = rc;
3946 case IOC_LIBCFS_PING_PEER: {
3947 struct lnet_ioctl_ping_data *ping = arg;
3948 struct lnet_peer *lp;
3949 signed long timeout;
3951 /* If timeout is negative then set default of 3 minutes */
3952 if (((s32)ping->op_param) <= 0 ||
3953 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3954 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3956 timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
3958 rc = lnet_ping(ping->ping_id, timeout,
3964 mutex_lock(&the_lnet.ln_api_mutex);
3965 lp = lnet_find_peer(ping->ping_id.nid);
3967 ping->ping_id.nid = lp->lp_primary_nid;
3968 ping->mr_info = lnet_peer_is_multi_rail(lp);
3969 lnet_peer_decref_locked(lp);
3971 mutex_unlock(&the_lnet.ln_api_mutex);
3973 ping->ping_count = rc;
3977 case IOC_LIBCFS_DISCOVER: {
3978 struct lnet_ioctl_ping_data *discover = arg;
3979 struct lnet_peer *lp;
3981 rc = lnet_discover(discover->ping_id, discover->op_param,
3983 discover->ping_count);
3987 mutex_lock(&the_lnet.ln_api_mutex);
3988 lp = lnet_find_peer(discover->ping_id.nid);
3990 discover->ping_id.nid = lp->lp_primary_nid;
3991 discover->mr_info = lnet_peer_is_multi_rail(lp);
3992 lnet_peer_decref_locked(lp);
3994 mutex_unlock(&the_lnet.ln_api_mutex);
3996 discover->ping_count = rc;
4001 ni = lnet_net2ni_addref(data->ioc_net);
4005 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
4008 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4015 EXPORT_SYMBOL(LNetCtl);
4017 void LNetDebugPeer(struct lnet_process_id id)
4019 lnet_debug_peer(id.nid);
4021 EXPORT_SYMBOL(LNetDebugPeer);
4024 * Determine if the specified peer \a nid is on the local node.
4026 * \param nid peer nid to check
4028 * \retval true If peer NID is on the local node.
4029 * \retval false If peer NID is not on the local node.
4031 bool LNetIsPeerLocal(lnet_nid_t nid)
4033 struct lnet_net *net;
4037 cpt = lnet_net_lock_current();
4038 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4039 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4040 if (ni->ni_nid == nid) {
4041 lnet_net_unlock(cpt);
4046 lnet_net_unlock(cpt);
4050 EXPORT_SYMBOL(LNetIsPeerLocal);
4053 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4054 * Note that all interfaces share a same PID, as requested by LNetNIInit().
4056 * \param index Index of the interface to look up.
4057 * \param id On successful return, this location will hold the
4058 * struct lnet_process_id ID of the interface.
4060 * \retval 0 If an interface exists at \a index.
4061 * \retval -ENOENT If no interface has been found.
4064 LNetGetId(unsigned int index, struct lnet_process_id *id)
4067 struct lnet_net *net;
4071 LASSERT(the_lnet.ln_refcount > 0);
4073 cpt = lnet_net_lock_current();
4075 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4076 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4080 id->nid = ni->ni_nid;
4081 id->pid = the_lnet.ln_pid;
4087 lnet_net_unlock(cpt);
4090 EXPORT_SYMBOL(LNetGetId);
4095 struct lnet_handle_md mdh;
4096 struct completion completion;
4100 lnet_ping_event_handler(struct lnet_event *event)
4102 struct ping_data *pd = event->md_user_ptr;
4104 CDEBUG(D_NET, "ping event (%d %d)%s\n",
4105 event->type, event->status,
4106 event->unlinked ? " unlinked" : "");
4108 if (event->status) {
4110 pd->rc = event->status;
4111 } else if (event->type == LNET_EVENT_REPLY) {
4113 pd->rc = event->mlength;
4115 if (event->unlinked)
4116 complete(&pd->completion);
4119 static int lnet_ping(struct lnet_process_id id, signed long timeout,
4120 struct lnet_process_id __user *ids, int n_ids)
4122 struct lnet_md md = { NULL };
4123 struct ping_data pd = { 0 };
4124 struct lnet_ping_buffer *pbuf;
4125 struct lnet_process_id tmpid;
4131 /* n_ids limit is arbitrary */
4132 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4136 * if the user buffer has more space than the lnet_interfaces_max
4137 * then only fill it up to lnet_interfaces_max
4139 if (n_ids > lnet_interfaces_max)
4140 n_ids = lnet_interfaces_max;
4142 if (id.pid == LNET_PID_ANY)
4143 id.pid = LNET_PID_LUSTRE;
4145 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4149 /* initialize md content */
4150 md.start = &pbuf->pb_info;
4151 md.length = LNET_PING_INFO_SIZE(n_ids);
4152 md.threshold = 2; /* GET/REPLY */
4154 md.options = LNET_MD_TRUNCATE;
4156 md.handler = lnet_ping_event_handler;
4158 init_completion(&pd.completion);
4160 rc = LNetMDBind(&md, LNET_UNLINK, &pd.mdh);
4162 CERROR("Can't bind MD: %d\n", rc);
4163 goto fail_ping_buffer_decref;
4166 rc = LNetGet(LNET_NID_ANY, pd.mdh, id,
4167 LNET_RESERVED_PORTAL,
4168 LNET_PROTO_PING_MATCHBITS, 0, false);
4171 /* Don't CERROR; this could be deliberate! */
4172 rc2 = LNetMDUnlink(pd.mdh);
4175 /* NB must wait for the UNLINK event below... */
4178 if (wait_for_completion_timeout(&pd.completion, timeout) == 0) {
4179 /* Ensure completion in finite time... */
4180 LNetMDUnlink(pd.mdh);
4181 wait_for_completion(&pd.completion);
4185 goto fail_ping_buffer_decref;
4189 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4191 rc = -EPROTO; /* if I can't parse... */
4194 CERROR("%s: ping info too short %d\n",
4195 libcfs_id2str(id), nob);
4196 goto fail_ping_buffer_decref;
4199 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4200 lnet_swap_pinginfo(pbuf);
4201 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4202 CERROR("%s: Unexpected magic %08x\n",
4203 libcfs_id2str(id), pbuf->pb_info.pi_magic);
4204 goto fail_ping_buffer_decref;
4207 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4208 CERROR("%s: ping w/o NI status: 0x%x\n",
4209 libcfs_id2str(id), pbuf->pb_info.pi_features);
4210 goto fail_ping_buffer_decref;
4213 if (nob < LNET_PING_INFO_SIZE(0)) {
4214 CERROR("%s: Short reply %d(%d min)\n",
4216 nob, (int)LNET_PING_INFO_SIZE(0));
4217 goto fail_ping_buffer_decref;
4220 if (pbuf->pb_info.pi_nnis < n_ids)
4221 n_ids = pbuf->pb_info.pi_nnis;
4223 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4224 CERROR("%s: Short reply %d(%d expected)\n",
4226 nob, (int)LNET_PING_INFO_SIZE(n_ids));
4227 goto fail_ping_buffer_decref;
4230 rc = -EFAULT; /* if I segv in copy_to_user()... */
4232 memset(&tmpid, 0, sizeof(tmpid));
4233 for (i = 0; i < n_ids; i++) {
4234 tmpid.pid = pbuf->pb_info.pi_pid;
4235 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4236 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4237 goto fail_ping_buffer_decref;
4239 rc = pbuf->pb_info.pi_nnis;
4241 fail_ping_buffer_decref:
4242 lnet_ping_buffer_decref(pbuf);
4247 lnet_discover(struct lnet_process_id id, __u32 force,
4248 struct lnet_process_id __user *ids, int n_ids)
4250 struct lnet_peer_ni *lpni;
4251 struct lnet_peer_ni *p;
4252 struct lnet_peer *lp;
4253 struct lnet_process_id *buf;
4259 id.nid == LNET_NID_ANY)
4262 if (id.pid == LNET_PID_ANY)
4263 id.pid = LNET_PID_LUSTRE;
4266 * If the user buffer has more space than the lnet_interfaces_max,
4267 * then only fill it up to lnet_interfaces_max.
4269 if (n_ids > lnet_interfaces_max)
4270 n_ids = lnet_interfaces_max;
4272 CFS_ALLOC_PTR_ARRAY(buf, n_ids);
4276 cpt = lnet_net_lock_current();
4277 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4284 * Clearing the NIDS_UPTODATE flag ensures the peer will
4285 * be discovered, provided discovery has not been disabled.
4287 lp = lpni->lpni_peer_net->lpn_peer;
4288 spin_lock(&lp->lp_lock);
4289 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4290 /* If the force flag is set, force a PING and PUSH as well. */
4292 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4293 spin_unlock(&lp->lp_lock);
4294 rc = lnet_discover_peer_locked(lpni, cpt, true);
4300 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4301 buf[i].pid = id.pid;
4302 buf[i].nid = p->lpni_nid;
4309 lnet_peer_ni_decref_locked(lpni);
4311 lnet_net_unlock(cpt);
4314 if (copy_to_user(ids, buf, rc * sizeof(*buf)))
4316 CFS_FREE_PTR_ARRAY(buf, n_ids);
4322 * Retrieve peer discovery status.
4324 * \retval 1 if lnet_peer_discovery_disabled is 0
4325 * \retval 0 if lnet_peer_discovery_disabled is 1
4328 LNetGetPeerDiscoveryStatus(void)
4330 return !lnet_peer_discovery_disabled;
4332 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);