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);
554 lnet_fini_locks(void)
558 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
559 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
563 lnet_descriptor_setup(void)
565 /* create specific kmem_cache for MEs and small MDs (i.e., originally
566 * allocated in <size-xxx> kmem_cache).
568 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
570 if (!lnet_mes_cachep)
573 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
574 LNET_SMALL_MD_SIZE, 0, 0,
576 if (!lnet_small_mds_cachep)
583 lnet_descriptor_cleanup(void)
586 if (lnet_small_mds_cachep) {
587 kmem_cache_destroy(lnet_small_mds_cachep);
588 lnet_small_mds_cachep = NULL;
591 if (lnet_mes_cachep) {
592 kmem_cache_destroy(lnet_mes_cachep);
593 lnet_mes_cachep = NULL;
598 lnet_create_remote_nets_table(void)
601 struct list_head *hash;
603 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
604 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
605 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
607 CERROR("Failed to create remote nets hash table\n");
611 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
612 INIT_LIST_HEAD(&hash[i]);
613 the_lnet.ln_remote_nets_hash = hash;
618 lnet_destroy_remote_nets_table(void)
622 if (the_lnet.ln_remote_nets_hash == NULL)
625 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
626 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
628 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
629 LNET_REMOTE_NETS_HASH_SIZE *
630 sizeof(the_lnet.ln_remote_nets_hash[0]));
631 the_lnet.ln_remote_nets_hash = NULL;
635 lnet_destroy_locks(void)
637 if (the_lnet.ln_res_lock != NULL) {
638 cfs_percpt_lock_free(the_lnet.ln_res_lock);
639 the_lnet.ln_res_lock = NULL;
642 if (the_lnet.ln_net_lock != NULL) {
643 cfs_percpt_lock_free(the_lnet.ln_net_lock);
644 the_lnet.ln_net_lock = NULL;
651 lnet_create_locks(void)
655 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
656 if (the_lnet.ln_res_lock == NULL)
659 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
660 if (the_lnet.ln_net_lock == NULL)
666 lnet_destroy_locks();
670 static void lnet_assert_wire_constants(void)
672 /* Wire protocol assertions generated by 'wirecheck'
673 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
674 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
675 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
678 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
679 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
680 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
681 CLASSERT(LNET_MSG_ACK == 0);
682 CLASSERT(LNET_MSG_PUT == 1);
683 CLASSERT(LNET_MSG_GET == 2);
684 CLASSERT(LNET_MSG_REPLY == 3);
685 CLASSERT(LNET_MSG_HELLO == 4);
687 /* Checks for struct lnet_handle_wire */
688 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
689 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
690 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
691 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
692 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
694 /* Checks for struct struct lnet_magicversion */
695 CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
696 CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
697 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
698 CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
699 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
700 CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
701 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
703 /* Checks for struct struct lnet_hdr */
704 CLASSERT((int)sizeof(struct lnet_hdr) == 72);
705 CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
706 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
707 CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
708 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
709 CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
710 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
711 CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
712 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
713 CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
714 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
715 CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
716 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
717 CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
718 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
721 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
722 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
723 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
724 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
725 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
726 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
729 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
730 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
731 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
732 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
733 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
734 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
735 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
736 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
737 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
738 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
741 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
742 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
743 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
744 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
745 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
746 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
747 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
748 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
749 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
750 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
753 CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
754 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
757 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
758 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
759 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
760 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
762 /* Checks for struct lnet_ni_status and related constants */
763 CLASSERT(LNET_NI_STATUS_INVALID == 0x00000000);
764 CLASSERT(LNET_NI_STATUS_UP == 0x15aac0de);
765 CLASSERT(LNET_NI_STATUS_DOWN == 0xdeadface);
767 /* Checks for struct lnet_ni_status */
768 CLASSERT((int)sizeof(struct lnet_ni_status) == 16);
769 CLASSERT((int)offsetof(struct lnet_ni_status, ns_nid) == 0);
770 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) == 8);
771 CLASSERT((int)offsetof(struct lnet_ni_status, ns_status) == 8);
772 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_status) == 4);
773 CLASSERT((int)offsetof(struct lnet_ni_status, ns_unused) == 12);
774 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) == 4);
776 /* Checks for struct lnet_ping_info and related constants */
777 CLASSERT(LNET_PROTO_PING_MAGIC == 0x70696E67);
778 CLASSERT(LNET_PING_FEAT_INVAL == 0);
779 CLASSERT(LNET_PING_FEAT_BASE == 1);
780 CLASSERT(LNET_PING_FEAT_NI_STATUS == 2);
781 CLASSERT(LNET_PING_FEAT_RTE_DISABLED == 4);
782 CLASSERT(LNET_PING_FEAT_MULTI_RAIL == 8);
783 CLASSERT(LNET_PING_FEAT_DISCOVERY == 16);
784 CLASSERT(LNET_PING_FEAT_BITS == 31);
786 /* Checks for struct lnet_ping_info */
787 CLASSERT((int)sizeof(struct lnet_ping_info) == 16);
788 CLASSERT((int)offsetof(struct lnet_ping_info, pi_magic) == 0);
789 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) == 4);
790 CLASSERT((int)offsetof(struct lnet_ping_info, pi_features) == 4);
791 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_features) == 4);
792 CLASSERT((int)offsetof(struct lnet_ping_info, pi_pid) == 8);
793 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) == 4);
794 CLASSERT((int)offsetof(struct lnet_ping_info, pi_nnis) == 12);
795 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) == 4);
796 CLASSERT((int)offsetof(struct lnet_ping_info, pi_ni) == 16);
797 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) == 0);
800 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
802 struct lnet_lnd *lnd;
803 struct list_head *tmp;
805 /* holding lnd mutex */
806 list_for_each(tmp, &the_lnet.ln_lnds) {
807 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
809 if (lnd->lnd_type == type)
816 lnet_get_lnd_timeout(void)
818 return lnet_lnd_timeout;
820 EXPORT_SYMBOL(lnet_get_lnd_timeout);
823 lnet_register_lnd(struct lnet_lnd *lnd)
825 mutex_lock(&the_lnet.ln_lnd_mutex);
827 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
828 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
830 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
831 lnd->lnd_refcount = 0;
833 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
835 mutex_unlock(&the_lnet.ln_lnd_mutex);
837 EXPORT_SYMBOL(lnet_register_lnd);
840 lnet_unregister_lnd(struct lnet_lnd *lnd)
842 mutex_lock(&the_lnet.ln_lnd_mutex);
844 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
845 LASSERT(lnd->lnd_refcount == 0);
847 list_del(&lnd->lnd_list);
848 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
850 mutex_unlock(&the_lnet.ln_lnd_mutex);
852 EXPORT_SYMBOL(lnet_unregister_lnd);
855 lnet_counters_get_common(struct lnet_counters_common *common)
857 struct lnet_counters *ctr;
860 memset(common, 0, sizeof(*common));
862 lnet_net_lock(LNET_LOCK_EX);
864 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
865 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
866 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
867 common->lcc_errors += ctr->lct_common.lcc_errors;
868 common->lcc_send_count += ctr->lct_common.lcc_send_count;
869 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
870 common->lcc_route_count += ctr->lct_common.lcc_route_count;
871 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
872 common->lcc_send_length += ctr->lct_common.lcc_send_length;
873 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
874 common->lcc_route_length += ctr->lct_common.lcc_route_length;
875 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
877 lnet_net_unlock(LNET_LOCK_EX);
879 EXPORT_SYMBOL(lnet_counters_get_common);
882 lnet_counters_get(struct lnet_counters *counters)
884 struct lnet_counters *ctr;
885 struct lnet_counters_health *health = &counters->lct_health;
888 memset(counters, 0, sizeof(*counters));
890 lnet_counters_get_common(&counters->lct_common);
892 lnet_net_lock(LNET_LOCK_EX);
894 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
895 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
896 health->lch_resend_count += ctr->lct_health.lch_resend_count;
897 health->lch_response_timeout_count +=
898 ctr->lct_health.lch_response_timeout_count;
899 health->lch_local_interrupt_count +=
900 ctr->lct_health.lch_local_interrupt_count;
901 health->lch_local_dropped_count +=
902 ctr->lct_health.lch_local_dropped_count;
903 health->lch_local_aborted_count +=
904 ctr->lct_health.lch_local_aborted_count;
905 health->lch_local_no_route_count +=
906 ctr->lct_health.lch_local_no_route_count;
907 health->lch_local_timeout_count +=
908 ctr->lct_health.lch_local_timeout_count;
909 health->lch_local_error_count +=
910 ctr->lct_health.lch_local_error_count;
911 health->lch_remote_dropped_count +=
912 ctr->lct_health.lch_remote_dropped_count;
913 health->lch_remote_error_count +=
914 ctr->lct_health.lch_remote_error_count;
915 health->lch_remote_timeout_count +=
916 ctr->lct_health.lch_remote_timeout_count;
917 health->lch_network_timeout_count +=
918 ctr->lct_health.lch_network_timeout_count;
920 lnet_net_unlock(LNET_LOCK_EX);
922 EXPORT_SYMBOL(lnet_counters_get);
925 lnet_counters_reset(void)
927 struct lnet_counters *counters;
930 lnet_net_lock(LNET_LOCK_EX);
932 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
933 memset(counters, 0, sizeof(struct lnet_counters));
935 lnet_net_unlock(LNET_LOCK_EX);
939 lnet_res_type2str(int type)
944 case LNET_COOKIE_TYPE_MD:
946 case LNET_COOKIE_TYPE_ME:
948 case LNET_COOKIE_TYPE_EQ:
954 lnet_res_container_cleanup(struct lnet_res_container *rec)
958 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
961 while (!list_empty(&rec->rec_active)) {
962 struct list_head *e = rec->rec_active.next;
965 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
966 lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
968 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
969 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
971 } else { /* NB: Active MEs should be attached on portals */
978 /* Found alive MD/ME/EQ, user really should unlink/free
979 * all of them before finalize LNet, but if someone didn't,
980 * we have to recycle garbage for him */
981 CERROR("%d active elements on exit of %s container\n",
982 count, lnet_res_type2str(rec->rec_type));
985 if (rec->rec_lh_hash != NULL) {
986 LIBCFS_FREE(rec->rec_lh_hash,
987 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
988 rec->rec_lh_hash = NULL;
991 rec->rec_type = 0; /* mark it as finalized */
995 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1000 LASSERT(rec->rec_type == 0);
1002 rec->rec_type = type;
1003 INIT_LIST_HEAD(&rec->rec_active);
1005 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1007 /* Arbitrary choice of hash table size */
1008 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1009 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1010 if (rec->rec_lh_hash == NULL) {
1015 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1016 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1021 CERROR("Failed to setup %s resource container\n",
1022 lnet_res_type2str(type));
1023 lnet_res_container_cleanup(rec);
1028 lnet_res_containers_destroy(struct lnet_res_container **recs)
1030 struct lnet_res_container *rec;
1033 cfs_percpt_for_each(rec, i, recs)
1034 lnet_res_container_cleanup(rec);
1036 cfs_percpt_free(recs);
1039 static struct lnet_res_container **
1040 lnet_res_containers_create(int type)
1042 struct lnet_res_container **recs;
1043 struct lnet_res_container *rec;
1047 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1049 CERROR("Failed to allocate %s resource containers\n",
1050 lnet_res_type2str(type));
1054 cfs_percpt_for_each(rec, i, recs) {
1055 rc = lnet_res_container_setup(rec, i, type);
1057 lnet_res_containers_destroy(recs);
1065 struct lnet_libhandle *
1066 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1068 /* ALWAYS called with lnet_res_lock held */
1069 struct list_head *head;
1070 struct lnet_libhandle *lh;
1073 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1076 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1077 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1079 list_for_each_entry(lh, head, lh_hash_chain) {
1080 if (lh->lh_cookie == cookie)
1088 lnet_res_lh_initialize(struct lnet_res_container *rec,
1089 struct lnet_libhandle *lh)
1091 /* ALWAYS called with lnet_res_lock held */
1092 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1095 lh->lh_cookie = rec->rec_lh_cookie;
1096 rec->rec_lh_cookie += 1 << ibits;
1098 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1100 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1104 lnet_create_array_of_queues(void)
1106 struct list_head **qs;
1107 struct list_head *q;
1110 qs = cfs_percpt_alloc(lnet_cpt_table(),
1111 sizeof(struct list_head));
1113 CERROR("Failed to allocate queues\n");
1117 cfs_percpt_for_each(q, i, qs)
1123 static int lnet_unprepare(void);
1126 lnet_prepare(lnet_pid_t requested_pid)
1128 /* Prepare to bring up the network */
1129 struct lnet_res_container **recs;
1132 if (requested_pid == LNET_PID_ANY) {
1133 /* Don't instantiate LNET just for me */
1137 LASSERT(the_lnet.ln_refcount == 0);
1139 the_lnet.ln_routing = 0;
1141 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1142 the_lnet.ln_pid = requested_pid;
1144 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1145 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1146 INIT_LIST_HEAD(&the_lnet.ln_nets);
1147 INIT_LIST_HEAD(&the_lnet.ln_routers);
1148 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1149 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1150 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1151 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1152 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1153 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1154 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1155 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1156 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
1157 init_completion(&the_lnet.ln_started);
1159 rc = lnet_descriptor_setup();
1163 rc = lnet_create_remote_nets_table();
1168 * NB the interface cookie in wire handles guards against delayed
1169 * replies and ACKs appearing valid after reboot.
1171 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1173 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1174 sizeof(struct lnet_counters));
1175 if (the_lnet.ln_counters == NULL) {
1176 CERROR("Failed to allocate counters for LNet\n");
1181 rc = lnet_peer_tables_create();
1185 rc = lnet_msg_containers_create();
1189 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1190 LNET_COOKIE_TYPE_EQ);
1194 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
1200 the_lnet.ln_me_containers = recs;
1202 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1208 the_lnet.ln_md_containers = recs;
1210 rc = lnet_portals_create();
1212 CERROR("Failed to create portals for LNet: %d\n", rc);
1216 the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1217 if (!the_lnet.ln_mt_zombie_rstqs) {
1230 lnet_unprepare (void)
1234 /* NB no LNET_LOCK since this is the last reference. All LND instances
1235 * have shut down already, so it is safe to unlink and free all
1236 * descriptors, even those that appear committed to a network op (eg MD
1237 * with non-zero pending count) */
1239 lnet_fail_nid(LNET_NID_ANY, 0);
1241 LASSERT(the_lnet.ln_refcount == 0);
1242 LASSERT(list_empty(&the_lnet.ln_test_peers));
1243 LASSERT(list_empty(&the_lnet.ln_nets));
1245 if (the_lnet.ln_mt_zombie_rstqs) {
1246 lnet_clean_zombie_rstqs();
1247 the_lnet.ln_mt_zombie_rstqs = NULL;
1250 if (!LNetEQHandleIsInvalid(the_lnet.ln_mt_eqh)) {
1251 rc = LNetEQFree(the_lnet.ln_mt_eqh);
1252 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
1256 lnet_portals_destroy();
1258 if (the_lnet.ln_md_containers != NULL) {
1259 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1260 the_lnet.ln_md_containers = NULL;
1263 if (the_lnet.ln_me_containers != NULL) {
1264 lnet_res_containers_destroy(the_lnet.ln_me_containers);
1265 the_lnet.ln_me_containers = NULL;
1268 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1270 lnet_msg_containers_destroy();
1272 lnet_rtrpools_free(0);
1274 if (the_lnet.ln_counters != NULL) {
1275 cfs_percpt_free(the_lnet.ln_counters);
1276 the_lnet.ln_counters = NULL;
1278 lnet_destroy_remote_nets_table();
1279 lnet_descriptor_cleanup();
1285 lnet_net2ni_locked(__u32 net_id, int cpt)
1288 struct lnet_net *net;
1290 LASSERT(cpt != LNET_LOCK_EX);
1292 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1293 if (net->net_id == net_id) {
1294 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1304 lnet_net2ni_addref(__u32 net)
1309 ni = lnet_net2ni_locked(net, 0);
1311 lnet_ni_addref_locked(ni, 0);
1316 EXPORT_SYMBOL(lnet_net2ni_addref);
1319 lnet_get_net_locked(__u32 net_id)
1321 struct lnet_net *net;
1323 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1324 if (net->net_id == net_id)
1332 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1337 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1342 val = hash_long(key, LNET_CPT_BITS);
1343 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1347 return (unsigned int)(key + val + (val >> 1)) % number;
1351 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1353 struct lnet_net *net;
1355 /* must called with hold of lnet_net_lock */
1356 if (LNET_CPT_NUMBER == 1)
1357 return 0; /* the only one */
1360 * If NI is provided then use the CPT identified in the NI cpt
1361 * list if one exists. If one doesn't exist, then that NI is
1362 * associated with all CPTs and it follows that the net it belongs
1363 * to is implicitly associated with all CPTs, so just hash the nid
1367 if (ni->ni_cpts != NULL)
1368 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1371 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1374 /* no NI provided so look at the net */
1375 net = lnet_get_net_locked(LNET_NIDNET(nid));
1377 if (net != NULL && net->net_cpts != NULL) {
1378 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1381 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1385 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1390 if (LNET_CPT_NUMBER == 1)
1391 return 0; /* the only one */
1393 cpt = lnet_net_lock_current();
1395 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1397 lnet_net_unlock(cpt);
1401 EXPORT_SYMBOL(lnet_cpt_of_nid);
1404 lnet_islocalnet_locked(__u32 net_id)
1406 struct lnet_net *net;
1409 net = lnet_get_net_locked(net_id);
1411 local = net != NULL;
1417 lnet_islocalnet(__u32 net_id)
1422 cpt = lnet_net_lock_current();
1424 local = lnet_islocalnet_locked(net_id);
1426 lnet_net_unlock(cpt);
1432 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1434 struct lnet_net *net;
1437 LASSERT(cpt != LNET_LOCK_EX);
1439 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1440 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1441 if (ni->ni_nid == nid)
1450 lnet_nid2ni_addref(lnet_nid_t nid)
1455 ni = lnet_nid2ni_locked(nid, 0);
1457 lnet_ni_addref_locked(ni, 0);
1462 EXPORT_SYMBOL(lnet_nid2ni_addref);
1465 lnet_islocalnid(lnet_nid_t nid)
1470 cpt = lnet_net_lock_current();
1471 ni = lnet_nid2ni_locked(nid, cpt);
1472 lnet_net_unlock(cpt);
1478 lnet_count_acceptor_nets(void)
1480 /* Return the # of NIs that need the acceptor. */
1482 struct lnet_net *net;
1485 cpt = lnet_net_lock_current();
1486 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1487 /* all socklnd type networks should have the acceptor
1489 if (net->net_lnd->lnd_accept != NULL)
1493 lnet_net_unlock(cpt);
1498 struct lnet_ping_buffer *
1499 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1501 struct lnet_ping_buffer *pbuf;
1503 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1505 pbuf->pb_nnis = nnis;
1506 atomic_set(&pbuf->pb_refcnt, 1);
1513 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1515 LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1516 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1519 static struct lnet_ping_buffer *
1520 lnet_ping_target_create(int nnis)
1522 struct lnet_ping_buffer *pbuf;
1524 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1526 CERROR("Can't allocate ping source [%d]\n", nnis);
1530 pbuf->pb_info.pi_nnis = nnis;
1531 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1532 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1533 pbuf->pb_info.pi_features =
1534 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1540 lnet_get_net_ni_count_locked(struct lnet_net *net)
1545 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1552 lnet_get_net_ni_count_pre(struct lnet_net *net)
1557 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1564 lnet_get_ni_count(void)
1567 struct lnet_net *net;
1572 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1573 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1583 lnet_get_net_count(void)
1585 struct lnet_net *net;
1590 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1600 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1602 struct lnet_ni_status *stat;
1606 __swab32s(&pbuf->pb_info.pi_magic);
1607 __swab32s(&pbuf->pb_info.pi_features);
1608 __swab32s(&pbuf->pb_info.pi_pid);
1609 __swab32s(&pbuf->pb_info.pi_nnis);
1610 nnis = pbuf->pb_info.pi_nnis;
1611 if (nnis > pbuf->pb_nnis)
1612 nnis = pbuf->pb_nnis;
1613 for (i = 0; i < nnis; i++) {
1614 stat = &pbuf->pb_info.pi_ni[i];
1615 __swab64s(&stat->ns_nid);
1616 __swab32s(&stat->ns_status);
1621 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1625 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1627 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1629 /* Loopback is guaranteed to be present */
1630 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1632 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1638 lnet_ping_target_destroy(void)
1640 struct lnet_net *net;
1643 lnet_net_lock(LNET_LOCK_EX);
1645 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1646 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1648 ni->ni_status = NULL;
1653 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1654 the_lnet.ln_ping_target = NULL;
1656 lnet_net_unlock(LNET_LOCK_EX);
1660 lnet_ping_target_event_handler(struct lnet_event *event)
1662 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1664 if (event->unlinked)
1665 lnet_ping_buffer_decref(pbuf);
1669 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1670 struct lnet_handle_md *ping_mdh,
1671 int ni_count, bool set_eq)
1673 struct lnet_process_id id = {
1674 .nid = LNET_NID_ANY,
1677 struct lnet_handle_me me_handle;
1678 struct lnet_md md = { NULL };
1682 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1683 &the_lnet.ln_ping_target_eq);
1685 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1690 *ppbuf = lnet_ping_target_create(ni_count);
1691 if (*ppbuf == NULL) {
1696 /* Ping target ME/MD */
1697 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1698 LNET_PROTO_PING_MATCHBITS, 0,
1699 LNET_UNLINK, LNET_INS_AFTER,
1702 CERROR("Can't create ping target ME: %d\n", rc);
1703 goto fail_decref_ping_buffer;
1706 /* initialize md content */
1707 md.start = &(*ppbuf)->pb_info;
1708 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1709 md.threshold = LNET_MD_THRESH_INF;
1711 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1712 LNET_MD_MANAGE_REMOTE;
1713 md.eq_handle = the_lnet.ln_ping_target_eq;
1714 md.user_ptr = *ppbuf;
1716 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, ping_mdh);
1718 CERROR("Can't attach ping target MD: %d\n", rc);
1719 goto fail_unlink_ping_me;
1721 lnet_ping_buffer_addref(*ppbuf);
1725 fail_unlink_ping_me:
1726 rc2 = LNetMEUnlink(me_handle);
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 };
1859 struct lnet_handle_me meh;
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 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1879 LNET_PROTO_PING_MATCHBITS, 0,
1880 LNET_UNLINK, LNET_INS_AFTER,
1883 CERROR("Can't create push target ME: %d\n", rc);
1884 goto fail_decref_pbuf;
1887 /* initialize md content */
1888 md.start = &pbuf->pb_info;
1889 md.length = LNET_PING_INFO_SIZE(nnis);
1890 md.threshold = LNET_MD_THRESH_INF;
1892 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1893 LNET_MD_MANAGE_REMOTE;
1895 md.eq_handle = the_lnet.ln_push_target_eq;
1897 rc = LNetMDAttach(meh, md, LNET_RETAIN, &mdh);
1899 CERROR("Can't attach push MD: %d\n", rc);
1900 goto fail_unlink_meh;
1902 lnet_ping_buffer_addref(pbuf);
1904 lnet_net_lock(LNET_LOCK_EX);
1905 old_pbuf = the_lnet.ln_push_target;
1906 old_mdh = the_lnet.ln_push_target_md;
1907 the_lnet.ln_push_target = pbuf;
1908 the_lnet.ln_push_target_md = mdh;
1909 lnet_net_unlock(LNET_LOCK_EX);
1912 LNetMDUnlink(old_mdh);
1913 lnet_ping_buffer_decref(old_pbuf);
1916 if (nnis < the_lnet.ln_push_target_nnis)
1919 CDEBUG(D_NET, "nnis %d success\n", nnis);
1926 lnet_ping_buffer_decref(pbuf);
1928 CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1932 static void lnet_push_target_event_handler(struct lnet_event *ev)
1934 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1936 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1937 lnet_swap_pinginfo(pbuf);
1939 lnet_peer_push_event(ev);
1941 lnet_ping_buffer_decref(pbuf);
1944 /* Initialize the push target. */
1945 static int lnet_push_target_init(void)
1949 if (the_lnet.ln_push_target)
1952 rc = LNetEQAlloc(0, lnet_push_target_event_handler,
1953 &the_lnet.ln_push_target_eq);
1955 CERROR("Can't allocated push target EQ: %d\n", rc);
1959 /* Start at the required minimum, we'll enlarge if required. */
1960 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1962 rc = lnet_push_target_resize();
1965 LNetEQFree(the_lnet.ln_push_target_eq);
1966 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1972 /* Clean up the push target. */
1973 static void lnet_push_target_fini(void)
1975 if (!the_lnet.ln_push_target)
1978 /* Unlink and invalidate to prevent new references. */
1979 LNetMDUnlink(the_lnet.ln_push_target_md);
1980 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1982 /* Wait for the unlink to complete. */
1983 while (lnet_ping_buffer_numref(the_lnet.ln_push_target) > 1) {
1984 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1985 set_current_state(TASK_UNINTERRUPTIBLE);
1986 schedule_timeout(cfs_time_seconds(1));
1989 lnet_ping_buffer_decref(the_lnet.ln_push_target);
1990 the_lnet.ln_push_target = NULL;
1991 the_lnet.ln_push_target_nnis = 0;
1993 LNetEQFree(the_lnet.ln_push_target_eq);
1994 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1998 lnet_ni_tq_credits(struct lnet_ni *ni)
2002 LASSERT(ni->ni_ncpts >= 1);
2004 if (ni->ni_ncpts == 1)
2005 return ni->ni_net->net_tunables.lct_max_tx_credits;
2007 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2008 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2009 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2015 lnet_ni_unlink_locked(struct lnet_ni *ni)
2017 /* move it to zombie list and nobody can find it anymore */
2018 LASSERT(!list_empty(&ni->ni_netlist));
2019 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2020 lnet_ni_decref_locked(ni, 0);
2024 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2029 struct list_head *zombie_list = &net->net_ni_zombie;
2032 * Now wait for the NIs I just nuked to show up on the zombie
2033 * list and shut them down in guaranteed thread context
2036 while (!list_empty(zombie_list)) {
2040 ni = list_entry(zombie_list->next,
2041 struct lnet_ni, ni_netlist);
2042 list_del_init(&ni->ni_netlist);
2043 /* the ni should be in deleting state. If it's not it's
2045 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2046 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2049 /* still busy, add it back to zombie list */
2050 list_add(&ni->ni_netlist, zombie_list);
2054 if (!list_empty(&ni->ni_netlist)) {
2055 lnet_net_unlock(LNET_LOCK_EX);
2057 if ((i & (-i)) == i) {
2059 "Waiting for zombie LNI %s\n",
2060 libcfs_nid2str(ni->ni_nid));
2062 set_current_state(TASK_UNINTERRUPTIBLE);
2063 schedule_timeout(cfs_time_seconds(1));
2064 lnet_net_lock(LNET_LOCK_EX);
2068 lnet_net_unlock(LNET_LOCK_EX);
2070 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2072 LASSERT(!in_interrupt());
2073 (net->net_lnd->lnd_shutdown)(ni);
2076 CDEBUG(D_LNI, "Removed LNI %s\n",
2077 libcfs_nid2str(ni->ni_nid));
2081 lnet_net_lock(LNET_LOCK_EX);
2085 /* shutdown down the NI and release refcount */
2087 lnet_shutdown_lndni(struct lnet_ni *ni)
2090 struct lnet_net *net = ni->ni_net;
2092 lnet_net_lock(LNET_LOCK_EX);
2094 ni->ni_state = LNET_NI_STATE_DELETING;
2096 lnet_ni_unlink_locked(ni);
2097 lnet_incr_dlc_seq();
2098 lnet_net_unlock(LNET_LOCK_EX);
2100 /* clear messages for this NI on the lazy portal */
2101 for (i = 0; i < the_lnet.ln_nportals; i++)
2102 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2104 lnet_net_lock(LNET_LOCK_EX);
2105 lnet_clear_zombies_nis_locked(net);
2106 lnet_net_unlock(LNET_LOCK_EX);
2110 lnet_shutdown_lndnet(struct lnet_net *net)
2114 lnet_net_lock(LNET_LOCK_EX);
2116 net->net_state = LNET_NET_STATE_DELETING;
2118 list_del_init(&net->net_list);
2120 while (!list_empty(&net->net_ni_list)) {
2121 ni = list_entry(net->net_ni_list.next,
2122 struct lnet_ni, ni_netlist);
2123 lnet_net_unlock(LNET_LOCK_EX);
2124 lnet_shutdown_lndni(ni);
2125 lnet_net_lock(LNET_LOCK_EX);
2128 lnet_net_unlock(LNET_LOCK_EX);
2130 /* Do peer table cleanup for this net */
2131 lnet_peer_tables_cleanup(net);
2133 lnet_net_lock(LNET_LOCK_EX);
2135 * decrement ref count on lnd only when the entire network goes
2138 net->net_lnd->lnd_refcount--;
2140 lnet_net_unlock(LNET_LOCK_EX);
2146 lnet_shutdown_lndnets(void)
2148 struct lnet_net *net;
2149 struct list_head resend;
2150 struct lnet_msg *msg, *tmp;
2152 INIT_LIST_HEAD(&resend);
2154 /* NB called holding the global mutex */
2156 /* All quiet on the API front */
2157 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2158 LASSERT(the_lnet.ln_refcount == 0);
2160 lnet_net_lock(LNET_LOCK_EX);
2161 the_lnet.ln_state = LNET_STATE_STOPPING;
2163 while (!list_empty(&the_lnet.ln_nets)) {
2165 * move the nets to the zombie list to avoid them being
2166 * picked up for new work. LONET is also included in the
2167 * Nets that will be moved to the zombie list
2169 net = list_entry(the_lnet.ln_nets.next,
2170 struct lnet_net, net_list);
2171 list_move(&net->net_list, &the_lnet.ln_net_zombie);
2174 /* Drop the cached loopback Net. */
2175 if (the_lnet.ln_loni != NULL) {
2176 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2177 the_lnet.ln_loni = NULL;
2179 lnet_net_unlock(LNET_LOCK_EX);
2181 /* iterate through the net zombie list and delete each net */
2182 while (!list_empty(&the_lnet.ln_net_zombie)) {
2183 net = list_entry(the_lnet.ln_net_zombie.next,
2184 struct lnet_net, net_list);
2185 lnet_shutdown_lndnet(net);
2188 spin_lock(&the_lnet.ln_msg_resend_lock);
2189 list_splice(&the_lnet.ln_msg_resend, &resend);
2190 spin_unlock(&the_lnet.ln_msg_resend_lock);
2192 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2193 list_del_init(&msg->msg_list);
2194 msg->msg_no_resend = true;
2195 lnet_finalize(msg, -ECANCELED);
2198 lnet_net_lock(LNET_LOCK_EX);
2199 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2200 lnet_net_unlock(LNET_LOCK_EX);
2204 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2207 struct lnet_tx_queue *tq;
2209 struct lnet_net *net = ni->ni_net;
2211 mutex_lock(&the_lnet.ln_lnd_mutex);
2214 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2215 ni->ni_lnd_tunables_set = true;
2218 rc = (net->net_lnd->lnd_startup)(ni);
2220 mutex_unlock(&the_lnet.ln_lnd_mutex);
2223 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2224 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2225 lnet_net_lock(LNET_LOCK_EX);
2226 net->net_lnd->lnd_refcount--;
2227 lnet_net_unlock(LNET_LOCK_EX);
2232 ni->ni_state = LNET_NI_STATE_ACTIVE;
2235 /* We keep a reference on the loopback net through the loopback NI */
2236 if (net->net_lnd->lnd_type == LOLND) {
2238 LASSERT(the_lnet.ln_loni == NULL);
2239 the_lnet.ln_loni = ni;
2240 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2241 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2242 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2243 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2247 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2248 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2249 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2250 libcfs_lnd2str(net->net_lnd->lnd_type),
2251 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2253 /* shutdown the NI since if we get here then it must've already
2256 lnet_shutdown_lndni(ni);
2260 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2261 tq->tq_credits_min =
2262 tq->tq_credits_max =
2263 tq->tq_credits = lnet_ni_tq_credits(ni);
2266 atomic_set(&ni->ni_tx_credits,
2267 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2268 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2270 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2271 libcfs_nid2str(ni->ni_nid),
2272 ni->ni_net->net_tunables.lct_peer_tx_credits,
2273 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2274 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2275 ni->ni_net->net_tunables.lct_peer_timeout);
2284 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2287 struct lnet_net *net_l = NULL;
2288 struct list_head local_ni_list;
2292 struct lnet_lnd *lnd;
2294 net->net_tunables.lct_peer_timeout;
2296 net->net_tunables.lct_max_tx_credits;
2297 int peerrtrcredits =
2298 net->net_tunables.lct_peer_rtr_credits;
2300 INIT_LIST_HEAD(&local_ni_list);
2303 * make sure that this net is unique. If it isn't then
2304 * we are adding interfaces to an already existing network, and
2305 * 'net' is just a convenient way to pass in the list.
2306 * if it is unique we need to find the LND and load it if
2309 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2310 lnd_type = LNET_NETTYP(net->net_id);
2312 mutex_lock(&the_lnet.ln_lnd_mutex);
2313 lnd = lnet_find_lnd_by_type(lnd_type);
2316 mutex_unlock(&the_lnet.ln_lnd_mutex);
2317 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2318 mutex_lock(&the_lnet.ln_lnd_mutex);
2320 lnd = lnet_find_lnd_by_type(lnd_type);
2322 mutex_unlock(&the_lnet.ln_lnd_mutex);
2323 CERROR("Can't load LND %s, module %s, rc=%d\n",
2324 libcfs_lnd2str(lnd_type),
2325 libcfs_lnd2modname(lnd_type), rc);
2326 #ifndef HAVE_MODULE_LOADING_SUPPORT
2327 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2328 "compiled with kernel module "
2329 "loading support.");
2336 lnet_net_lock(LNET_LOCK_EX);
2337 lnd->lnd_refcount++;
2338 lnet_net_unlock(LNET_LOCK_EX);
2342 mutex_unlock(&the_lnet.ln_lnd_mutex);
2348 * net_l: if the network being added is unique then net_l
2349 * will point to that network
2350 * if the network being added is not unique then
2351 * net_l points to the existing network.
2353 * When we enter the loop below, we'll pick NIs off he
2354 * network beign added and start them up, then add them to
2355 * a local ni list. Once we've successfully started all
2356 * the NIs then we join the local NI list (of started up
2357 * networks) with the net_l->net_ni_list, which should
2358 * point to the correct network to add the new ni list to
2360 * If any of the new NIs fail to start up, then we want to
2361 * iterate through the local ni list, which should include
2362 * any NIs which were successfully started up, and shut
2365 * After than we want to delete the network being added,
2366 * to avoid a memory leak.
2370 * When a network uses TCP bonding then all its interfaces
2371 * must be specified when the network is first defined: the
2372 * TCP bonding code doesn't allow for interfaces to be added
2375 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2376 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2381 while (!list_empty(&net->net_ni_added)) {
2382 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2384 list_del_init(&ni->ni_netlist);
2386 /* make sure that the the NI we're about to start
2387 * up is actually unique. if it's not fail. */
2388 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2389 ni->ni_interfaces[0])) {
2394 /* adjust the pointer the parent network, just in case it
2395 * the net is a duplicate */
2398 rc = lnet_startup_lndni(ni, tun);
2400 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
2401 ni->ni_net->net_lnd->lnd_query != NULL);
2407 list_add_tail(&ni->ni_netlist, &local_ni_list);
2412 lnet_net_lock(LNET_LOCK_EX);
2413 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2414 lnet_incr_dlc_seq();
2415 lnet_net_unlock(LNET_LOCK_EX);
2417 /* if the network is not unique then we don't want to keep
2418 * it around after we're done. Free it. Otherwise add that
2419 * net to the global the_lnet.ln_nets */
2420 if (net_l != net && net_l != NULL) {
2422 * TODO - note. currently the tunables can not be updated
2427 net->net_state = LNET_NET_STATE_ACTIVE;
2429 * restore tunables after it has been overwitten by the
2432 if (peer_timeout != -1)
2433 net->net_tunables.lct_peer_timeout = peer_timeout;
2434 if (maxtxcredits != -1)
2435 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2436 if (peerrtrcredits != -1)
2437 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2439 lnet_net_lock(LNET_LOCK_EX);
2440 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2441 lnet_net_unlock(LNET_LOCK_EX);
2444 /* update net count */
2445 lnet_current_net_count = lnet_get_net_count();
2451 * shutdown the new NIs that are being started up
2452 * free the NET being started
2454 while (!list_empty(&local_ni_list)) {
2455 ni = list_entry(local_ni_list.next, struct lnet_ni,
2458 lnet_shutdown_lndni(ni);
2468 lnet_startup_lndnets(struct list_head *netlist)
2470 struct lnet_net *net;
2475 * Change to running state before bringing up the LNDs. This
2476 * allows lnet_shutdown_lndnets() to assert that we've passed
2479 lnet_net_lock(LNET_LOCK_EX);
2480 the_lnet.ln_state = LNET_STATE_RUNNING;
2481 lnet_net_unlock(LNET_LOCK_EX);
2483 while (!list_empty(netlist)) {
2484 net = list_entry(netlist->next, struct lnet_net, net_list);
2485 list_del_init(&net->net_list);
2487 rc = lnet_startup_lndnet(net, NULL);
2497 lnet_shutdown_lndnets();
2503 * Initialize LNet library.
2505 * Automatically called at module loading time. Caller has to call
2506 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2507 * latter returned 0. It must be called exactly once.
2509 * \retval 0 on success
2510 * \retval -ve on failures.
2512 int lnet_lib_init(void)
2516 lnet_assert_wire_constants();
2518 /* refer to global cfs_cpt_table for now */
2519 the_lnet.ln_cpt_table = cfs_cpt_table;
2520 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
2522 LASSERT(the_lnet.ln_cpt_number > 0);
2523 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2524 /* we are under risk of consuming all lh_cookie */
2525 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2526 "please change setting of CPT-table and retry\n",
2527 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2531 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2532 the_lnet.ln_cpt_bits++;
2534 rc = lnet_create_locks();
2536 CERROR("Can't create LNet global locks: %d\n", rc);
2540 the_lnet.ln_refcount = 0;
2541 INIT_LIST_HEAD(&the_lnet.ln_lnds);
2542 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2543 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2545 /* The hash table size is the number of bits it takes to express the set
2546 * ln_num_routes, minus 1 (better to under estimate than over so we
2547 * don't waste memory). */
2548 if (rnet_htable_size <= 0)
2549 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2550 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2551 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2552 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2553 order_base_2(rnet_htable_size) - 1);
2555 /* All LNDs apart from the LOLND are in separate modules. They
2556 * register themselves when their module loads, and unregister
2557 * themselves when their module is unloaded. */
2558 lnet_register_lnd(&the_lolnd);
2563 * Finalize LNet library.
2565 * \pre lnet_lib_init() called with success.
2566 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2568 void lnet_lib_exit(void)
2570 LASSERT(the_lnet.ln_refcount == 0);
2572 while (!list_empty(&the_lnet.ln_lnds))
2573 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
2574 struct lnet_lnd, lnd_list));
2575 lnet_destroy_locks();
2579 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2581 * Users must call this function at least once before any other functions.
2582 * For each successful call there must be a corresponding call to
2583 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2586 * The PID used by LNet may be different from the one requested.
2589 * \param requested_pid PID requested by the caller.
2591 * \return >= 0 on success, and < 0 error code on failures.
2594 LNetNIInit(lnet_pid_t requested_pid)
2596 int im_a_router = 0;
2599 struct lnet_ping_buffer *pbuf;
2600 struct lnet_handle_md ping_mdh;
2601 struct list_head net_head;
2602 struct lnet_net *net;
2604 INIT_LIST_HEAD(&net_head);
2606 mutex_lock(&the_lnet.ln_api_mutex);
2608 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2610 if (the_lnet.ln_refcount > 0) {
2611 rc = the_lnet.ln_refcount++;
2612 mutex_unlock(&the_lnet.ln_api_mutex);
2616 rc = lnet_prepare(requested_pid);
2618 mutex_unlock(&the_lnet.ln_api_mutex);
2622 /* create a network for Loopback network */
2623 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2626 goto err_empty_list;
2629 /* Add in the loopback NI */
2630 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2632 goto err_empty_list;
2635 /* If LNet is being initialized via DLC it is possible
2636 * that the user requests not to load module parameters (ones which
2637 * are supported by DLC) on initialization. Therefore, make sure not
2638 * to load networks, routes and forwarding from module parameters
2639 * in this case. On cleanup in case of failure only clean up
2640 * routes if it has been loaded */
2641 if (!the_lnet.ln_nis_from_mod_params) {
2642 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2645 goto err_empty_list;
2648 ni_count = lnet_startup_lndnets(&net_head);
2651 goto err_empty_list;
2654 if (!the_lnet.ln_nis_from_mod_params) {
2655 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2657 goto err_shutdown_lndnis;
2659 rc = lnet_rtrpools_alloc(im_a_router);
2661 goto err_destroy_routes;
2664 rc = lnet_acceptor_start();
2666 goto err_destroy_routes;
2668 the_lnet.ln_refcount = 1;
2669 /* Now I may use my own API functions... */
2671 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2673 goto err_acceptor_stop;
2675 lnet_ping_target_update(pbuf, ping_mdh);
2677 rc = LNetEQAlloc(0, lnet_mt_event_handler, &the_lnet.ln_mt_eqh);
2679 CERROR("Can't allocate monitor thread EQ: %d\n", rc);
2683 rc = lnet_push_target_init();
2687 rc = lnet_peer_discovery_start();
2689 goto err_destroy_push_target;
2691 rc = lnet_monitor_thr_start();
2693 goto err_stop_discovery_thr;
2696 lnet_router_debugfs_init();
2698 mutex_unlock(&the_lnet.ln_api_mutex);
2700 complete_all(&the_lnet.ln_started);
2702 /* wait for all routers to start */
2703 lnet_wait_router_start();
2707 err_stop_discovery_thr:
2708 lnet_peer_discovery_stop();
2709 err_destroy_push_target:
2710 lnet_push_target_fini();
2712 lnet_ping_target_fini();
2714 the_lnet.ln_refcount = 0;
2715 lnet_acceptor_stop();
2717 if (!the_lnet.ln_nis_from_mod_params)
2718 lnet_destroy_routes();
2719 err_shutdown_lndnis:
2720 lnet_shutdown_lndnets();
2724 mutex_unlock(&the_lnet.ln_api_mutex);
2725 while (!list_empty(&net_head)) {
2726 struct lnet_net *net;
2728 net = list_entry(net_head.next, struct lnet_net, net_list);
2729 list_del_init(&net->net_list);
2734 EXPORT_SYMBOL(LNetNIInit);
2737 * Stop LNet interfaces, routing, and forwarding.
2739 * Users must call this function once for each successful call to LNetNIInit().
2740 * Once the LNetNIFini() operation has been started, the results of pending
2741 * API operations are undefined.
2743 * \return always 0 for current implementation.
2748 mutex_lock(&the_lnet.ln_api_mutex);
2750 LASSERT(the_lnet.ln_refcount > 0);
2752 if (the_lnet.ln_refcount != 1) {
2753 the_lnet.ln_refcount--;
2755 LASSERT(!the_lnet.ln_niinit_self);
2759 lnet_router_debugfs_fini();
2760 lnet_monitor_thr_stop();
2761 lnet_peer_discovery_stop();
2762 lnet_push_target_fini();
2763 lnet_ping_target_fini();
2765 /* Teardown fns that use my own API functions BEFORE here */
2766 the_lnet.ln_refcount = 0;
2768 lnet_acceptor_stop();
2769 lnet_destroy_routes();
2770 lnet_shutdown_lndnets();
2774 mutex_unlock(&the_lnet.ln_api_mutex);
2777 EXPORT_SYMBOL(LNetNIFini);
2780 * Grabs the ni data from the ni structure and fills the out
2783 * \param[in] ni network interface structure
2784 * \param[out] cfg_ni NI config information
2785 * \param[out] tun network and LND tunables
2788 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2789 struct lnet_ioctl_config_lnd_tunables *tun,
2790 struct lnet_ioctl_element_stats *stats,
2793 size_t min_size = 0;
2796 if (!ni || !cfg_ni || !tun)
2799 if (ni->ni_interfaces[0] != NULL) {
2800 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2801 if (ni->ni_interfaces[i] != NULL) {
2802 strncpy(cfg_ni->lic_ni_intf[i],
2803 ni->ni_interfaces[i],
2804 sizeof(cfg_ni->lic_ni_intf[i]));
2809 cfg_ni->lic_nid = ni->ni_nid;
2810 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2811 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2813 cfg_ni->lic_status = ni->ni_status->ns_status;
2814 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2815 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2817 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2820 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2821 LNET_STATS_TYPE_SEND);
2822 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2823 LNET_STATS_TYPE_RECV);
2824 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2825 LNET_STATS_TYPE_DROP);
2829 * tun->lt_tun will always be present, but in order to be
2830 * backwards compatible, we need to deal with the cases when
2831 * tun->lt_tun is smaller than what the kernel has, because it
2832 * comes from an older version of a userspace program, then we'll
2833 * need to copy as much information as we have available space.
2835 min_size = tun_size - sizeof(tun->lt_cmn);
2836 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2838 /* copy over the cpts */
2839 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2840 ni->ni_cpts == NULL) {
2841 for (i = 0; i < ni->ni_ncpts; i++)
2842 cfg_ni->lic_cpts[i] = i;
2845 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2846 i < LNET_MAX_SHOW_NUM_CPT;
2848 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2850 cfg_ni->lic_ncpts = ni->ni_ncpts;
2854 * NOTE: This is a legacy function left in the code to be backwards
2855 * compatible with older userspace programs. It should eventually be
2858 * Grabs the ni data from the ni structure and fills the out
2861 * \param[in] ni network interface structure
2862 * \param[out] config config information
2865 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2866 struct lnet_ioctl_config_data *config)
2868 struct lnet_ioctl_net_config *net_config;
2869 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2870 size_t min_size, tunable_size = 0;
2876 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2880 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2881 ARRAY_SIZE(net_config->ni_interfaces));
2883 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2884 if (!ni->ni_interfaces[i])
2887 strncpy(net_config->ni_interfaces[i],
2888 ni->ni_interfaces[i],
2889 sizeof(net_config->ni_interfaces[i]));
2892 config->cfg_nid = ni->ni_nid;
2893 config->cfg_config_u.cfg_net.net_peer_timeout =
2894 ni->ni_net->net_tunables.lct_peer_timeout;
2895 config->cfg_config_u.cfg_net.net_max_tx_credits =
2896 ni->ni_net->net_tunables.lct_max_tx_credits;
2897 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2898 ni->ni_net->net_tunables.lct_peer_tx_credits;
2899 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2900 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2902 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2903 net_config->ni_status = LNET_NI_STATUS_UP;
2905 net_config->ni_status = ni->ni_status->ns_status;
2908 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2910 for (i = 0; i < num_cpts; i++)
2911 net_config->ni_cpts[i] = ni->ni_cpts[i];
2913 config->cfg_ncpts = num_cpts;
2917 * See if user land tools sent in a newer and larger version
2918 * of struct lnet_tunables than what the kernel uses.
2920 min_size = sizeof(*config) + sizeof(*net_config);
2922 if (config->cfg_hdr.ioc_len > min_size)
2923 tunable_size = config->cfg_hdr.ioc_len - min_size;
2925 /* Don't copy too much data to user space */
2926 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2927 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2929 if (lnd_cfg && min_size) {
2930 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2931 config->cfg_config_u.cfg_net.net_interface_count = 1;
2933 /* Tell user land that kernel side has less data */
2934 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2935 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2936 config->cfg_hdr.ioc_len -= min_size;
2942 lnet_get_ni_idx_locked(int idx)
2945 struct lnet_net *net;
2947 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2948 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2958 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2961 struct lnet_net *net = mynet;
2964 * It is possible that the net has been cleaned out while there is
2965 * a message being sent. This function accessed the net without
2966 * checking if the list is empty
2970 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2972 if (list_empty(&net->net_ni_list))
2974 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2980 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2981 /* if you reached the end of the ni list and the net is
2982 * specified, then there are no more nis in that net */
2986 /* we reached the end of this net ni list. move to the
2988 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2989 /* no more nets and no more NIs. */
2992 /* get the next net */
2993 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2995 if (list_empty(&net->net_ni_list))
2997 /* get the ni on it */
2998 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3004 if (list_empty(&prev->ni_netlist))
3007 /* there are more nis left */
3008 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3014 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3019 int idx = config->cfg_count;
3021 cpt = lnet_net_lock_current();
3023 ni = lnet_get_ni_idx_locked(idx);
3028 lnet_fill_ni_info_legacy(ni, config);
3032 lnet_net_unlock(cpt);
3037 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3038 struct lnet_ioctl_config_lnd_tunables *tun,
3039 struct lnet_ioctl_element_stats *stats,
3046 if (!cfg_ni || !tun || !stats)
3049 cpt = lnet_net_lock_current();
3051 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3056 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3060 lnet_net_unlock(cpt);
3064 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3073 cpt = lnet_net_lock_current();
3075 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3078 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3082 lnet_net_unlock(cpt);
3087 static int lnet_add_net_common(struct lnet_net *net,
3088 struct lnet_ioctl_config_lnd_tunables *tun)
3091 struct lnet_ping_buffer *pbuf;
3092 struct lnet_handle_md ping_mdh;
3094 struct lnet_remotenet *rnet;
3096 int num_acceptor_nets;
3098 lnet_net_lock(LNET_LOCK_EX);
3099 rnet = lnet_find_rnet_locked(net->net_id);
3100 lnet_net_unlock(LNET_LOCK_EX);
3102 * make sure that the net added doesn't invalidate the current
3103 * configuration LNet is keeping
3106 CERROR("Adding net %s will invalidate routing configuration\n",
3107 libcfs_net2str(net->net_id));
3113 * make sure you calculate the correct number of slots in the ping
3114 * buffer. Since the ping info is a flattened list of all the NIs,
3115 * we should allocate enough slots to accomodate the number of NIs
3116 * which will be added.
3118 * since ni hasn't been configured yet, use
3119 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3121 net_ni_count = lnet_get_net_ni_count_pre(net);
3123 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3124 net_ni_count + lnet_get_ni_count(),
3132 memcpy(&net->net_tunables,
3133 &tun->lt_cmn, sizeof(net->net_tunables));
3135 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3138 * before starting this network get a count of the current TCP
3139 * networks which require the acceptor thread running. If that
3140 * count is == 0 before we start up this network, then we'd want to
3141 * start up the acceptor thread after starting up this network
3143 num_acceptor_nets = lnet_count_acceptor_nets();
3145 net_id = net->net_id;
3147 rc = lnet_startup_lndnet(net,
3148 (tun) ? &tun->lt_tun : NULL);
3152 lnet_net_lock(LNET_LOCK_EX);
3153 net = lnet_get_net_locked(net_id);
3154 lnet_net_unlock(LNET_LOCK_EX);
3159 * Start the acceptor thread if this is the first network
3160 * being added that requires the thread.
3162 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
3163 rc = lnet_acceptor_start();
3165 /* shutdown the net that we just started */
3166 CERROR("Failed to start up acceptor thread\n");
3167 lnet_shutdown_lndnet(net);
3172 lnet_net_lock(LNET_LOCK_EX);
3173 lnet_peer_net_added(net);
3174 lnet_net_unlock(LNET_LOCK_EX);
3176 lnet_ping_target_update(pbuf, ping_mdh);
3181 lnet_ping_md_unlink(pbuf, &ping_mdh);
3182 lnet_ping_buffer_decref(pbuf);
3186 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3187 struct lnet_ioctl_config_lnd_tunables *tun)
3189 struct lnet_net *net;
3192 struct list_head net_head;
3194 INIT_LIST_HEAD(&net_head);
3196 rc = lnet_parse_ip2nets(&nets, ip2nets);
3200 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3204 mutex_lock(&the_lnet.ln_api_mutex);
3205 while (!list_empty(&net_head)) {
3206 net = list_entry(net_head.next, struct lnet_net, net_list);
3207 list_del_init(&net->net_list);
3208 rc = lnet_add_net_common(net, tun);
3214 mutex_unlock(&the_lnet.ln_api_mutex);
3216 while (!list_empty(&net_head)) {
3217 net = list_entry(net_head.next, struct lnet_net, net_list);
3218 list_del_init(&net->net_list);
3224 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3226 struct lnet_net *net;
3228 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3230 __u32 net_id, lnd_type;
3232 /* get the tunables if they are available */
3233 if (conf->lic_cfg_hdr.ioc_len >=
3234 sizeof(*conf) + sizeof(*tun))
3235 tun = (struct lnet_ioctl_config_lnd_tunables *)
3238 /* handle legacy ip2nets from DLC */
3239 if (conf->lic_legacy_ip2nets[0] != '\0')
3240 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3243 net_id = LNET_NIDNET(conf->lic_nid);
3244 lnd_type = LNET_NETTYP(net_id);
3246 if (!libcfs_isknown_lnd(lnd_type)) {
3247 CERROR("No valid net and lnd information provided\n");
3251 net = lnet_net_alloc(net_id, NULL);
3255 for (i = 0; i < conf->lic_ncpts; i++) {
3256 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3260 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3261 conf->lic_ni_intf[0]);
3265 mutex_lock(&the_lnet.ln_api_mutex);
3267 rc = lnet_add_net_common(net, tun);
3269 mutex_unlock(&the_lnet.ln_api_mutex);
3274 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3276 struct lnet_net *net;
3278 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3279 struct lnet_ping_buffer *pbuf;
3280 struct lnet_handle_md ping_mdh;
3285 /* don't allow userspace to shutdown the LOLND */
3286 if (LNET_NETTYP(net_id) == LOLND)
3289 mutex_lock(&the_lnet.ln_api_mutex);
3293 net = lnet_get_net_locked(net_id);
3295 CERROR("net %s not found\n",
3296 libcfs_net2str(net_id));
3301 addr = LNET_NIDADDR(conf->lic_nid);
3303 /* remove the entire net */
3304 net_count = lnet_get_net_ni_count_locked(net);
3308 /* create and link a new ping info, before removing the old one */
3309 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3310 lnet_get_ni_count() - net_count,
3313 goto unlock_api_mutex;
3315 lnet_shutdown_lndnet(net);
3317 if (lnet_count_acceptor_nets() == 0)
3318 lnet_acceptor_stop();
3320 lnet_ping_target_update(pbuf, ping_mdh);
3322 goto unlock_api_mutex;
3325 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3327 CERROR("nid %s not found\n",
3328 libcfs_nid2str(conf->lic_nid));
3333 net_count = lnet_get_net_ni_count_locked(net);
3337 /* create and link a new ping info, before removing the old one */
3338 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3339 lnet_get_ni_count() - 1, false);
3341 goto unlock_api_mutex;
3343 lnet_shutdown_lndni(ni);
3345 if (lnet_count_acceptor_nets() == 0)
3346 lnet_acceptor_stop();
3348 lnet_ping_target_update(pbuf, ping_mdh);
3350 /* check if the net is empty and remove it if it is */
3352 lnet_shutdown_lndnet(net);
3354 goto unlock_api_mutex;
3359 mutex_unlock(&the_lnet.ln_api_mutex);
3365 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3366 * They are only expected to be called for unique networks.
3367 * That can be as a result of older DLC library
3368 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3371 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3373 struct lnet_net *net;
3374 struct list_head net_head;
3376 struct lnet_ioctl_config_lnd_tunables tun;
3377 char *nets = conf->cfg_config_u.cfg_net.net_intf;
3379 INIT_LIST_HEAD(&net_head);
3381 /* Create a net/ni structures for the network string */
3382 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3384 return rc == 0 ? -EINVAL : rc;
3386 mutex_lock(&the_lnet.ln_api_mutex);
3389 rc = -EINVAL; /* only add one network per call */
3390 goto out_unlock_clean;
3393 net = list_entry(net_head.next, struct lnet_net, net_list);
3394 list_del_init(&net->net_list);
3396 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3398 memset(&tun, 0, sizeof(tun));
3400 tun.lt_cmn.lct_peer_timeout =
3401 conf->cfg_config_u.cfg_net.net_peer_timeout;
3402 tun.lt_cmn.lct_peer_tx_credits =
3403 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3404 tun.lt_cmn.lct_peer_rtr_credits =
3405 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3406 tun.lt_cmn.lct_max_tx_credits =
3407 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3409 rc = lnet_add_net_common(net, &tun);
3412 mutex_unlock(&the_lnet.ln_api_mutex);
3413 while (!list_empty(&net_head)) {
3414 /* net_head list is empty in success case */
3415 net = list_entry(net_head.next, struct lnet_net, net_list);
3416 list_del_init(&net->net_list);
3423 lnet_dyn_del_net(__u32 net_id)
3425 struct lnet_net *net;
3426 struct lnet_ping_buffer *pbuf;
3427 struct lnet_handle_md ping_mdh;
3431 /* don't allow userspace to shutdown the LOLND */
3432 if (LNET_NETTYP(net_id) == LOLND)
3435 mutex_lock(&the_lnet.ln_api_mutex);
3439 net = lnet_get_net_locked(net_id);
3446 net_ni_count = lnet_get_net_ni_count_locked(net);
3450 /* create and link a new ping info, before removing the old one */
3451 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3452 lnet_get_ni_count() - net_ni_count, false);
3456 lnet_shutdown_lndnet(net);
3458 if (lnet_count_acceptor_nets() == 0)
3459 lnet_acceptor_stop();
3461 lnet_ping_target_update(pbuf, ping_mdh);
3464 mutex_unlock(&the_lnet.ln_api_mutex);
3469 void lnet_incr_dlc_seq(void)
3471 atomic_inc(&lnet_dlc_seq_no);
3474 __u32 lnet_get_dlc_seq_locked(void)
3476 return atomic_read(&lnet_dlc_seq_no);
3480 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3482 struct lnet_net *net;
3485 lnet_net_lock(LNET_LOCK_EX);
3486 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3487 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3488 if (ni->ni_nid == nid || all) {
3489 atomic_set(&ni->ni_healthv, value);
3490 if (list_empty(&ni->ni_recovery) &&
3491 value < LNET_MAX_HEALTH_VALUE) {
3492 CERROR("manually adding local NI %s to recovery\n",
3493 libcfs_nid2str(ni->ni_nid));
3494 list_add_tail(&ni->ni_recovery,
3495 &the_lnet.ln_mt_localNIRecovq);
3496 lnet_ni_addref_locked(ni, 0);
3499 lnet_net_unlock(LNET_LOCK_EX);
3505 lnet_net_unlock(LNET_LOCK_EX);
3509 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3513 lnet_nid_t nid = stats->hlni_nid;
3515 cpt = lnet_net_lock_current();
3516 ni = lnet_nid2ni_locked(nid, cpt);
3523 stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3524 stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3525 stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3526 stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3527 stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3528 stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3529 stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3532 lnet_net_unlock(cpt);
3538 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3543 lnet_net_lock(LNET_LOCK_EX);
3544 list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3545 list->rlst_nid_array[i] = ni->ni_nid;
3547 if (i >= LNET_MAX_SHOW_NUM_NID)
3550 lnet_net_unlock(LNET_LOCK_EX);
3551 list->rlst_num_nids = i;
3557 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3559 struct lnet_peer_ni *lpni;
3562 lnet_net_lock(LNET_LOCK_EX);
3563 list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3564 list->rlst_nid_array[i] = lpni->lpni_nid;
3566 if (i >= LNET_MAX_SHOW_NUM_NID)
3569 lnet_net_unlock(LNET_LOCK_EX);
3570 list->rlst_num_nids = i;
3576 * LNet ioctl handler.
3580 LNetCtl(unsigned int cmd, void *arg)
3582 struct libcfs_ioctl_data *data = arg;
3583 struct lnet_ioctl_config_data *config;
3584 struct lnet_process_id id = {0};
3588 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3589 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3592 case IOC_LIBCFS_GET_NI:
3593 rc = LNetGetId(data->ioc_count, &id);
3594 data->ioc_nid = id.nid;
3597 case IOC_LIBCFS_FAIL_NID:
3598 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3600 case IOC_LIBCFS_ADD_ROUTE: {
3601 /* default router sensitivity to 1 */
3602 unsigned int sensitivity = 1;
3605 if (config->cfg_hdr.ioc_len < sizeof(*config))
3608 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3610 config->cfg_config_u.cfg_route.rtr_sensitivity;
3613 mutex_lock(&the_lnet.ln_api_mutex);
3614 rc = lnet_add_route(config->cfg_net,
3615 config->cfg_config_u.cfg_route.rtr_hop,
3617 config->cfg_config_u.cfg_route.
3618 rtr_priority, sensitivity);
3619 mutex_unlock(&the_lnet.ln_api_mutex);
3623 case IOC_LIBCFS_DEL_ROUTE:
3626 if (config->cfg_hdr.ioc_len < sizeof(*config))
3629 mutex_lock(&the_lnet.ln_api_mutex);
3630 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3631 mutex_unlock(&the_lnet.ln_api_mutex);
3634 case IOC_LIBCFS_GET_ROUTE:
3637 if (config->cfg_hdr.ioc_len < sizeof(*config))
3640 mutex_lock(&the_lnet.ln_api_mutex);
3641 rc = lnet_get_route(config->cfg_count,
3643 &config->cfg_config_u.cfg_route.rtr_hop,
3645 &config->cfg_config_u.cfg_route.rtr_flags,
3646 &config->cfg_config_u.cfg_route.
3648 &config->cfg_config_u.cfg_route.
3650 mutex_unlock(&the_lnet.ln_api_mutex);
3653 case IOC_LIBCFS_GET_LOCAL_NI: {
3654 struct lnet_ioctl_config_ni *cfg_ni;
3655 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3656 struct lnet_ioctl_element_stats *stats;
3661 /* get the tunables if they are available */
3662 if (cfg_ni->lic_cfg_hdr.ioc_len <
3663 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3666 stats = (struct lnet_ioctl_element_stats *)
3668 tun = (struct lnet_ioctl_config_lnd_tunables *)
3669 (cfg_ni->lic_bulk + sizeof(*stats));
3671 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3674 mutex_lock(&the_lnet.ln_api_mutex);
3675 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3676 mutex_unlock(&the_lnet.ln_api_mutex);
3680 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3681 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3683 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3686 mutex_lock(&the_lnet.ln_api_mutex);
3687 rc = lnet_get_ni_stats(msg_stats);
3688 mutex_unlock(&the_lnet.ln_api_mutex);
3693 case IOC_LIBCFS_GET_NET: {
3694 size_t total = sizeof(*config) +
3695 sizeof(struct lnet_ioctl_net_config);
3698 if (config->cfg_hdr.ioc_len < total)
3701 mutex_lock(&the_lnet.ln_api_mutex);
3702 rc = lnet_get_net_config(config);
3703 mutex_unlock(&the_lnet.ln_api_mutex);
3707 case IOC_LIBCFS_GET_LNET_STATS:
3709 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3711 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3714 mutex_lock(&the_lnet.ln_api_mutex);
3715 lnet_counters_get(&lnet_stats->st_cntrs);
3716 mutex_unlock(&the_lnet.ln_api_mutex);
3720 case IOC_LIBCFS_CONFIG_RTR:
3723 if (config->cfg_hdr.ioc_len < sizeof(*config))
3726 mutex_lock(&the_lnet.ln_api_mutex);
3727 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3728 rc = lnet_rtrpools_enable();
3729 mutex_unlock(&the_lnet.ln_api_mutex);
3732 lnet_rtrpools_disable();
3733 mutex_unlock(&the_lnet.ln_api_mutex);
3736 case IOC_LIBCFS_ADD_BUF:
3739 if (config->cfg_hdr.ioc_len < sizeof(*config))
3742 mutex_lock(&the_lnet.ln_api_mutex);
3743 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3745 config->cfg_config_u.cfg_buffers.
3747 config->cfg_config_u.cfg_buffers.
3749 mutex_unlock(&the_lnet.ln_api_mutex);
3752 case IOC_LIBCFS_SET_NUMA_RANGE: {
3753 struct lnet_ioctl_set_value *numa;
3755 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3757 lnet_net_lock(LNET_LOCK_EX);
3758 lnet_numa_range = numa->sv_value;
3759 lnet_net_unlock(LNET_LOCK_EX);
3763 case IOC_LIBCFS_GET_NUMA_RANGE: {
3764 struct lnet_ioctl_set_value *numa;
3766 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3768 numa->sv_value = lnet_numa_range;
3772 case IOC_LIBCFS_GET_BUF: {
3773 struct lnet_ioctl_pool_cfg *pool_cfg;
3774 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3778 if (config->cfg_hdr.ioc_len < total)
3781 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3783 mutex_lock(&the_lnet.ln_api_mutex);
3784 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3785 mutex_unlock(&the_lnet.ln_api_mutex);
3789 case IOC_LIBCFS_GET_LOCAL_HSTATS: {
3790 struct lnet_ioctl_local_ni_hstats *stats = arg;
3792 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
3795 mutex_lock(&the_lnet.ln_api_mutex);
3796 rc = lnet_get_local_ni_hstats(stats);
3797 mutex_unlock(&the_lnet.ln_api_mutex);
3802 case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
3803 struct lnet_ioctl_recovery_list *list = arg;
3804 if (list->rlst_hdr.ioc_len < sizeof(*list))
3807 mutex_lock(&the_lnet.ln_api_mutex);
3808 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
3809 rc = lnet_get_local_ni_recovery_list(list);
3811 rc = lnet_get_peer_ni_recovery_list(list);
3812 mutex_unlock(&the_lnet.ln_api_mutex);
3816 case IOC_LIBCFS_ADD_PEER_NI: {
3817 struct lnet_ioctl_peer_cfg *cfg = arg;
3819 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3822 mutex_lock(&the_lnet.ln_api_mutex);
3823 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3826 mutex_unlock(&the_lnet.ln_api_mutex);
3830 case IOC_LIBCFS_DEL_PEER_NI: {
3831 struct lnet_ioctl_peer_cfg *cfg = arg;
3833 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3836 mutex_lock(&the_lnet.ln_api_mutex);
3837 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3838 cfg->prcfg_cfg_nid);
3839 mutex_unlock(&the_lnet.ln_api_mutex);
3843 case IOC_LIBCFS_GET_PEER_INFO: {
3844 struct lnet_ioctl_peer *peer_info = arg;
3846 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3849 mutex_lock(&the_lnet.ln_api_mutex);
3850 rc = lnet_get_peer_ni_info(
3851 peer_info->pr_count,
3853 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3854 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3855 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3856 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3857 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3858 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3859 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3860 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3861 mutex_unlock(&the_lnet.ln_api_mutex);
3865 case IOC_LIBCFS_GET_PEER_NI: {
3866 struct lnet_ioctl_peer_cfg *cfg = arg;
3868 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3871 mutex_lock(&the_lnet.ln_api_mutex);
3872 rc = lnet_get_peer_info(cfg,
3873 (void __user *)cfg->prcfg_bulk);
3874 mutex_unlock(&the_lnet.ln_api_mutex);
3878 case IOC_LIBCFS_GET_PEER_LIST: {
3879 struct lnet_ioctl_peer_cfg *cfg = arg;
3881 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3884 mutex_lock(&the_lnet.ln_api_mutex);
3885 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3886 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3887 mutex_unlock(&the_lnet.ln_api_mutex);
3891 case IOC_LIBCFS_SET_HEALHV: {
3892 struct lnet_ioctl_reset_health_cfg *cfg = arg;
3894 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
3896 if (cfg->rh_value < 0 ||
3897 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
3898 value = LNET_MAX_HEALTH_VALUE;
3900 value = cfg->rh_value;
3901 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
3902 value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
3903 "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
3904 mutex_lock(&the_lnet.ln_api_mutex);
3905 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
3906 lnet_ni_set_healthv(cfg->rh_nid, value,
3909 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
3911 mutex_unlock(&the_lnet.ln_api_mutex);
3915 case IOC_LIBCFS_NOTIFY_ROUTER: {
3916 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3918 /* The deadline passed in by the user should be some time in
3919 * seconds in the future since the UNIX epoch. We have to map
3920 * that deadline to the wall clock.
3922 deadline += ktime_get_seconds();
3923 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
3927 case IOC_LIBCFS_LNET_DIST:
3928 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3929 if (rc < 0 && rc != -EHOSTUNREACH)
3932 data->ioc_u32[0] = rc;
3935 case IOC_LIBCFS_TESTPROTOCOMPAT:
3936 lnet_net_lock(LNET_LOCK_EX);
3937 the_lnet.ln_testprotocompat = data->ioc_flags;
3938 lnet_net_unlock(LNET_LOCK_EX);
3941 case IOC_LIBCFS_LNET_FAULT:
3942 return lnet_fault_ctl(data->ioc_flags, data);
3944 case IOC_LIBCFS_PING: {
3945 signed long timeout;
3947 id.nid = data->ioc_nid;
3948 id.pid = data->ioc_u32[0];
3950 /* If timeout is negative then set default of 3 minutes */
3951 if (((s32)data->ioc_u32[1] <= 0) ||
3952 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3953 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3955 timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
3957 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3958 data->ioc_plen1 / sizeof(struct lnet_process_id));
3963 data->ioc_count = rc;
3967 case IOC_LIBCFS_PING_PEER: {
3968 struct lnet_ioctl_ping_data *ping = arg;
3969 struct lnet_peer *lp;
3970 signed long timeout;
3972 /* If timeout is negative then set default of 3 minutes */
3973 if (((s32)ping->op_param) <= 0 ||
3974 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3975 timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3977 timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
3979 rc = lnet_ping(ping->ping_id, timeout,
3985 mutex_lock(&the_lnet.ln_api_mutex);
3986 lp = lnet_find_peer(ping->ping_id.nid);
3988 ping->ping_id.nid = lp->lp_primary_nid;
3989 ping->mr_info = lnet_peer_is_multi_rail(lp);
3990 lnet_peer_decref_locked(lp);
3992 mutex_unlock(&the_lnet.ln_api_mutex);
3994 ping->ping_count = rc;
3998 case IOC_LIBCFS_DISCOVER: {
3999 struct lnet_ioctl_ping_data *discover = arg;
4000 struct lnet_peer *lp;
4002 rc = lnet_discover(discover->ping_id, discover->op_param,
4004 discover->ping_count);
4008 mutex_lock(&the_lnet.ln_api_mutex);
4009 lp = lnet_find_peer(discover->ping_id.nid);
4011 discover->ping_id.nid = lp->lp_primary_nid;
4012 discover->mr_info = lnet_peer_is_multi_rail(lp);
4013 lnet_peer_decref_locked(lp);
4015 mutex_unlock(&the_lnet.ln_api_mutex);
4017 discover->ping_count = rc;
4022 ni = lnet_net2ni_addref(data->ioc_net);
4026 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
4029 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4036 EXPORT_SYMBOL(LNetCtl);
4038 void LNetDebugPeer(struct lnet_process_id id)
4040 lnet_debug_peer(id.nid);
4042 EXPORT_SYMBOL(LNetDebugPeer);
4045 * Determine if the specified peer \a nid is on the local node.
4047 * \param nid peer nid to check
4049 * \retval true If peer NID is on the local node.
4050 * \retval false If peer NID is not on the local node.
4052 bool LNetIsPeerLocal(lnet_nid_t nid)
4054 struct lnet_net *net;
4058 cpt = lnet_net_lock_current();
4059 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4060 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4061 if (ni->ni_nid == nid) {
4062 lnet_net_unlock(cpt);
4067 lnet_net_unlock(cpt);
4071 EXPORT_SYMBOL(LNetIsPeerLocal);
4074 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4075 * Note that all interfaces share a same PID, as requested by LNetNIInit().
4077 * \param index Index of the interface to look up.
4078 * \param id On successful return, this location will hold the
4079 * struct lnet_process_id ID of the interface.
4081 * \retval 0 If an interface exists at \a index.
4082 * \retval -ENOENT If no interface has been found.
4085 LNetGetId(unsigned int index, struct lnet_process_id *id)
4088 struct lnet_net *net;
4092 LASSERT(the_lnet.ln_refcount > 0);
4094 cpt = lnet_net_lock_current();
4096 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4097 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4101 id->nid = ni->ni_nid;
4102 id->pid = the_lnet.ln_pid;
4108 lnet_net_unlock(cpt);
4111 EXPORT_SYMBOL(LNetGetId);
4113 static int lnet_ping(struct lnet_process_id id, signed long timeout,
4114 struct lnet_process_id __user *ids, int n_ids)
4116 struct lnet_handle_eq eqh;
4117 struct lnet_handle_md mdh;
4118 struct lnet_event event;
4119 struct lnet_md md = { NULL };
4123 const signed long a_long_time = cfs_time_seconds(60);
4124 struct lnet_ping_buffer *pbuf;
4125 struct lnet_process_id tmpid;
4132 /* n_ids limit is arbitrary */
4133 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4137 * if the user buffer has more space than the lnet_interfaces_max
4138 * then only fill it up to lnet_interfaces_max
4140 if (n_ids > lnet_interfaces_max)
4141 n_ids = lnet_interfaces_max;
4143 if (id.pid == LNET_PID_ANY)
4144 id.pid = LNET_PID_LUSTRE;
4146 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4150 /* NB 2 events max (including any unlink event) */
4151 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
4153 CERROR("Can't allocate EQ: %d\n", rc);
4154 goto fail_ping_buffer_decref;
4157 /* initialize md content */
4158 md.start = &pbuf->pb_info;
4159 md.length = LNET_PING_INFO_SIZE(n_ids);
4160 md.threshold = 2; /* GET/REPLY */
4162 md.options = LNET_MD_TRUNCATE;
4166 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
4168 CERROR("Can't bind MD: %d\n", rc);
4172 rc = LNetGet(LNET_NID_ANY, mdh, id,
4173 LNET_RESERVED_PORTAL,
4174 LNET_PROTO_PING_MATCHBITS, 0, false);
4177 /* Don't CERROR; this could be deliberate! */
4178 rc2 = LNetMDUnlink(mdh);
4181 /* NB must wait for the UNLINK event below... */
4183 timeout = a_long_time;
4187 /* MUST block for unlink to complete */
4189 blocked = cfs_block_allsigs();
4191 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
4194 cfs_restore_sigs(blocked);
4196 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
4197 (rc2 <= 0) ? -1 : event.type,
4198 (rc2 <= 0) ? -1 : event.status,
4199 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
4201 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
4203 if (rc2 <= 0 || event.status != 0) {
4204 /* timeout or error */
4205 if (!replied && rc == 0)
4206 rc = (rc2 < 0) ? rc2 :
4207 (rc2 == 0) ? -ETIMEDOUT :
4211 /* Ensure completion in finite time... */
4213 /* No assertion (racing with network) */
4215 timeout = a_long_time;
4216 } else if (rc2 == 0) {
4217 /* timed out waiting for unlink */
4218 CWARN("ping %s: late network completion\n",
4221 } else if (event.type == LNET_EVENT_REPLY) {
4225 } while (rc2 <= 0 || !event.unlinked);
4229 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
4236 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4238 rc = -EPROTO; /* if I can't parse... */
4241 CERROR("%s: ping info too short %d\n",
4242 libcfs_id2str(id), nob);
4246 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4247 lnet_swap_pinginfo(pbuf);
4248 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4249 CERROR("%s: Unexpected magic %08x\n",
4250 libcfs_id2str(id), pbuf->pb_info.pi_magic);
4254 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4255 CERROR("%s: ping w/o NI status: 0x%x\n",
4256 libcfs_id2str(id), pbuf->pb_info.pi_features);
4260 if (nob < LNET_PING_INFO_SIZE(0)) {
4261 CERROR("%s: Short reply %d(%d min)\n",
4263 nob, (int)LNET_PING_INFO_SIZE(0));
4267 if (pbuf->pb_info.pi_nnis < n_ids)
4268 n_ids = pbuf->pb_info.pi_nnis;
4270 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4271 CERROR("%s: Short reply %d(%d expected)\n",
4273 nob, (int)LNET_PING_INFO_SIZE(n_ids));
4277 rc = -EFAULT; /* if I segv in copy_to_user()... */
4279 memset(&tmpid, 0, sizeof(tmpid));
4280 for (i = 0; i < n_ids; i++) {
4281 tmpid.pid = pbuf->pb_info.pi_pid;
4282 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4283 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4286 rc = pbuf->pb_info.pi_nnis;
4289 rc2 = LNetEQFree(eqh);
4291 CERROR("rc2 %d\n", rc2);
4294 fail_ping_buffer_decref:
4295 lnet_ping_buffer_decref(pbuf);
4300 lnet_discover(struct lnet_process_id id, __u32 force,
4301 struct lnet_process_id __user *ids, int n_ids)
4303 struct lnet_peer_ni *lpni;
4304 struct lnet_peer_ni *p;
4305 struct lnet_peer *lp;
4306 struct lnet_process_id *buf;
4310 int max_intf = lnet_interfaces_max;
4314 id.nid == LNET_NID_ANY)
4317 if (id.pid == LNET_PID_ANY)
4318 id.pid = LNET_PID_LUSTRE;
4321 * if the user buffer has more space than the max_intf
4322 * then only fill it up to max_intf
4324 if (n_ids > max_intf)
4327 buf_size = n_ids * sizeof(*buf);
4329 LIBCFS_ALLOC(buf, buf_size);
4333 cpt = lnet_net_lock_current();
4334 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4341 * Clearing the NIDS_UPTODATE flag ensures the peer will
4342 * be discovered, provided discovery has not been disabled.
4344 lp = lpni->lpni_peer_net->lpn_peer;
4345 spin_lock(&lp->lp_lock);
4346 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4347 /* If the force flag is set, force a PING and PUSH as well. */
4349 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4350 spin_unlock(&lp->lp_lock);
4351 rc = lnet_discover_peer_locked(lpni, cpt, true);
4355 /* Peer may have changed. */
4356 lp = lpni->lpni_peer_net->lpn_peer;
4357 if (lp->lp_nnis < n_ids)
4358 n_ids = lp->lp_nnis;
4362 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4363 buf[i].pid = id.pid;
4364 buf[i].nid = p->lpni_nid;
4369 lnet_net_unlock(cpt);
4372 if (copy_to_user(ids, buf, n_ids * sizeof(*buf)))
4378 lnet_peer_ni_decref_locked(lpni);
4380 lnet_net_unlock(cpt);
4382 LIBCFS_FREE(buf, buf_size);
4388 * Retrieve peer discovery status.
4390 * \retval 1 if lnet_peer_discovery_disabled is 0
4391 * \retval 0 if lnet_peer_discovery_disabled is 1
4394 LNetGetPeerDiscoveryStatus(void)
4396 return !lnet_peer_discovery_disabled;
4398 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);