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>
41 #include <lnet/lib-lnet.h>
43 #define D_LNI D_CONSOLE
46 * initialize ln_api_mutex statically, since it needs to be used in
47 * discovery_set callback. That module parameter callback can be called
48 * before module init completes. The mutex needs to be ready for use then.
50 struct lnet the_lnet = {
51 .ln_api_mutex = __MUTEX_INITIALIZER(the_lnet.ln_api_mutex),
52 }; /* THE state of the network */
53 EXPORT_SYMBOL(the_lnet);
55 static char *ip2nets = "";
56 module_param(ip2nets, charp, 0444);
57 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
59 static char *networks = "";
60 module_param(networks, charp, 0444);
61 MODULE_PARM_DESC(networks, "local networks");
63 static char *routes = "";
64 module_param(routes, charp, 0444);
65 MODULE_PARM_DESC(routes, "routes to non-local networks");
67 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
68 module_param(rnet_htable_size, int, 0444);
69 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
71 static int use_tcp_bonding = false;
72 module_param(use_tcp_bonding, int, 0444);
73 MODULE_PARM_DESC(use_tcp_bonding,
74 "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
76 unsigned int lnet_numa_range = 0;
77 module_param(lnet_numa_range, uint, 0444);
78 MODULE_PARM_DESC(lnet_numa_range,
79 "NUMA range to consider during Multi-Rail selection");
82 * lnet_health_sensitivity determines by how much we decrement the health
83 * value on sending error. The value defaults to 100, which means health
84 * interface health is decremented by 100 points every failure.
86 unsigned int lnet_health_sensitivity = 100;
87 static int sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
88 #ifdef HAVE_KERNEL_PARAM_OPS
89 static struct kernel_param_ops param_ops_health_sensitivity = {
90 .set = sensitivity_set,
93 #define param_check_health_sensitivity(name, p) \
94 __param_check(name, p, int)
95 module_param(lnet_health_sensitivity, health_sensitivity, S_IRUGO|S_IWUSR);
97 module_param_call(lnet_health_sensitivity, sensitivity_set, param_get_int,
98 &lnet_health_sensitivity, S_IRUGO|S_IWUSR);
100 MODULE_PARM_DESC(lnet_health_sensitivity,
101 "Value to decrement the health value by on error");
104 * lnet_recovery_interval determines how often we should perform recovery
105 * on unhealthy interfaces.
107 unsigned int lnet_recovery_interval = 1;
108 static int recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp);
109 #ifdef HAVE_KERNEL_PARAM_OPS
110 static struct kernel_param_ops param_ops_recovery_interval = {
111 .set = recovery_interval_set,
112 .get = param_get_int,
114 #define param_check_recovery_interval(name, p) \
115 __param_check(name, p, int)
116 module_param(lnet_recovery_interval, recovery_interval, S_IRUGO|S_IWUSR);
118 module_param_call(lnet_recovery_interval, recovery_interval_set, param_get_int,
119 &lnet_recovery_interval, S_IRUGO|S_IWUSR);
121 MODULE_PARM_DESC(lnet_recovery_interval,
122 "Interval to recover unhealthy interfaces in seconds");
124 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
125 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
127 static struct kernel_param_ops param_ops_interfaces_max = {
129 .get = param_get_int,
132 #define param_check_interfaces_max(name, p) \
133 __param_check(name, p, int)
135 #ifdef HAVE_KERNEL_PARAM_OPS
136 module_param(lnet_interfaces_max, interfaces_max, 0644);
138 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
139 ¶m_ops_interfaces_max, 0644);
141 MODULE_PARM_DESC(lnet_interfaces_max,
142 "Maximum number of interfaces in a node.");
144 unsigned lnet_peer_discovery_disabled = 0;
145 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
147 static struct kernel_param_ops param_ops_discovery_disabled = {
148 .set = discovery_set,
149 .get = param_get_int,
152 #define param_check_discovery_disabled(name, p) \
153 __param_check(name, p, int)
154 #ifdef HAVE_KERNEL_PARAM_OPS
155 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
157 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
158 ¶m_ops_discovery_disabled, 0644);
160 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
161 "Set to 1 to disable peer discovery on this node.");
163 unsigned int lnet_drop_asym_route;
164 static int drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp);
166 static struct kernel_param_ops param_ops_drop_asym_route = {
167 .set = drop_asym_route_set,
168 .get = param_get_int,
171 #define param_check_drop_asym_route(name, p) \
172 __param_check(name, p, int)
173 #ifdef HAVE_KERNEL_PARAM_OPS
174 module_param(lnet_drop_asym_route, drop_asym_route, 0644);
176 module_param_call(lnet_drop_asym_route, drop_asym_route_set, param_get_int,
177 ¶m_ops_drop_asym_route, 0644);
179 MODULE_PARM_DESC(lnet_drop_asym_route,
180 "Set to 1 to drop asymmetrical route messages.");
182 #define LNET_TRANSACTION_TIMEOUT_NO_HEALTH_DEFAULT 50
183 #define LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT 10
185 unsigned lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT;
186 static int transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp);
187 #ifdef HAVE_KERNEL_PARAM_OPS
188 static struct kernel_param_ops param_ops_transaction_timeout = {
189 .set = transaction_to_set,
190 .get = param_get_int,
193 #define param_check_transaction_timeout(name, p) \
194 __param_check(name, p, int)
195 module_param(lnet_transaction_timeout, transaction_timeout, S_IRUGO|S_IWUSR);
197 module_param_call(lnet_transaction_timeout, transaction_to_set, param_get_int,
198 &lnet_transaction_timeout, S_IRUGO|S_IWUSR);
200 MODULE_PARM_DESC(lnet_transaction_timeout,
201 "Maximum number of seconds to wait for a peer response.");
203 #define LNET_RETRY_COUNT_HEALTH_DEFAULT 3
204 unsigned lnet_retry_count = LNET_RETRY_COUNT_HEALTH_DEFAULT;
205 static int retry_count_set(const char *val, cfs_kernel_param_arg_t *kp);
206 #ifdef HAVE_KERNEL_PARAM_OPS
207 static struct kernel_param_ops param_ops_retry_count = {
208 .set = retry_count_set,
209 .get = param_get_int,
212 #define param_check_retry_count(name, p) \
213 __param_check(name, p, int)
214 module_param(lnet_retry_count, retry_count, S_IRUGO|S_IWUSR);
216 module_param_call(lnet_retry_count, retry_count_set, param_get_int,
217 &lnet_retry_count, S_IRUGO|S_IWUSR);
219 MODULE_PARM_DESC(lnet_retry_count,
220 "Maximum number of times to retry transmitting a message");
223 unsigned lnet_lnd_timeout = LNET_LND_DEFAULT_TIMEOUT;
224 unsigned int lnet_current_net_count;
227 * This sequence number keeps track of how many times DLC was used to
228 * update the local NIs. It is incremented when a NI is added or
229 * removed and checked when sending a message to determine if there is
230 * a need to re-run the selection algorithm. See lnet_select_pathway()
231 * for more details on its usage.
233 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
235 static int lnet_ping(struct lnet_process_id id, signed long timeout,
236 struct lnet_process_id __user *ids, int n_ids);
238 static int lnet_discover(struct lnet_process_id id, __u32 force,
239 struct lnet_process_id __user *ids, int n_ids);
242 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
245 unsigned *sensitivity = (unsigned *)kp->arg;
248 rc = kstrtoul(val, 0, &value);
250 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
255 * The purpose of locking the api_mutex here is to ensure that
256 * the correct value ends up stored properly.
258 mutex_lock(&the_lnet.ln_api_mutex);
260 if (value > LNET_MAX_HEALTH_VALUE) {
261 mutex_unlock(&the_lnet.ln_api_mutex);
262 CERROR("Invalid health value. Maximum: %d value = %lu\n",
263 LNET_MAX_HEALTH_VALUE, value);
268 * if we're turning on health then use the health timeout
271 if (*sensitivity == 0 && value != 0) {
272 lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT;
273 lnet_retry_count = LNET_RETRY_COUNT_HEALTH_DEFAULT;
275 * if we're turning off health then use the no health timeout
278 } else if (*sensitivity != 0 && value == 0) {
279 lnet_transaction_timeout =
280 LNET_TRANSACTION_TIMEOUT_NO_HEALTH_DEFAULT;
281 lnet_retry_count = 0;
284 *sensitivity = value;
286 mutex_unlock(&the_lnet.ln_api_mutex);
292 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
295 unsigned *interval = (unsigned *)kp->arg;
298 rc = kstrtoul(val, 0, &value);
300 CERROR("Invalid module parameter value for 'lnet_recovery_interval'\n");
305 CERROR("lnet_recovery_interval must be at least 1 second\n");
310 * The purpose of locking the api_mutex here is to ensure that
311 * the correct value ends up stored properly.
313 mutex_lock(&the_lnet.ln_api_mutex);
317 mutex_unlock(&the_lnet.ln_api_mutex);
323 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
326 unsigned *discovery = (unsigned *)kp->arg;
328 struct lnet_ping_buffer *pbuf;
330 rc = kstrtoul(val, 0, &value);
332 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
336 value = (value) ? 1 : 0;
339 * The purpose of locking the api_mutex here is to ensure that
340 * the correct value ends up stored properly.
342 mutex_lock(&the_lnet.ln_api_mutex);
344 if (value == *discovery) {
345 mutex_unlock(&the_lnet.ln_api_mutex);
351 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
352 mutex_unlock(&the_lnet.ln_api_mutex);
356 /* tell peers that discovery setting has changed */
357 lnet_net_lock(LNET_LOCK_EX);
358 pbuf = the_lnet.ln_ping_target;
360 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
362 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
363 lnet_net_unlock(LNET_LOCK_EX);
365 lnet_push_update_to_peers(1);
367 mutex_unlock(&the_lnet.ln_api_mutex);
373 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
376 unsigned int *drop_asym_route = (unsigned int *)kp->arg;
379 rc = kstrtoul(val, 0, &value);
381 CERROR("Invalid module parameter value for "
382 "'lnet_drop_asym_route'\n");
387 * The purpose of locking the api_mutex here is to ensure that
388 * the correct value ends up stored properly.
390 mutex_lock(&the_lnet.ln_api_mutex);
392 if (value == *drop_asym_route) {
393 mutex_unlock(&the_lnet.ln_api_mutex);
397 *drop_asym_route = value;
399 mutex_unlock(&the_lnet.ln_api_mutex);
405 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
408 unsigned *transaction_to = (unsigned *)kp->arg;
411 rc = kstrtoul(val, 0, &value);
413 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
418 * The purpose of locking the api_mutex here is to ensure that
419 * the correct value ends up stored properly.
421 mutex_lock(&the_lnet.ln_api_mutex);
423 if (value < lnet_retry_count || value == 0) {
424 mutex_unlock(&the_lnet.ln_api_mutex);
425 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
426 "Has to be greater than lnet_retry_count (%u)\n",
427 value, lnet_retry_count);
431 if (value == *transaction_to) {
432 mutex_unlock(&the_lnet.ln_api_mutex);
436 *transaction_to = value;
437 if (lnet_retry_count == 0)
438 lnet_lnd_timeout = value;
440 lnet_lnd_timeout = value / lnet_retry_count;
442 mutex_unlock(&the_lnet.ln_api_mutex);
448 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
451 unsigned *retry_count = (unsigned *)kp->arg;
454 rc = kstrtoul(val, 0, &value);
456 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
461 * The purpose of locking the api_mutex here is to ensure that
462 * the correct value ends up stored properly.
464 mutex_lock(&the_lnet.ln_api_mutex);
466 if (lnet_health_sensitivity == 0) {
467 mutex_unlock(&the_lnet.ln_api_mutex);
468 CERROR("Can not set retry_count when health feature is turned off\n");
472 if (value > lnet_transaction_timeout) {
473 mutex_unlock(&the_lnet.ln_api_mutex);
474 CERROR("Invalid value for lnet_retry_count (%lu). "
475 "Has to be smaller than lnet_transaction_timeout (%u)\n",
476 value, lnet_transaction_timeout);
480 *retry_count = value;
483 lnet_lnd_timeout = lnet_transaction_timeout;
485 lnet_lnd_timeout = lnet_transaction_timeout / value;
487 mutex_unlock(&the_lnet.ln_api_mutex);
493 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
497 rc = kstrtoint(val, 0, &value);
499 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
503 if (value < LNET_INTERFACES_MIN) {
504 CWARN("max interfaces provided are too small, setting to %d\n",
505 LNET_INTERFACES_MAX_DEFAULT);
506 value = LNET_INTERFACES_MAX_DEFAULT;
509 *(int *)kp->arg = value;
515 lnet_get_routes(void)
521 lnet_get_networks(void)
526 if (*networks != 0 && *ip2nets != 0) {
527 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
528 "'ip2nets' but not both at once\n");
533 rc = lnet_parse_ip2nets(&nets, ip2nets);
534 return (rc == 0) ? nets : NULL;
544 lnet_init_locks(void)
546 spin_lock_init(&the_lnet.ln_eq_wait_lock);
547 spin_lock_init(&the_lnet.ln_msg_resend_lock);
548 init_waitqueue_head(&the_lnet.ln_eq_waitq);
549 init_completion(&the_lnet.ln_mt_wait_complete);
550 mutex_init(&the_lnet.ln_lnd_mutex);
553 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
554 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
556 struct kmem_cache *lnet_rspt_cachep; /* response tracker cache */
557 struct kmem_cache *lnet_msg_cachep;
560 lnet_slab_setup(void)
562 /* create specific kmem_cache for MEs and small MDs (i.e., originally
563 * allocated in <size-xxx> kmem_cache).
565 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
567 if (!lnet_mes_cachep)
570 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
571 LNET_SMALL_MD_SIZE, 0, 0,
573 if (!lnet_small_mds_cachep)
576 lnet_rspt_cachep = kmem_cache_create("lnet_rspt", sizeof(struct lnet_rsp_tracker),
578 if (!lnet_rspt_cachep)
581 lnet_msg_cachep = kmem_cache_create("lnet_msg", sizeof(struct lnet_msg),
583 if (!lnet_msg_cachep)
590 lnet_slab_cleanup(void)
592 if (lnet_msg_cachep) {
593 kmem_cache_destroy(lnet_msg_cachep);
594 lnet_msg_cachep = NULL;
598 if (lnet_rspt_cachep) {
599 kmem_cache_destroy(lnet_rspt_cachep);
600 lnet_rspt_cachep = NULL;
603 if (lnet_small_mds_cachep) {
604 kmem_cache_destroy(lnet_small_mds_cachep);
605 lnet_small_mds_cachep = NULL;
608 if (lnet_mes_cachep) {
609 kmem_cache_destroy(lnet_mes_cachep);
610 lnet_mes_cachep = NULL;
615 lnet_create_remote_nets_table(void)
618 struct list_head *hash;
620 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
621 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
622 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
624 CERROR("Failed to create remote nets hash table\n");
628 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
629 INIT_LIST_HEAD(&hash[i]);
630 the_lnet.ln_remote_nets_hash = hash;
635 lnet_destroy_remote_nets_table(void)
639 if (the_lnet.ln_remote_nets_hash == NULL)
642 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
643 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
645 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
646 LNET_REMOTE_NETS_HASH_SIZE *
647 sizeof(the_lnet.ln_remote_nets_hash[0]));
648 the_lnet.ln_remote_nets_hash = NULL;
652 lnet_destroy_locks(void)
654 if (the_lnet.ln_res_lock != NULL) {
655 cfs_percpt_lock_free(the_lnet.ln_res_lock);
656 the_lnet.ln_res_lock = NULL;
659 if (the_lnet.ln_net_lock != NULL) {
660 cfs_percpt_lock_free(the_lnet.ln_net_lock);
661 the_lnet.ln_net_lock = NULL;
666 lnet_create_locks(void)
670 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
671 if (the_lnet.ln_res_lock == NULL)
674 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
675 if (the_lnet.ln_net_lock == NULL)
681 lnet_destroy_locks();
685 static void lnet_assert_wire_constants(void)
687 /* Wire protocol assertions generated by 'wirecheck'
688 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
689 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
690 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
693 BUILD_BUG_ON(LNET_PROTO_TCP_MAGIC != 0xeebc0ded);
694 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MAJOR != 1);
695 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MINOR != 0);
696 BUILD_BUG_ON(LNET_MSG_ACK != 0);
697 BUILD_BUG_ON(LNET_MSG_PUT != 1);
698 BUILD_BUG_ON(LNET_MSG_GET != 2);
699 BUILD_BUG_ON(LNET_MSG_REPLY != 3);
700 BUILD_BUG_ON(LNET_MSG_HELLO != 4);
702 /* Checks for struct lnet_handle_wire */
703 BUILD_BUG_ON((int)sizeof(struct lnet_handle_wire) != 16);
704 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
705 wh_interface_cookie) != 0);
706 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) != 8);
707 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
708 wh_object_cookie) != 8);
709 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) != 8);
711 /* Checks for struct struct lnet_magicversion */
712 BUILD_BUG_ON((int)sizeof(struct lnet_magicversion) != 8);
713 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, magic) != 0);
714 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->magic) != 4);
715 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, version_major) != 4);
716 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_major) != 2);
717 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion,
718 version_minor) != 6);
719 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_minor) != 2);
721 /* Checks for struct struct lnet_hdr */
722 BUILD_BUG_ON((int)sizeof(struct lnet_hdr) != 72);
723 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_nid) != 0);
724 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_nid) != 8);
725 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_nid) != 8);
726 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_nid) != 8);
727 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_pid) != 16);
728 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_pid) != 4);
729 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_pid) != 20);
730 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_pid) != 4);
731 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, type) != 24);
732 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->type) != 4);
733 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, payload_length) != 28);
734 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->payload_length) != 4);
735 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg) != 32);
736 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg) != 40);
739 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) != 32);
740 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) != 16);
741 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.match_bits) != 48);
742 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) != 8);
743 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.mlength) != 56);
744 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) != 4);
747 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) != 32);
748 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) != 16);
749 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.match_bits) != 48);
750 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) != 8);
751 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.hdr_data) != 56);
752 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) != 8);
753 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ptl_index) != 64);
754 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) != 4);
755 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.offset) != 68);
756 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) != 4);
759 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.return_wmd) != 32);
760 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) != 16);
761 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.match_bits) != 48);
762 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) != 8);
763 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.ptl_index) != 56);
764 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) != 4);
765 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.src_offset) != 60);
766 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) != 4);
767 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.sink_length) != 64);
768 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) != 4);
771 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) != 32);
772 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) != 16);
775 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.incarnation) != 32);
776 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) != 8);
777 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.type) != 40);
778 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) != 4);
780 /* Checks for struct lnet_ni_status and related constants */
781 BUILD_BUG_ON(LNET_NI_STATUS_INVALID != 0x00000000);
782 BUILD_BUG_ON(LNET_NI_STATUS_UP != 0x15aac0de);
783 BUILD_BUG_ON(LNET_NI_STATUS_DOWN != 0xdeadface);
785 /* Checks for struct lnet_ni_status */
786 BUILD_BUG_ON((int)sizeof(struct lnet_ni_status) != 16);
787 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_nid) != 0);
788 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) != 8);
789 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_status) != 8);
790 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_status) != 4);
791 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_unused) != 12);
792 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) != 4);
794 /* Checks for struct lnet_ping_info and related constants */
795 BUILD_BUG_ON(LNET_PROTO_PING_MAGIC != 0x70696E67);
796 BUILD_BUG_ON(LNET_PING_FEAT_INVAL != 0);
797 BUILD_BUG_ON(LNET_PING_FEAT_BASE != 1);
798 BUILD_BUG_ON(LNET_PING_FEAT_NI_STATUS != 2);
799 BUILD_BUG_ON(LNET_PING_FEAT_RTE_DISABLED != 4);
800 BUILD_BUG_ON(LNET_PING_FEAT_MULTI_RAIL != 8);
801 BUILD_BUG_ON(LNET_PING_FEAT_DISCOVERY != 16);
802 BUILD_BUG_ON(LNET_PING_FEAT_BITS != 31);
804 /* Checks for struct lnet_ping_info */
805 BUILD_BUG_ON((int)sizeof(struct lnet_ping_info) != 16);
806 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_magic) != 0);
807 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) != 4);
808 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_features) != 4);
809 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_features) != 4);
810 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_pid) != 8);
811 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) != 4);
812 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_nnis) != 12);
813 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) != 4);
814 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_ni) != 16);
815 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) != 0);
818 static const struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
820 const struct lnet_lnd *lnd;
822 /* holding lnd mutex */
823 if (type >= NUM_LNDS)
825 lnd = the_lnet.ln_lnds[type];
826 LASSERT(!lnd || lnd->lnd_type == type);
832 lnet_get_lnd_timeout(void)
834 return lnet_lnd_timeout;
836 EXPORT_SYMBOL(lnet_get_lnd_timeout);
839 lnet_register_lnd(const struct lnet_lnd *lnd)
841 mutex_lock(&the_lnet.ln_lnd_mutex);
843 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
844 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
846 the_lnet.ln_lnds[lnd->lnd_type] = lnd;
848 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
850 mutex_unlock(&the_lnet.ln_lnd_mutex);
852 EXPORT_SYMBOL(lnet_register_lnd);
855 lnet_unregister_lnd(const struct lnet_lnd *lnd)
857 mutex_lock(&the_lnet.ln_lnd_mutex);
859 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
861 the_lnet.ln_lnds[lnd->lnd_type] = NULL;
862 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
864 mutex_unlock(&the_lnet.ln_lnd_mutex);
866 EXPORT_SYMBOL(lnet_unregister_lnd);
869 lnet_counters_get_common(struct lnet_counters_common *common)
871 struct lnet_counters *ctr;
874 memset(common, 0, sizeof(*common));
876 lnet_net_lock(LNET_LOCK_EX);
878 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
879 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
880 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
881 common->lcc_errors += ctr->lct_common.lcc_errors;
882 common->lcc_send_count += ctr->lct_common.lcc_send_count;
883 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
884 common->lcc_route_count += ctr->lct_common.lcc_route_count;
885 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
886 common->lcc_send_length += ctr->lct_common.lcc_send_length;
887 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
888 common->lcc_route_length += ctr->lct_common.lcc_route_length;
889 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
891 lnet_net_unlock(LNET_LOCK_EX);
893 EXPORT_SYMBOL(lnet_counters_get_common);
896 lnet_counters_get(struct lnet_counters *counters)
898 struct lnet_counters *ctr;
899 struct lnet_counters_health *health = &counters->lct_health;
902 memset(counters, 0, sizeof(*counters));
904 lnet_counters_get_common(&counters->lct_common);
906 lnet_net_lock(LNET_LOCK_EX);
908 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
909 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
910 health->lch_resend_count += ctr->lct_health.lch_resend_count;
911 health->lch_response_timeout_count +=
912 ctr->lct_health.lch_response_timeout_count;
913 health->lch_local_interrupt_count +=
914 ctr->lct_health.lch_local_interrupt_count;
915 health->lch_local_dropped_count +=
916 ctr->lct_health.lch_local_dropped_count;
917 health->lch_local_aborted_count +=
918 ctr->lct_health.lch_local_aborted_count;
919 health->lch_local_no_route_count +=
920 ctr->lct_health.lch_local_no_route_count;
921 health->lch_local_timeout_count +=
922 ctr->lct_health.lch_local_timeout_count;
923 health->lch_local_error_count +=
924 ctr->lct_health.lch_local_error_count;
925 health->lch_remote_dropped_count +=
926 ctr->lct_health.lch_remote_dropped_count;
927 health->lch_remote_error_count +=
928 ctr->lct_health.lch_remote_error_count;
929 health->lch_remote_timeout_count +=
930 ctr->lct_health.lch_remote_timeout_count;
931 health->lch_network_timeout_count +=
932 ctr->lct_health.lch_network_timeout_count;
934 lnet_net_unlock(LNET_LOCK_EX);
936 EXPORT_SYMBOL(lnet_counters_get);
939 lnet_counters_reset(void)
941 struct lnet_counters *counters;
944 lnet_net_lock(LNET_LOCK_EX);
946 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
947 memset(counters, 0, sizeof(struct lnet_counters));
949 lnet_net_unlock(LNET_LOCK_EX);
953 lnet_res_type2str(int type)
958 case LNET_COOKIE_TYPE_MD:
960 case LNET_COOKIE_TYPE_ME:
962 case LNET_COOKIE_TYPE_EQ:
968 lnet_res_container_cleanup(struct lnet_res_container *rec)
972 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
975 while (!list_empty(&rec->rec_active)) {
976 struct list_head *e = rec->rec_active.next;
979 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
980 lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
982 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
983 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
985 } else { /* NB: Active MEs should be attached on portals */
992 /* Found alive MD/ME/EQ, user really should unlink/free
993 * all of them before finalize LNet, but if someone didn't,
994 * we have to recycle garbage for him */
995 CERROR("%d active elements on exit of %s container\n",
996 count, lnet_res_type2str(rec->rec_type));
999 if (rec->rec_lh_hash != NULL) {
1000 LIBCFS_FREE(rec->rec_lh_hash,
1001 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1002 rec->rec_lh_hash = NULL;
1005 rec->rec_type = 0; /* mark it as finalized */
1009 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1014 LASSERT(rec->rec_type == 0);
1016 rec->rec_type = type;
1017 INIT_LIST_HEAD(&rec->rec_active);
1019 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1021 /* Arbitrary choice of hash table size */
1022 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1023 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1024 if (rec->rec_lh_hash == NULL) {
1029 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1030 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1035 CERROR("Failed to setup %s resource container\n",
1036 lnet_res_type2str(type));
1037 lnet_res_container_cleanup(rec);
1042 lnet_res_containers_destroy(struct lnet_res_container **recs)
1044 struct lnet_res_container *rec;
1047 cfs_percpt_for_each(rec, i, recs)
1048 lnet_res_container_cleanup(rec);
1050 cfs_percpt_free(recs);
1053 static struct lnet_res_container **
1054 lnet_res_containers_create(int type)
1056 struct lnet_res_container **recs;
1057 struct lnet_res_container *rec;
1061 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1063 CERROR("Failed to allocate %s resource containers\n",
1064 lnet_res_type2str(type));
1068 cfs_percpt_for_each(rec, i, recs) {
1069 rc = lnet_res_container_setup(rec, i, type);
1071 lnet_res_containers_destroy(recs);
1079 struct lnet_libhandle *
1080 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1082 /* ALWAYS called with lnet_res_lock held */
1083 struct list_head *head;
1084 struct lnet_libhandle *lh;
1087 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1090 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1091 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1093 list_for_each_entry(lh, head, lh_hash_chain) {
1094 if (lh->lh_cookie == cookie)
1102 lnet_res_lh_initialize(struct lnet_res_container *rec,
1103 struct lnet_libhandle *lh)
1105 /* ALWAYS called with lnet_res_lock held */
1106 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1109 lh->lh_cookie = rec->rec_lh_cookie;
1110 rec->rec_lh_cookie += 1 << ibits;
1112 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1114 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1118 lnet_create_array_of_queues(void)
1120 struct list_head **qs;
1121 struct list_head *q;
1124 qs = cfs_percpt_alloc(lnet_cpt_table(),
1125 sizeof(struct list_head));
1127 CERROR("Failed to allocate queues\n");
1131 cfs_percpt_for_each(q, i, qs)
1137 static int lnet_unprepare(void);
1140 lnet_prepare(lnet_pid_t requested_pid)
1142 /* Prepare to bring up the network */
1143 struct lnet_res_container **recs;
1146 if (requested_pid == LNET_PID_ANY) {
1147 /* Don't instantiate LNET just for me */
1151 LASSERT(the_lnet.ln_refcount == 0);
1153 the_lnet.ln_routing = 0;
1155 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1156 the_lnet.ln_pid = requested_pid;
1158 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1159 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1160 INIT_LIST_HEAD(&the_lnet.ln_nets);
1161 INIT_LIST_HEAD(&the_lnet.ln_routers);
1162 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1163 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1164 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1165 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1166 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1167 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1168 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1169 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1170 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
1171 init_completion(&the_lnet.ln_started);
1173 rc = lnet_slab_setup();
1177 rc = lnet_create_remote_nets_table();
1182 * NB the interface cookie in wire handles guards against delayed
1183 * replies and ACKs appearing valid after reboot.
1185 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1187 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1188 sizeof(struct lnet_counters));
1189 if (the_lnet.ln_counters == NULL) {
1190 CERROR("Failed to allocate counters for LNet\n");
1195 rc = lnet_peer_tables_create();
1199 rc = lnet_msg_containers_create();
1203 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1204 LNET_COOKIE_TYPE_EQ);
1208 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1214 the_lnet.ln_md_containers = recs;
1216 rc = lnet_portals_create();
1218 CERROR("Failed to create portals for LNet: %d\n", rc);
1222 the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1223 if (!the_lnet.ln_mt_zombie_rstqs) {
1236 lnet_unprepare (void)
1240 /* NB no LNET_LOCK since this is the last reference. All LND instances
1241 * have shut down already, so it is safe to unlink and free all
1242 * descriptors, even those that appear committed to a network op (eg MD
1243 * with non-zero pending count) */
1245 lnet_fail_nid(LNET_NID_ANY, 0);
1247 LASSERT(the_lnet.ln_refcount == 0);
1248 LASSERT(list_empty(&the_lnet.ln_test_peers));
1249 LASSERT(list_empty(&the_lnet.ln_nets));
1251 if (the_lnet.ln_mt_zombie_rstqs) {
1252 lnet_clean_zombie_rstqs();
1253 the_lnet.ln_mt_zombie_rstqs = NULL;
1256 if (!LNetEQHandleIsInvalid(the_lnet.ln_mt_eqh)) {
1257 rc = LNetEQFree(the_lnet.ln_mt_eqh);
1258 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
1262 lnet_portals_destroy();
1264 if (the_lnet.ln_md_containers != NULL) {
1265 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1266 the_lnet.ln_md_containers = NULL;
1269 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1271 lnet_msg_containers_destroy();
1273 lnet_rtrpools_free(0);
1275 if (the_lnet.ln_counters != NULL) {
1276 cfs_percpt_free(the_lnet.ln_counters);
1277 the_lnet.ln_counters = NULL;
1279 lnet_destroy_remote_nets_table();
1280 lnet_slab_cleanup();
1286 lnet_net2ni_locked(__u32 net_id, int cpt)
1289 struct lnet_net *net;
1291 LASSERT(cpt != LNET_LOCK_EX);
1293 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1294 if (net->net_id == net_id) {
1295 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1305 lnet_net2ni_addref(__u32 net)
1310 ni = lnet_net2ni_locked(net, 0);
1312 lnet_ni_addref_locked(ni, 0);
1317 EXPORT_SYMBOL(lnet_net2ni_addref);
1320 lnet_get_net_locked(__u32 net_id)
1322 struct lnet_net *net;
1324 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1325 if (net->net_id == net_id)
1333 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1338 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1343 val = hash_long(key, LNET_CPT_BITS);
1344 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1348 return (unsigned int)(key + val + (val >> 1)) % number;
1352 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1354 struct lnet_net *net;
1356 /* must called with hold of lnet_net_lock */
1357 if (LNET_CPT_NUMBER == 1)
1358 return 0; /* the only one */
1361 * If NI is provided then use the CPT identified in the NI cpt
1362 * list if one exists. If one doesn't exist, then that NI is
1363 * associated with all CPTs and it follows that the net it belongs
1364 * to is implicitly associated with all CPTs, so just hash the nid
1368 if (ni->ni_cpts != NULL)
1369 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1372 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1375 /* no NI provided so look at the net */
1376 net = lnet_get_net_locked(LNET_NIDNET(nid));
1378 if (net != NULL && net->net_cpts != NULL) {
1379 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1382 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1386 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1391 if (LNET_CPT_NUMBER == 1)
1392 return 0; /* the only one */
1394 cpt = lnet_net_lock_current();
1396 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1398 lnet_net_unlock(cpt);
1402 EXPORT_SYMBOL(lnet_cpt_of_nid);
1405 lnet_islocalnet_locked(__u32 net_id)
1407 struct lnet_net *net;
1410 net = lnet_get_net_locked(net_id);
1412 local = net != NULL;
1418 lnet_islocalnet(__u32 net_id)
1423 cpt = lnet_net_lock_current();
1425 local = lnet_islocalnet_locked(net_id);
1427 lnet_net_unlock(cpt);
1433 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1435 struct lnet_net *net;
1438 LASSERT(cpt != LNET_LOCK_EX);
1440 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1441 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1442 if (ni->ni_nid == nid)
1451 lnet_nid2ni_addref(lnet_nid_t nid)
1456 ni = lnet_nid2ni_locked(nid, 0);
1458 lnet_ni_addref_locked(ni, 0);
1463 EXPORT_SYMBOL(lnet_nid2ni_addref);
1466 lnet_islocalnid(lnet_nid_t nid)
1471 cpt = lnet_net_lock_current();
1472 ni = lnet_nid2ni_locked(nid, cpt);
1473 lnet_net_unlock(cpt);
1479 lnet_count_acceptor_nets(void)
1481 /* Return the # of NIs that need the acceptor. */
1483 struct lnet_net *net;
1486 cpt = lnet_net_lock_current();
1487 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1488 /* all socklnd type networks should have the acceptor
1490 if (net->net_lnd->lnd_accept != NULL)
1494 lnet_net_unlock(cpt);
1499 struct lnet_ping_buffer *
1500 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1502 struct lnet_ping_buffer *pbuf;
1504 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1506 pbuf->pb_nnis = nnis;
1507 atomic_set(&pbuf->pb_refcnt, 1);
1514 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1516 LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1517 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1520 static struct lnet_ping_buffer *
1521 lnet_ping_target_create(int nnis)
1523 struct lnet_ping_buffer *pbuf;
1525 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1527 CERROR("Can't allocate ping source [%d]\n", nnis);
1531 pbuf->pb_info.pi_nnis = nnis;
1532 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1533 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1534 pbuf->pb_info.pi_features =
1535 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1541 lnet_get_net_ni_count_locked(struct lnet_net *net)
1546 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1553 lnet_get_net_ni_count_pre(struct lnet_net *net)
1558 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1565 lnet_get_ni_count(void)
1568 struct lnet_net *net;
1573 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1574 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1584 lnet_get_net_count(void)
1586 struct lnet_net *net;
1591 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1601 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1603 struct lnet_ni_status *stat;
1607 __swab32s(&pbuf->pb_info.pi_magic);
1608 __swab32s(&pbuf->pb_info.pi_features);
1609 __swab32s(&pbuf->pb_info.pi_pid);
1610 __swab32s(&pbuf->pb_info.pi_nnis);
1611 nnis = pbuf->pb_info.pi_nnis;
1612 if (nnis > pbuf->pb_nnis)
1613 nnis = pbuf->pb_nnis;
1614 for (i = 0; i < nnis; i++) {
1615 stat = &pbuf->pb_info.pi_ni[i];
1616 __swab64s(&stat->ns_nid);
1617 __swab32s(&stat->ns_status);
1622 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1626 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1628 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1630 /* Loopback is guaranteed to be present */
1631 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1633 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1639 lnet_ping_target_destroy(void)
1641 struct lnet_net *net;
1644 lnet_net_lock(LNET_LOCK_EX);
1646 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1647 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1649 ni->ni_status = NULL;
1654 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1655 the_lnet.ln_ping_target = NULL;
1657 lnet_net_unlock(LNET_LOCK_EX);
1661 lnet_ping_target_event_handler(struct lnet_event *event)
1663 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1665 if (event->unlinked)
1666 lnet_ping_buffer_decref(pbuf);
1670 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1671 struct lnet_handle_md *ping_mdh,
1672 int ni_count, bool set_eq)
1674 struct lnet_process_id id = {
1675 .nid = LNET_NID_ANY,
1679 struct lnet_md md = { NULL };
1683 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1684 &the_lnet.ln_ping_target_eq);
1686 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1691 *ppbuf = lnet_ping_target_create(ni_count);
1692 if (*ppbuf == NULL) {
1697 /* Ping target ME/MD */
1698 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1699 LNET_PROTO_PING_MATCHBITS, 0,
1700 LNET_UNLINK, LNET_INS_AFTER);
1703 CERROR("Can't create ping target ME: %d\n", rc);
1704 goto fail_decref_ping_buffer;
1707 /* initialize md content */
1708 md.start = &(*ppbuf)->pb_info;
1709 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1710 md.threshold = LNET_MD_THRESH_INF;
1712 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1713 LNET_MD_MANAGE_REMOTE;
1714 md.eq_handle = the_lnet.ln_ping_target_eq;
1715 md.user_ptr = *ppbuf;
1717 rc = LNetMDAttach(me, md, LNET_RETAIN, ping_mdh);
1719 CERROR("Can't attach ping target MD: %d\n", rc);
1720 goto fail_unlink_ping_me;
1722 lnet_ping_buffer_addref(*ppbuf);
1726 fail_unlink_ping_me:
1728 fail_decref_ping_buffer:
1729 LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1730 lnet_ping_buffer_decref(*ppbuf);
1734 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1741 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1742 struct lnet_handle_md *ping_mdh)
1744 sigset_t blocked = cfs_block_allsigs();
1746 LNetMDUnlink(*ping_mdh);
1747 LNetInvalidateMDHandle(ping_mdh);
1749 /* NB the MD could be busy; this just starts the unlink */
1750 while (lnet_ping_buffer_numref(pbuf) > 1) {
1751 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1752 set_current_state(TASK_UNINTERRUPTIBLE);
1753 schedule_timeout(cfs_time_seconds(1));
1756 cfs_restore_sigs(blocked);
1760 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1763 struct lnet_net *net;
1764 struct lnet_ni_status *ns;
1769 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1770 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1771 LASSERT(i < pbuf->pb_nnis);
1773 ns = &pbuf->pb_info.pi_ni[i];
1775 ns->ns_nid = ni->ni_nid;
1778 ns->ns_status = (ni->ni_status != NULL) ?
1779 ni->ni_status->ns_status :
1788 * We (ab)use the ns_status of the loopback interface to
1789 * transmit the sequence number. The first interface listed
1790 * must be the loopback interface.
1792 rc = lnet_ping_info_validate(&pbuf->pb_info);
1794 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1797 LNET_PING_BUFFER_SEQNO(pbuf) =
1798 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1802 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1803 struct lnet_handle_md ping_mdh)
1805 struct lnet_ping_buffer *old_pbuf = NULL;
1806 struct lnet_handle_md old_ping_md;
1808 /* switch the NIs to point to the new ping info created */
1809 lnet_net_lock(LNET_LOCK_EX);
1811 if (!the_lnet.ln_routing)
1812 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1813 if (!lnet_peer_discovery_disabled)
1814 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1816 /* Ensure only known feature bits have been set. */
1817 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1818 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1820 lnet_ping_target_install_locked(pbuf);
1822 if (the_lnet.ln_ping_target) {
1823 old_pbuf = the_lnet.ln_ping_target;
1824 old_ping_md = the_lnet.ln_ping_target_md;
1826 the_lnet.ln_ping_target_md = ping_mdh;
1827 the_lnet.ln_ping_target = pbuf;
1829 lnet_net_unlock(LNET_LOCK_EX);
1832 /* unlink and free the old ping info */
1833 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1834 lnet_ping_buffer_decref(old_pbuf);
1837 lnet_push_update_to_peers(0);
1841 lnet_ping_target_fini(void)
1845 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1846 &the_lnet.ln_ping_target_md);
1848 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1851 lnet_ping_target_destroy();
1854 /* Resize the push target. */
1855 int lnet_push_target_resize(void)
1857 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1858 struct lnet_md md = { NULL };
1860 struct lnet_handle_md mdh;
1861 struct lnet_handle_md old_mdh;
1862 struct lnet_ping_buffer *pbuf;
1863 struct lnet_ping_buffer *old_pbuf;
1864 int nnis = the_lnet.ln_push_target_nnis;
1872 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1878 me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1879 LNET_PROTO_PING_MATCHBITS, 0,
1880 LNET_UNLINK, LNET_INS_AFTER);
1884 CERROR("Can't create push target ME: %d\n", rc);
1885 goto fail_decref_pbuf;
1888 /* initialize md content */
1889 md.start = &pbuf->pb_info;
1890 md.length = LNET_PING_INFO_SIZE(nnis);
1891 md.threshold = LNET_MD_THRESH_INF;
1893 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1894 LNET_MD_MANAGE_REMOTE;
1896 md.eq_handle = the_lnet.ln_push_target_eq;
1898 rc = LNetMDAttach(me, md, LNET_RETAIN, &mdh);
1900 CERROR("Can't attach push MD: %d\n", rc);
1901 goto fail_unlink_me;
1903 lnet_ping_buffer_addref(pbuf);
1905 lnet_net_lock(LNET_LOCK_EX);
1906 old_pbuf = the_lnet.ln_push_target;
1907 old_mdh = the_lnet.ln_push_target_md;
1908 the_lnet.ln_push_target = pbuf;
1909 the_lnet.ln_push_target_md = mdh;
1910 lnet_net_unlock(LNET_LOCK_EX);
1913 LNetMDUnlink(old_mdh);
1914 lnet_ping_buffer_decref(old_pbuf);
1917 if (nnis < the_lnet.ln_push_target_nnis)
1920 CDEBUG(D_NET, "nnis %d success\n", nnis);
1927 lnet_ping_buffer_decref(pbuf);
1929 CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1933 static void lnet_push_target_event_handler(struct lnet_event *ev)
1935 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1937 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1938 lnet_swap_pinginfo(pbuf);
1940 lnet_peer_push_event(ev);
1942 lnet_ping_buffer_decref(pbuf);
1945 /* Initialize the push target. */
1946 static int lnet_push_target_init(void)
1950 if (the_lnet.ln_push_target)
1953 rc = LNetEQAlloc(0, lnet_push_target_event_handler,
1954 &the_lnet.ln_push_target_eq);
1956 CERROR("Can't allocated push target EQ: %d\n", rc);
1960 /* Start at the required minimum, we'll enlarge if required. */
1961 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1963 rc = lnet_push_target_resize();
1966 LNetEQFree(the_lnet.ln_push_target_eq);
1967 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1973 /* Clean up the push target. */
1974 static void lnet_push_target_fini(void)
1976 if (!the_lnet.ln_push_target)
1979 /* Unlink and invalidate to prevent new references. */
1980 LNetMDUnlink(the_lnet.ln_push_target_md);
1981 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1983 /* Wait for the unlink to complete. */
1984 while (lnet_ping_buffer_numref(the_lnet.ln_push_target) > 1) {
1985 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1986 set_current_state(TASK_UNINTERRUPTIBLE);
1987 schedule_timeout(cfs_time_seconds(1));
1990 lnet_ping_buffer_decref(the_lnet.ln_push_target);
1991 the_lnet.ln_push_target = NULL;
1992 the_lnet.ln_push_target_nnis = 0;
1994 LNetEQFree(the_lnet.ln_push_target_eq);
1995 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1999 lnet_ni_tq_credits(struct lnet_ni *ni)
2003 LASSERT(ni->ni_ncpts >= 1);
2005 if (ni->ni_ncpts == 1)
2006 return ni->ni_net->net_tunables.lct_max_tx_credits;
2008 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2009 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2010 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2016 lnet_ni_unlink_locked(struct lnet_ni *ni)
2018 /* move it to zombie list and nobody can find it anymore */
2019 LASSERT(!list_empty(&ni->ni_netlist));
2020 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2021 lnet_ni_decref_locked(ni, 0);
2025 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2030 struct list_head *zombie_list = &net->net_ni_zombie;
2033 * Now wait for the NIs I just nuked to show up on the zombie
2034 * list and shut them down in guaranteed thread context
2037 while (!list_empty(zombie_list)) {
2041 ni = list_entry(zombie_list->next,
2042 struct lnet_ni, ni_netlist);
2043 list_del_init(&ni->ni_netlist);
2044 /* the ni should be in deleting state. If it's not it's
2046 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2047 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2050 /* still busy, add it back to zombie list */
2051 list_add(&ni->ni_netlist, zombie_list);
2055 if (!list_empty(&ni->ni_netlist)) {
2056 lnet_net_unlock(LNET_LOCK_EX);
2058 if ((i & (-i)) == i) {
2060 "Waiting for zombie LNI %s\n",
2061 libcfs_nid2str(ni->ni_nid));
2063 set_current_state(TASK_UNINTERRUPTIBLE);
2064 schedule_timeout(cfs_time_seconds(1));
2065 lnet_net_lock(LNET_LOCK_EX);
2069 lnet_net_unlock(LNET_LOCK_EX);
2071 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2073 LASSERT(!in_interrupt());
2074 /* Holding the mutex makes it safe for lnd_shutdown
2075 * to call module_put(). Module unload cannot finish
2076 * until lnet_unregister_lnd() completes, and that
2077 * requires the mutex.
2079 mutex_lock(&the_lnet.ln_lnd_mutex);
2080 (net->net_lnd->lnd_shutdown)(ni);
2081 mutex_unlock(&the_lnet.ln_lnd_mutex);
2084 CDEBUG(D_LNI, "Removed LNI %s\n",
2085 libcfs_nid2str(ni->ni_nid));
2089 lnet_net_lock(LNET_LOCK_EX);
2093 /* shutdown down the NI and release refcount */
2095 lnet_shutdown_lndni(struct lnet_ni *ni)
2098 struct lnet_net *net = ni->ni_net;
2100 lnet_net_lock(LNET_LOCK_EX);
2102 ni->ni_state = LNET_NI_STATE_DELETING;
2104 lnet_ni_unlink_locked(ni);
2105 lnet_incr_dlc_seq();
2106 lnet_net_unlock(LNET_LOCK_EX);
2108 /* clear messages for this NI on the lazy portal */
2109 for (i = 0; i < the_lnet.ln_nportals; i++)
2110 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2112 lnet_net_lock(LNET_LOCK_EX);
2113 lnet_clear_zombies_nis_locked(net);
2114 lnet_net_unlock(LNET_LOCK_EX);
2118 lnet_shutdown_lndnet(struct lnet_net *net)
2122 lnet_net_lock(LNET_LOCK_EX);
2124 list_del_init(&net->net_list);
2126 while (!list_empty(&net->net_ni_list)) {
2127 ni = list_entry(net->net_ni_list.next,
2128 struct lnet_ni, ni_netlist);
2129 lnet_net_unlock(LNET_LOCK_EX);
2130 lnet_shutdown_lndni(ni);
2131 lnet_net_lock(LNET_LOCK_EX);
2134 lnet_net_unlock(LNET_LOCK_EX);
2136 /* Do peer table cleanup for this net */
2137 lnet_peer_tables_cleanup(net);
2143 lnet_shutdown_lndnets(void)
2145 struct lnet_net *net;
2147 struct lnet_msg *msg, *tmp;
2149 /* NB called holding the global mutex */
2151 /* All quiet on the API front */
2152 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2153 LASSERT(the_lnet.ln_refcount == 0);
2155 lnet_net_lock(LNET_LOCK_EX);
2156 the_lnet.ln_state = LNET_STATE_STOPPING;
2158 while (!list_empty(&the_lnet.ln_nets)) {
2160 * move the nets to the zombie list to avoid them being
2161 * picked up for new work. LONET is also included in the
2162 * Nets that will be moved to the zombie list
2164 net = list_entry(the_lnet.ln_nets.next,
2165 struct lnet_net, net_list);
2166 list_move(&net->net_list, &the_lnet.ln_net_zombie);
2169 /* Drop the cached loopback Net. */
2170 if (the_lnet.ln_loni != NULL) {
2171 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2172 the_lnet.ln_loni = NULL;
2174 lnet_net_unlock(LNET_LOCK_EX);
2176 /* iterate through the net zombie list and delete each net */
2177 while (!list_empty(&the_lnet.ln_net_zombie)) {
2178 net = list_entry(the_lnet.ln_net_zombie.next,
2179 struct lnet_net, net_list);
2180 lnet_shutdown_lndnet(net);
2183 spin_lock(&the_lnet.ln_msg_resend_lock);
2184 list_splice(&the_lnet.ln_msg_resend, &resend);
2185 spin_unlock(&the_lnet.ln_msg_resend_lock);
2187 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2188 list_del_init(&msg->msg_list);
2189 msg->msg_no_resend = true;
2190 lnet_finalize(msg, -ECANCELED);
2193 lnet_net_lock(LNET_LOCK_EX);
2194 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2195 lnet_net_unlock(LNET_LOCK_EX);
2199 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2202 struct lnet_tx_queue *tq;
2204 struct lnet_net *net = ni->ni_net;
2206 mutex_lock(&the_lnet.ln_lnd_mutex);
2209 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2210 ni->ni_lnd_tunables_set = true;
2213 rc = (net->net_lnd->lnd_startup)(ni);
2215 mutex_unlock(&the_lnet.ln_lnd_mutex);
2218 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2219 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2224 ni->ni_state = LNET_NI_STATE_ACTIVE;
2227 /* We keep a reference on the loopback net through the loopback NI */
2228 if (net->net_lnd->lnd_type == LOLND) {
2230 LASSERT(the_lnet.ln_loni == NULL);
2231 the_lnet.ln_loni = ni;
2232 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2233 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2234 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2235 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2239 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2240 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2241 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2242 libcfs_lnd2str(net->net_lnd->lnd_type),
2243 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2245 /* shutdown the NI since if we get here then it must've already
2248 lnet_shutdown_lndni(ni);
2252 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2253 tq->tq_credits_min =
2254 tq->tq_credits_max =
2255 tq->tq_credits = lnet_ni_tq_credits(ni);
2258 atomic_set(&ni->ni_tx_credits,
2259 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2260 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2262 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2263 libcfs_nid2str(ni->ni_nid),
2264 ni->ni_net->net_tunables.lct_peer_tx_credits,
2265 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2266 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2267 ni->ni_net->net_tunables.lct_peer_timeout);
2276 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2279 struct lnet_net *net_l = NULL;
2280 LIST_HEAD(local_ni_list);
2284 const struct lnet_lnd *lnd;
2286 net->net_tunables.lct_peer_timeout;
2288 net->net_tunables.lct_max_tx_credits;
2289 int peerrtrcredits =
2290 net->net_tunables.lct_peer_rtr_credits;
2293 * make sure that this net is unique. If it isn't then
2294 * we are adding interfaces to an already existing network, and
2295 * 'net' is just a convenient way to pass in the list.
2296 * if it is unique we need to find the LND and load it if
2299 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2300 lnd_type = LNET_NETTYP(net->net_id);
2302 mutex_lock(&the_lnet.ln_lnd_mutex);
2303 lnd = lnet_find_lnd_by_type(lnd_type);
2306 mutex_unlock(&the_lnet.ln_lnd_mutex);
2307 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2308 mutex_lock(&the_lnet.ln_lnd_mutex);
2310 lnd = lnet_find_lnd_by_type(lnd_type);
2312 mutex_unlock(&the_lnet.ln_lnd_mutex);
2313 CERROR("Can't load LND %s, module %s, rc=%d\n",
2314 libcfs_lnd2str(lnd_type),
2315 libcfs_lnd2modname(lnd_type), rc);
2316 #ifndef HAVE_MODULE_LOADING_SUPPORT
2317 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2318 "compiled with kernel module "
2319 "loading support.");
2328 mutex_unlock(&the_lnet.ln_lnd_mutex);
2334 * net_l: if the network being added is unique then net_l
2335 * will point to that network
2336 * if the network being added is not unique then
2337 * net_l points to the existing network.
2339 * When we enter the loop below, we'll pick NIs off he
2340 * network beign added and start them up, then add them to
2341 * a local ni list. Once we've successfully started all
2342 * the NIs then we join the local NI list (of started up
2343 * networks) with the net_l->net_ni_list, which should
2344 * point to the correct network to add the new ni list to
2346 * If any of the new NIs fail to start up, then we want to
2347 * iterate through the local ni list, which should include
2348 * any NIs which were successfully started up, and shut
2351 * After than we want to delete the network being added,
2352 * to avoid a memory leak.
2356 * When a network uses TCP bonding then all its interfaces
2357 * must be specified when the network is first defined: the
2358 * TCP bonding code doesn't allow for interfaces to be added
2361 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2362 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2367 while (!list_empty(&net->net_ni_added)) {
2368 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2370 list_del_init(&ni->ni_netlist);
2372 /* make sure that the the NI we're about to start
2373 * up is actually unique. if it's not fail. */
2374 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2375 ni->ni_interfaces[0])) {
2380 /* adjust the pointer the parent network, just in case it
2381 * the net is a duplicate */
2384 rc = lnet_startup_lndni(ni, tun);
2389 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
2390 ni->ni_net->net_lnd->lnd_query != NULL);
2393 list_add_tail(&ni->ni_netlist, &local_ni_list);
2398 lnet_net_lock(LNET_LOCK_EX);
2399 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2400 lnet_incr_dlc_seq();
2401 lnet_net_unlock(LNET_LOCK_EX);
2403 /* if the network is not unique then we don't want to keep
2404 * it around after we're done. Free it. Otherwise add that
2405 * net to the global the_lnet.ln_nets */
2406 if (net_l != net && net_l != NULL) {
2408 * TODO - note. currently the tunables can not be updated
2414 * restore tunables after it has been overwitten by the
2417 if (peer_timeout != -1)
2418 net->net_tunables.lct_peer_timeout = peer_timeout;
2419 if (maxtxcredits != -1)
2420 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2421 if (peerrtrcredits != -1)
2422 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2424 lnet_net_lock(LNET_LOCK_EX);
2425 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2426 lnet_net_unlock(LNET_LOCK_EX);
2429 /* update net count */
2430 lnet_current_net_count = lnet_get_net_count();
2436 * shutdown the new NIs that are being started up
2437 * free the NET being started
2439 while (!list_empty(&local_ni_list)) {
2440 ni = list_entry(local_ni_list.next, struct lnet_ni,
2443 lnet_shutdown_lndni(ni);
2453 lnet_startup_lndnets(struct list_head *netlist)
2455 struct lnet_net *net;
2460 * Change to running state before bringing up the LNDs. This
2461 * allows lnet_shutdown_lndnets() to assert that we've passed
2464 lnet_net_lock(LNET_LOCK_EX);
2465 the_lnet.ln_state = LNET_STATE_RUNNING;
2466 lnet_net_unlock(LNET_LOCK_EX);
2468 while (!list_empty(netlist)) {
2469 net = list_entry(netlist->next, struct lnet_net, net_list);
2470 list_del_init(&net->net_list);
2472 rc = lnet_startup_lndnet(net, NULL);
2482 lnet_shutdown_lndnets();
2488 * Initialize LNet library.
2490 * Automatically called at module loading time. Caller has to call
2491 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2492 * latter returned 0. It must be called exactly once.
2494 * \retval 0 on success
2495 * \retval -ve on failures.
2497 int lnet_lib_init(void)
2501 lnet_assert_wire_constants();
2503 /* refer to global cfs_cpt_table for now */
2504 the_lnet.ln_cpt_table = cfs_cpt_table;
2505 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
2507 LASSERT(the_lnet.ln_cpt_number > 0);
2508 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2509 /* we are under risk of consuming all lh_cookie */
2510 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2511 "please change setting of CPT-table and retry\n",
2512 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2516 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2517 the_lnet.ln_cpt_bits++;
2519 rc = lnet_create_locks();
2521 CERROR("Can't create LNet global locks: %d\n", rc);
2525 the_lnet.ln_refcount = 0;
2526 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2527 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2529 /* The hash table size is the number of bits it takes to express the set
2530 * ln_num_routes, minus 1 (better to under estimate than over so we
2531 * don't waste memory). */
2532 if (rnet_htable_size <= 0)
2533 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2534 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2535 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2536 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2537 order_base_2(rnet_htable_size) - 1);
2539 /* All LNDs apart from the LOLND are in separate modules. They
2540 * register themselves when their module loads, and unregister
2541 * themselves when their module is unloaded. */
2542 lnet_register_lnd(&the_lolnd);
2547 * Finalize LNet library.
2549 * \pre lnet_lib_init() called with success.
2550 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2552 * As this happens at module-unload, all lnds must already be unloaded,
2553 * so they must already be unregistered.
2555 void lnet_lib_exit(void)
2559 LASSERT(the_lnet.ln_refcount == 0);
2560 lnet_unregister_lnd(&the_lolnd);
2561 for (i = 0; i < NUM_LNDS; i++)
2562 LASSERT(!the_lnet.ln_lnds[i]);
2563 lnet_destroy_locks();
2567 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2569 * Users must call this function at least once before any other functions.
2570 * For each successful call there must be a corresponding call to
2571 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2574 * The PID used by LNet may be different from the one requested.
2577 * \param requested_pid PID requested by the caller.
2579 * \return >= 0 on success, and < 0 error code on failures.
2582 LNetNIInit(lnet_pid_t requested_pid)
2584 int im_a_router = 0;
2587 struct lnet_ping_buffer *pbuf;
2588 struct lnet_handle_md ping_mdh;
2589 LIST_HEAD(net_head);
2590 struct lnet_net *net;
2592 mutex_lock(&the_lnet.ln_api_mutex);
2594 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2596 if (the_lnet.ln_refcount > 0) {
2597 rc = the_lnet.ln_refcount++;
2598 mutex_unlock(&the_lnet.ln_api_mutex);
2602 rc = lnet_prepare(requested_pid);
2604 mutex_unlock(&the_lnet.ln_api_mutex);
2608 /* create a network for Loopback network */
2609 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2612 goto err_empty_list;
2615 /* Add in the loopback NI */
2616 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2618 goto err_empty_list;
2621 /* If LNet is being initialized via DLC it is possible
2622 * that the user requests not to load module parameters (ones which
2623 * are supported by DLC) on initialization. Therefore, make sure not
2624 * to load networks, routes and forwarding from module parameters
2625 * in this case. On cleanup in case of failure only clean up
2626 * routes if it has been loaded */
2627 if (!the_lnet.ln_nis_from_mod_params) {
2628 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2631 goto err_empty_list;
2634 ni_count = lnet_startup_lndnets(&net_head);
2637 goto err_empty_list;
2640 if (!the_lnet.ln_nis_from_mod_params) {
2641 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2643 goto err_shutdown_lndnis;
2645 rc = lnet_rtrpools_alloc(im_a_router);
2647 goto err_destroy_routes;
2650 rc = lnet_acceptor_start();
2652 goto err_destroy_routes;
2654 the_lnet.ln_refcount = 1;
2655 /* Now I may use my own API functions... */
2657 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2659 goto err_acceptor_stop;
2661 lnet_ping_target_update(pbuf, ping_mdh);
2663 rc = LNetEQAlloc(0, lnet_mt_event_handler, &the_lnet.ln_mt_eqh);
2665 CERROR("Can't allocate monitor thread EQ: %d\n", rc);
2669 rc = lnet_push_target_init();
2673 rc = lnet_peer_discovery_start();
2675 goto err_destroy_push_target;
2677 rc = lnet_monitor_thr_start();
2679 goto err_stop_discovery_thr;
2682 lnet_router_debugfs_init();
2684 mutex_unlock(&the_lnet.ln_api_mutex);
2686 complete_all(&the_lnet.ln_started);
2688 /* wait for all routers to start */
2689 lnet_wait_router_start();
2693 err_stop_discovery_thr:
2694 lnet_peer_discovery_stop();
2695 err_destroy_push_target:
2696 lnet_push_target_fini();
2698 lnet_ping_target_fini();
2700 the_lnet.ln_refcount = 0;
2701 lnet_acceptor_stop();
2703 if (!the_lnet.ln_nis_from_mod_params)
2704 lnet_destroy_routes();
2705 err_shutdown_lndnis:
2706 lnet_shutdown_lndnets();
2710 mutex_unlock(&the_lnet.ln_api_mutex);
2711 while (!list_empty(&net_head)) {
2712 struct lnet_net *net;
2714 net = list_entry(net_head.next, struct lnet_net, net_list);
2715 list_del_init(&net->net_list);
2720 EXPORT_SYMBOL(LNetNIInit);
2723 * Stop LNet interfaces, routing, and forwarding.
2725 * Users must call this function once for each successful call to LNetNIInit().
2726 * Once the LNetNIFini() operation has been started, the results of pending
2727 * API operations are undefined.
2729 * \return always 0 for current implementation.
2734 mutex_lock(&the_lnet.ln_api_mutex);
2736 LASSERT(the_lnet.ln_refcount > 0);
2738 if (the_lnet.ln_refcount != 1) {
2739 the_lnet.ln_refcount--;
2741 LASSERT(!the_lnet.ln_niinit_self);
2745 lnet_router_debugfs_fini();
2746 lnet_monitor_thr_stop();
2747 lnet_peer_discovery_stop();
2748 lnet_push_target_fini();
2749 lnet_ping_target_fini();
2751 /* Teardown fns that use my own API functions BEFORE here */
2752 the_lnet.ln_refcount = 0;
2754 lnet_acceptor_stop();
2755 lnet_destroy_routes();
2756 lnet_shutdown_lndnets();
2760 mutex_unlock(&the_lnet.ln_api_mutex);
2763 EXPORT_SYMBOL(LNetNIFini);
2766 * Grabs the ni data from the ni structure and fills the out
2769 * \param[in] ni network interface structure
2770 * \param[out] cfg_ni NI config information
2771 * \param[out] tun network and LND tunables
2774 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2775 struct lnet_ioctl_config_lnd_tunables *tun,
2776 struct lnet_ioctl_element_stats *stats,
2779 size_t min_size = 0;
2782 if (!ni || !cfg_ni || !tun)
2785 if (ni->ni_interfaces[0] != NULL) {
2786 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2787 if (ni->ni_interfaces[i] != NULL) {
2788 strncpy(cfg_ni->lic_ni_intf[i],
2789 ni->ni_interfaces[i],
2790 sizeof(cfg_ni->lic_ni_intf[i]));
2795 cfg_ni->lic_nid = ni->ni_nid;
2796 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2797 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2799 cfg_ni->lic_status = ni->ni_status->ns_status;
2800 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2801 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2803 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2806 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2807 LNET_STATS_TYPE_SEND);
2808 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2809 LNET_STATS_TYPE_RECV);
2810 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2811 LNET_STATS_TYPE_DROP);
2815 * tun->lt_tun will always be present, but in order to be
2816 * backwards compatible, we need to deal with the cases when
2817 * tun->lt_tun is smaller than what the kernel has, because it
2818 * comes from an older version of a userspace program, then we'll
2819 * need to copy as much information as we have available space.
2821 min_size = tun_size - sizeof(tun->lt_cmn);
2822 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2824 /* copy over the cpts */
2825 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2826 ni->ni_cpts == NULL) {
2827 for (i = 0; i < ni->ni_ncpts; i++)
2828 cfg_ni->lic_cpts[i] = i;
2831 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2832 i < LNET_MAX_SHOW_NUM_CPT;
2834 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2836 cfg_ni->lic_ncpts = ni->ni_ncpts;
2840 * NOTE: This is a legacy function left in the code to be backwards
2841 * compatible with older userspace programs. It should eventually be
2844 * Grabs the ni data from the ni structure and fills the out
2847 * \param[in] ni network interface structure
2848 * \param[out] config config information
2851 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2852 struct lnet_ioctl_config_data *config)
2854 struct lnet_ioctl_net_config *net_config;
2855 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2856 size_t min_size, tunable_size = 0;
2862 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2866 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2867 ARRAY_SIZE(net_config->ni_interfaces));
2869 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2870 if (!ni->ni_interfaces[i])
2873 strncpy(net_config->ni_interfaces[i],
2874 ni->ni_interfaces[i],
2875 sizeof(net_config->ni_interfaces[i]));
2878 config->cfg_nid = ni->ni_nid;
2879 config->cfg_config_u.cfg_net.net_peer_timeout =
2880 ni->ni_net->net_tunables.lct_peer_timeout;
2881 config->cfg_config_u.cfg_net.net_max_tx_credits =
2882 ni->ni_net->net_tunables.lct_max_tx_credits;
2883 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2884 ni->ni_net->net_tunables.lct_peer_tx_credits;
2885 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2886 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2888 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2889 net_config->ni_status = LNET_NI_STATUS_UP;
2891 net_config->ni_status = ni->ni_status->ns_status;
2894 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2896 for (i = 0; i < num_cpts; i++)
2897 net_config->ni_cpts[i] = ni->ni_cpts[i];
2899 config->cfg_ncpts = num_cpts;
2903 * See if user land tools sent in a newer and larger version
2904 * of struct lnet_tunables than what the kernel uses.
2906 min_size = sizeof(*config) + sizeof(*net_config);
2908 if (config->cfg_hdr.ioc_len > min_size)
2909 tunable_size = config->cfg_hdr.ioc_len - min_size;
2911 /* Don't copy too much data to user space */
2912 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2913 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2915 if (lnd_cfg && min_size) {
2916 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2917 config->cfg_config_u.cfg_net.net_interface_count = 1;
2919 /* Tell user land that kernel side has less data */
2920 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2921 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2922 config->cfg_hdr.ioc_len -= min_size;
2928 lnet_get_ni_idx_locked(int idx)
2931 struct lnet_net *net;
2933 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2934 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2944 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2947 struct lnet_net *net = mynet;
2950 * It is possible that the net has been cleaned out while there is
2951 * a message being sent. This function accessed the net without
2952 * checking if the list is empty
2956 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2958 if (list_empty(&net->net_ni_list))
2960 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2966 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2967 /* if you reached the end of the ni list and the net is
2968 * specified, then there are no more nis in that net */
2972 /* we reached the end of this net ni list. move to the
2974 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2975 /* no more nets and no more NIs. */
2978 /* get the next net */
2979 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2981 if (list_empty(&net->net_ni_list))
2983 /* get the ni on it */
2984 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2990 if (list_empty(&prev->ni_netlist))
2993 /* there are more nis left */
2994 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3000 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3005 int idx = config->cfg_count;
3007 cpt = lnet_net_lock_current();
3009 ni = lnet_get_ni_idx_locked(idx);
3014 lnet_fill_ni_info_legacy(ni, config);
3018 lnet_net_unlock(cpt);
3023 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3024 struct lnet_ioctl_config_lnd_tunables *tun,
3025 struct lnet_ioctl_element_stats *stats,
3032 if (!cfg_ni || !tun || !stats)
3035 cpt = lnet_net_lock_current();
3037 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3042 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3046 lnet_net_unlock(cpt);
3050 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3059 cpt = lnet_net_lock_current();
3061 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3064 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3068 lnet_net_unlock(cpt);
3073 static int lnet_add_net_common(struct lnet_net *net,
3074 struct lnet_ioctl_config_lnd_tunables *tun)
3077 struct lnet_ping_buffer *pbuf;
3078 struct lnet_handle_md ping_mdh;
3080 struct lnet_remotenet *rnet;
3082 int num_acceptor_nets;
3084 lnet_net_lock(LNET_LOCK_EX);
3085 rnet = lnet_find_rnet_locked(net->net_id);
3086 lnet_net_unlock(LNET_LOCK_EX);
3088 * make sure that the net added doesn't invalidate the current
3089 * configuration LNet is keeping
3092 CERROR("Adding net %s will invalidate routing configuration\n",
3093 libcfs_net2str(net->net_id));
3099 * make sure you calculate the correct number of slots in the ping
3100 * buffer. Since the ping info is a flattened list of all the NIs,
3101 * we should allocate enough slots to accomodate the number of NIs
3102 * which will be added.
3104 * since ni hasn't been configured yet, use
3105 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3107 net_ni_count = lnet_get_net_ni_count_pre(net);
3109 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3110 net_ni_count + lnet_get_ni_count(),
3118 memcpy(&net->net_tunables,
3119 &tun->lt_cmn, sizeof(net->net_tunables));
3121 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3124 * before starting this network get a count of the current TCP
3125 * networks which require the acceptor thread running. If that
3126 * count is == 0 before we start up this network, then we'd want to
3127 * start up the acceptor thread after starting up this network
3129 num_acceptor_nets = lnet_count_acceptor_nets();
3131 net_id = net->net_id;
3133 rc = lnet_startup_lndnet(net,
3134 (tun) ? &tun->lt_tun : NULL);
3138 lnet_net_lock(LNET_LOCK_EX);
3139 net = lnet_get_net_locked(net_id);
3140 lnet_net_unlock(LNET_LOCK_EX);
3145 * Start the acceptor thread if this is the first network
3146 * being added that requires the thread.
3148 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
3149 rc = lnet_acceptor_start();
3151 /* shutdown the net that we just started */
3152 CERROR("Failed to start up acceptor thread\n");
3153 lnet_shutdown_lndnet(net);
3158 lnet_net_lock(LNET_LOCK_EX);
3159 lnet_peer_net_added(net);
3160 lnet_net_unlock(LNET_LOCK_EX);
3162 lnet_ping_target_update(pbuf, ping_mdh);
3167 lnet_ping_md_unlink(pbuf, &ping_mdh);
3168 lnet_ping_buffer_decref(pbuf);
3172 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3173 struct lnet_ioctl_config_lnd_tunables *tun)
3175 struct lnet_net *net;
3178 LIST_HEAD(net_head);
3180 rc = lnet_parse_ip2nets(&nets, ip2nets);
3184 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3188 mutex_lock(&the_lnet.ln_api_mutex);
3189 while (!list_empty(&net_head)) {
3190 net = list_entry(net_head.next, struct lnet_net, net_list);
3191 list_del_init(&net->net_list);
3192 rc = lnet_add_net_common(net, tun);
3198 mutex_unlock(&the_lnet.ln_api_mutex);
3200 while (!list_empty(&net_head)) {
3201 net = list_entry(net_head.next, struct lnet_net, net_list);
3202 list_del_init(&net->net_list);
3208 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3210 struct lnet_net *net;
3212 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3214 __u32 net_id, lnd_type;
3216 /* get the tunables if they are available */
3217 if (conf->lic_cfg_hdr.ioc_len >=
3218 sizeof(*conf) + sizeof(*tun))
3219 tun = (struct lnet_ioctl_config_lnd_tunables *)
3222 /* handle legacy ip2nets from DLC */
3223 if (conf->lic_legacy_ip2nets[0] != '\0')
3224 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3227 net_id = LNET_NIDNET(conf->lic_nid);
3228 lnd_type = LNET_NETTYP(net_id);
3230 if (!libcfs_isknown_lnd(lnd_type)) {
3231 CERROR("No valid net and lnd information provided\n");
3235 net = lnet_net_alloc(net_id, NULL);
3239 for (i = 0; i < conf->lic_ncpts; i++) {
3240 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3244 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3245 conf->lic_ni_intf[0]);
3249 mutex_lock(&the_lnet.ln_api_mutex);
3251 rc = lnet_add_net_common(net, tun);
3253 mutex_unlock(&the_lnet.ln_api_mutex);
3258 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3260 struct lnet_net *net;
3262 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3263 struct lnet_ping_buffer *pbuf;
3264 struct lnet_handle_md ping_mdh;
3269 /* don't allow userspace to shutdown the LOLND */
3270 if (LNET_NETTYP(net_id) == LOLND)
3273 mutex_lock(&the_lnet.ln_api_mutex);
3277 net = lnet_get_net_locked(net_id);
3279 CERROR("net %s not found\n",
3280 libcfs_net2str(net_id));
3285 addr = LNET_NIDADDR(conf->lic_nid);
3287 /* remove the entire net */
3288 net_count = lnet_get_net_ni_count_locked(net);
3292 /* create and link a new ping info, before removing the old one */
3293 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3294 lnet_get_ni_count() - net_count,
3297 goto unlock_api_mutex;
3299 lnet_shutdown_lndnet(net);
3301 if (lnet_count_acceptor_nets() == 0)
3302 lnet_acceptor_stop();
3304 lnet_ping_target_update(pbuf, ping_mdh);
3306 goto unlock_api_mutex;
3309 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3311 CERROR("nid %s not found\n",
3312 libcfs_nid2str(conf->lic_nid));
3317 net_count = lnet_get_net_ni_count_locked(net);
3321 /* create and link a new ping info, before removing the old one */
3322 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3323 lnet_get_ni_count() - 1, false);
3325 goto unlock_api_mutex;
3327 lnet_shutdown_lndni(ni);
3329 if (lnet_count_acceptor_nets() == 0)
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 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 if (lnet_count_acceptor_nets() == 0)
3441 lnet_acceptor_stop();
3443 lnet_ping_target_update(pbuf, ping_mdh);
3446 mutex_unlock(&the_lnet.ln_api_mutex);
3451 void lnet_incr_dlc_seq(void)
3453 atomic_inc(&lnet_dlc_seq_no);
3456 __u32 lnet_get_dlc_seq_locked(void)
3458 return atomic_read(&lnet_dlc_seq_no);
3462 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3464 struct lnet_net *net;
3467 lnet_net_lock(LNET_LOCK_EX);
3468 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3469 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3470 if (ni->ni_nid == nid || all) {
3471 atomic_set(&ni->ni_healthv, value);
3472 if (list_empty(&ni->ni_recovery) &&
3473 value < LNET_MAX_HEALTH_VALUE) {
3474 CERROR("manually adding local NI %s to recovery\n",
3475 libcfs_nid2str(ni->ni_nid));
3476 list_add_tail(&ni->ni_recovery,
3477 &the_lnet.ln_mt_localNIRecovq);
3478 lnet_ni_addref_locked(ni, 0);
3481 lnet_net_unlock(LNET_LOCK_EX);
3487 lnet_net_unlock(LNET_LOCK_EX);
3491 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3495 lnet_nid_t nid = stats->hlni_nid;
3497 cpt = lnet_net_lock_current();
3498 ni = lnet_nid2ni_locked(nid, cpt);
3505 stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3506 stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3507 stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3508 stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3509 stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3510 stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3511 stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3514 lnet_net_unlock(cpt);
3520 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3525 lnet_net_lock(LNET_LOCK_EX);
3526 list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3527 list->rlst_nid_array[i] = ni->ni_nid;
3529 if (i >= LNET_MAX_SHOW_NUM_NID)
3532 lnet_net_unlock(LNET_LOCK_EX);
3533 list->rlst_num_nids = i;
3539 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3541 struct lnet_peer_ni *lpni;
3544 lnet_net_lock(LNET_LOCK_EX);
3545 list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3546 list->rlst_nid_array[i] = lpni->lpni_nid;
3548 if (i >= LNET_MAX_SHOW_NUM_NID)
3551 lnet_net_unlock(LNET_LOCK_EX);
3552 list->rlst_num_nids = i;
3558 * LNet ioctl handler.
3562 LNetCtl(unsigned int cmd, void *arg)
3564 struct libcfs_ioctl_data *data = arg;
3565 struct lnet_ioctl_config_data *config;
3566 struct lnet_process_id id = {0};
3570 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3571 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3574 case IOC_LIBCFS_GET_NI:
3575 rc = LNetGetId(data->ioc_count, &id);
3576 data->ioc_nid = id.nid;
3579 case IOC_LIBCFS_FAIL_NID:
3580 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3582 case IOC_LIBCFS_ADD_ROUTE: {
3583 /* default router sensitivity to 1 */
3584 unsigned int sensitivity = 1;
3587 if (config->cfg_hdr.ioc_len < sizeof(*config))
3590 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3592 config->cfg_config_u.cfg_route.rtr_sensitivity;
3595 mutex_lock(&the_lnet.ln_api_mutex);
3596 rc = lnet_add_route(config->cfg_net,
3597 config->cfg_config_u.cfg_route.rtr_hop,
3599 config->cfg_config_u.cfg_route.
3600 rtr_priority, sensitivity);
3601 mutex_unlock(&the_lnet.ln_api_mutex);
3605 case IOC_LIBCFS_DEL_ROUTE:
3608 if (config->cfg_hdr.ioc_len < sizeof(*config))
3611 mutex_lock(&the_lnet.ln_api_mutex);
3612 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3613 mutex_unlock(&the_lnet.ln_api_mutex);
3616 case IOC_LIBCFS_GET_ROUTE:
3619 if (config->cfg_hdr.ioc_len < sizeof(*config))
3622 mutex_lock(&the_lnet.ln_api_mutex);
3623 rc = lnet_get_route(config->cfg_count,
3625 &config->cfg_config_u.cfg_route.rtr_hop,
3627 &config->cfg_config_u.cfg_route.rtr_flags,
3628 &config->cfg_config_u.cfg_route.
3630 &config->cfg_config_u.cfg_route.
3632 mutex_unlock(&the_lnet.ln_api_mutex);
3635 case IOC_LIBCFS_GET_LOCAL_NI: {
3636 struct lnet_ioctl_config_ni *cfg_ni;
3637 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3638 struct lnet_ioctl_element_stats *stats;
3643 /* get the tunables if they are available */
3644 if (cfg_ni->lic_cfg_hdr.ioc_len <
3645 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3648 stats = (struct lnet_ioctl_element_stats *)
3650 tun = (struct lnet_ioctl_config_lnd_tunables *)
3651 (cfg_ni->lic_bulk + sizeof(*stats));
3653 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3656 mutex_lock(&the_lnet.ln_api_mutex);
3657 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3658 mutex_unlock(&the_lnet.ln_api_mutex);
3662 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3663 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3665 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3668 mutex_lock(&the_lnet.ln_api_mutex);
3669 rc = lnet_get_ni_stats(msg_stats);
3670 mutex_unlock(&the_lnet.ln_api_mutex);
3675 case IOC_LIBCFS_GET_NET: {
3676 size_t total = sizeof(*config) +
3677 sizeof(struct lnet_ioctl_net_config);
3680 if (config->cfg_hdr.ioc_len < total)
3683 mutex_lock(&the_lnet.ln_api_mutex);
3684 rc = lnet_get_net_config(config);
3685 mutex_unlock(&the_lnet.ln_api_mutex);
3689 case IOC_LIBCFS_GET_LNET_STATS:
3691 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3693 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3696 mutex_lock(&the_lnet.ln_api_mutex);
3697 lnet_counters_get(&lnet_stats->st_cntrs);
3698 mutex_unlock(&the_lnet.ln_api_mutex);
3702 case IOC_LIBCFS_CONFIG_RTR:
3705 if (config->cfg_hdr.ioc_len < sizeof(*config))
3708 mutex_lock(&the_lnet.ln_api_mutex);
3709 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3710 rc = lnet_rtrpools_enable();
3711 mutex_unlock(&the_lnet.ln_api_mutex);
3714 lnet_rtrpools_disable();
3715 mutex_unlock(&the_lnet.ln_api_mutex);
3718 case IOC_LIBCFS_ADD_BUF:
3721 if (config->cfg_hdr.ioc_len < sizeof(*config))
3724 mutex_lock(&the_lnet.ln_api_mutex);
3725 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3727 config->cfg_config_u.cfg_buffers.
3729 config->cfg_config_u.cfg_buffers.
3731 mutex_unlock(&the_lnet.ln_api_mutex);
3734 case IOC_LIBCFS_SET_NUMA_RANGE: {
3735 struct lnet_ioctl_set_value *numa;
3737 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3739 lnet_net_lock(LNET_LOCK_EX);
3740 lnet_numa_range = numa->sv_value;
3741 lnet_net_unlock(LNET_LOCK_EX);
3745 case IOC_LIBCFS_GET_NUMA_RANGE: {
3746 struct lnet_ioctl_set_value *numa;
3748 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3750 numa->sv_value = lnet_numa_range;
3754 case IOC_LIBCFS_GET_BUF: {
3755 struct lnet_ioctl_pool_cfg *pool_cfg;
3756 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3760 if (config->cfg_hdr.ioc_len < total)
3763 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3765 mutex_lock(&the_lnet.ln_api_mutex);
3766 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3767 mutex_unlock(&the_lnet.ln_api_mutex);
3771 case IOC_LIBCFS_GET_LOCAL_HSTATS: {
3772 struct lnet_ioctl_local_ni_hstats *stats = arg;
3774 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
3777 mutex_lock(&the_lnet.ln_api_mutex);
3778 rc = lnet_get_local_ni_hstats(stats);
3779 mutex_unlock(&the_lnet.ln_api_mutex);
3784 case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
3785 struct lnet_ioctl_recovery_list *list = arg;
3786 if (list->rlst_hdr.ioc_len < sizeof(*list))
3789 mutex_lock(&the_lnet.ln_api_mutex);
3790 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
3791 rc = lnet_get_local_ni_recovery_list(list);
3793 rc = lnet_get_peer_ni_recovery_list(list);
3794 mutex_unlock(&the_lnet.ln_api_mutex);
3798 case IOC_LIBCFS_ADD_PEER_NI: {
3799 struct lnet_ioctl_peer_cfg *cfg = arg;
3801 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3804 mutex_lock(&the_lnet.ln_api_mutex);
3805 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3808 mutex_unlock(&the_lnet.ln_api_mutex);
3812 case IOC_LIBCFS_DEL_PEER_NI: {
3813 struct lnet_ioctl_peer_cfg *cfg = arg;
3815 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3818 mutex_lock(&the_lnet.ln_api_mutex);
3819 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3820 cfg->prcfg_cfg_nid);
3821 mutex_unlock(&the_lnet.ln_api_mutex);
3825 case IOC_LIBCFS_GET_PEER_INFO: {
3826 struct lnet_ioctl_peer *peer_info = arg;
3828 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3831 mutex_lock(&the_lnet.ln_api_mutex);
3832 rc = lnet_get_peer_ni_info(
3833 peer_info->pr_count,
3835 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3836 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3837 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3838 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3839 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3840 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3841 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3842 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3843 mutex_unlock(&the_lnet.ln_api_mutex);
3847 case IOC_LIBCFS_GET_PEER_NI: {
3848 struct lnet_ioctl_peer_cfg *cfg = arg;
3850 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3853 mutex_lock(&the_lnet.ln_api_mutex);
3854 rc = lnet_get_peer_info(cfg,
3855 (void __user *)cfg->prcfg_bulk);
3856 mutex_unlock(&the_lnet.ln_api_mutex);
3860 case IOC_LIBCFS_GET_PEER_LIST: {
3861 struct lnet_ioctl_peer_cfg *cfg = arg;
3863 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3866 mutex_lock(&the_lnet.ln_api_mutex);
3867 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3868 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3869 mutex_unlock(&the_lnet.ln_api_mutex);
3873 case IOC_LIBCFS_SET_HEALHV: {
3874 struct lnet_ioctl_reset_health_cfg *cfg = arg;
3876 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
3878 if (cfg->rh_value < 0 ||
3879 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
3880 value = LNET_MAX_HEALTH_VALUE;
3882 value = cfg->rh_value;
3883 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
3884 value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
3885 "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
3886 mutex_lock(&the_lnet.ln_api_mutex);
3887 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
3888 lnet_ni_set_healthv(cfg->rh_nid, value,
3891 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
3893 mutex_unlock(&the_lnet.ln_api_mutex);
3897 case IOC_LIBCFS_NOTIFY_ROUTER: {
3898 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3900 /* The deadline passed in by the user should be some time in
3901 * seconds in the future since the UNIX epoch. We have to map
3902 * that deadline to the wall clock.
3904 deadline += ktime_get_seconds();
3905 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
3909 case IOC_LIBCFS_LNET_DIST:
3910 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3911 if (rc < 0 && rc != -EHOSTUNREACH)
3914 data->ioc_u32[0] = rc;
3917 case IOC_LIBCFS_TESTPROTOCOMPAT:
3918 the_lnet.ln_testprotocompat = data->ioc_flags;
3921 case IOC_LIBCFS_LNET_FAULT:
3922 return lnet_fault_ctl(data->ioc_flags, data);
3924 case IOC_LIBCFS_PING: {
3925 signed long timeout;
3927 id.nid = data->ioc_nid;
3928 id.pid = data->ioc_u32[0];
3930 /* If timeout is negative then set default of 3 minutes */
3931 if (((s32)data->ioc_u32[1] <= 0) ||
3932 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3933 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3935 timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
3937 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3938 data->ioc_plen1 / sizeof(struct lnet_process_id));
3943 data->ioc_count = rc;
3947 case IOC_LIBCFS_PING_PEER: {
3948 struct lnet_ioctl_ping_data *ping = arg;
3949 struct lnet_peer *lp;
3950 signed long timeout;
3952 /* If timeout is negative then set default of 3 minutes */
3953 if (((s32)ping->op_param) <= 0 ||
3954 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3955 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3957 timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
3959 rc = lnet_ping(ping->ping_id, timeout,
3965 mutex_lock(&the_lnet.ln_api_mutex);
3966 lp = lnet_find_peer(ping->ping_id.nid);
3968 ping->ping_id.nid = lp->lp_primary_nid;
3969 ping->mr_info = lnet_peer_is_multi_rail(lp);
3970 lnet_peer_decref_locked(lp);
3972 mutex_unlock(&the_lnet.ln_api_mutex);
3974 ping->ping_count = rc;
3978 case IOC_LIBCFS_DISCOVER: {
3979 struct lnet_ioctl_ping_data *discover = arg;
3980 struct lnet_peer *lp;
3982 rc = lnet_discover(discover->ping_id, discover->op_param,
3984 discover->ping_count);
3988 mutex_lock(&the_lnet.ln_api_mutex);
3989 lp = lnet_find_peer(discover->ping_id.nid);
3991 discover->ping_id.nid = lp->lp_primary_nid;
3992 discover->mr_info = lnet_peer_is_multi_rail(lp);
3993 lnet_peer_decref_locked(lp);
3995 mutex_unlock(&the_lnet.ln_api_mutex);
3997 discover->ping_count = rc;
4002 ni = lnet_net2ni_addref(data->ioc_net);
4006 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
4009 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4016 EXPORT_SYMBOL(LNetCtl);
4018 void LNetDebugPeer(struct lnet_process_id id)
4020 lnet_debug_peer(id.nid);
4022 EXPORT_SYMBOL(LNetDebugPeer);
4025 * Determine if the specified peer \a nid is on the local node.
4027 * \param nid peer nid to check
4029 * \retval true If peer NID is on the local node.
4030 * \retval false If peer NID is not on the local node.
4032 bool LNetIsPeerLocal(lnet_nid_t nid)
4034 struct lnet_net *net;
4038 cpt = lnet_net_lock_current();
4039 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4040 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4041 if (ni->ni_nid == nid) {
4042 lnet_net_unlock(cpt);
4047 lnet_net_unlock(cpt);
4051 EXPORT_SYMBOL(LNetIsPeerLocal);
4054 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4055 * Note that all interfaces share a same PID, as requested by LNetNIInit().
4057 * \param index Index of the interface to look up.
4058 * \param id On successful return, this location will hold the
4059 * struct lnet_process_id ID of the interface.
4061 * \retval 0 If an interface exists at \a index.
4062 * \retval -ENOENT If no interface has been found.
4065 LNetGetId(unsigned int index, struct lnet_process_id *id)
4068 struct lnet_net *net;
4072 LASSERT(the_lnet.ln_refcount > 0);
4074 cpt = lnet_net_lock_current();
4076 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4077 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4081 id->nid = ni->ni_nid;
4082 id->pid = the_lnet.ln_pid;
4088 lnet_net_unlock(cpt);
4091 EXPORT_SYMBOL(LNetGetId);
4093 static int lnet_ping(struct lnet_process_id id, signed long timeout,
4094 struct lnet_process_id __user *ids, int n_ids)
4096 struct lnet_handle_eq eqh;
4097 struct lnet_handle_md mdh;
4098 struct lnet_event event;
4099 struct lnet_md md = { NULL };
4103 const signed long a_long_time = cfs_time_seconds(60);
4104 struct lnet_ping_buffer *pbuf;
4105 struct lnet_process_id tmpid;
4112 /* n_ids limit is arbitrary */
4113 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4117 * if the user buffer has more space than the lnet_interfaces_max
4118 * then only fill it up to lnet_interfaces_max
4120 if (n_ids > lnet_interfaces_max)
4121 n_ids = lnet_interfaces_max;
4123 if (id.pid == LNET_PID_ANY)
4124 id.pid = LNET_PID_LUSTRE;
4126 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4130 /* NB 2 events max (including any unlink event) */
4131 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
4133 CERROR("Can't allocate EQ: %d\n", rc);
4134 goto fail_ping_buffer_decref;
4137 /* initialize md content */
4138 md.start = &pbuf->pb_info;
4139 md.length = LNET_PING_INFO_SIZE(n_ids);
4140 md.threshold = 2; /* GET/REPLY */
4142 md.options = LNET_MD_TRUNCATE;
4146 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
4148 CERROR("Can't bind MD: %d\n", rc);
4152 rc = LNetGet(LNET_NID_ANY, mdh, id,
4153 LNET_RESERVED_PORTAL,
4154 LNET_PROTO_PING_MATCHBITS, 0, false);
4157 /* Don't CERROR; this could be deliberate! */
4158 rc2 = LNetMDUnlink(mdh);
4161 /* NB must wait for the UNLINK event below... */
4163 timeout = a_long_time;
4167 /* MUST block for unlink to complete */
4169 blocked = cfs_block_allsigs();
4171 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
4174 cfs_restore_sigs(blocked);
4176 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
4177 (rc2 <= 0) ? -1 : event.type,
4178 (rc2 <= 0) ? -1 : event.status,
4179 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
4181 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
4183 if (rc2 <= 0 || event.status != 0) {
4184 /* timeout or error */
4185 if (!replied && rc == 0)
4186 rc = (rc2 < 0) ? rc2 :
4187 (rc2 == 0) ? -ETIMEDOUT :
4191 /* Ensure completion in finite time... */
4193 /* No assertion (racing with network) */
4195 timeout = a_long_time;
4196 } else if (rc2 == 0) {
4197 /* timed out waiting for unlink */
4198 CWARN("ping %s: late network completion\n",
4201 } else if (event.type == LNET_EVENT_REPLY) {
4205 } while (rc2 <= 0 || !event.unlinked);
4209 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
4216 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4218 rc = -EPROTO; /* if I can't parse... */
4221 CERROR("%s: ping info too short %d\n",
4222 libcfs_id2str(id), nob);
4226 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4227 lnet_swap_pinginfo(pbuf);
4228 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4229 CERROR("%s: Unexpected magic %08x\n",
4230 libcfs_id2str(id), pbuf->pb_info.pi_magic);
4234 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4235 CERROR("%s: ping w/o NI status: 0x%x\n",
4236 libcfs_id2str(id), pbuf->pb_info.pi_features);
4240 if (nob < LNET_PING_INFO_SIZE(0)) {
4241 CERROR("%s: Short reply %d(%d min)\n",
4243 nob, (int)LNET_PING_INFO_SIZE(0));
4247 if (pbuf->pb_info.pi_nnis < n_ids)
4248 n_ids = pbuf->pb_info.pi_nnis;
4250 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4251 CERROR("%s: Short reply %d(%d expected)\n",
4253 nob, (int)LNET_PING_INFO_SIZE(n_ids));
4257 rc = -EFAULT; /* if I segv in copy_to_user()... */
4259 memset(&tmpid, 0, sizeof(tmpid));
4260 for (i = 0; i < n_ids; i++) {
4261 tmpid.pid = pbuf->pb_info.pi_pid;
4262 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4263 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4266 rc = pbuf->pb_info.pi_nnis;
4269 rc2 = LNetEQFree(eqh);
4271 CERROR("rc2 %d\n", rc2);
4274 fail_ping_buffer_decref:
4275 lnet_ping_buffer_decref(pbuf);
4280 lnet_discover(struct lnet_process_id id, __u32 force,
4281 struct lnet_process_id __user *ids, int n_ids)
4283 struct lnet_peer_ni *lpni;
4284 struct lnet_peer_ni *p;
4285 struct lnet_peer *lp;
4286 struct lnet_process_id *buf;
4290 int max_intf = lnet_interfaces_max;
4294 id.nid == LNET_NID_ANY)
4297 if (id.pid == LNET_PID_ANY)
4298 id.pid = LNET_PID_LUSTRE;
4301 * if the user buffer has more space than the max_intf
4302 * then only fill it up to max_intf
4304 if (n_ids > max_intf)
4307 buf_size = n_ids * sizeof(*buf);
4309 LIBCFS_ALLOC(buf, buf_size);
4313 cpt = lnet_net_lock_current();
4314 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4321 * Clearing the NIDS_UPTODATE flag ensures the peer will
4322 * be discovered, provided discovery has not been disabled.
4324 lp = lpni->lpni_peer_net->lpn_peer;
4325 spin_lock(&lp->lp_lock);
4326 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4327 /* If the force flag is set, force a PING and PUSH as well. */
4329 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4330 spin_unlock(&lp->lp_lock);
4331 rc = lnet_discover_peer_locked(lpni, cpt, true);
4335 /* Peer may have changed. */
4336 lp = lpni->lpni_peer_net->lpn_peer;
4337 if (lp->lp_nnis < n_ids)
4338 n_ids = lp->lp_nnis;
4342 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4343 buf[i].pid = id.pid;
4344 buf[i].nid = p->lpni_nid;
4349 lnet_net_unlock(cpt);
4352 if (copy_to_user(ids, buf, n_ids * sizeof(*buf)))
4358 lnet_peer_ni_decref_locked(lpni);
4360 lnet_net_unlock(cpt);
4362 LIBCFS_FREE(buf, buf_size);
4368 * Retrieve peer discovery status.
4370 * \retval 1 if lnet_peer_discovery_disabled is 0
4371 * \retval 0 if lnet_peer_discovery_disabled is 1
4374 LNetGetPeerDiscoveryStatus(void)
4376 return !lnet_peer_discovery_disabled;
4378 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);