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;
226 * This sequence number keeps track of how many times DLC was used to
227 * update the local NIs. It is incremented when a NI is added or
228 * removed and checked when sending a message to determine if there is
229 * a need to re-run the selection algorithm. See lnet_select_pathway()
230 * for more details on its usage.
232 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
234 static int lnet_ping(struct lnet_process_id id, signed long timeout,
235 struct lnet_process_id __user *ids, int n_ids);
237 static int lnet_discover(struct lnet_process_id id, __u32 force,
238 struct lnet_process_id __user *ids, int n_ids);
241 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
244 unsigned *sensitivity = (unsigned *)kp->arg;
247 rc = kstrtoul(val, 0, &value);
249 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
254 * The purpose of locking the api_mutex here is to ensure that
255 * the correct value ends up stored properly.
257 mutex_lock(&the_lnet.ln_api_mutex);
259 if (value > LNET_MAX_HEALTH_VALUE) {
260 mutex_unlock(&the_lnet.ln_api_mutex);
261 CERROR("Invalid health value. Maximum: %d value = %lu\n",
262 LNET_MAX_HEALTH_VALUE, value);
267 * if we're turning on health then use the health timeout
270 if (*sensitivity == 0 && value != 0) {
271 lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT;
272 lnet_retry_count = LNET_RETRY_COUNT_HEALTH_DEFAULT;
274 * if we're turning off health then use the no health timeout
277 } else if (*sensitivity != 0 && value == 0) {
278 lnet_transaction_timeout =
279 LNET_TRANSACTION_TIMEOUT_NO_HEALTH_DEFAULT;
280 lnet_retry_count = 0;
283 *sensitivity = value;
285 mutex_unlock(&the_lnet.ln_api_mutex);
291 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
294 unsigned *interval = (unsigned *)kp->arg;
297 rc = kstrtoul(val, 0, &value);
299 CERROR("Invalid module parameter value for 'lnet_recovery_interval'\n");
304 CERROR("lnet_recovery_interval must be at least 1 second\n");
309 * The purpose of locking the api_mutex here is to ensure that
310 * the correct value ends up stored properly.
312 mutex_lock(&the_lnet.ln_api_mutex);
316 mutex_unlock(&the_lnet.ln_api_mutex);
322 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
325 unsigned *discovery = (unsigned *)kp->arg;
327 struct lnet_ping_buffer *pbuf;
329 rc = kstrtoul(val, 0, &value);
331 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
335 value = (value) ? 1 : 0;
338 * The purpose of locking the api_mutex here is to ensure that
339 * the correct value ends up stored properly.
341 mutex_lock(&the_lnet.ln_api_mutex);
343 if (value == *discovery) {
344 mutex_unlock(&the_lnet.ln_api_mutex);
350 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
351 mutex_unlock(&the_lnet.ln_api_mutex);
355 /* tell peers that discovery setting has changed */
356 lnet_net_lock(LNET_LOCK_EX);
357 pbuf = the_lnet.ln_ping_target;
359 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
361 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
362 lnet_net_unlock(LNET_LOCK_EX);
364 lnet_push_update_to_peers(1);
366 mutex_unlock(&the_lnet.ln_api_mutex);
372 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
375 unsigned int *drop_asym_route = (unsigned int *)kp->arg;
378 rc = kstrtoul(val, 0, &value);
380 CERROR("Invalid module parameter value for "
381 "'lnet_drop_asym_route'\n");
386 * The purpose of locking the api_mutex here is to ensure that
387 * the correct value ends up stored properly.
389 mutex_lock(&the_lnet.ln_api_mutex);
391 if (value == *drop_asym_route) {
392 mutex_unlock(&the_lnet.ln_api_mutex);
396 *drop_asym_route = value;
398 mutex_unlock(&the_lnet.ln_api_mutex);
404 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
407 unsigned *transaction_to = (unsigned *)kp->arg;
410 rc = kstrtoul(val, 0, &value);
412 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
417 * The purpose of locking the api_mutex here is to ensure that
418 * the correct value ends up stored properly.
420 mutex_lock(&the_lnet.ln_api_mutex);
422 if (value < lnet_retry_count || value == 0) {
423 mutex_unlock(&the_lnet.ln_api_mutex);
424 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
425 "Has to be greater than lnet_retry_count (%u)\n",
426 value, lnet_retry_count);
430 if (value == *transaction_to) {
431 mutex_unlock(&the_lnet.ln_api_mutex);
435 *transaction_to = value;
436 if (lnet_retry_count == 0)
437 lnet_lnd_timeout = value;
439 lnet_lnd_timeout = value / lnet_retry_count;
441 mutex_unlock(&the_lnet.ln_api_mutex);
447 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
450 unsigned *retry_count = (unsigned *)kp->arg;
453 rc = kstrtoul(val, 0, &value);
455 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
460 * The purpose of locking the api_mutex here is to ensure that
461 * the correct value ends up stored properly.
463 mutex_lock(&the_lnet.ln_api_mutex);
465 if (lnet_health_sensitivity == 0) {
466 mutex_unlock(&the_lnet.ln_api_mutex);
467 CERROR("Can not set retry_count when health feature is turned off\n");
471 if (value > lnet_transaction_timeout) {
472 mutex_unlock(&the_lnet.ln_api_mutex);
473 CERROR("Invalid value for lnet_retry_count (%lu). "
474 "Has to be smaller than lnet_transaction_timeout (%u)\n",
475 value, lnet_transaction_timeout);
479 *retry_count = value;
482 lnet_lnd_timeout = lnet_transaction_timeout;
484 lnet_lnd_timeout = lnet_transaction_timeout / value;
486 mutex_unlock(&the_lnet.ln_api_mutex);
492 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
496 rc = kstrtoint(val, 0, &value);
498 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
502 if (value < LNET_INTERFACES_MIN) {
503 CWARN("max interfaces provided are too small, setting to %d\n",
504 LNET_INTERFACES_MAX_DEFAULT);
505 value = LNET_INTERFACES_MAX_DEFAULT;
508 *(int *)kp->arg = value;
514 lnet_get_routes(void)
520 lnet_get_networks(void)
525 if (*networks != 0 && *ip2nets != 0) {
526 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
527 "'ip2nets' but not both at once\n");
532 rc = lnet_parse_ip2nets(&nets, ip2nets);
533 return (rc == 0) ? nets : NULL;
543 lnet_init_locks(void)
545 spin_lock_init(&the_lnet.ln_eq_wait_lock);
546 spin_lock_init(&the_lnet.ln_msg_resend_lock);
547 init_waitqueue_head(&the_lnet.ln_eq_waitq);
548 init_waitqueue_head(&the_lnet.ln_mt_waitq);
549 mutex_init(&the_lnet.ln_lnd_mutex);
553 lnet_fini_locks(void)
557 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
558 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
562 lnet_descriptor_setup(void)
564 /* create specific kmem_cache for MEs and small MDs (i.e., originally
565 * allocated in <size-xxx> kmem_cache).
567 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
569 if (!lnet_mes_cachep)
572 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
573 LNET_SMALL_MD_SIZE, 0, 0,
575 if (!lnet_small_mds_cachep)
582 lnet_descriptor_cleanup(void)
585 if (lnet_small_mds_cachep) {
586 kmem_cache_destroy(lnet_small_mds_cachep);
587 lnet_small_mds_cachep = NULL;
590 if (lnet_mes_cachep) {
591 kmem_cache_destroy(lnet_mes_cachep);
592 lnet_mes_cachep = NULL;
597 lnet_create_remote_nets_table(void)
600 struct list_head *hash;
602 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
603 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
604 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
606 CERROR("Failed to create remote nets hash table\n");
610 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
611 INIT_LIST_HEAD(&hash[i]);
612 the_lnet.ln_remote_nets_hash = hash;
617 lnet_destroy_remote_nets_table(void)
621 if (the_lnet.ln_remote_nets_hash == NULL)
624 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
625 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
627 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
628 LNET_REMOTE_NETS_HASH_SIZE *
629 sizeof(the_lnet.ln_remote_nets_hash[0]));
630 the_lnet.ln_remote_nets_hash = NULL;
634 lnet_destroy_locks(void)
636 if (the_lnet.ln_res_lock != NULL) {
637 cfs_percpt_lock_free(the_lnet.ln_res_lock);
638 the_lnet.ln_res_lock = NULL;
641 if (the_lnet.ln_net_lock != NULL) {
642 cfs_percpt_lock_free(the_lnet.ln_net_lock);
643 the_lnet.ln_net_lock = NULL;
650 lnet_create_locks(void)
654 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
655 if (the_lnet.ln_res_lock == NULL)
658 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
659 if (the_lnet.ln_net_lock == NULL)
665 lnet_destroy_locks();
669 static void lnet_assert_wire_constants(void)
671 /* Wire protocol assertions generated by 'wirecheck'
672 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
673 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
674 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
677 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
678 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
679 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
680 CLASSERT(LNET_MSG_ACK == 0);
681 CLASSERT(LNET_MSG_PUT == 1);
682 CLASSERT(LNET_MSG_GET == 2);
683 CLASSERT(LNET_MSG_REPLY == 3);
684 CLASSERT(LNET_MSG_HELLO == 4);
686 /* Checks for struct lnet_handle_wire */
687 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
688 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
689 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
690 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
691 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
693 /* Checks for struct struct lnet_magicversion */
694 CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
695 CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
696 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
697 CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
698 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
699 CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
700 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
702 /* Checks for struct struct lnet_hdr */
703 CLASSERT((int)sizeof(struct lnet_hdr) == 72);
704 CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
705 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
706 CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
707 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
708 CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
709 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
710 CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
711 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
712 CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
713 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
714 CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
715 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
716 CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
717 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
720 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
721 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
722 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
723 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
724 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
725 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
728 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
729 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
730 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
731 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
732 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
733 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
734 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
735 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
736 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
737 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
740 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
741 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
742 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
743 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
744 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
745 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
746 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
747 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
748 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
749 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
752 CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
753 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
756 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
757 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
758 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
759 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
761 /* Checks for struct lnet_ni_status and related constants */
762 CLASSERT(LNET_NI_STATUS_INVALID == 0x00000000);
763 CLASSERT(LNET_NI_STATUS_UP == 0x15aac0de);
764 CLASSERT(LNET_NI_STATUS_DOWN == 0xdeadface);
766 /* Checks for struct lnet_ni_status */
767 CLASSERT((int)sizeof(struct lnet_ni_status) == 16);
768 CLASSERT((int)offsetof(struct lnet_ni_status, ns_nid) == 0);
769 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) == 8);
770 CLASSERT((int)offsetof(struct lnet_ni_status, ns_status) == 8);
771 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_status) == 4);
772 CLASSERT((int)offsetof(struct lnet_ni_status, ns_unused) == 12);
773 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) == 4);
775 /* Checks for struct lnet_ping_info and related constants */
776 CLASSERT(LNET_PROTO_PING_MAGIC == 0x70696E67);
777 CLASSERT(LNET_PING_FEAT_INVAL == 0);
778 CLASSERT(LNET_PING_FEAT_BASE == 1);
779 CLASSERT(LNET_PING_FEAT_NI_STATUS == 2);
780 CLASSERT(LNET_PING_FEAT_RTE_DISABLED == 4);
781 CLASSERT(LNET_PING_FEAT_MULTI_RAIL == 8);
782 CLASSERT(LNET_PING_FEAT_DISCOVERY == 16);
783 CLASSERT(LNET_PING_FEAT_BITS == 31);
785 /* Checks for struct lnet_ping_info */
786 CLASSERT((int)sizeof(struct lnet_ping_info) == 16);
787 CLASSERT((int)offsetof(struct lnet_ping_info, pi_magic) == 0);
788 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) == 4);
789 CLASSERT((int)offsetof(struct lnet_ping_info, pi_features) == 4);
790 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_features) == 4);
791 CLASSERT((int)offsetof(struct lnet_ping_info, pi_pid) == 8);
792 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) == 4);
793 CLASSERT((int)offsetof(struct lnet_ping_info, pi_nnis) == 12);
794 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) == 4);
795 CLASSERT((int)offsetof(struct lnet_ping_info, pi_ni) == 16);
796 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) == 0);
799 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
801 struct lnet_lnd *lnd;
802 struct list_head *tmp;
804 /* holding lnd mutex */
805 list_for_each(tmp, &the_lnet.ln_lnds) {
806 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
808 if (lnd->lnd_type == type)
815 lnet_get_lnd_timeout(void)
817 return lnet_lnd_timeout;
819 EXPORT_SYMBOL(lnet_get_lnd_timeout);
822 lnet_register_lnd(struct lnet_lnd *lnd)
824 mutex_lock(&the_lnet.ln_lnd_mutex);
826 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
827 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
829 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
830 lnd->lnd_refcount = 0;
832 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
834 mutex_unlock(&the_lnet.ln_lnd_mutex);
836 EXPORT_SYMBOL(lnet_register_lnd);
839 lnet_unregister_lnd(struct lnet_lnd *lnd)
841 mutex_lock(&the_lnet.ln_lnd_mutex);
843 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
844 LASSERT(lnd->lnd_refcount == 0);
846 list_del(&lnd->lnd_list);
847 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
849 mutex_unlock(&the_lnet.ln_lnd_mutex);
851 EXPORT_SYMBOL(lnet_unregister_lnd);
854 lnet_counters_get_common(struct lnet_counters_common *common)
856 struct lnet_counters *ctr;
859 memset(common, 0, sizeof(*common));
861 lnet_net_lock(LNET_LOCK_EX);
863 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
864 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
865 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
866 common->lcc_errors += ctr->lct_common.lcc_errors;
867 common->lcc_send_count += ctr->lct_common.lcc_send_count;
868 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
869 common->lcc_route_count += ctr->lct_common.lcc_route_count;
870 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
871 common->lcc_send_length += ctr->lct_common.lcc_send_length;
872 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
873 common->lcc_route_length += ctr->lct_common.lcc_route_length;
874 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
876 lnet_net_unlock(LNET_LOCK_EX);
878 EXPORT_SYMBOL(lnet_counters_get_common);
881 lnet_counters_get(struct lnet_counters *counters)
883 struct lnet_counters *ctr;
884 struct lnet_counters_health *health = &counters->lct_health;
887 memset(counters, 0, sizeof(*counters));
889 lnet_counters_get_common(&counters->lct_common);
891 lnet_net_lock(LNET_LOCK_EX);
893 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
894 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
895 health->lch_resend_count += ctr->lct_health.lch_resend_count;
896 health->lch_response_timeout_count +=
897 ctr->lct_health.lch_response_timeout_count;
898 health->lch_local_interrupt_count +=
899 ctr->lct_health.lch_local_interrupt_count;
900 health->lch_local_dropped_count +=
901 ctr->lct_health.lch_local_dropped_count;
902 health->lch_local_aborted_count +=
903 ctr->lct_health.lch_local_aborted_count;
904 health->lch_local_no_route_count +=
905 ctr->lct_health.lch_local_no_route_count;
906 health->lch_local_timeout_count +=
907 ctr->lct_health.lch_local_timeout_count;
908 health->lch_local_error_count +=
909 ctr->lct_health.lch_local_error_count;
910 health->lch_remote_dropped_count +=
911 ctr->lct_health.lch_remote_dropped_count;
912 health->lch_remote_error_count +=
913 ctr->lct_health.lch_remote_error_count;
914 health->lch_remote_timeout_count +=
915 ctr->lct_health.lch_remote_timeout_count;
916 health->lch_network_timeout_count +=
917 ctr->lct_health.lch_network_timeout_count;
919 lnet_net_unlock(LNET_LOCK_EX);
921 EXPORT_SYMBOL(lnet_counters_get);
924 lnet_counters_reset(void)
926 struct lnet_counters *counters;
929 lnet_net_lock(LNET_LOCK_EX);
931 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
932 memset(counters, 0, sizeof(struct lnet_counters));
934 lnet_net_unlock(LNET_LOCK_EX);
938 lnet_res_type2str(int type)
943 case LNET_COOKIE_TYPE_MD:
945 case LNET_COOKIE_TYPE_ME:
947 case LNET_COOKIE_TYPE_EQ:
953 lnet_res_container_cleanup(struct lnet_res_container *rec)
957 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
960 while (!list_empty(&rec->rec_active)) {
961 struct list_head *e = rec->rec_active.next;
964 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
965 lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
967 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
968 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
970 } else { /* NB: Active MEs should be attached on portals */
977 /* Found alive MD/ME/EQ, user really should unlink/free
978 * all of them before finalize LNet, but if someone didn't,
979 * we have to recycle garbage for him */
980 CERROR("%d active elements on exit of %s container\n",
981 count, lnet_res_type2str(rec->rec_type));
984 if (rec->rec_lh_hash != NULL) {
985 LIBCFS_FREE(rec->rec_lh_hash,
986 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
987 rec->rec_lh_hash = NULL;
990 rec->rec_type = 0; /* mark it as finalized */
994 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
999 LASSERT(rec->rec_type == 0);
1001 rec->rec_type = type;
1002 INIT_LIST_HEAD(&rec->rec_active);
1004 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1006 /* Arbitrary choice of hash table size */
1007 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1008 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1009 if (rec->rec_lh_hash == NULL) {
1014 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1015 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1020 CERROR("Failed to setup %s resource container\n",
1021 lnet_res_type2str(type));
1022 lnet_res_container_cleanup(rec);
1027 lnet_res_containers_destroy(struct lnet_res_container **recs)
1029 struct lnet_res_container *rec;
1032 cfs_percpt_for_each(rec, i, recs)
1033 lnet_res_container_cleanup(rec);
1035 cfs_percpt_free(recs);
1038 static struct lnet_res_container **
1039 lnet_res_containers_create(int type)
1041 struct lnet_res_container **recs;
1042 struct lnet_res_container *rec;
1046 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1048 CERROR("Failed to allocate %s resource containers\n",
1049 lnet_res_type2str(type));
1053 cfs_percpt_for_each(rec, i, recs) {
1054 rc = lnet_res_container_setup(rec, i, type);
1056 lnet_res_containers_destroy(recs);
1064 struct lnet_libhandle *
1065 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1067 /* ALWAYS called with lnet_res_lock held */
1068 struct list_head *head;
1069 struct lnet_libhandle *lh;
1072 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1075 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1076 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1078 list_for_each_entry(lh, head, lh_hash_chain) {
1079 if (lh->lh_cookie == cookie)
1087 lnet_res_lh_initialize(struct lnet_res_container *rec,
1088 struct lnet_libhandle *lh)
1090 /* ALWAYS called with lnet_res_lock held */
1091 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1094 lh->lh_cookie = rec->rec_lh_cookie;
1095 rec->rec_lh_cookie += 1 << ibits;
1097 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1099 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1102 static int lnet_unprepare(void);
1105 lnet_prepare(lnet_pid_t requested_pid)
1107 /* Prepare to bring up the network */
1108 struct lnet_res_container **recs;
1111 if (requested_pid == LNET_PID_ANY) {
1112 /* Don't instantiate LNET just for me */
1116 LASSERT(the_lnet.ln_refcount == 0);
1118 the_lnet.ln_routing = 0;
1120 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1121 the_lnet.ln_pid = requested_pid;
1123 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1124 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1125 INIT_LIST_HEAD(&the_lnet.ln_nets);
1126 INIT_LIST_HEAD(&the_lnet.ln_routers);
1127 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1128 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1129 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1130 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1131 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1132 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1133 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1134 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1135 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
1137 rc = lnet_descriptor_setup();
1141 rc = lnet_create_remote_nets_table();
1146 * NB the interface cookie in wire handles guards against delayed
1147 * replies and ACKs appearing valid after reboot.
1149 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1151 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1152 sizeof(struct lnet_counters));
1153 if (the_lnet.ln_counters == NULL) {
1154 CERROR("Failed to allocate counters for LNet\n");
1159 rc = lnet_peer_tables_create();
1163 rc = lnet_msg_containers_create();
1167 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1168 LNET_COOKIE_TYPE_EQ);
1172 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
1178 the_lnet.ln_me_containers = recs;
1180 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1186 the_lnet.ln_md_containers = recs;
1188 rc = lnet_portals_create();
1190 CERROR("Failed to create portals for LNet: %d\n", rc);
1202 lnet_unprepare (void)
1206 /* NB no LNET_LOCK since this is the last reference. All LND instances
1207 * have shut down already, so it is safe to unlink and free all
1208 * descriptors, even those that appear committed to a network op (eg MD
1209 * with non-zero pending count) */
1211 lnet_fail_nid(LNET_NID_ANY, 0);
1213 LASSERT(the_lnet.ln_refcount == 0);
1214 LASSERT(list_empty(&the_lnet.ln_test_peers));
1215 LASSERT(list_empty(&the_lnet.ln_nets));
1217 if (!LNetEQHandleIsInvalid(the_lnet.ln_mt_eqh)) {
1218 rc = LNetEQFree(the_lnet.ln_mt_eqh);
1219 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
1223 lnet_portals_destroy();
1225 if (the_lnet.ln_md_containers != NULL) {
1226 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1227 the_lnet.ln_md_containers = NULL;
1230 if (the_lnet.ln_me_containers != NULL) {
1231 lnet_res_containers_destroy(the_lnet.ln_me_containers);
1232 the_lnet.ln_me_containers = NULL;
1235 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1237 lnet_msg_containers_destroy();
1239 lnet_rtrpools_free(0);
1241 if (the_lnet.ln_counters != NULL) {
1242 cfs_percpt_free(the_lnet.ln_counters);
1243 the_lnet.ln_counters = NULL;
1245 lnet_destroy_remote_nets_table();
1246 lnet_descriptor_cleanup();
1252 lnet_net2ni_locked(__u32 net_id, int cpt)
1255 struct lnet_net *net;
1257 LASSERT(cpt != LNET_LOCK_EX);
1259 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1260 if (net->net_id == net_id) {
1261 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1271 lnet_net2ni_addref(__u32 net)
1276 ni = lnet_net2ni_locked(net, 0);
1278 lnet_ni_addref_locked(ni, 0);
1283 EXPORT_SYMBOL(lnet_net2ni_addref);
1286 lnet_get_net_locked(__u32 net_id)
1288 struct lnet_net *net;
1290 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1291 if (net->net_id == net_id)
1299 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1304 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1309 val = hash_long(key, LNET_CPT_BITS);
1310 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1314 return (unsigned int)(key + val + (val >> 1)) % number;
1318 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1320 struct lnet_net *net;
1322 /* must called with hold of lnet_net_lock */
1323 if (LNET_CPT_NUMBER == 1)
1324 return 0; /* the only one */
1327 * If NI is provided then use the CPT identified in the NI cpt
1328 * list if one exists. If one doesn't exist, then that NI is
1329 * associated with all CPTs and it follows that the net it belongs
1330 * to is implicitly associated with all CPTs, so just hash the nid
1334 if (ni->ni_cpts != NULL)
1335 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1338 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1341 /* no NI provided so look at the net */
1342 net = lnet_get_net_locked(LNET_NIDNET(nid));
1344 if (net != NULL && net->net_cpts != NULL) {
1345 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1348 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1352 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1357 if (LNET_CPT_NUMBER == 1)
1358 return 0; /* the only one */
1360 cpt = lnet_net_lock_current();
1362 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1364 lnet_net_unlock(cpt);
1368 EXPORT_SYMBOL(lnet_cpt_of_nid);
1371 lnet_islocalnet(__u32 net_id)
1373 struct lnet_net *net;
1377 cpt = lnet_net_lock_current();
1379 net = lnet_get_net_locked(net_id);
1381 local = net != NULL;
1383 lnet_net_unlock(cpt);
1389 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1391 struct lnet_net *net;
1394 LASSERT(cpt != LNET_LOCK_EX);
1396 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1397 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1398 if (ni->ni_nid == nid)
1407 lnet_nid2ni_addref(lnet_nid_t nid)
1412 ni = lnet_nid2ni_locked(nid, 0);
1414 lnet_ni_addref_locked(ni, 0);
1419 EXPORT_SYMBOL(lnet_nid2ni_addref);
1422 lnet_islocalnid(lnet_nid_t nid)
1427 cpt = lnet_net_lock_current();
1428 ni = lnet_nid2ni_locked(nid, cpt);
1429 lnet_net_unlock(cpt);
1435 lnet_count_acceptor_nets(void)
1437 /* Return the # of NIs that need the acceptor. */
1439 struct lnet_net *net;
1442 cpt = lnet_net_lock_current();
1443 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1444 /* all socklnd type networks should have the acceptor
1446 if (net->net_lnd->lnd_accept != NULL)
1450 lnet_net_unlock(cpt);
1455 struct lnet_ping_buffer *
1456 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1458 struct lnet_ping_buffer *pbuf;
1460 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1462 pbuf->pb_nnis = nnis;
1463 atomic_set(&pbuf->pb_refcnt, 1);
1470 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1472 LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1473 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1476 static struct lnet_ping_buffer *
1477 lnet_ping_target_create(int nnis)
1479 struct lnet_ping_buffer *pbuf;
1481 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1483 CERROR("Can't allocate ping source [%d]\n", nnis);
1487 pbuf->pb_info.pi_nnis = nnis;
1488 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1489 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1490 pbuf->pb_info.pi_features =
1491 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1497 lnet_get_net_ni_count_locked(struct lnet_net *net)
1502 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1509 lnet_get_net_ni_count_pre(struct lnet_net *net)
1514 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1521 lnet_get_ni_count(void)
1524 struct lnet_net *net;
1529 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1530 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1540 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1544 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1546 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1548 /* Loopback is guaranteed to be present */
1549 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1551 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1557 lnet_ping_target_destroy(void)
1559 struct lnet_net *net;
1562 lnet_net_lock(LNET_LOCK_EX);
1564 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1565 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1567 ni->ni_status = NULL;
1572 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1573 the_lnet.ln_ping_target = NULL;
1575 lnet_net_unlock(LNET_LOCK_EX);
1579 lnet_ping_target_event_handler(struct lnet_event *event)
1581 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1583 if (event->unlinked)
1584 lnet_ping_buffer_decref(pbuf);
1588 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1589 struct lnet_handle_md *ping_mdh,
1590 int ni_count, bool set_eq)
1592 struct lnet_process_id id = {
1593 .nid = LNET_NID_ANY,
1596 struct lnet_handle_me me_handle;
1597 struct lnet_md md = { NULL };
1601 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1602 &the_lnet.ln_ping_target_eq);
1604 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1609 *ppbuf = lnet_ping_target_create(ni_count);
1610 if (*ppbuf == NULL) {
1615 /* Ping target ME/MD */
1616 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1617 LNET_PROTO_PING_MATCHBITS, 0,
1618 LNET_UNLINK, LNET_INS_AFTER,
1621 CERROR("Can't create ping target ME: %d\n", rc);
1622 goto fail_decref_ping_buffer;
1625 /* initialize md content */
1626 md.start = &(*ppbuf)->pb_info;
1627 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1628 md.threshold = LNET_MD_THRESH_INF;
1630 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1631 LNET_MD_MANAGE_REMOTE;
1632 md.eq_handle = the_lnet.ln_ping_target_eq;
1633 md.user_ptr = *ppbuf;
1635 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, ping_mdh);
1637 CERROR("Can't attach ping target MD: %d\n", rc);
1638 goto fail_unlink_ping_me;
1640 lnet_ping_buffer_addref(*ppbuf);
1644 fail_unlink_ping_me:
1645 rc2 = LNetMEUnlink(me_handle);
1647 fail_decref_ping_buffer:
1648 LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1649 lnet_ping_buffer_decref(*ppbuf);
1653 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1660 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1661 struct lnet_handle_md *ping_mdh)
1663 sigset_t blocked = cfs_block_allsigs();
1665 LNetMDUnlink(*ping_mdh);
1666 LNetInvalidateMDHandle(ping_mdh);
1668 /* NB the MD could be busy; this just starts the unlink */
1669 while (lnet_ping_buffer_numref(pbuf) > 1) {
1670 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1671 set_current_state(TASK_UNINTERRUPTIBLE);
1672 schedule_timeout(cfs_time_seconds(1));
1675 cfs_restore_sigs(blocked);
1679 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1682 struct lnet_net *net;
1683 struct lnet_ni_status *ns;
1688 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1689 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1690 LASSERT(i < pbuf->pb_nnis);
1692 ns = &pbuf->pb_info.pi_ni[i];
1694 ns->ns_nid = ni->ni_nid;
1697 ns->ns_status = (ni->ni_status != NULL) ?
1698 ni->ni_status->ns_status :
1707 * We (ab)use the ns_status of the loopback interface to
1708 * transmit the sequence number. The first interface listed
1709 * must be the loopback interface.
1711 rc = lnet_ping_info_validate(&pbuf->pb_info);
1713 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1716 LNET_PING_BUFFER_SEQNO(pbuf) =
1717 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1721 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1722 struct lnet_handle_md ping_mdh)
1724 struct lnet_ping_buffer *old_pbuf = NULL;
1725 struct lnet_handle_md old_ping_md;
1727 /* switch the NIs to point to the new ping info created */
1728 lnet_net_lock(LNET_LOCK_EX);
1730 if (!the_lnet.ln_routing)
1731 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1732 if (!lnet_peer_discovery_disabled)
1733 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1735 /* Ensure only known feature bits have been set. */
1736 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1737 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1739 lnet_ping_target_install_locked(pbuf);
1741 if (the_lnet.ln_ping_target) {
1742 old_pbuf = the_lnet.ln_ping_target;
1743 old_ping_md = the_lnet.ln_ping_target_md;
1745 the_lnet.ln_ping_target_md = ping_mdh;
1746 the_lnet.ln_ping_target = pbuf;
1748 lnet_net_unlock(LNET_LOCK_EX);
1751 /* unlink and free the old ping info */
1752 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1753 lnet_ping_buffer_decref(old_pbuf);
1756 lnet_push_update_to_peers(0);
1760 lnet_ping_target_fini(void)
1764 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1765 &the_lnet.ln_ping_target_md);
1767 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1770 lnet_ping_target_destroy();
1773 /* Resize the push target. */
1774 int lnet_push_target_resize(void)
1776 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1777 struct lnet_md md = { NULL };
1778 struct lnet_handle_me meh;
1779 struct lnet_handle_md mdh;
1780 struct lnet_handle_md old_mdh;
1781 struct lnet_ping_buffer *pbuf;
1782 struct lnet_ping_buffer *old_pbuf;
1783 int nnis = the_lnet.ln_push_target_nnis;
1791 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1797 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1798 LNET_PROTO_PING_MATCHBITS, 0,
1799 LNET_UNLINK, LNET_INS_AFTER,
1802 CERROR("Can't create push target ME: %d\n", rc);
1803 goto fail_decref_pbuf;
1806 /* initialize md content */
1807 md.start = &pbuf->pb_info;
1808 md.length = LNET_PING_INFO_SIZE(nnis);
1809 md.threshold = LNET_MD_THRESH_INF;
1811 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1812 LNET_MD_MANAGE_REMOTE;
1814 md.eq_handle = the_lnet.ln_push_target_eq;
1816 rc = LNetMDAttach(meh, md, LNET_RETAIN, &mdh);
1818 CERROR("Can't attach push MD: %d\n", rc);
1819 goto fail_unlink_meh;
1821 lnet_ping_buffer_addref(pbuf);
1823 lnet_net_lock(LNET_LOCK_EX);
1824 old_pbuf = the_lnet.ln_push_target;
1825 old_mdh = the_lnet.ln_push_target_md;
1826 the_lnet.ln_push_target = pbuf;
1827 the_lnet.ln_push_target_md = mdh;
1828 lnet_net_unlock(LNET_LOCK_EX);
1831 LNetMDUnlink(old_mdh);
1832 lnet_ping_buffer_decref(old_pbuf);
1835 if (nnis < the_lnet.ln_push_target_nnis)
1838 CDEBUG(D_NET, "nnis %d success\n", nnis);
1845 lnet_ping_buffer_decref(pbuf);
1847 CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1851 static void lnet_push_target_event_handler(struct lnet_event *ev)
1853 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1855 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1856 lnet_swap_pinginfo(pbuf);
1858 lnet_peer_push_event(ev);
1860 lnet_ping_buffer_decref(pbuf);
1863 /* Initialize the push target. */
1864 static int lnet_push_target_init(void)
1868 if (the_lnet.ln_push_target)
1871 rc = LNetEQAlloc(0, lnet_push_target_event_handler,
1872 &the_lnet.ln_push_target_eq);
1874 CERROR("Can't allocated push target EQ: %d\n", rc);
1878 /* Start at the required minimum, we'll enlarge if required. */
1879 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1881 rc = lnet_push_target_resize();
1884 LNetEQFree(the_lnet.ln_push_target_eq);
1885 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1891 /* Clean up the push target. */
1892 static void lnet_push_target_fini(void)
1894 if (!the_lnet.ln_push_target)
1897 /* Unlink and invalidate to prevent new references. */
1898 LNetMDUnlink(the_lnet.ln_push_target_md);
1899 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1901 /* Wait for the unlink to complete. */
1902 while (lnet_ping_buffer_numref(the_lnet.ln_push_target) > 1) {
1903 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1904 set_current_state(TASK_UNINTERRUPTIBLE);
1905 schedule_timeout(cfs_time_seconds(1));
1908 lnet_ping_buffer_decref(the_lnet.ln_push_target);
1909 the_lnet.ln_push_target = NULL;
1910 the_lnet.ln_push_target_nnis = 0;
1912 LNetEQFree(the_lnet.ln_push_target_eq);
1913 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1917 lnet_ni_tq_credits(struct lnet_ni *ni)
1921 LASSERT(ni->ni_ncpts >= 1);
1923 if (ni->ni_ncpts == 1)
1924 return ni->ni_net->net_tunables.lct_max_tx_credits;
1926 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1927 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1928 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1934 lnet_ni_unlink_locked(struct lnet_ni *ni)
1936 /* move it to zombie list and nobody can find it anymore */
1937 LASSERT(!list_empty(&ni->ni_netlist));
1938 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1939 lnet_ni_decref_locked(ni, 0);
1943 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1948 struct list_head *zombie_list = &net->net_ni_zombie;
1951 * Now wait for the NIs I just nuked to show up on the zombie
1952 * list and shut them down in guaranteed thread context
1955 while (!list_empty(zombie_list)) {
1959 ni = list_entry(zombie_list->next,
1960 struct lnet_ni, ni_netlist);
1961 list_del_init(&ni->ni_netlist);
1962 /* the ni should be in deleting state. If it's not it's
1964 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
1965 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1968 /* still busy, add it back to zombie list */
1969 list_add(&ni->ni_netlist, zombie_list);
1973 if (!list_empty(&ni->ni_netlist)) {
1974 lnet_net_unlock(LNET_LOCK_EX);
1976 if ((i & (-i)) == i) {
1978 "Waiting for zombie LNI %s\n",
1979 libcfs_nid2str(ni->ni_nid));
1981 set_current_state(TASK_UNINTERRUPTIBLE);
1982 schedule_timeout(cfs_time_seconds(1));
1983 lnet_net_lock(LNET_LOCK_EX);
1987 lnet_net_unlock(LNET_LOCK_EX);
1989 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1991 LASSERT(!in_interrupt());
1992 (net->net_lnd->lnd_shutdown)(ni);
1995 CDEBUG(D_LNI, "Removed LNI %s\n",
1996 libcfs_nid2str(ni->ni_nid));
2000 lnet_net_lock(LNET_LOCK_EX);
2004 /* shutdown down the NI and release refcount */
2006 lnet_shutdown_lndni(struct lnet_ni *ni)
2009 struct lnet_net *net = ni->ni_net;
2011 lnet_net_lock(LNET_LOCK_EX);
2013 ni->ni_state = LNET_NI_STATE_DELETING;
2015 lnet_ni_unlink_locked(ni);
2016 lnet_incr_dlc_seq();
2017 lnet_net_unlock(LNET_LOCK_EX);
2019 /* clear messages for this NI on the lazy portal */
2020 for (i = 0; i < the_lnet.ln_nportals; i++)
2021 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2023 lnet_net_lock(LNET_LOCK_EX);
2024 lnet_clear_zombies_nis_locked(net);
2025 lnet_net_unlock(LNET_LOCK_EX);
2029 lnet_shutdown_lndnet(struct lnet_net *net)
2033 lnet_net_lock(LNET_LOCK_EX);
2035 net->net_state = LNET_NET_STATE_DELETING;
2037 list_del_init(&net->net_list);
2039 while (!list_empty(&net->net_ni_list)) {
2040 ni = list_entry(net->net_ni_list.next,
2041 struct lnet_ni, ni_netlist);
2042 lnet_net_unlock(LNET_LOCK_EX);
2043 lnet_shutdown_lndni(ni);
2044 lnet_net_lock(LNET_LOCK_EX);
2047 lnet_net_unlock(LNET_LOCK_EX);
2049 /* Do peer table cleanup for this net */
2050 lnet_peer_tables_cleanup(net);
2052 lnet_net_lock(LNET_LOCK_EX);
2054 * decrement ref count on lnd only when the entire network goes
2057 net->net_lnd->lnd_refcount--;
2059 lnet_net_unlock(LNET_LOCK_EX);
2065 lnet_shutdown_lndnets(void)
2067 struct lnet_net *net;
2068 struct list_head resend;
2069 struct lnet_msg *msg, *tmp;
2071 INIT_LIST_HEAD(&resend);
2073 /* NB called holding the global mutex */
2075 /* All quiet on the API front */
2076 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2077 LASSERT(the_lnet.ln_refcount == 0);
2079 lnet_net_lock(LNET_LOCK_EX);
2080 the_lnet.ln_state = LNET_STATE_STOPPING;
2082 while (!list_empty(&the_lnet.ln_nets)) {
2084 * move the nets to the zombie list to avoid them being
2085 * picked up for new work. LONET is also included in the
2086 * Nets that will be moved to the zombie list
2088 net = list_entry(the_lnet.ln_nets.next,
2089 struct lnet_net, net_list);
2090 list_move(&net->net_list, &the_lnet.ln_net_zombie);
2093 /* Drop the cached loopback Net. */
2094 if (the_lnet.ln_loni != NULL) {
2095 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2096 the_lnet.ln_loni = NULL;
2098 lnet_net_unlock(LNET_LOCK_EX);
2100 /* iterate through the net zombie list and delete each net */
2101 while (!list_empty(&the_lnet.ln_net_zombie)) {
2102 net = list_entry(the_lnet.ln_net_zombie.next,
2103 struct lnet_net, net_list);
2104 lnet_shutdown_lndnet(net);
2107 spin_lock(&the_lnet.ln_msg_resend_lock);
2108 list_splice(&the_lnet.ln_msg_resend, &resend);
2109 spin_unlock(&the_lnet.ln_msg_resend_lock);
2111 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2112 list_del_init(&msg->msg_list);
2113 msg->msg_no_resend = true;
2114 lnet_finalize(msg, -ECANCELED);
2117 lnet_net_lock(LNET_LOCK_EX);
2118 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2119 lnet_net_unlock(LNET_LOCK_EX);
2123 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2126 struct lnet_tx_queue *tq;
2128 struct lnet_net *net = ni->ni_net;
2130 mutex_lock(&the_lnet.ln_lnd_mutex);
2133 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2134 ni->ni_lnd_tunables_set = true;
2137 rc = (net->net_lnd->lnd_startup)(ni);
2139 mutex_unlock(&the_lnet.ln_lnd_mutex);
2142 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2143 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2144 lnet_net_lock(LNET_LOCK_EX);
2145 net->net_lnd->lnd_refcount--;
2146 lnet_net_unlock(LNET_LOCK_EX);
2151 ni->ni_state = LNET_NI_STATE_ACTIVE;
2154 /* We keep a reference on the loopback net through the loopback NI */
2155 if (net->net_lnd->lnd_type == LOLND) {
2157 LASSERT(the_lnet.ln_loni == NULL);
2158 the_lnet.ln_loni = ni;
2159 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2160 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2161 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2162 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2166 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2167 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2168 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2169 libcfs_lnd2str(net->net_lnd->lnd_type),
2170 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2172 /* shutdown the NI since if we get here then it must've already
2175 lnet_shutdown_lndni(ni);
2179 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2180 tq->tq_credits_min =
2181 tq->tq_credits_max =
2182 tq->tq_credits = lnet_ni_tq_credits(ni);
2185 atomic_set(&ni->ni_tx_credits,
2186 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2187 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2189 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2190 libcfs_nid2str(ni->ni_nid),
2191 ni->ni_net->net_tunables.lct_peer_tx_credits,
2192 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2193 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2194 ni->ni_net->net_tunables.lct_peer_timeout);
2203 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2206 struct lnet_net *net_l = NULL;
2207 struct list_head local_ni_list;
2211 struct lnet_lnd *lnd;
2213 net->net_tunables.lct_peer_timeout;
2215 net->net_tunables.lct_max_tx_credits;
2216 int peerrtrcredits =
2217 net->net_tunables.lct_peer_rtr_credits;
2219 INIT_LIST_HEAD(&local_ni_list);
2222 * make sure that this net is unique. If it isn't then
2223 * we are adding interfaces to an already existing network, and
2224 * 'net' is just a convenient way to pass in the list.
2225 * if it is unique we need to find the LND and load it if
2228 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2229 lnd_type = LNET_NETTYP(net->net_id);
2231 mutex_lock(&the_lnet.ln_lnd_mutex);
2232 lnd = lnet_find_lnd_by_type(lnd_type);
2235 mutex_unlock(&the_lnet.ln_lnd_mutex);
2236 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2237 mutex_lock(&the_lnet.ln_lnd_mutex);
2239 lnd = lnet_find_lnd_by_type(lnd_type);
2241 mutex_unlock(&the_lnet.ln_lnd_mutex);
2242 CERROR("Can't load LND %s, module %s, rc=%d\n",
2243 libcfs_lnd2str(lnd_type),
2244 libcfs_lnd2modname(lnd_type), rc);
2245 #ifndef HAVE_MODULE_LOADING_SUPPORT
2246 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2247 "compiled with kernel module "
2248 "loading support.");
2255 lnet_net_lock(LNET_LOCK_EX);
2256 lnd->lnd_refcount++;
2257 lnet_net_unlock(LNET_LOCK_EX);
2261 mutex_unlock(&the_lnet.ln_lnd_mutex);
2267 * net_l: if the network being added is unique then net_l
2268 * will point to that network
2269 * if the network being added is not unique then
2270 * net_l points to the existing network.
2272 * When we enter the loop below, we'll pick NIs off he
2273 * network beign added and start them up, then add them to
2274 * a local ni list. Once we've successfully started all
2275 * the NIs then we join the local NI list (of started up
2276 * networks) with the net_l->net_ni_list, which should
2277 * point to the correct network to add the new ni list to
2279 * If any of the new NIs fail to start up, then we want to
2280 * iterate through the local ni list, which should include
2281 * any NIs which were successfully started up, and shut
2284 * After than we want to delete the network being added,
2285 * to avoid a memory leak.
2289 * When a network uses TCP bonding then all its interfaces
2290 * must be specified when the network is first defined: the
2291 * TCP bonding code doesn't allow for interfaces to be added
2294 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2295 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2300 while (!list_empty(&net->net_ni_added)) {
2301 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2303 list_del_init(&ni->ni_netlist);
2305 /* make sure that the the NI we're about to start
2306 * up is actually unique. if it's not fail. */
2307 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2308 ni->ni_interfaces[0])) {
2313 /* adjust the pointer the parent network, just in case it
2314 * the net is a duplicate */
2317 rc = lnet_startup_lndni(ni, tun);
2319 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
2320 ni->ni_net->net_lnd->lnd_query != NULL);
2326 list_add_tail(&ni->ni_netlist, &local_ni_list);
2331 lnet_net_lock(LNET_LOCK_EX);
2332 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2333 lnet_incr_dlc_seq();
2334 lnet_net_unlock(LNET_LOCK_EX);
2336 /* if the network is not unique then we don't want to keep
2337 * it around after we're done. Free it. Otherwise add that
2338 * net to the global the_lnet.ln_nets */
2339 if (net_l != net && net_l != NULL) {
2341 * TODO - note. currently the tunables can not be updated
2346 net->net_state = LNET_NET_STATE_ACTIVE;
2348 * restore tunables after it has been overwitten by the
2351 if (peer_timeout != -1)
2352 net->net_tunables.lct_peer_timeout = peer_timeout;
2353 if (maxtxcredits != -1)
2354 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2355 if (peerrtrcredits != -1)
2356 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2358 lnet_net_lock(LNET_LOCK_EX);
2359 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2360 lnet_net_unlock(LNET_LOCK_EX);
2367 * shutdown the new NIs that are being started up
2368 * free the NET being started
2370 while (!list_empty(&local_ni_list)) {
2371 ni = list_entry(local_ni_list.next, struct lnet_ni,
2374 lnet_shutdown_lndni(ni);
2384 lnet_startup_lndnets(struct list_head *netlist)
2386 struct lnet_net *net;
2391 * Change to running state before bringing up the LNDs. This
2392 * allows lnet_shutdown_lndnets() to assert that we've passed
2395 lnet_net_lock(LNET_LOCK_EX);
2396 the_lnet.ln_state = LNET_STATE_RUNNING;
2397 lnet_net_unlock(LNET_LOCK_EX);
2399 while (!list_empty(netlist)) {
2400 net = list_entry(netlist->next, struct lnet_net, net_list);
2401 list_del_init(&net->net_list);
2403 rc = lnet_startup_lndnet(net, NULL);
2413 lnet_shutdown_lndnets();
2419 * Initialize LNet library.
2421 * Automatically called at module loading time. Caller has to call
2422 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2423 * latter returned 0. It must be called exactly once.
2425 * \retval 0 on success
2426 * \retval -ve on failures.
2428 int lnet_lib_init(void)
2432 lnet_assert_wire_constants();
2434 /* refer to global cfs_cpt_table for now */
2435 the_lnet.ln_cpt_table = cfs_cpt_table;
2436 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
2438 LASSERT(the_lnet.ln_cpt_number > 0);
2439 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2440 /* we are under risk of consuming all lh_cookie */
2441 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2442 "please change setting of CPT-table and retry\n",
2443 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2447 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2448 the_lnet.ln_cpt_bits++;
2450 rc = lnet_create_locks();
2452 CERROR("Can't create LNet global locks: %d\n", rc);
2456 the_lnet.ln_refcount = 0;
2457 LNetInvalidateEQHandle(&the_lnet.ln_rc_eqh);
2458 INIT_LIST_HEAD(&the_lnet.ln_lnds);
2459 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2460 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
2461 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2462 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
2464 /* The hash table size is the number of bits it takes to express the set
2465 * ln_num_routes, minus 1 (better to under estimate than over so we
2466 * don't waste memory). */
2467 if (rnet_htable_size <= 0)
2468 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2469 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2470 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2471 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2472 order_base_2(rnet_htable_size) - 1);
2474 /* All LNDs apart from the LOLND are in separate modules. They
2475 * register themselves when their module loads, and unregister
2476 * themselves when their module is unloaded. */
2477 lnet_register_lnd(&the_lolnd);
2482 * Finalize LNet library.
2484 * \pre lnet_lib_init() called with success.
2485 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2487 void lnet_lib_exit(void)
2489 LASSERT(the_lnet.ln_refcount == 0);
2491 while (!list_empty(&the_lnet.ln_lnds))
2492 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
2493 struct lnet_lnd, lnd_list));
2494 lnet_destroy_locks();
2498 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2500 * Users must call this function at least once before any other functions.
2501 * For each successful call there must be a corresponding call to
2502 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2505 * The PID used by LNet may be different from the one requested.
2508 * \param requested_pid PID requested by the caller.
2510 * \return >= 0 on success, and < 0 error code on failures.
2513 LNetNIInit(lnet_pid_t requested_pid)
2515 int im_a_router = 0;
2518 struct lnet_ping_buffer *pbuf;
2519 struct lnet_handle_md ping_mdh;
2520 struct list_head net_head;
2521 struct lnet_net *net;
2523 INIT_LIST_HEAD(&net_head);
2525 mutex_lock(&the_lnet.ln_api_mutex);
2527 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2529 if (the_lnet.ln_refcount > 0) {
2530 rc = the_lnet.ln_refcount++;
2531 mutex_unlock(&the_lnet.ln_api_mutex);
2535 rc = lnet_prepare(requested_pid);
2537 mutex_unlock(&the_lnet.ln_api_mutex);
2541 /* create a network for Loopback network */
2542 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2545 goto err_empty_list;
2548 /* Add in the loopback NI */
2549 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2551 goto err_empty_list;
2554 /* If LNet is being initialized via DLC it is possible
2555 * that the user requests not to load module parameters (ones which
2556 * are supported by DLC) on initialization. Therefore, make sure not
2557 * to load networks, routes and forwarding from module parameters
2558 * in this case. On cleanup in case of failure only clean up
2559 * routes if it has been loaded */
2560 if (!the_lnet.ln_nis_from_mod_params) {
2561 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2564 goto err_empty_list;
2567 ni_count = lnet_startup_lndnets(&net_head);
2570 goto err_empty_list;
2573 if (!the_lnet.ln_nis_from_mod_params) {
2574 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2576 goto err_shutdown_lndnis;
2578 rc = lnet_check_routes();
2580 goto err_destroy_routes;
2582 rc = lnet_rtrpools_alloc(im_a_router);
2584 goto err_destroy_routes;
2587 rc = lnet_acceptor_start();
2589 goto err_destroy_routes;
2591 the_lnet.ln_refcount = 1;
2592 /* Now I may use my own API functions... */
2594 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2596 goto err_acceptor_stop;
2598 lnet_ping_target_update(pbuf, ping_mdh);
2600 rc = LNetEQAlloc(0, lnet_mt_event_handler, &the_lnet.ln_mt_eqh);
2602 CERROR("Can't allocate monitor thread EQ: %d\n", rc);
2606 rc = lnet_monitor_thr_start();
2610 rc = lnet_push_target_init();
2612 goto err_stop_monitor_thr;
2614 rc = lnet_peer_discovery_start();
2616 goto err_destroy_push_target;
2619 lnet_router_debugfs_init();
2621 mutex_unlock(&the_lnet.ln_api_mutex);
2625 err_destroy_push_target:
2626 lnet_push_target_fini();
2627 err_stop_monitor_thr:
2628 lnet_monitor_thr_stop();
2630 lnet_ping_target_fini();
2632 the_lnet.ln_refcount = 0;
2633 lnet_acceptor_stop();
2635 if (!the_lnet.ln_nis_from_mod_params)
2636 lnet_destroy_routes();
2637 err_shutdown_lndnis:
2638 lnet_shutdown_lndnets();
2642 mutex_unlock(&the_lnet.ln_api_mutex);
2643 while (!list_empty(&net_head)) {
2644 struct lnet_net *net;
2646 net = list_entry(net_head.next, struct lnet_net, net_list);
2647 list_del_init(&net->net_list);
2652 EXPORT_SYMBOL(LNetNIInit);
2655 * Stop LNet interfaces, routing, and forwarding.
2657 * Users must call this function once for each successful call to LNetNIInit().
2658 * Once the LNetNIFini() operation has been started, the results of pending
2659 * API operations are undefined.
2661 * \return always 0 for current implementation.
2666 mutex_lock(&the_lnet.ln_api_mutex);
2668 LASSERT(the_lnet.ln_refcount > 0);
2670 if (the_lnet.ln_refcount != 1) {
2671 the_lnet.ln_refcount--;
2673 LASSERT(!the_lnet.ln_niinit_self);
2677 lnet_router_debugfs_fini();
2678 lnet_peer_discovery_stop();
2679 lnet_push_target_fini();
2680 lnet_monitor_thr_stop();
2681 lnet_ping_target_fini();
2683 /* Teardown fns that use my own API functions BEFORE here */
2684 the_lnet.ln_refcount = 0;
2686 lnet_acceptor_stop();
2687 lnet_destroy_routes();
2688 lnet_shutdown_lndnets();
2692 mutex_unlock(&the_lnet.ln_api_mutex);
2695 EXPORT_SYMBOL(LNetNIFini);
2698 * Grabs the ni data from the ni structure and fills the out
2701 * \param[in] ni network interface structure
2702 * \param[out] cfg_ni NI config information
2703 * \param[out] tun network and LND tunables
2706 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2707 struct lnet_ioctl_config_lnd_tunables *tun,
2708 struct lnet_ioctl_element_stats *stats,
2711 size_t min_size = 0;
2714 if (!ni || !cfg_ni || !tun)
2717 if (ni->ni_interfaces[0] != NULL) {
2718 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2719 if (ni->ni_interfaces[i] != NULL) {
2720 strncpy(cfg_ni->lic_ni_intf[i],
2721 ni->ni_interfaces[i],
2722 sizeof(cfg_ni->lic_ni_intf[i]));
2727 cfg_ni->lic_nid = ni->ni_nid;
2728 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2729 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2731 cfg_ni->lic_status = ni->ni_status->ns_status;
2732 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2733 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2735 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2738 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2739 LNET_STATS_TYPE_SEND);
2740 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2741 LNET_STATS_TYPE_RECV);
2742 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2743 LNET_STATS_TYPE_DROP);
2747 * tun->lt_tun will always be present, but in order to be
2748 * backwards compatible, we need to deal with the cases when
2749 * tun->lt_tun is smaller than what the kernel has, because it
2750 * comes from an older version of a userspace program, then we'll
2751 * need to copy as much information as we have available space.
2753 min_size = tun_size - sizeof(tun->lt_cmn);
2754 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2756 /* copy over the cpts */
2757 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2758 ni->ni_cpts == NULL) {
2759 for (i = 0; i < ni->ni_ncpts; i++)
2760 cfg_ni->lic_cpts[i] = i;
2763 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2764 i < LNET_MAX_SHOW_NUM_CPT;
2766 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2768 cfg_ni->lic_ncpts = ni->ni_ncpts;
2772 * NOTE: This is a legacy function left in the code to be backwards
2773 * compatible with older userspace programs. It should eventually be
2776 * Grabs the ni data from the ni structure and fills the out
2779 * \param[in] ni network interface structure
2780 * \param[out] config config information
2783 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2784 struct lnet_ioctl_config_data *config)
2786 struct lnet_ioctl_net_config *net_config;
2787 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2788 size_t min_size, tunable_size = 0;
2794 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2798 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2799 ARRAY_SIZE(net_config->ni_interfaces));
2801 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2802 if (!ni->ni_interfaces[i])
2805 strncpy(net_config->ni_interfaces[i],
2806 ni->ni_interfaces[i],
2807 sizeof(net_config->ni_interfaces[i]));
2810 config->cfg_nid = ni->ni_nid;
2811 config->cfg_config_u.cfg_net.net_peer_timeout =
2812 ni->ni_net->net_tunables.lct_peer_timeout;
2813 config->cfg_config_u.cfg_net.net_max_tx_credits =
2814 ni->ni_net->net_tunables.lct_max_tx_credits;
2815 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2816 ni->ni_net->net_tunables.lct_peer_tx_credits;
2817 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2818 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2820 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2821 net_config->ni_status = LNET_NI_STATUS_UP;
2823 net_config->ni_status = ni->ni_status->ns_status;
2826 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2828 for (i = 0; i < num_cpts; i++)
2829 net_config->ni_cpts[i] = ni->ni_cpts[i];
2831 config->cfg_ncpts = num_cpts;
2835 * See if user land tools sent in a newer and larger version
2836 * of struct lnet_tunables than what the kernel uses.
2838 min_size = sizeof(*config) + sizeof(*net_config);
2840 if (config->cfg_hdr.ioc_len > min_size)
2841 tunable_size = config->cfg_hdr.ioc_len - min_size;
2843 /* Don't copy too much data to user space */
2844 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2845 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2847 if (lnd_cfg && min_size) {
2848 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2849 config->cfg_config_u.cfg_net.net_interface_count = 1;
2851 /* Tell user land that kernel side has less data */
2852 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2853 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2854 config->cfg_hdr.ioc_len -= min_size;
2860 lnet_get_ni_idx_locked(int idx)
2863 struct lnet_net *net;
2865 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2866 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2876 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2879 struct lnet_net *net = mynet;
2882 * It is possible that the net has been cleaned out while there is
2883 * a message being sent. This function accessed the net without
2884 * checking if the list is empty
2888 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2890 if (list_empty(&net->net_ni_list))
2892 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2898 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2899 /* if you reached the end of the ni list and the net is
2900 * specified, then there are no more nis in that net */
2904 /* we reached the end of this net ni list. move to the
2906 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2907 /* no more nets and no more NIs. */
2910 /* get the next net */
2911 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2913 if (list_empty(&net->net_ni_list))
2915 /* get the ni on it */
2916 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2922 if (list_empty(&prev->ni_netlist))
2925 /* there are more nis left */
2926 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2932 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2937 int idx = config->cfg_count;
2939 cpt = lnet_net_lock_current();
2941 ni = lnet_get_ni_idx_locked(idx);
2946 lnet_fill_ni_info_legacy(ni, config);
2950 lnet_net_unlock(cpt);
2955 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2956 struct lnet_ioctl_config_lnd_tunables *tun,
2957 struct lnet_ioctl_element_stats *stats,
2964 if (!cfg_ni || !tun || !stats)
2967 cpt = lnet_net_lock_current();
2969 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2974 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
2978 lnet_net_unlock(cpt);
2982 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
2991 cpt = lnet_net_lock_current();
2993 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
2996 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3000 lnet_net_unlock(cpt);
3005 static int lnet_add_net_common(struct lnet_net *net,
3006 struct lnet_ioctl_config_lnd_tunables *tun)
3009 struct lnet_ping_buffer *pbuf;
3010 struct lnet_handle_md ping_mdh;
3012 struct lnet_remotenet *rnet;
3014 int num_acceptor_nets;
3016 lnet_net_lock(LNET_LOCK_EX);
3017 rnet = lnet_find_rnet_locked(net->net_id);
3018 lnet_net_unlock(LNET_LOCK_EX);
3020 * make sure that the net added doesn't invalidate the current
3021 * configuration LNet is keeping
3024 CERROR("Adding net %s will invalidate routing configuration\n",
3025 libcfs_net2str(net->net_id));
3031 * make sure you calculate the correct number of slots in the ping
3032 * buffer. Since the ping info is a flattened list of all the NIs,
3033 * we should allocate enough slots to accomodate the number of NIs
3034 * which will be added.
3036 * since ni hasn't been configured yet, use
3037 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3039 net_ni_count = lnet_get_net_ni_count_pre(net);
3041 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3042 net_ni_count + lnet_get_ni_count(),
3050 memcpy(&net->net_tunables,
3051 &tun->lt_cmn, sizeof(net->net_tunables));
3053 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3056 * before starting this network get a count of the current TCP
3057 * networks which require the acceptor thread running. If that
3058 * count is == 0 before we start up this network, then we'd want to
3059 * start up the acceptor thread after starting up this network
3061 num_acceptor_nets = lnet_count_acceptor_nets();
3063 net_id = net->net_id;
3065 rc = lnet_startup_lndnet(net,
3066 (tun) ? &tun->lt_tun : NULL);
3070 lnet_net_lock(LNET_LOCK_EX);
3071 net = lnet_get_net_locked(net_id);
3072 lnet_net_unlock(LNET_LOCK_EX);
3077 * Start the acceptor thread if this is the first network
3078 * being added that requires the thread.
3080 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
3081 rc = lnet_acceptor_start();
3083 /* shutdown the net that we just started */
3084 CERROR("Failed to start up acceptor thread\n");
3085 lnet_shutdown_lndnet(net);
3090 lnet_net_lock(LNET_LOCK_EX);
3091 lnet_peer_net_added(net);
3092 lnet_net_unlock(LNET_LOCK_EX);
3094 lnet_ping_target_update(pbuf, ping_mdh);
3099 lnet_ping_md_unlink(pbuf, &ping_mdh);
3100 lnet_ping_buffer_decref(pbuf);
3104 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3105 struct lnet_ioctl_config_lnd_tunables *tun)
3107 struct lnet_net *net;
3110 struct list_head net_head;
3112 INIT_LIST_HEAD(&net_head);
3114 rc = lnet_parse_ip2nets(&nets, ip2nets);
3118 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3122 mutex_lock(&the_lnet.ln_api_mutex);
3123 while (!list_empty(&net_head)) {
3124 net = list_entry(net_head.next, struct lnet_net, net_list);
3125 list_del_init(&net->net_list);
3126 rc = lnet_add_net_common(net, tun);
3132 mutex_unlock(&the_lnet.ln_api_mutex);
3134 while (!list_empty(&net_head)) {
3135 net = list_entry(net_head.next, struct lnet_net, net_list);
3136 list_del_init(&net->net_list);
3142 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3144 struct lnet_net *net;
3146 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3148 __u32 net_id, lnd_type;
3150 /* get the tunables if they are available */
3151 if (conf->lic_cfg_hdr.ioc_len >=
3152 sizeof(*conf) + sizeof(*tun))
3153 tun = (struct lnet_ioctl_config_lnd_tunables *)
3156 /* handle legacy ip2nets from DLC */
3157 if (conf->lic_legacy_ip2nets[0] != '\0')
3158 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3161 net_id = LNET_NIDNET(conf->lic_nid);
3162 lnd_type = LNET_NETTYP(net_id);
3164 if (!libcfs_isknown_lnd(lnd_type)) {
3165 CERROR("No valid net and lnd information provided\n");
3169 net = lnet_net_alloc(net_id, NULL);
3173 for (i = 0; i < conf->lic_ncpts; i++) {
3174 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3178 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3179 conf->lic_ni_intf[0]);
3183 mutex_lock(&the_lnet.ln_api_mutex);
3185 rc = lnet_add_net_common(net, tun);
3187 mutex_unlock(&the_lnet.ln_api_mutex);
3192 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3194 struct lnet_net *net;
3196 __u32 net_id = LNET_NIDNET(conf->lic_nid);
3197 struct lnet_ping_buffer *pbuf;
3198 struct lnet_handle_md ping_mdh;
3203 /* don't allow userspace to shutdown the LOLND */
3204 if (LNET_NETTYP(net_id) == LOLND)
3207 mutex_lock(&the_lnet.ln_api_mutex);
3211 net = lnet_get_net_locked(net_id);
3213 CERROR("net %s not found\n",
3214 libcfs_net2str(net_id));
3219 addr = LNET_NIDADDR(conf->lic_nid);
3221 /* remove the entire net */
3222 net_count = lnet_get_net_ni_count_locked(net);
3226 /* create and link a new ping info, before removing the old one */
3227 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3228 lnet_get_ni_count() - net_count,
3231 goto unlock_api_mutex;
3233 lnet_shutdown_lndnet(net);
3235 if (lnet_count_acceptor_nets() == 0)
3236 lnet_acceptor_stop();
3238 lnet_ping_target_update(pbuf, ping_mdh);
3240 goto unlock_api_mutex;
3243 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3245 CERROR("nid %s not found\n",
3246 libcfs_nid2str(conf->lic_nid));
3251 net_count = lnet_get_net_ni_count_locked(net);
3255 /* create and link a new ping info, before removing the old one */
3256 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3257 lnet_get_ni_count() - 1, false);
3259 goto unlock_api_mutex;
3261 lnet_shutdown_lndni(ni);
3263 if (lnet_count_acceptor_nets() == 0)
3264 lnet_acceptor_stop();
3266 lnet_ping_target_update(pbuf, ping_mdh);
3268 /* check if the net is empty and remove it if it is */
3270 lnet_shutdown_lndnet(net);
3272 goto unlock_api_mutex;
3277 mutex_unlock(&the_lnet.ln_api_mutex);
3283 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3284 * They are only expected to be called for unique networks.
3285 * That can be as a result of older DLC library
3286 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3289 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3291 struct lnet_net *net;
3292 struct list_head net_head;
3294 struct lnet_ioctl_config_lnd_tunables tun;
3295 char *nets = conf->cfg_config_u.cfg_net.net_intf;
3297 INIT_LIST_HEAD(&net_head);
3299 /* Create a net/ni structures for the network string */
3300 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3302 return rc == 0 ? -EINVAL : rc;
3304 mutex_lock(&the_lnet.ln_api_mutex);
3307 rc = -EINVAL; /* only add one network per call */
3308 goto out_unlock_clean;
3311 net = list_entry(net_head.next, struct lnet_net, net_list);
3312 list_del_init(&net->net_list);
3314 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3316 memset(&tun, 0, sizeof(tun));
3318 tun.lt_cmn.lct_peer_timeout =
3319 conf->cfg_config_u.cfg_net.net_peer_timeout;
3320 tun.lt_cmn.lct_peer_tx_credits =
3321 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3322 tun.lt_cmn.lct_peer_rtr_credits =
3323 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3324 tun.lt_cmn.lct_max_tx_credits =
3325 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3327 rc = lnet_add_net_common(net, &tun);
3330 mutex_unlock(&the_lnet.ln_api_mutex);
3331 while (!list_empty(&net_head)) {
3332 /* net_head list is empty in success case */
3333 net = list_entry(net_head.next, struct lnet_net, net_list);
3334 list_del_init(&net->net_list);
3341 lnet_dyn_del_net(__u32 net_id)
3343 struct lnet_net *net;
3344 struct lnet_ping_buffer *pbuf;
3345 struct lnet_handle_md ping_mdh;
3349 /* don't allow userspace to shutdown the LOLND */
3350 if (LNET_NETTYP(net_id) == LOLND)
3353 mutex_lock(&the_lnet.ln_api_mutex);
3357 net = lnet_get_net_locked(net_id);
3364 net_ni_count = lnet_get_net_ni_count_locked(net);
3368 /* create and link a new ping info, before removing the old one */
3369 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3370 lnet_get_ni_count() - net_ni_count, false);
3374 lnet_shutdown_lndnet(net);
3376 if (lnet_count_acceptor_nets() == 0)
3377 lnet_acceptor_stop();
3379 lnet_ping_target_update(pbuf, ping_mdh);
3382 mutex_unlock(&the_lnet.ln_api_mutex);
3387 void lnet_incr_dlc_seq(void)
3389 atomic_inc(&lnet_dlc_seq_no);
3392 __u32 lnet_get_dlc_seq_locked(void)
3394 return atomic_read(&lnet_dlc_seq_no);
3398 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3400 struct lnet_net *net;
3403 lnet_net_lock(LNET_LOCK_EX);
3404 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3405 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3406 if (ni->ni_nid == nid || all) {
3407 atomic_set(&ni->ni_healthv, value);
3408 if (list_empty(&ni->ni_recovery) &&
3409 value < LNET_MAX_HEALTH_VALUE) {
3410 CERROR("manually adding local NI %s to recovery\n",
3411 libcfs_nid2str(ni->ni_nid));
3412 list_add_tail(&ni->ni_recovery,
3413 &the_lnet.ln_mt_localNIRecovq);
3414 lnet_ni_addref_locked(ni, 0);
3417 lnet_net_unlock(LNET_LOCK_EX);
3423 lnet_net_unlock(LNET_LOCK_EX);
3427 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3431 lnet_nid_t nid = stats->hlni_nid;
3433 cpt = lnet_net_lock_current();
3434 ni = lnet_nid2ni_locked(nid, cpt);
3441 stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3442 stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3443 stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3444 stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3445 stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3446 stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3447 stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3450 lnet_net_unlock(cpt);
3456 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3461 lnet_net_lock(LNET_LOCK_EX);
3462 list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3463 list->rlst_nid_array[i] = ni->ni_nid;
3465 if (i >= LNET_MAX_SHOW_NUM_NID)
3468 lnet_net_unlock(LNET_LOCK_EX);
3469 list->rlst_num_nids = i;
3475 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3477 struct lnet_peer_ni *lpni;
3480 lnet_net_lock(LNET_LOCK_EX);
3481 list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3482 list->rlst_nid_array[i] = lpni->lpni_nid;
3484 if (i >= LNET_MAX_SHOW_NUM_NID)
3487 lnet_net_unlock(LNET_LOCK_EX);
3488 list->rlst_num_nids = i;
3494 * LNet ioctl handler.
3498 LNetCtl(unsigned int cmd, void *arg)
3500 struct libcfs_ioctl_data *data = arg;
3501 struct lnet_ioctl_config_data *config;
3502 struct lnet_process_id id = {0};
3506 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3507 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3510 case IOC_LIBCFS_GET_NI:
3511 rc = LNetGetId(data->ioc_count, &id);
3512 data->ioc_nid = id.nid;
3515 case IOC_LIBCFS_FAIL_NID:
3516 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3518 case IOC_LIBCFS_ADD_ROUTE:
3521 if (config->cfg_hdr.ioc_len < sizeof(*config))
3524 mutex_lock(&the_lnet.ln_api_mutex);
3525 rc = lnet_add_route(config->cfg_net,
3526 config->cfg_config_u.cfg_route.rtr_hop,
3528 config->cfg_config_u.cfg_route.
3531 rc = lnet_check_routes();
3533 lnet_del_route(config->cfg_net,
3536 mutex_unlock(&the_lnet.ln_api_mutex);
3539 case IOC_LIBCFS_DEL_ROUTE:
3542 if (config->cfg_hdr.ioc_len < sizeof(*config))
3545 mutex_lock(&the_lnet.ln_api_mutex);
3546 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3547 mutex_unlock(&the_lnet.ln_api_mutex);
3550 case IOC_LIBCFS_GET_ROUTE:
3553 if (config->cfg_hdr.ioc_len < sizeof(*config))
3556 mutex_lock(&the_lnet.ln_api_mutex);
3557 rc = lnet_get_route(config->cfg_count,
3559 &config->cfg_config_u.cfg_route.rtr_hop,
3561 &config->cfg_config_u.cfg_route.rtr_flags,
3562 &config->cfg_config_u.cfg_route.
3564 mutex_unlock(&the_lnet.ln_api_mutex);
3567 case IOC_LIBCFS_GET_LOCAL_NI: {
3568 struct lnet_ioctl_config_ni *cfg_ni;
3569 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3570 struct lnet_ioctl_element_stats *stats;
3575 /* get the tunables if they are available */
3576 if (cfg_ni->lic_cfg_hdr.ioc_len <
3577 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3580 stats = (struct lnet_ioctl_element_stats *)
3582 tun = (struct lnet_ioctl_config_lnd_tunables *)
3583 (cfg_ni->lic_bulk + sizeof(*stats));
3585 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3588 mutex_lock(&the_lnet.ln_api_mutex);
3589 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3590 mutex_unlock(&the_lnet.ln_api_mutex);
3594 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3595 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3597 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3600 mutex_lock(&the_lnet.ln_api_mutex);
3601 rc = lnet_get_ni_stats(msg_stats);
3602 mutex_unlock(&the_lnet.ln_api_mutex);
3607 case IOC_LIBCFS_GET_NET: {
3608 size_t total = sizeof(*config) +
3609 sizeof(struct lnet_ioctl_net_config);
3612 if (config->cfg_hdr.ioc_len < total)
3615 mutex_lock(&the_lnet.ln_api_mutex);
3616 rc = lnet_get_net_config(config);
3617 mutex_unlock(&the_lnet.ln_api_mutex);
3621 case IOC_LIBCFS_GET_LNET_STATS:
3623 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3625 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3628 mutex_lock(&the_lnet.ln_api_mutex);
3629 lnet_counters_get(&lnet_stats->st_cntrs);
3630 mutex_unlock(&the_lnet.ln_api_mutex);
3634 case IOC_LIBCFS_CONFIG_RTR:
3637 if (config->cfg_hdr.ioc_len < sizeof(*config))
3640 mutex_lock(&the_lnet.ln_api_mutex);
3641 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3642 rc = lnet_rtrpools_enable();
3643 mutex_unlock(&the_lnet.ln_api_mutex);
3646 lnet_rtrpools_disable();
3647 mutex_unlock(&the_lnet.ln_api_mutex);
3650 case IOC_LIBCFS_ADD_BUF:
3653 if (config->cfg_hdr.ioc_len < sizeof(*config))
3656 mutex_lock(&the_lnet.ln_api_mutex);
3657 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3659 config->cfg_config_u.cfg_buffers.
3661 config->cfg_config_u.cfg_buffers.
3663 mutex_unlock(&the_lnet.ln_api_mutex);
3666 case IOC_LIBCFS_SET_NUMA_RANGE: {
3667 struct lnet_ioctl_set_value *numa;
3669 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3671 lnet_net_lock(LNET_LOCK_EX);
3672 lnet_numa_range = numa->sv_value;
3673 lnet_net_unlock(LNET_LOCK_EX);
3677 case IOC_LIBCFS_GET_NUMA_RANGE: {
3678 struct lnet_ioctl_set_value *numa;
3680 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3682 numa->sv_value = lnet_numa_range;
3686 case IOC_LIBCFS_GET_BUF: {
3687 struct lnet_ioctl_pool_cfg *pool_cfg;
3688 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3692 if (config->cfg_hdr.ioc_len < total)
3695 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3697 mutex_lock(&the_lnet.ln_api_mutex);
3698 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3699 mutex_unlock(&the_lnet.ln_api_mutex);
3703 case IOC_LIBCFS_GET_LOCAL_HSTATS: {
3704 struct lnet_ioctl_local_ni_hstats *stats = arg;
3706 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
3709 mutex_lock(&the_lnet.ln_api_mutex);
3710 rc = lnet_get_local_ni_hstats(stats);
3711 mutex_unlock(&the_lnet.ln_api_mutex);
3716 case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
3717 struct lnet_ioctl_recovery_list *list = arg;
3718 if (list->rlst_hdr.ioc_len < sizeof(*list))
3721 mutex_lock(&the_lnet.ln_api_mutex);
3722 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
3723 rc = lnet_get_local_ni_recovery_list(list);
3725 rc = lnet_get_peer_ni_recovery_list(list);
3726 mutex_unlock(&the_lnet.ln_api_mutex);
3730 case IOC_LIBCFS_ADD_PEER_NI: {
3731 struct lnet_ioctl_peer_cfg *cfg = arg;
3733 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3736 mutex_lock(&the_lnet.ln_api_mutex);
3737 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3740 mutex_unlock(&the_lnet.ln_api_mutex);
3744 case IOC_LIBCFS_DEL_PEER_NI: {
3745 struct lnet_ioctl_peer_cfg *cfg = arg;
3747 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3750 mutex_lock(&the_lnet.ln_api_mutex);
3751 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3752 cfg->prcfg_cfg_nid);
3753 mutex_unlock(&the_lnet.ln_api_mutex);
3757 case IOC_LIBCFS_GET_PEER_INFO: {
3758 struct lnet_ioctl_peer *peer_info = arg;
3760 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3763 mutex_lock(&the_lnet.ln_api_mutex);
3764 rc = lnet_get_peer_ni_info(
3765 peer_info->pr_count,
3767 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3768 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3769 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3770 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3771 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3772 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3773 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3774 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3775 mutex_unlock(&the_lnet.ln_api_mutex);
3779 case IOC_LIBCFS_GET_PEER_NI: {
3780 struct lnet_ioctl_peer_cfg *cfg = arg;
3782 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3785 mutex_lock(&the_lnet.ln_api_mutex);
3786 rc = lnet_get_peer_info(cfg,
3787 (void __user *)cfg->prcfg_bulk);
3788 mutex_unlock(&the_lnet.ln_api_mutex);
3792 case IOC_LIBCFS_GET_PEER_LIST: {
3793 struct lnet_ioctl_peer_cfg *cfg = arg;
3795 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3798 mutex_lock(&the_lnet.ln_api_mutex);
3799 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3800 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3801 mutex_unlock(&the_lnet.ln_api_mutex);
3805 case IOC_LIBCFS_SET_HEALHV: {
3806 struct lnet_ioctl_reset_health_cfg *cfg = arg;
3808 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
3810 if (cfg->rh_value < 0 ||
3811 cfg->rh_value > LNET_MAX_HEALTH_VALUE)
3812 value = LNET_MAX_HEALTH_VALUE;
3814 value = cfg->rh_value;
3815 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
3816 value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
3817 "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
3818 mutex_lock(&the_lnet.ln_api_mutex);
3819 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
3820 lnet_ni_set_healthv(cfg->rh_nid, value,
3823 lnet_peer_ni_set_healthv(cfg->rh_nid, value,
3825 mutex_unlock(&the_lnet.ln_api_mutex);
3829 case IOC_LIBCFS_NOTIFY_ROUTER: {
3830 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3832 /* The deadline passed in by the user should be some time in
3833 * seconds in the future since the UNIX epoch. We have to map
3834 * that deadline to the wall clock.
3836 deadline += ktime_get_seconds();
3837 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
3841 case IOC_LIBCFS_LNET_DIST:
3842 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3843 if (rc < 0 && rc != -EHOSTUNREACH)
3846 data->ioc_u32[0] = rc;
3849 case IOC_LIBCFS_TESTPROTOCOMPAT:
3850 lnet_net_lock(LNET_LOCK_EX);
3851 the_lnet.ln_testprotocompat = data->ioc_flags;
3852 lnet_net_unlock(LNET_LOCK_EX);
3855 case IOC_LIBCFS_LNET_FAULT:
3856 return lnet_fault_ctl(data->ioc_flags, data);
3858 case IOC_LIBCFS_PING: {
3859 signed long timeout;
3861 id.nid = data->ioc_nid;
3862 id.pid = data->ioc_u32[0];
3864 /* If timeout is negative then set default of 3 minutes */
3865 if (((s32)data->ioc_u32[1] <= 0) ||
3866 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3867 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3869 timeout = msecs_to_jiffies(data->ioc_u32[1]);
3871 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3872 data->ioc_plen1 / sizeof(struct lnet_process_id));
3877 data->ioc_count = rc;
3881 case IOC_LIBCFS_PING_PEER: {
3882 struct lnet_ioctl_ping_data *ping = arg;
3883 struct lnet_peer *lp;
3884 signed long timeout;
3886 /* If timeout is negative then set default of 3 minutes */
3887 if (((s32)ping->op_param) <= 0 ||
3888 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3889 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3891 timeout = msecs_to_jiffies(ping->op_param);
3893 rc = lnet_ping(ping->ping_id, timeout,
3899 mutex_lock(&the_lnet.ln_api_mutex);
3900 lp = lnet_find_peer(ping->ping_id.nid);
3902 ping->ping_id.nid = lp->lp_primary_nid;
3903 ping->mr_info = lnet_peer_is_multi_rail(lp);
3904 lnet_peer_decref_locked(lp);
3906 mutex_unlock(&the_lnet.ln_api_mutex);
3908 ping->ping_count = rc;
3912 case IOC_LIBCFS_DISCOVER: {
3913 struct lnet_ioctl_ping_data *discover = arg;
3914 struct lnet_peer *lp;
3916 rc = lnet_discover(discover->ping_id, discover->op_param,
3918 discover->ping_count);
3922 mutex_lock(&the_lnet.ln_api_mutex);
3923 lp = lnet_find_peer(discover->ping_id.nid);
3925 discover->ping_id.nid = lp->lp_primary_nid;
3926 discover->mr_info = lnet_peer_is_multi_rail(lp);
3927 lnet_peer_decref_locked(lp);
3929 mutex_unlock(&the_lnet.ln_api_mutex);
3931 discover->ping_count = rc;
3936 ni = lnet_net2ni_addref(data->ioc_net);
3940 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3943 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
3950 EXPORT_SYMBOL(LNetCtl);
3952 void LNetDebugPeer(struct lnet_process_id id)
3954 lnet_debug_peer(id.nid);
3956 EXPORT_SYMBOL(LNetDebugPeer);
3959 * Determine if the specified peer \a nid is on the local node.
3961 * \param nid peer nid to check
3963 * \retval true If peer NID is on the local node.
3964 * \retval false If peer NID is not on the local node.
3966 bool LNetIsPeerLocal(lnet_nid_t nid)
3968 struct lnet_net *net;
3972 cpt = lnet_net_lock_current();
3973 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3974 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3975 if (ni->ni_nid == nid) {
3976 lnet_net_unlock(cpt);
3981 lnet_net_unlock(cpt);
3985 EXPORT_SYMBOL(LNetIsPeerLocal);
3988 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
3989 * Note that all interfaces share a same PID, as requested by LNetNIInit().
3991 * \param index Index of the interface to look up.
3992 * \param id On successful return, this location will hold the
3993 * struct lnet_process_id ID of the interface.
3995 * \retval 0 If an interface exists at \a index.
3996 * \retval -ENOENT If no interface has been found.
3999 LNetGetId(unsigned int index, struct lnet_process_id *id)
4002 struct lnet_net *net;
4006 LASSERT(the_lnet.ln_refcount > 0);
4008 cpt = lnet_net_lock_current();
4010 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4011 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4015 id->nid = ni->ni_nid;
4016 id->pid = the_lnet.ln_pid;
4022 lnet_net_unlock(cpt);
4025 EXPORT_SYMBOL(LNetGetId);
4027 static int lnet_ping(struct lnet_process_id id, signed long timeout,
4028 struct lnet_process_id __user *ids, int n_ids)
4030 struct lnet_handle_eq eqh;
4031 struct lnet_handle_md mdh;
4032 struct lnet_event event;
4033 struct lnet_md md = { NULL };
4037 const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
4038 struct lnet_ping_buffer *pbuf;
4039 struct lnet_process_id tmpid;
4046 /* n_ids limit is arbitrary */
4047 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4051 * if the user buffer has more space than the lnet_interfaces_max
4052 * then only fill it up to lnet_interfaces_max
4054 if (n_ids > lnet_interfaces_max)
4055 n_ids = lnet_interfaces_max;
4057 if (id.pid == LNET_PID_ANY)
4058 id.pid = LNET_PID_LUSTRE;
4060 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4064 /* NB 2 events max (including any unlink event) */
4065 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
4067 CERROR("Can't allocate EQ: %d\n", rc);
4068 goto fail_ping_buffer_decref;
4071 /* initialize md content */
4072 md.start = &pbuf->pb_info;
4073 md.length = LNET_PING_INFO_SIZE(n_ids);
4074 md.threshold = 2; /* GET/REPLY */
4076 md.options = LNET_MD_TRUNCATE;
4080 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
4082 CERROR("Can't bind MD: %d\n", rc);
4086 rc = LNetGet(LNET_NID_ANY, mdh, id,
4087 LNET_RESERVED_PORTAL,
4088 LNET_PROTO_PING_MATCHBITS, 0, false);
4091 /* Don't CERROR; this could be deliberate! */
4092 rc2 = LNetMDUnlink(mdh);
4095 /* NB must wait for the UNLINK event below... */
4097 timeout = a_long_time;
4101 /* MUST block for unlink to complete */
4103 blocked = cfs_block_allsigs();
4105 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
4108 cfs_restore_sigs(blocked);
4110 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
4111 (rc2 <= 0) ? -1 : event.type,
4112 (rc2 <= 0) ? -1 : event.status,
4113 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
4115 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
4117 if (rc2 <= 0 || event.status != 0) {
4118 /* timeout or error */
4119 if (!replied && rc == 0)
4120 rc = (rc2 < 0) ? rc2 :
4121 (rc2 == 0) ? -ETIMEDOUT :
4125 /* Ensure completion in finite time... */
4127 /* No assertion (racing with network) */
4129 timeout = a_long_time;
4130 } else if (rc2 == 0) {
4131 /* timed out waiting for unlink */
4132 CWARN("ping %s: late network completion\n",
4135 } else if (event.type == LNET_EVENT_REPLY) {
4139 } while (rc2 <= 0 || !event.unlinked);
4143 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
4150 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4152 rc = -EPROTO; /* if I can't parse... */
4155 CERROR("%s: ping info too short %d\n",
4156 libcfs_id2str(id), nob);
4160 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4161 lnet_swap_pinginfo(pbuf);
4162 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4163 CERROR("%s: Unexpected magic %08x\n",
4164 libcfs_id2str(id), pbuf->pb_info.pi_magic);
4168 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4169 CERROR("%s: ping w/o NI status: 0x%x\n",
4170 libcfs_id2str(id), pbuf->pb_info.pi_features);
4174 if (nob < LNET_PING_INFO_SIZE(0)) {
4175 CERROR("%s: Short reply %d(%d min)\n",
4177 nob, (int)LNET_PING_INFO_SIZE(0));
4181 if (pbuf->pb_info.pi_nnis < n_ids)
4182 n_ids = pbuf->pb_info.pi_nnis;
4184 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4185 CERROR("%s: Short reply %d(%d expected)\n",
4187 nob, (int)LNET_PING_INFO_SIZE(n_ids));
4191 rc = -EFAULT; /* if I segv in copy_to_user()... */
4193 memset(&tmpid, 0, sizeof(tmpid));
4194 for (i = 0; i < n_ids; i++) {
4195 tmpid.pid = pbuf->pb_info.pi_pid;
4196 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4197 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4200 rc = pbuf->pb_info.pi_nnis;
4203 rc2 = LNetEQFree(eqh);
4205 CERROR("rc2 %d\n", rc2);
4208 fail_ping_buffer_decref:
4209 lnet_ping_buffer_decref(pbuf);
4214 lnet_discover(struct lnet_process_id id, __u32 force,
4215 struct lnet_process_id __user *ids, int n_ids)
4217 struct lnet_peer_ni *lpni;
4218 struct lnet_peer_ni *p;
4219 struct lnet_peer *lp;
4220 struct lnet_process_id *buf;
4224 int max_intf = lnet_interfaces_max;
4228 id.nid == LNET_NID_ANY)
4231 if (id.pid == LNET_PID_ANY)
4232 id.pid = LNET_PID_LUSTRE;
4235 * if the user buffer has more space than the max_intf
4236 * then only fill it up to max_intf
4238 if (n_ids > max_intf)
4241 buf_size = n_ids * sizeof(*buf);
4243 LIBCFS_ALLOC(buf, buf_size);
4247 cpt = lnet_net_lock_current();
4248 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4255 * Clearing the NIDS_UPTODATE flag ensures the peer will
4256 * be discovered, provided discovery has not been disabled.
4258 lp = lpni->lpni_peer_net->lpn_peer;
4259 spin_lock(&lp->lp_lock);
4260 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4261 /* If the force flag is set, force a PING and PUSH as well. */
4263 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4264 spin_unlock(&lp->lp_lock);
4265 rc = lnet_discover_peer_locked(lpni, cpt, true);
4269 /* Peer may have changed. */
4270 lp = lpni->lpni_peer_net->lpn_peer;
4271 if (lp->lp_nnis < n_ids)
4272 n_ids = lp->lp_nnis;
4276 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4277 buf[i].pid = id.pid;
4278 buf[i].nid = p->lpni_nid;
4283 lnet_net_unlock(cpt);
4286 if (copy_to_user(ids, buf, n_ids * sizeof(*buf)))
4292 lnet_peer_ni_decref_locked(lpni);
4294 lnet_net_unlock(cpt);
4296 LIBCFS_FREE(buf, buf_size);
4302 * Retrieve peer discovery status.
4304 * \retval 1 if lnet_peer_discovery_disabled is 0
4305 * \retval 0 if lnet_peer_discovery_disabled is 1
4308 LNetGetPeerDiscoveryStatus(void)
4310 return !lnet_peer_discovery_disabled;
4312 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);