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/
32 #define DEBUG_SUBSYSTEM S_LNET
34 #include <linux/ctype.h>
35 #include <linux/log2.h>
36 #include <linux/ktime.h>
37 #include <linux/moduleparam.h>
38 #include <linux/uaccess.h>
39 #ifdef HAVE_SCHED_HEADERS
40 #include <linux/sched/signal.h>
42 #include <lnet/udsp.h>
43 #include <lnet/lib-lnet.h>
45 #define D_LNI D_CONSOLE
48 * initialize ln_api_mutex statically, since it needs to be used in
49 * discovery_set callback. That module parameter callback can be called
50 * before module init completes. The mutex needs to be ready for use then.
52 struct lnet the_lnet = {
53 .ln_api_mutex = __MUTEX_INITIALIZER(the_lnet.ln_api_mutex),
54 }; /* THE state of the network */
55 EXPORT_SYMBOL(the_lnet);
57 static char *ip2nets = "";
58 module_param(ip2nets, charp, 0444);
59 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
61 static char *networks = "";
62 module_param(networks, charp, 0444);
63 MODULE_PARM_DESC(networks, "local networks");
65 static char *routes = "";
66 module_param(routes, charp, 0444);
67 MODULE_PARM_DESC(routes, "routes to non-local networks");
69 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
70 module_param(rnet_htable_size, int, 0444);
71 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
73 static int use_tcp_bonding;
74 module_param(use_tcp_bonding, int, 0444);
75 MODULE_PARM_DESC(use_tcp_bonding,
76 "use_tcp_bonding parameter has been removed");
78 unsigned int lnet_numa_range = 0;
79 module_param(lnet_numa_range, uint, 0444);
80 MODULE_PARM_DESC(lnet_numa_range,
81 "NUMA range to consider during Multi-Rail selection");
84 * lnet_health_sensitivity determines by how much we decrement the health
85 * value on sending error. The value defaults to 100, which means health
86 * interface health is decremented by 100 points every failure.
88 unsigned int lnet_health_sensitivity = 100;
89 static int sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
90 #ifdef HAVE_KERNEL_PARAM_OPS
91 static struct kernel_param_ops param_ops_health_sensitivity = {
92 .set = sensitivity_set,
95 #define param_check_health_sensitivity(name, p) \
96 __param_check(name, p, int)
97 module_param(lnet_health_sensitivity, health_sensitivity, S_IRUGO|S_IWUSR);
99 module_param_call(lnet_health_sensitivity, sensitivity_set, param_get_int,
100 &lnet_health_sensitivity, S_IRUGO|S_IWUSR);
102 MODULE_PARM_DESC(lnet_health_sensitivity,
103 "Value to decrement the health value by on error");
106 * lnet_recovery_interval determines how often we should perform recovery
107 * on unhealthy interfaces.
109 unsigned int lnet_recovery_interval = 1;
110 static int recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp);
111 #ifdef HAVE_KERNEL_PARAM_OPS
112 static struct kernel_param_ops param_ops_recovery_interval = {
113 .set = recovery_interval_set,
114 .get = param_get_int,
116 #define param_check_recovery_interval(name, p) \
117 __param_check(name, p, int)
118 module_param(lnet_recovery_interval, recovery_interval, S_IRUGO|S_IWUSR);
120 module_param_call(lnet_recovery_interval, recovery_interval_set, param_get_int,
121 &lnet_recovery_interval, S_IRUGO|S_IWUSR);
123 MODULE_PARM_DESC(lnet_recovery_interval,
124 "DEPRECATED - Interval to recover unhealthy interfaces in seconds");
126 unsigned int lnet_recovery_limit;
127 module_param(lnet_recovery_limit, uint, 0644);
128 MODULE_PARM_DESC(lnet_recovery_limit,
129 "How long to attempt recovery of unhealthy peer interfaces in seconds. Set to 0 to allow indefinite recovery");
131 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
132 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
134 static struct kernel_param_ops param_ops_interfaces_max = {
136 .get = param_get_int,
139 #define param_check_interfaces_max(name, p) \
140 __param_check(name, p, int)
142 #ifdef HAVE_KERNEL_PARAM_OPS
143 module_param(lnet_interfaces_max, interfaces_max, 0644);
145 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
146 ¶m_ops_interfaces_max, 0644);
148 MODULE_PARM_DESC(lnet_interfaces_max,
149 "Maximum number of interfaces in a node.");
151 unsigned lnet_peer_discovery_disabled = 0;
152 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
154 static struct kernel_param_ops param_ops_discovery_disabled = {
155 .set = discovery_set,
156 .get = param_get_int,
159 #define param_check_discovery_disabled(name, p) \
160 __param_check(name, p, int)
161 #ifdef HAVE_KERNEL_PARAM_OPS
162 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
164 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
165 ¶m_ops_discovery_disabled, 0644);
167 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
168 "Set to 1 to disable peer discovery on this node.");
170 unsigned int lnet_drop_asym_route;
171 static int drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp);
173 static struct kernel_param_ops param_ops_drop_asym_route = {
174 .set = drop_asym_route_set,
175 .get = param_get_int,
178 #define param_check_drop_asym_route(name, p) \
179 __param_check(name, p, int)
180 #ifdef HAVE_KERNEL_PARAM_OPS
181 module_param(lnet_drop_asym_route, drop_asym_route, 0644);
183 module_param_call(lnet_drop_asym_route, drop_asym_route_set, param_get_int,
184 ¶m_ops_drop_asym_route, 0644);
186 MODULE_PARM_DESC(lnet_drop_asym_route,
187 "Set to 1 to drop asymmetrical route messages.");
189 #define LNET_TRANSACTION_TIMEOUT_DEFAULT 50
190 unsigned int lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_DEFAULT;
191 static int transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp);
192 #ifdef HAVE_KERNEL_PARAM_OPS
193 static struct kernel_param_ops param_ops_transaction_timeout = {
194 .set = transaction_to_set,
195 .get = param_get_int,
198 #define param_check_transaction_timeout(name, p) \
199 __param_check(name, p, int)
200 module_param(lnet_transaction_timeout, transaction_timeout, S_IRUGO|S_IWUSR);
202 module_param_call(lnet_transaction_timeout, transaction_to_set, param_get_int,
203 &lnet_transaction_timeout, S_IRUGO|S_IWUSR);
205 MODULE_PARM_DESC(lnet_transaction_timeout,
206 "Maximum number of seconds to wait for a peer response.");
208 #define LNET_RETRY_COUNT_DEFAULT 2
209 unsigned int lnet_retry_count = LNET_RETRY_COUNT_DEFAULT;
210 static int retry_count_set(const char *val, cfs_kernel_param_arg_t *kp);
211 #ifdef HAVE_KERNEL_PARAM_OPS
212 static struct kernel_param_ops param_ops_retry_count = {
213 .set = retry_count_set,
214 .get = param_get_int,
217 #define param_check_retry_count(name, p) \
218 __param_check(name, p, int)
219 module_param(lnet_retry_count, retry_count, S_IRUGO|S_IWUSR);
221 module_param_call(lnet_retry_count, retry_count_set, param_get_int,
222 &lnet_retry_count, S_IRUGO|S_IWUSR);
224 MODULE_PARM_DESC(lnet_retry_count,
225 "Maximum number of times to retry transmitting a message");
227 unsigned int lnet_response_tracking = 3;
228 static int response_tracking_set(const char *val, cfs_kernel_param_arg_t *kp);
230 #ifdef HAVE_KERNEL_PARAM_OPS
231 static struct kernel_param_ops param_ops_response_tracking = {
232 .set = response_tracking_set,
233 .get = param_get_int,
236 #define param_check_response_tracking(name, p) \
237 __param_check(name, p, int)
238 module_param(lnet_response_tracking, response_tracking, 0644);
240 module_param_call(lnet_response_tracking, response_tracking_set, param_get_int,
241 &lnet_response_tracking, 0644);
243 MODULE_PARM_DESC(lnet_response_tracking,
244 "(0|1|2|3) LNet Internal Only|GET Reply only|PUT ACK only|Full Tracking (default)");
246 #define LNET_LND_TIMEOUT_DEFAULT ((LNET_TRANSACTION_TIMEOUT_DEFAULT - 1) / \
247 (LNET_RETRY_COUNT_DEFAULT + 1))
248 unsigned int lnet_lnd_timeout = LNET_LND_TIMEOUT_DEFAULT;
249 static void lnet_set_lnd_timeout(void)
251 lnet_lnd_timeout = (lnet_transaction_timeout - 1) /
252 (lnet_retry_count + 1);
256 * This sequence number keeps track of how many times DLC was used to
257 * update the local NIs. It is incremented when a NI is added or
258 * removed and checked when sending a message to determine if there is
259 * a need to re-run the selection algorithm. See lnet_select_pathway()
260 * for more details on its usage.
262 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
264 static int lnet_ping(struct lnet_process_id id, signed long timeout,
265 struct lnet_process_id __user *ids, int n_ids);
267 static int lnet_discover(struct lnet_process_id id, __u32 force,
268 struct lnet_process_id __user *ids, int n_ids);
271 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
274 unsigned *sensitivity = (unsigned *)kp->arg;
277 rc = kstrtoul(val, 0, &value);
279 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
284 * The purpose of locking the api_mutex here is to ensure that
285 * the correct value ends up stored properly.
287 mutex_lock(&the_lnet.ln_api_mutex);
289 if (value > LNET_MAX_HEALTH_VALUE) {
290 mutex_unlock(&the_lnet.ln_api_mutex);
291 CERROR("Invalid health value. Maximum: %d value = %lu\n",
292 LNET_MAX_HEALTH_VALUE, value);
296 if (*sensitivity != 0 && value == 0 && lnet_retry_count != 0) {
297 lnet_retry_count = 0;
298 lnet_set_lnd_timeout();
301 *sensitivity = value;
303 mutex_unlock(&the_lnet.ln_api_mutex);
309 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
311 CWARN("'lnet_recovery_interval' has been deprecated\n");
317 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
320 unsigned *discovery_off = (unsigned *)kp->arg;
322 struct lnet_ping_buffer *pbuf;
324 rc = kstrtoul(val, 0, &value);
326 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
330 value = (value) ? 1 : 0;
333 * The purpose of locking the api_mutex here is to ensure that
334 * the correct value ends up stored properly.
336 mutex_lock(&the_lnet.ln_api_mutex);
338 if (value == *discovery_off) {
339 mutex_unlock(&the_lnet.ln_api_mutex);
344 * We still want to set the discovery value even when LNet is not
345 * running. This is the case when LNet is being loaded and we want
346 * the module parameters to take effect. Otherwise if we're
347 * changing the value dynamically, we want to set it after
350 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
351 *discovery_off = value;
352 mutex_unlock(&the_lnet.ln_api_mutex);
356 /* tell peers that discovery setting has changed */
357 lnet_net_lock(LNET_LOCK_EX);
358 pbuf = the_lnet.ln_ping_target;
360 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
362 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
363 lnet_net_unlock(LNET_LOCK_EX);
365 /* only send a push when we're turning off discovery */
366 if (*discovery_off <= 0 && value > 0)
367 lnet_push_update_to_peers(1);
368 *discovery_off = value;
370 mutex_unlock(&the_lnet.ln_api_mutex);
376 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
379 unsigned int *drop_asym_route = (unsigned int *)kp->arg;
382 rc = kstrtoul(val, 0, &value);
384 CERROR("Invalid module parameter value for "
385 "'lnet_drop_asym_route'\n");
390 * The purpose of locking the api_mutex here is to ensure that
391 * the correct value ends up stored properly.
393 mutex_lock(&the_lnet.ln_api_mutex);
395 if (value == *drop_asym_route) {
396 mutex_unlock(&the_lnet.ln_api_mutex);
400 *drop_asym_route = value;
402 mutex_unlock(&the_lnet.ln_api_mutex);
408 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
411 unsigned *transaction_to = (unsigned *)kp->arg;
414 rc = kstrtoul(val, 0, &value);
416 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
421 * The purpose of locking the api_mutex here is to ensure that
422 * the correct value ends up stored properly.
424 mutex_lock(&the_lnet.ln_api_mutex);
426 if (value <= lnet_retry_count || value == 0) {
427 mutex_unlock(&the_lnet.ln_api_mutex);
428 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
429 "Has to be greater than lnet_retry_count (%u)\n",
430 value, lnet_retry_count);
434 if (value == *transaction_to) {
435 mutex_unlock(&the_lnet.ln_api_mutex);
439 *transaction_to = value;
440 /* Update the lnet_lnd_timeout now that we've modified the
441 * transaction timeout
443 lnet_set_lnd_timeout();
445 mutex_unlock(&the_lnet.ln_api_mutex);
451 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
454 unsigned *retry_count = (unsigned *)kp->arg;
457 rc = kstrtoul(val, 0, &value);
459 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
464 * The purpose of locking the api_mutex here is to ensure that
465 * the correct value ends up stored properly.
467 mutex_lock(&the_lnet.ln_api_mutex);
469 if (lnet_health_sensitivity == 0 && value > 0) {
470 mutex_unlock(&the_lnet.ln_api_mutex);
471 CERROR("Can not set lnet_retry_count when health feature is turned off\n");
475 if (value > lnet_transaction_timeout) {
476 mutex_unlock(&the_lnet.ln_api_mutex);
477 CERROR("Invalid value for lnet_retry_count (%lu). "
478 "Has to be smaller than lnet_transaction_timeout (%u)\n",
479 value, lnet_transaction_timeout);
483 *retry_count = value;
485 /* Update the lnet_lnd_timeout now that we've modified the
488 lnet_set_lnd_timeout();
490 mutex_unlock(&the_lnet.ln_api_mutex);
496 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
500 rc = kstrtoint(val, 0, &value);
502 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
506 if (value < LNET_INTERFACES_MIN) {
507 CWARN("max interfaces provided are too small, setting to %d\n",
508 LNET_INTERFACES_MAX_DEFAULT);
509 value = LNET_INTERFACES_MAX_DEFAULT;
512 *(int *)kp->arg = value;
518 response_tracking_set(const char *val, cfs_kernel_param_arg_t *kp)
521 unsigned long new_value;
523 rc = kstrtoul(val, 0, &new_value);
525 CERROR("Invalid value for 'lnet_response_tracking'\n");
529 if (new_value < 0 || new_value > 3) {
530 CWARN("Invalid value (%lu) for 'lnet_response_tracking'\n",
535 lnet_response_tracking = new_value;
541 lnet_get_routes(void)
547 lnet_get_networks(void)
552 if (*networks != 0 && *ip2nets != 0) {
553 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
554 "'ip2nets' but not both at once\n");
559 rc = lnet_parse_ip2nets(&nets, ip2nets);
560 return (rc == 0) ? nets : NULL;
570 lnet_init_locks(void)
572 spin_lock_init(&the_lnet.ln_eq_wait_lock);
573 spin_lock_init(&the_lnet.ln_msg_resend_lock);
574 init_completion(&the_lnet.ln_mt_wait_complete);
575 mutex_init(&the_lnet.ln_lnd_mutex);
578 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
579 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
581 struct kmem_cache *lnet_udsp_cachep; /* udsp cache */
582 struct kmem_cache *lnet_rspt_cachep; /* response tracker cache */
583 struct kmem_cache *lnet_msg_cachep;
586 lnet_slab_setup(void)
588 /* create specific kmem_cache for MEs and small MDs (i.e., originally
589 * allocated in <size-xxx> kmem_cache).
591 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
593 if (!lnet_mes_cachep)
596 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
597 LNET_SMALL_MD_SIZE, 0, 0,
599 if (!lnet_small_mds_cachep)
602 lnet_udsp_cachep = kmem_cache_create("lnet_udsp",
603 sizeof(struct lnet_udsp),
605 if (!lnet_udsp_cachep)
608 lnet_rspt_cachep = kmem_cache_create("lnet_rspt", sizeof(struct lnet_rsp_tracker),
610 if (!lnet_rspt_cachep)
613 lnet_msg_cachep = kmem_cache_create("lnet_msg", sizeof(struct lnet_msg),
615 if (!lnet_msg_cachep)
622 lnet_slab_cleanup(void)
624 if (lnet_msg_cachep) {
625 kmem_cache_destroy(lnet_msg_cachep);
626 lnet_msg_cachep = NULL;
629 if (lnet_rspt_cachep) {
630 kmem_cache_destroy(lnet_rspt_cachep);
631 lnet_rspt_cachep = NULL;
634 if (lnet_udsp_cachep) {
635 kmem_cache_destroy(lnet_udsp_cachep);
636 lnet_udsp_cachep = NULL;
639 if (lnet_small_mds_cachep) {
640 kmem_cache_destroy(lnet_small_mds_cachep);
641 lnet_small_mds_cachep = NULL;
644 if (lnet_mes_cachep) {
645 kmem_cache_destroy(lnet_mes_cachep);
646 lnet_mes_cachep = NULL;
651 lnet_create_remote_nets_table(void)
654 struct list_head *hash;
656 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
657 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
658 CFS_ALLOC_PTR_ARRAY(hash, LNET_REMOTE_NETS_HASH_SIZE);
660 CERROR("Failed to create remote nets hash table\n");
664 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
665 INIT_LIST_HEAD(&hash[i]);
666 the_lnet.ln_remote_nets_hash = hash;
671 lnet_destroy_remote_nets_table(void)
675 if (the_lnet.ln_remote_nets_hash == NULL)
678 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
679 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
681 CFS_FREE_PTR_ARRAY(the_lnet.ln_remote_nets_hash,
682 LNET_REMOTE_NETS_HASH_SIZE);
683 the_lnet.ln_remote_nets_hash = NULL;
687 lnet_destroy_locks(void)
689 if (the_lnet.ln_res_lock != NULL) {
690 cfs_percpt_lock_free(the_lnet.ln_res_lock);
691 the_lnet.ln_res_lock = NULL;
694 if (the_lnet.ln_net_lock != NULL) {
695 cfs_percpt_lock_free(the_lnet.ln_net_lock);
696 the_lnet.ln_net_lock = NULL;
701 lnet_create_locks(void)
705 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
706 if (the_lnet.ln_res_lock == NULL)
709 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
710 if (the_lnet.ln_net_lock == NULL)
716 lnet_destroy_locks();
720 static void lnet_assert_wire_constants(void)
722 /* Wire protocol assertions generated by 'wirecheck'
723 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
724 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
725 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
729 BUILD_BUG_ON(LNET_PROTO_TCP_MAGIC != 0xeebc0ded);
730 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MAJOR != 1);
731 BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MINOR != 0);
732 BUILD_BUG_ON(LNET_MSG_ACK != 0);
733 BUILD_BUG_ON(LNET_MSG_PUT != 1);
734 BUILD_BUG_ON(LNET_MSG_GET != 2);
735 BUILD_BUG_ON(LNET_MSG_REPLY != 3);
736 BUILD_BUG_ON(LNET_MSG_HELLO != 4);
738 BUILD_BUG_ON((int)sizeof(lnet_nid_t) != 8);
739 BUILD_BUG_ON((int)sizeof(lnet_pid_t) != 4);
741 /* Checks for struct lnet_nid */
742 BUILD_BUG_ON((int)sizeof(struct lnet_nid) != 20);
743 BUILD_BUG_ON((int)offsetof(struct lnet_nid, nid_size) != 0);
744 BUILD_BUG_ON((int)sizeof(((struct lnet_nid *)0)->nid_size) != 1);
745 BUILD_BUG_ON((int)offsetof(struct lnet_nid, nid_type) != 1);
746 BUILD_BUG_ON((int)sizeof(((struct lnet_nid *)0)->nid_type) != 1);
747 BUILD_BUG_ON((int)offsetof(struct lnet_nid, nid_num) != 2);
748 BUILD_BUG_ON((int)sizeof(((struct lnet_nid *)0)->nid_num) != 2);
749 BUILD_BUG_ON((int)offsetof(struct lnet_nid, nid_addr) != 4);
750 BUILD_BUG_ON((int)sizeof(((struct lnet_nid *)0)->nid_addr) != 16);
752 /* Checks for struct lnet_process_id_packed */
753 BUILD_BUG_ON((int)sizeof(struct lnet_process_id_packed) != 12);
754 BUILD_BUG_ON((int)offsetof(struct lnet_process_id_packed, nid) != 0);
755 BUILD_BUG_ON((int)sizeof(((struct lnet_process_id_packed *)0)->nid) != 8);
756 BUILD_BUG_ON((int)offsetof(struct lnet_process_id_packed, pid) != 8);
757 BUILD_BUG_ON((int)sizeof(((struct lnet_process_id_packed *)0)->pid) != 4);
759 /* Checks for struct lnet_handle_wire */
760 BUILD_BUG_ON((int)sizeof(struct lnet_handle_wire) != 16);
761 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
762 wh_interface_cookie) != 0);
763 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) != 8);
764 BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
765 wh_object_cookie) != 8);
766 BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) != 8);
768 /* Checks for struct struct lnet_magicversion */
769 BUILD_BUG_ON((int)sizeof(struct lnet_magicversion) != 8);
770 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, magic) != 0);
771 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->magic) != 4);
772 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, version_major) != 4);
773 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_major) != 2);
774 BUILD_BUG_ON((int)offsetof(struct lnet_magicversion,
775 version_minor) != 6);
776 BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_minor) != 2);
778 /* Checks for struct struct lnet_hdr */
779 BUILD_BUG_ON((int)sizeof(struct lnet_hdr) != 72);
780 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_nid) != 0);
781 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_nid) != 8);
782 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_nid) != 8);
783 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_nid) != 8);
784 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_pid) != 16);
785 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_pid) != 4);
786 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_pid) != 20);
787 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_pid) != 4);
788 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, type) != 24);
789 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->type) != 4);
790 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, payload_length) != 28);
791 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->payload_length) != 4);
792 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg) != 32);
793 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg) != 40);
796 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) != 32);
797 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) != 16);
798 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.match_bits) != 48);
799 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) != 8);
800 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.mlength) != 56);
801 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) != 4);
804 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) != 32);
805 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) != 16);
806 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.match_bits) != 48);
807 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) != 8);
808 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.hdr_data) != 56);
809 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) != 8);
810 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ptl_index) != 64);
811 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) != 4);
812 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.offset) != 68);
813 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) != 4);
816 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.return_wmd) != 32);
817 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) != 16);
818 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.match_bits) != 48);
819 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) != 8);
820 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.ptl_index) != 56);
821 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) != 4);
822 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.src_offset) != 60);
823 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) != 4);
824 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.sink_length) != 64);
825 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) != 4);
828 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) != 32);
829 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) != 16);
832 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.incarnation) != 32);
833 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) != 8);
834 BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.type) != 40);
835 BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) != 4);
837 /* Checks for struct lnet_ni_status and related constants */
838 BUILD_BUG_ON(LNET_NI_STATUS_INVALID != 0x00000000);
839 BUILD_BUG_ON(LNET_NI_STATUS_UP != 0x15aac0de);
840 BUILD_BUG_ON(LNET_NI_STATUS_DOWN != 0xdeadface);
842 /* Checks for struct lnet_ni_status */
843 BUILD_BUG_ON((int)sizeof(struct lnet_ni_status) != 16);
844 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_nid) != 0);
845 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) != 8);
846 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_status) != 8);
847 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_status) != 4);
848 BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_unused) != 12);
849 BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) != 4);
851 /* Checks for struct lnet_ping_info and related constants */
852 BUILD_BUG_ON(LNET_PROTO_PING_MAGIC != 0x70696E67);
853 BUILD_BUG_ON(LNET_PING_FEAT_INVAL != 0);
854 BUILD_BUG_ON(LNET_PING_FEAT_BASE != 1);
855 BUILD_BUG_ON(LNET_PING_FEAT_NI_STATUS != 2);
856 BUILD_BUG_ON(LNET_PING_FEAT_RTE_DISABLED != 4);
857 BUILD_BUG_ON(LNET_PING_FEAT_MULTI_RAIL != 8);
858 BUILD_BUG_ON(LNET_PING_FEAT_DISCOVERY != 16);
859 BUILD_BUG_ON(LNET_PING_FEAT_BITS != 31);
861 /* Checks for struct lnet_ping_info */
862 BUILD_BUG_ON((int)sizeof(struct lnet_ping_info) != 16);
863 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_magic) != 0);
864 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) != 4);
865 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_features) != 4);
866 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_features) != 4);
867 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_pid) != 8);
868 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) != 4);
869 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_nnis) != 12);
870 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) != 4);
871 BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_ni) != 16);
872 BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) != 0);
874 /* Acceptor connection request */
875 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
877 /* Checks for struct lnet_acceptor_connreq */
878 BUILD_BUG_ON((int)sizeof(struct lnet_acceptor_connreq) != 16);
879 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq, acr_magic) != 0);
880 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq *)0)->acr_magic) != 4);
881 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq, acr_version) != 4);
882 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq *)0)->acr_version) != 4);
883 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq, acr_nid) != 8);
884 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq *)0)->acr_nid) != 8);
886 /* Checks for struct lnet_acceptor_connreq_v2 */
887 BUILD_BUG_ON((int)sizeof(struct lnet_acceptor_connreq_v2) != 28);
888 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq_v2, acr_magic) != 0);
889 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq_v2 *)0)->acr_magic) != 4);
890 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq_v2, acr_version) != 4);
891 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq_v2 *)0)->acr_version) != 4);
892 BUILD_BUG_ON((int)offsetof(struct lnet_acceptor_connreq_v2, acr_nid) != 8);
893 BUILD_BUG_ON((int)sizeof(((struct lnet_acceptor_connreq_v2 *)0)->acr_nid) != 20);
895 /* Checks for struct lnet_counters_common */
896 BUILD_BUG_ON((int)sizeof(struct lnet_counters_common) != 60);
897 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_msgs_alloc) != 0);
898 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_msgs_alloc) != 4);
899 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_msgs_max) != 4);
900 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_msgs_max) != 4);
901 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_errors) != 8);
902 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_errors) != 4);
903 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_send_count) != 12);
904 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_send_count) != 4);
905 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_recv_count) != 16);
906 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_recv_count) != 4);
907 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_route_count) != 20);
908 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_route_count) != 4);
909 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_drop_count) != 24);
910 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_drop_count) != 4);
911 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_send_length) != 28);
912 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_send_length) != 8);
913 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_recv_length) != 36);
914 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_recv_length) != 8);
915 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_route_length) != 44);
916 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_route_length) != 8);
917 BUILD_BUG_ON((int)offsetof(struct lnet_counters_common, lcc_drop_length) != 52);
918 BUILD_BUG_ON((int)sizeof(((struct lnet_counters_common *)0)->lcc_drop_length) != 8);
921 static const struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
923 const struct lnet_lnd *lnd;
925 /* holding lnd mutex */
926 if (type >= NUM_LNDS)
928 lnd = the_lnet.ln_lnds[type];
929 LASSERT(!lnd || lnd->lnd_type == type);
935 lnet_get_lnd_timeout(void)
937 return lnet_lnd_timeout;
939 EXPORT_SYMBOL(lnet_get_lnd_timeout);
942 lnet_register_lnd(const struct lnet_lnd *lnd)
944 mutex_lock(&the_lnet.ln_lnd_mutex);
946 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
947 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
949 the_lnet.ln_lnds[lnd->lnd_type] = lnd;
951 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
953 mutex_unlock(&the_lnet.ln_lnd_mutex);
955 EXPORT_SYMBOL(lnet_register_lnd);
958 lnet_unregister_lnd(const struct lnet_lnd *lnd)
960 mutex_lock(&the_lnet.ln_lnd_mutex);
962 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
964 the_lnet.ln_lnds[lnd->lnd_type] = NULL;
965 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
967 mutex_unlock(&the_lnet.ln_lnd_mutex);
969 EXPORT_SYMBOL(lnet_unregister_lnd);
972 lnet_counters_get_common_locked(struct lnet_counters_common *common)
974 struct lnet_counters *ctr;
977 /* FIXME !!! Their is no assert_lnet_net_locked() to ensure this
978 * actually called under the protection of the lnet_net_lock.
980 memset(common, 0, sizeof(*common));
982 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
983 common->lcc_msgs_max += ctr->lct_common.lcc_msgs_max;
984 common->lcc_msgs_alloc += ctr->lct_common.lcc_msgs_alloc;
985 common->lcc_errors += ctr->lct_common.lcc_errors;
986 common->lcc_send_count += ctr->lct_common.lcc_send_count;
987 common->lcc_recv_count += ctr->lct_common.lcc_recv_count;
988 common->lcc_route_count += ctr->lct_common.lcc_route_count;
989 common->lcc_drop_count += ctr->lct_common.lcc_drop_count;
990 common->lcc_send_length += ctr->lct_common.lcc_send_length;
991 common->lcc_recv_length += ctr->lct_common.lcc_recv_length;
992 common->lcc_route_length += ctr->lct_common.lcc_route_length;
993 common->lcc_drop_length += ctr->lct_common.lcc_drop_length;
998 lnet_counters_get_common(struct lnet_counters_common *common)
1000 lnet_net_lock(LNET_LOCK_EX);
1001 lnet_counters_get_common_locked(common);
1002 lnet_net_unlock(LNET_LOCK_EX);
1004 EXPORT_SYMBOL(lnet_counters_get_common);
1007 lnet_counters_get(struct lnet_counters *counters)
1009 struct lnet_counters *ctr;
1010 struct lnet_counters_health *health = &counters->lct_health;
1013 memset(counters, 0, sizeof(*counters));
1015 lnet_net_lock(LNET_LOCK_EX);
1017 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1018 GOTO(out_unlock, rc = -ENODEV);
1020 lnet_counters_get_common_locked(&counters->lct_common);
1022 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
1023 health->lch_rst_alloc += ctr->lct_health.lch_rst_alloc;
1024 health->lch_resend_count += ctr->lct_health.lch_resend_count;
1025 health->lch_response_timeout_count +=
1026 ctr->lct_health.lch_response_timeout_count;
1027 health->lch_local_interrupt_count +=
1028 ctr->lct_health.lch_local_interrupt_count;
1029 health->lch_local_dropped_count +=
1030 ctr->lct_health.lch_local_dropped_count;
1031 health->lch_local_aborted_count +=
1032 ctr->lct_health.lch_local_aborted_count;
1033 health->lch_local_no_route_count +=
1034 ctr->lct_health.lch_local_no_route_count;
1035 health->lch_local_timeout_count +=
1036 ctr->lct_health.lch_local_timeout_count;
1037 health->lch_local_error_count +=
1038 ctr->lct_health.lch_local_error_count;
1039 health->lch_remote_dropped_count +=
1040 ctr->lct_health.lch_remote_dropped_count;
1041 health->lch_remote_error_count +=
1042 ctr->lct_health.lch_remote_error_count;
1043 health->lch_remote_timeout_count +=
1044 ctr->lct_health.lch_remote_timeout_count;
1045 health->lch_network_timeout_count +=
1046 ctr->lct_health.lch_network_timeout_count;
1049 lnet_net_unlock(LNET_LOCK_EX);
1052 EXPORT_SYMBOL(lnet_counters_get);
1055 lnet_counters_reset(void)
1057 struct lnet_counters *counters;
1060 lnet_net_lock(LNET_LOCK_EX);
1062 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1065 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
1066 memset(counters, 0, sizeof(struct lnet_counters));
1068 lnet_net_unlock(LNET_LOCK_EX);
1072 lnet_res_type2str(int type)
1077 case LNET_COOKIE_TYPE_MD:
1079 case LNET_COOKIE_TYPE_ME:
1081 case LNET_COOKIE_TYPE_EQ:
1087 lnet_res_container_cleanup(struct lnet_res_container *rec)
1091 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
1094 while (!list_empty(&rec->rec_active)) {
1095 struct list_head *e = rec->rec_active.next;
1098 if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
1099 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
1101 } else { /* NB: Active MEs should be attached on portals */
1108 /* Found alive MD/ME/EQ, user really should unlink/free
1109 * all of them before finalize LNet, but if someone didn't,
1110 * we have to recycle garbage for him */
1111 CERROR("%d active elements on exit of %s container\n",
1112 count, lnet_res_type2str(rec->rec_type));
1115 if (rec->rec_lh_hash != NULL) {
1116 CFS_FREE_PTR_ARRAY(rec->rec_lh_hash, LNET_LH_HASH_SIZE);
1117 rec->rec_lh_hash = NULL;
1120 rec->rec_type = 0; /* mark it as finalized */
1124 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1129 LASSERT(rec->rec_type == 0);
1131 rec->rec_type = type;
1132 INIT_LIST_HEAD(&rec->rec_active);
1134 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1136 /* Arbitrary choice of hash table size */
1137 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1138 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1139 if (rec->rec_lh_hash == NULL) {
1144 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1145 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1150 CERROR("Failed to setup %s resource container\n",
1151 lnet_res_type2str(type));
1152 lnet_res_container_cleanup(rec);
1157 lnet_res_containers_destroy(struct lnet_res_container **recs)
1159 struct lnet_res_container *rec;
1162 cfs_percpt_for_each(rec, i, recs)
1163 lnet_res_container_cleanup(rec);
1165 cfs_percpt_free(recs);
1168 static struct lnet_res_container **
1169 lnet_res_containers_create(int type)
1171 struct lnet_res_container **recs;
1172 struct lnet_res_container *rec;
1176 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1178 CERROR("Failed to allocate %s resource containers\n",
1179 lnet_res_type2str(type));
1183 cfs_percpt_for_each(rec, i, recs) {
1184 rc = lnet_res_container_setup(rec, i, type);
1186 lnet_res_containers_destroy(recs);
1194 struct lnet_libhandle *
1195 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1197 /* ALWAYS called with lnet_res_lock held */
1198 struct list_head *head;
1199 struct lnet_libhandle *lh;
1202 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1205 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1206 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1208 list_for_each_entry(lh, head, lh_hash_chain) {
1209 if (lh->lh_cookie == cookie)
1217 lnet_res_lh_initialize(struct lnet_res_container *rec,
1218 struct lnet_libhandle *lh)
1220 /* ALWAYS called with lnet_res_lock held */
1221 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1224 lh->lh_cookie = rec->rec_lh_cookie;
1225 rec->rec_lh_cookie += 1 << ibits;
1227 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1229 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1233 lnet_create_array_of_queues(void)
1235 struct list_head **qs;
1236 struct list_head *q;
1239 qs = cfs_percpt_alloc(lnet_cpt_table(),
1240 sizeof(struct list_head));
1242 CERROR("Failed to allocate queues\n");
1246 cfs_percpt_for_each(q, i, qs)
1252 static int lnet_unprepare(void);
1255 lnet_prepare(lnet_pid_t requested_pid)
1257 /* Prepare to bring up the network */
1258 struct lnet_res_container **recs;
1261 if (requested_pid == LNET_PID_ANY) {
1262 /* Don't instantiate LNET just for me */
1266 LASSERT(the_lnet.ln_refcount == 0);
1268 the_lnet.ln_routing = 0;
1270 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1271 the_lnet.ln_pid = requested_pid;
1273 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1274 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1275 INIT_LIST_HEAD(&the_lnet.ln_nets);
1276 INIT_LIST_HEAD(&the_lnet.ln_routers);
1277 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1278 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1279 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1280 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1281 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1282 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1283 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1284 INIT_LIST_HEAD(&the_lnet.ln_udsp_list);
1285 init_waitqueue_head(&the_lnet.ln_dc_waitq);
1286 the_lnet.ln_mt_handler = NULL;
1287 init_completion(&the_lnet.ln_started);
1289 rc = lnet_slab_setup();
1293 rc = lnet_create_remote_nets_table();
1298 * NB the interface cookie in wire handles guards against delayed
1299 * replies and ACKs appearing valid after reboot.
1301 the_lnet.ln_interface_cookie = ktime_get_real_ns();
1303 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1304 sizeof(struct lnet_counters));
1305 if (the_lnet.ln_counters == NULL) {
1306 CERROR("Failed to allocate counters for LNet\n");
1311 rc = lnet_peer_tables_create();
1315 rc = lnet_msg_containers_create();
1319 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1320 LNET_COOKIE_TYPE_EQ);
1324 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1330 the_lnet.ln_md_containers = recs;
1332 rc = lnet_portals_create();
1334 CERROR("Failed to create portals for LNet: %d\n", rc);
1338 the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1339 if (!the_lnet.ln_mt_zombie_rstqs) {
1352 lnet_unprepare (void)
1354 /* NB no LNET_LOCK since this is the last reference. All LND instances
1355 * have shut down already, so it is safe to unlink and free all
1356 * descriptors, even those that appear committed to a network op (eg MD
1357 * with non-zero pending count) */
1359 lnet_fail_nid(LNET_NID_ANY, 0);
1361 LASSERT(the_lnet.ln_refcount == 0);
1362 LASSERT(list_empty(&the_lnet.ln_test_peers));
1363 LASSERT(list_empty(&the_lnet.ln_nets));
1365 if (the_lnet.ln_mt_zombie_rstqs) {
1366 lnet_clean_zombie_rstqs();
1367 the_lnet.ln_mt_zombie_rstqs = NULL;
1370 lnet_assert_handler_unused(the_lnet.ln_mt_handler);
1371 the_lnet.ln_mt_handler = NULL;
1373 lnet_portals_destroy();
1375 if (the_lnet.ln_md_containers != NULL) {
1376 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1377 the_lnet.ln_md_containers = NULL;
1380 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1382 lnet_msg_containers_destroy();
1384 lnet_rtrpools_free(0);
1386 if (the_lnet.ln_counters != NULL) {
1387 cfs_percpt_free(the_lnet.ln_counters);
1388 the_lnet.ln_counters = NULL;
1390 lnet_destroy_remote_nets_table();
1391 lnet_udsp_destroy(true);
1392 lnet_slab_cleanup();
1398 lnet_net2ni_locked(__u32 net_id, int cpt)
1401 struct lnet_net *net;
1403 LASSERT(cpt != LNET_LOCK_EX);
1405 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1406 if (net->net_id == net_id) {
1407 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1417 lnet_net2ni_addref(__u32 net)
1422 ni = lnet_net2ni_locked(net, 0);
1424 lnet_ni_addref_locked(ni, 0);
1429 EXPORT_SYMBOL(lnet_net2ni_addref);
1432 lnet_get_net_locked(__u32 net_id)
1434 struct lnet_net *net;
1436 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1437 if (net->net_id == net_id)
1445 lnet_net_clr_pref_rtrs(struct lnet_net *net)
1447 struct list_head zombies;
1448 struct lnet_nid_list *ne;
1449 struct lnet_nid_list *tmp;
1451 INIT_LIST_HEAD(&zombies);
1453 lnet_net_lock(LNET_LOCK_EX);
1454 list_splice_init(&net->net_rtr_pref_nids, &zombies);
1455 lnet_net_unlock(LNET_LOCK_EX);
1457 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1458 list_del_init(&ne->nl_list);
1459 LIBCFS_FREE(ne, sizeof(*ne));
1464 lnet_net_add_pref_rtr(struct lnet_net *net,
1465 struct lnet_nid *gw_nid)
1466 __must_hold(&the_lnet.ln_api_mutex)
1468 struct lnet_nid_list *ne;
1470 /* This function is called with api_mutex held. When the api_mutex
1471 * is held the list can not be modified, as it is only modified as
1472 * a result of applying a UDSP and that happens under api_mutex
1475 list_for_each_entry(ne, &net->net_rtr_pref_nids, nl_list) {
1476 if (nid_same(&ne->nl_nid, gw_nid))
1480 LIBCFS_ALLOC(ne, sizeof(*ne));
1484 ne->nl_nid = *gw_nid;
1486 /* Lock the cpt to protect against addition and checks in the
1487 * selection algorithm
1489 lnet_net_lock(LNET_LOCK_EX);
1490 list_add(&ne->nl_list, &net->net_rtr_pref_nids);
1491 lnet_net_unlock(LNET_LOCK_EX);
1497 lnet_net_is_pref_rtr_locked(struct lnet_net *net, struct lnet_nid *rtr_nid)
1499 struct lnet_nid_list *ne;
1501 CDEBUG(D_NET, "%s: rtr pref empty: %d\n",
1502 libcfs_net2str(net->net_id),
1503 list_empty(&net->net_rtr_pref_nids));
1505 if (list_empty(&net->net_rtr_pref_nids))
1508 list_for_each_entry(ne, &net->net_rtr_pref_nids, nl_list) {
1509 CDEBUG(D_NET, "Comparing pref %s with gw %s\n",
1510 libcfs_nidstr(&ne->nl_nid),
1511 libcfs_nidstr(rtr_nid));
1512 if (nid_same(rtr_nid, &ne->nl_nid))
1520 lnet_nid4_cpt_hash(lnet_nid_t nid, unsigned int number)
1525 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1530 val = hash_long(key, LNET_CPT_BITS);
1531 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1535 return (unsigned int)(key + val + (val >> 1)) % number;
1539 lnet_nid_cpt_hash(struct lnet_nid *nid, unsigned int number)
1545 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1550 if (nid_is_nid4(nid))
1551 return lnet_nid4_cpt_hash(lnet_nid_to_nid4(nid), number);
1553 for (i = 0; i < 4; i++)
1554 h = hash_32(nid->nid_addr[i]^h, 32);
1555 val = hash_32(LNET_NID_NET(nid) ^ h, LNET_CPT_BITS);
1558 return (unsigned int)(h + val + (val >> 1)) % number;
1562 lnet_cpt_of_nid_locked(struct lnet_nid *nid, struct lnet_ni *ni)
1564 struct lnet_net *net;
1566 /* must called with hold of lnet_net_lock */
1567 if (LNET_CPT_NUMBER == 1)
1568 return 0; /* the only one */
1571 * If NI is provided then use the CPT identified in the NI cpt
1572 * list if one exists. If one doesn't exist, then that NI is
1573 * associated with all CPTs and it follows that the net it belongs
1574 * to is implicitly associated with all CPTs, so just hash the nid
1578 if (ni->ni_cpts != NULL)
1579 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1582 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1585 /* no NI provided so look at the net */
1586 net = lnet_get_net_locked(LNET_NID_NET(nid));
1588 if (net != NULL && net->net_cpts != NULL) {
1589 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1592 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1596 lnet_nid2cpt(struct lnet_nid *nid, struct lnet_ni *ni)
1601 if (LNET_CPT_NUMBER == 1)
1602 return 0; /* the only one */
1604 cpt = lnet_net_lock_current();
1606 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1608 lnet_net_unlock(cpt);
1612 EXPORT_SYMBOL(lnet_nid2cpt);
1615 lnet_cpt_of_nid(lnet_nid_t nid4, struct lnet_ni *ni)
1617 struct lnet_nid nid;
1619 if (LNET_CPT_NUMBER == 1)
1620 return 0; /* the only one */
1622 lnet_nid4_to_nid(nid4, &nid);
1623 return lnet_nid2cpt(&nid, ni);
1625 EXPORT_SYMBOL(lnet_cpt_of_nid);
1628 lnet_islocalnet_locked(__u32 net_id)
1630 struct lnet_net *net;
1633 net = lnet_get_net_locked(net_id);
1635 local = net != NULL;
1641 lnet_islocalnet(__u32 net_id)
1646 cpt = lnet_net_lock_current();
1648 local = lnet_islocalnet_locked(net_id);
1650 lnet_net_unlock(cpt);
1656 lnet_nid_to_ni_locked(struct lnet_nid *nid, int cpt)
1658 struct lnet_net *net;
1661 LASSERT(cpt != LNET_LOCK_EX);
1663 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1664 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1665 if (nid_same(&ni->ni_nid, nid))
1674 lnet_nid2ni_locked(lnet_nid_t nid4, int cpt)
1676 struct lnet_nid nid;
1678 lnet_nid4_to_nid(nid4, &nid);
1679 return lnet_nid_to_ni_locked(&nid, cpt);
1683 lnet_nid2ni_addref(lnet_nid_t nid4)
1686 struct lnet_nid nid;
1688 lnet_nid4_to_nid(nid4, &nid);
1691 ni = lnet_nid_to_ni_locked(&nid, 0);
1693 lnet_ni_addref_locked(ni, 0);
1698 EXPORT_SYMBOL(lnet_nid2ni_addref);
1701 lnet_nid_to_ni_addref(struct lnet_nid *nid)
1706 ni = lnet_nid_to_ni_locked(nid, 0);
1708 lnet_ni_addref_locked(ni, 0);
1713 EXPORT_SYMBOL(lnet_nid_to_ni_addref);
1716 lnet_islocalnid4(lnet_nid_t nid)
1721 cpt = lnet_net_lock_current();
1722 ni = lnet_nid2ni_locked(nid, cpt);
1723 lnet_net_unlock(cpt);
1729 lnet_islocalnid(struct lnet_nid *nid)
1734 cpt = lnet_net_lock_current();
1735 ni = lnet_nid_to_ni_locked(nid, cpt);
1736 lnet_net_unlock(cpt);
1742 lnet_count_acceptor_nets(void)
1744 /* Return the # of NIs that need the acceptor. */
1746 struct lnet_net *net;
1749 cpt = lnet_net_lock_current();
1750 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1751 /* all socklnd type networks should have the acceptor
1753 if (net->net_lnd->lnd_accept != NULL)
1757 lnet_net_unlock(cpt);
1762 struct lnet_ping_buffer *
1763 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1765 struct lnet_ping_buffer *pbuf;
1767 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1769 pbuf->pb_nnis = nnis;
1770 pbuf->pb_needs_post = false;
1771 atomic_set(&pbuf->pb_refcnt, 1);
1778 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1780 LASSERT(atomic_read(&pbuf->pb_refcnt) == 0);
1781 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1784 static struct lnet_ping_buffer *
1785 lnet_ping_target_create(int nnis)
1787 struct lnet_ping_buffer *pbuf;
1789 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1791 CERROR("Can't allocate ping source [%d]\n", nnis);
1795 pbuf->pb_info.pi_nnis = nnis;
1796 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1797 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1798 pbuf->pb_info.pi_features =
1799 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1805 lnet_get_net_ni_count_locked(struct lnet_net *net)
1810 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1817 lnet_get_net_ni_count_pre(struct lnet_net *net)
1822 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1829 lnet_get_ni_count(void)
1832 struct lnet_net *net;
1837 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1838 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1848 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1850 struct lnet_ni_status *stat;
1854 __swab32s(&pbuf->pb_info.pi_magic);
1855 __swab32s(&pbuf->pb_info.pi_features);
1856 __swab32s(&pbuf->pb_info.pi_pid);
1857 __swab32s(&pbuf->pb_info.pi_nnis);
1858 nnis = pbuf->pb_info.pi_nnis;
1859 if (nnis > pbuf->pb_nnis)
1860 nnis = pbuf->pb_nnis;
1861 for (i = 0; i < nnis; i++) {
1862 stat = &pbuf->pb_info.pi_ni[i];
1863 __swab64s(&stat->ns_nid);
1864 __swab32s(&stat->ns_status);
1869 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1873 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1875 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1877 /* Loopback is guaranteed to be present */
1878 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1880 if (LNET_PING_INFO_LONI(pinfo) != LNET_NID_LO_0)
1886 lnet_ping_target_destroy(void)
1888 struct lnet_net *net;
1891 lnet_net_lock(LNET_LOCK_EX);
1893 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1894 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1896 ni->ni_status = NULL;
1901 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1902 the_lnet.ln_ping_target = NULL;
1904 lnet_net_unlock(LNET_LOCK_EX);
1908 lnet_ping_target_event_handler(struct lnet_event *event)
1910 struct lnet_ping_buffer *pbuf = event->md_user_ptr;
1912 if (event->unlinked)
1913 lnet_ping_buffer_decref(pbuf);
1917 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1918 struct lnet_handle_md *ping_mdh,
1919 int ni_count, bool set_eq)
1921 struct lnet_processid id = {
1922 .nid = LNET_ANY_NID,
1926 struct lnet_md md = { NULL };
1930 the_lnet.ln_ping_target_handler =
1931 lnet_ping_target_event_handler;
1933 *ppbuf = lnet_ping_target_create(ni_count);
1934 if (*ppbuf == NULL) {
1939 /* Ping target ME/MD */
1940 me = LNetMEAttach(LNET_RESERVED_PORTAL, &id,
1941 LNET_PROTO_PING_MATCHBITS, 0,
1942 LNET_UNLINK, LNET_INS_AFTER);
1945 CERROR("Can't create ping target ME: %d\n", rc);
1946 goto fail_decref_ping_buffer;
1949 /* initialize md content */
1950 md.start = &(*ppbuf)->pb_info;
1951 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1952 md.threshold = LNET_MD_THRESH_INF;
1954 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1955 LNET_MD_MANAGE_REMOTE;
1956 md.handler = the_lnet.ln_ping_target_handler;
1957 md.user_ptr = *ppbuf;
1959 rc = LNetMDAttach(me, &md, LNET_RETAIN, ping_mdh);
1961 CERROR("Can't attach ping target MD: %d\n", rc);
1962 goto fail_decref_ping_buffer;
1964 lnet_ping_buffer_addref(*ppbuf);
1968 fail_decref_ping_buffer:
1969 LASSERT(atomic_read(&(*ppbuf)->pb_refcnt) == 1);
1970 lnet_ping_buffer_decref(*ppbuf);
1977 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1978 struct lnet_handle_md *ping_mdh)
1980 LNetMDUnlink(*ping_mdh);
1981 LNetInvalidateMDHandle(ping_mdh);
1983 /* NB the MD could be busy; this just starts the unlink */
1984 wait_var_event_warning(&pbuf->pb_refcnt,
1985 atomic_read(&pbuf->pb_refcnt) <= 1,
1986 "Still waiting for ping data MD to unlink\n");
1990 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1993 struct lnet_net *net;
1994 struct lnet_ni_status *ns;
1999 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2000 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2001 LASSERT(i < pbuf->pb_nnis);
2003 ns = &pbuf->pb_info.pi_ni[i];
2005 if (!nid_is_nid4(&ni->ni_nid))
2007 ns->ns_nid = lnet_nid_to_nid4(&ni->ni_nid);
2010 ns->ns_status = lnet_ni_get_status_locked(ni);
2018 * We (ab)use the ns_status of the loopback interface to
2019 * transmit the sequence number. The first interface listed
2020 * must be the loopback interface.
2022 rc = lnet_ping_info_validate(&pbuf->pb_info);
2024 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
2027 LNET_PING_BUFFER_SEQNO(pbuf) =
2028 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
2032 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
2033 struct lnet_handle_md ping_mdh)
2035 struct lnet_ping_buffer *old_pbuf = NULL;
2036 struct lnet_handle_md old_ping_md;
2038 /* switch the NIs to point to the new ping info created */
2039 lnet_net_lock(LNET_LOCK_EX);
2041 if (!the_lnet.ln_routing)
2042 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
2043 if (!lnet_peer_discovery_disabled)
2044 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
2046 /* Ensure only known feature bits have been set. */
2047 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
2048 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
2050 lnet_ping_target_install_locked(pbuf);
2052 if (the_lnet.ln_ping_target) {
2053 old_pbuf = the_lnet.ln_ping_target;
2054 old_ping_md = the_lnet.ln_ping_target_md;
2056 the_lnet.ln_ping_target_md = ping_mdh;
2057 the_lnet.ln_ping_target = pbuf;
2059 lnet_net_unlock(LNET_LOCK_EX);
2062 /* unlink and free the old ping info */
2063 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
2064 lnet_ping_buffer_decref(old_pbuf);
2067 lnet_push_update_to_peers(0);
2071 lnet_ping_target_fini(void)
2073 lnet_ping_md_unlink(the_lnet.ln_ping_target,
2074 &the_lnet.ln_ping_target_md);
2076 lnet_assert_handler_unused(the_lnet.ln_ping_target_handler);
2077 lnet_ping_target_destroy();
2080 /* Resize the push target. */
2081 int lnet_push_target_resize(void)
2083 struct lnet_handle_md mdh;
2084 struct lnet_handle_md old_mdh;
2085 struct lnet_ping_buffer *pbuf;
2086 struct lnet_ping_buffer *old_pbuf;
2091 nnis = the_lnet.ln_push_target_nnis;
2093 CDEBUG(D_NET, "Invalid nnis %d\n", nnis);
2097 /* NB: lnet_ping_buffer_alloc() sets pbuf refcount to 1. That ref is
2098 * dropped when we need to resize again (see "old_pbuf" below) or when
2099 * LNet is shutdown (see lnet_push_target_fini())
2101 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
2103 CDEBUG(D_NET, "Can't allocate pbuf for nnis %d\n", nnis);
2107 rc = lnet_push_target_post(pbuf, &mdh);
2109 CDEBUG(D_NET, "Failed to post push target: %d\n", rc);
2110 lnet_ping_buffer_decref(pbuf);
2114 lnet_net_lock(LNET_LOCK_EX);
2115 old_pbuf = the_lnet.ln_push_target;
2116 old_mdh = the_lnet.ln_push_target_md;
2117 the_lnet.ln_push_target = pbuf;
2118 the_lnet.ln_push_target_md = mdh;
2119 lnet_net_unlock(LNET_LOCK_EX);
2122 LNetMDUnlink(old_mdh);
2123 /* Drop ref set by lnet_ping_buffer_alloc() */
2124 lnet_ping_buffer_decref(old_pbuf);
2127 /* Received another push or reply that requires a larger buffer */
2128 if (nnis < the_lnet.ln_push_target_nnis)
2131 CDEBUG(D_NET, "nnis %d success\n", nnis);
2135 int lnet_push_target_post(struct lnet_ping_buffer *pbuf,
2136 struct lnet_handle_md *mdhp)
2138 struct lnet_processid id = { LNET_ANY_NID, LNET_PID_ANY };
2139 struct lnet_md md = { NULL };
2143 me = LNetMEAttach(LNET_RESERVED_PORTAL, &id,
2144 LNET_PROTO_PING_MATCHBITS, 0,
2145 LNET_UNLINK, LNET_INS_AFTER);
2148 CERROR("Can't create push target ME: %d\n", rc);
2152 pbuf->pb_needs_post = false;
2154 /* This reference is dropped by lnet_push_target_event_handler() */
2155 lnet_ping_buffer_addref(pbuf);
2157 /* initialize md content */
2158 md.start = &pbuf->pb_info;
2159 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2162 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE;
2164 md.handler = the_lnet.ln_push_target_handler;
2166 rc = LNetMDAttach(me, &md, LNET_UNLINK, mdhp);
2168 CERROR("Can't attach push MD: %d\n", rc);
2169 lnet_ping_buffer_decref(pbuf);
2170 pbuf->pb_needs_post = true;
2174 CDEBUG(D_NET, "posted push target %p\n", pbuf);
2179 static void lnet_push_target_event_handler(struct lnet_event *ev)
2181 struct lnet_ping_buffer *pbuf = ev->md_user_ptr;
2183 CDEBUG(D_NET, "type %d status %d unlinked %d\n", ev->type, ev->status,
2186 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2187 lnet_swap_pinginfo(pbuf);
2189 if (ev->type == LNET_EVENT_UNLINK) {
2190 /* Drop ref added by lnet_push_target_post() */
2191 lnet_ping_buffer_decref(pbuf);
2195 lnet_peer_push_event(ev);
2197 /* Drop ref added by lnet_push_target_post */
2198 lnet_ping_buffer_decref(pbuf);
2201 /* Initialize the push target. */
2202 static int lnet_push_target_init(void)
2206 if (the_lnet.ln_push_target)
2209 the_lnet.ln_push_target_handler =
2210 lnet_push_target_event_handler;
2212 rc = LNetSetLazyPortal(LNET_RESERVED_PORTAL);
2215 /* Start at the required minimum, we'll enlarge if required. */
2216 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
2218 rc = lnet_push_target_resize();
2221 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2222 the_lnet.ln_push_target_handler = NULL;
2228 /* Clean up the push target. */
2229 static void lnet_push_target_fini(void)
2231 if (!the_lnet.ln_push_target)
2234 /* Unlink and invalidate to prevent new references. */
2235 LNetMDUnlink(the_lnet.ln_push_target_md);
2236 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
2238 /* Wait for the unlink to complete. */
2239 wait_var_event_warning(&the_lnet.ln_push_target->pb_refcnt,
2240 atomic_read(&the_lnet.ln_push_target->pb_refcnt) <= 1,
2241 "Still waiting for ping data MD to unlink\n");
2243 /* Drop ref set by lnet_ping_buffer_alloc() */
2244 lnet_ping_buffer_decref(the_lnet.ln_push_target);
2245 the_lnet.ln_push_target = NULL;
2246 the_lnet.ln_push_target_nnis = 0;
2248 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2249 lnet_assert_handler_unused(the_lnet.ln_push_target_handler);
2250 the_lnet.ln_push_target_handler = NULL;
2254 lnet_ni_tq_credits(struct lnet_ni *ni)
2258 LASSERT(ni->ni_ncpts >= 1);
2260 if (ni->ni_ncpts == 1)
2261 return ni->ni_net->net_tunables.lct_max_tx_credits;
2263 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2264 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2265 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2271 lnet_ni_unlink_locked(struct lnet_ni *ni)
2273 /* move it to zombie list and nobody can find it anymore */
2274 LASSERT(!list_empty(&ni->ni_netlist));
2275 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2276 lnet_ni_decref_locked(ni, 0);
2280 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2285 struct list_head *zombie_list = &net->net_ni_zombie;
2288 * Now wait for the NIs I just nuked to show up on the zombie
2289 * list and shut them down in guaranteed thread context
2292 while (!list_empty(zombie_list)) {
2296 ni = list_entry(zombie_list->next,
2297 struct lnet_ni, ni_netlist);
2298 list_del_init(&ni->ni_netlist);
2299 /* the ni should be in deleting state. If it's not it's
2301 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2302 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2305 /* still busy, add it back to zombie list */
2306 list_add(&ni->ni_netlist, zombie_list);
2310 if (!list_empty(&ni->ni_netlist)) {
2311 /* Unlock mutex while waiting to allow other
2312 * threads to read the LNet state and fall through
2315 lnet_net_unlock(LNET_LOCK_EX);
2316 mutex_unlock(&the_lnet.ln_api_mutex);
2319 if ((i & (-i)) == i) {
2321 "Waiting for zombie LNI %s\n",
2322 libcfs_nidstr(&ni->ni_nid));
2324 schedule_timeout_uninterruptible(cfs_time_seconds(1));
2326 mutex_lock(&the_lnet.ln_api_mutex);
2327 lnet_net_lock(LNET_LOCK_EX);
2331 lnet_net_unlock(LNET_LOCK_EX);
2333 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2335 LASSERT(!in_interrupt());
2336 /* Holding the mutex makes it safe for lnd_shutdown
2337 * to call module_put(). Module unload cannot finish
2338 * until lnet_unregister_lnd() completes, and that
2339 * requires the mutex.
2341 mutex_lock(&the_lnet.ln_lnd_mutex);
2342 (net->net_lnd->lnd_shutdown)(ni);
2343 mutex_unlock(&the_lnet.ln_lnd_mutex);
2346 CDEBUG(D_LNI, "Removed LNI %s\n",
2347 libcfs_nidstr(&ni->ni_nid));
2351 lnet_net_lock(LNET_LOCK_EX);
2355 /* shutdown down the NI and release refcount */
2357 lnet_shutdown_lndni(struct lnet_ni *ni)
2360 struct lnet_net *net = ni->ni_net;
2362 lnet_net_lock(LNET_LOCK_EX);
2364 ni->ni_state = LNET_NI_STATE_DELETING;
2366 lnet_ni_unlink_locked(ni);
2367 lnet_incr_dlc_seq();
2368 lnet_net_unlock(LNET_LOCK_EX);
2370 /* clear messages for this NI on the lazy portal */
2371 for (i = 0; i < the_lnet.ln_nportals; i++)
2372 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2374 lnet_net_lock(LNET_LOCK_EX);
2375 lnet_clear_zombies_nis_locked(net);
2376 lnet_net_unlock(LNET_LOCK_EX);
2380 lnet_shutdown_lndnet(struct lnet_net *net)
2384 lnet_net_lock(LNET_LOCK_EX);
2386 list_del_init(&net->net_list);
2388 while (!list_empty(&net->net_ni_list)) {
2389 ni = list_entry(net->net_ni_list.next,
2390 struct lnet_ni, ni_netlist);
2391 lnet_net_unlock(LNET_LOCK_EX);
2392 lnet_shutdown_lndni(ni);
2393 lnet_net_lock(LNET_LOCK_EX);
2396 lnet_net_unlock(LNET_LOCK_EX);
2398 /* Do peer table cleanup for this net */
2399 lnet_peer_tables_cleanup(net);
2405 lnet_shutdown_lndnets(void)
2407 struct lnet_net *net;
2409 struct lnet_msg *msg, *tmp;
2411 /* NB called holding the global mutex */
2413 /* All quiet on the API front */
2414 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2415 LASSERT(the_lnet.ln_refcount == 0);
2417 lnet_net_lock(LNET_LOCK_EX);
2418 the_lnet.ln_state = LNET_STATE_STOPPING;
2421 * move the nets to the zombie list to avoid them being
2422 * picked up for new work. LONET is also included in the
2423 * Nets that will be moved to the zombie list
2425 list_splice_init(&the_lnet.ln_nets, &the_lnet.ln_net_zombie);
2427 /* Drop the cached loopback Net. */
2428 if (the_lnet.ln_loni != NULL) {
2429 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2430 the_lnet.ln_loni = NULL;
2432 lnet_net_unlock(LNET_LOCK_EX);
2434 /* iterate through the net zombie list and delete each net */
2435 while (!list_empty(&the_lnet.ln_net_zombie)) {
2436 net = list_entry(the_lnet.ln_net_zombie.next,
2437 struct lnet_net, net_list);
2438 lnet_shutdown_lndnet(net);
2441 spin_lock(&the_lnet.ln_msg_resend_lock);
2442 list_splice(&the_lnet.ln_msg_resend, &resend);
2443 spin_unlock(&the_lnet.ln_msg_resend_lock);
2445 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2446 list_del_init(&msg->msg_list);
2447 msg->msg_no_resend = true;
2448 lnet_finalize(msg, -ECANCELED);
2451 lnet_net_lock(LNET_LOCK_EX);
2452 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2453 lnet_net_unlock(LNET_LOCK_EX);
2457 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2460 struct lnet_tx_queue *tq;
2462 struct lnet_net *net = ni->ni_net;
2464 mutex_lock(&the_lnet.ln_lnd_mutex);
2467 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2468 ni->ni_lnd_tunables_set = true;
2471 rc = (net->net_lnd->lnd_startup)(ni);
2473 mutex_unlock(&the_lnet.ln_lnd_mutex);
2476 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2477 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2482 ni->ni_state = LNET_NI_STATE_ACTIVE;
2485 /* We keep a reference on the loopback net through the loopback NI */
2486 if (net->net_lnd->lnd_type == LOLND) {
2488 LASSERT(the_lnet.ln_loni == NULL);
2489 the_lnet.ln_loni = ni;
2490 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2491 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2492 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2493 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2497 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2498 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2499 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2500 libcfs_lnd2str(net->net_lnd->lnd_type),
2501 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2503 /* shutdown the NI since if we get here then it must've already
2506 lnet_shutdown_lndni(ni);
2510 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2511 tq->tq_credits_min =
2512 tq->tq_credits_max =
2513 tq->tq_credits = lnet_ni_tq_credits(ni);
2516 atomic_set(&ni->ni_tx_credits,
2517 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2518 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2520 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2521 libcfs_nidstr(&ni->ni_nid),
2522 ni->ni_net->net_tunables.lct_peer_tx_credits,
2523 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2524 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2525 ni->ni_net->net_tunables.lct_peer_timeout);
2534 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2537 struct lnet_net *net_l = NULL;
2538 LIST_HEAD(local_ni_list);
2542 const struct lnet_lnd *lnd;
2544 net->net_tunables.lct_peer_timeout;
2546 net->net_tunables.lct_max_tx_credits;
2547 int peerrtrcredits =
2548 net->net_tunables.lct_peer_rtr_credits;
2551 * make sure that this net is unique. If it isn't then
2552 * we are adding interfaces to an already existing network, and
2553 * 'net' is just a convenient way to pass in the list.
2554 * if it is unique we need to find the LND and load it if
2557 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2558 lnd_type = LNET_NETTYP(net->net_id);
2560 mutex_lock(&the_lnet.ln_lnd_mutex);
2561 lnd = lnet_find_lnd_by_type(lnd_type);
2564 mutex_unlock(&the_lnet.ln_lnd_mutex);
2565 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2566 mutex_lock(&the_lnet.ln_lnd_mutex);
2568 lnd = lnet_find_lnd_by_type(lnd_type);
2570 mutex_unlock(&the_lnet.ln_lnd_mutex);
2571 CERROR("Can't load LND %s, module %s, rc=%d\n",
2572 libcfs_lnd2str(lnd_type),
2573 libcfs_lnd2modname(lnd_type), rc);
2574 #ifndef HAVE_MODULE_LOADING_SUPPORT
2575 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2576 "compiled with kernel module "
2577 "loading support.");
2586 mutex_unlock(&the_lnet.ln_lnd_mutex);
2592 * net_l: if the network being added is unique then net_l
2593 * will point to that network
2594 * if the network being added is not unique then
2595 * net_l points to the existing network.
2597 * When we enter the loop below, we'll pick NIs off he
2598 * network beign added and start them up, then add them to
2599 * a local ni list. Once we've successfully started all
2600 * the NIs then we join the local NI list (of started up
2601 * networks) with the net_l->net_ni_list, which should
2602 * point to the correct network to add the new ni list to
2604 * If any of the new NIs fail to start up, then we want to
2605 * iterate through the local ni list, which should include
2606 * any NIs which were successfully started up, and shut
2609 * After than we want to delete the network being added,
2610 * to avoid a memory leak.
2612 while (!list_empty(&net->net_ni_added)) {
2613 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2615 list_del_init(&ni->ni_netlist);
2617 /* make sure that the the NI we're about to start
2618 * up is actually unique. if it's not fail. */
2619 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2620 ni->ni_interface)) {
2625 /* adjust the pointer the parent network, just in case it
2626 * the net is a duplicate */
2629 rc = lnet_startup_lndni(ni, tun);
2635 list_add_tail(&ni->ni_netlist, &local_ni_list);
2640 lnet_net_lock(LNET_LOCK_EX);
2641 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2642 lnet_incr_dlc_seq();
2643 lnet_net_unlock(LNET_LOCK_EX);
2645 /* if the network is not unique then we don't want to keep
2646 * it around after we're done. Free it. Otherwise add that
2647 * net to the global the_lnet.ln_nets */
2648 if (net_l != net && net_l != NULL) {
2650 * TODO - note. currently the tunables can not be updated
2656 * restore tunables after it has been overwitten by the
2659 if (peer_timeout != -1)
2660 net->net_tunables.lct_peer_timeout = peer_timeout;
2661 if (maxtxcredits != -1)
2662 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2663 if (peerrtrcredits != -1)
2664 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2666 lnet_net_lock(LNET_LOCK_EX);
2667 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2668 lnet_net_unlock(LNET_LOCK_EX);
2675 * shutdown the new NIs that are being started up
2676 * free the NET being started
2678 while (!list_empty(&local_ni_list)) {
2679 ni = list_entry(local_ni_list.next, struct lnet_ni,
2682 lnet_shutdown_lndni(ni);
2692 lnet_startup_lndnets(struct list_head *netlist)
2694 struct lnet_net *net;
2699 * Change to running state before bringing up the LNDs. This
2700 * allows lnet_shutdown_lndnets() to assert that we've passed
2703 lnet_net_lock(LNET_LOCK_EX);
2704 the_lnet.ln_state = LNET_STATE_RUNNING;
2705 lnet_net_unlock(LNET_LOCK_EX);
2707 while (!list_empty(netlist)) {
2708 net = list_entry(netlist->next, struct lnet_net, net_list);
2709 list_del_init(&net->net_list);
2711 rc = lnet_startup_lndnet(net, NULL);
2721 lnet_shutdown_lndnets();
2726 static int lnet_genl_parse_list(struct sk_buff *msg,
2727 const struct ln_key_list *data[], u16 idx)
2729 const struct ln_key_list *list = data[idx];
2730 const struct ln_key_props *props;
2731 struct nlattr *node;
2737 if (!list->lkl_maxattr)
2740 props = list->lkl_list;
2744 node = nla_nest_start(msg, LN_SCALAR_ATTR_LIST);
2748 for (count = 1; count <= list->lkl_maxattr; count++) {
2749 struct nlattr *key = nla_nest_start(msg, count);
2752 nla_put_u16(msg, LN_SCALAR_ATTR_LIST_SIZE,
2755 nla_put_u16(msg, LN_SCALAR_ATTR_INDEX, count);
2756 if (props[count].lkp_values)
2757 nla_put_string(msg, LN_SCALAR_ATTR_VALUE,
2758 props[count].lkp_values);
2759 if (props[count].lkp_key_format)
2760 nla_put_u16(msg, LN_SCALAR_ATTR_KEY_FORMAT,
2761 props[count].lkp_key_format);
2762 nla_put_u16(msg, LN_SCALAR_ATTR_NLA_TYPE,
2763 props[count].lkp_data_type);
2764 if (props[count].lkp_data_type == NLA_NESTED) {
2767 rc = lnet_genl_parse_list(msg, data, ++idx);
2772 nla_nest_end(msg, key);
2775 nla_nest_end(msg, node);
2779 int lnet_genl_send_scalar_list(struct sk_buff *msg, u32 portid, u32 seq,
2780 const struct genl_family *family, int flags,
2781 u8 cmd, const struct ln_key_list *data[])
2789 hdr = genlmsg_put(msg, portid, seq, family, flags, cmd);
2791 GOTO(canceled, rc = -EMSGSIZE);
2793 rc = lnet_genl_parse_list(msg, data, 0);
2797 genlmsg_end(msg, hdr);
2800 genlmsg_cancel(msg, hdr);
2803 EXPORT_SYMBOL(lnet_genl_send_scalar_list);
2806 * Initialize LNet library.
2808 * Automatically called at module loading time. Caller has to call
2809 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2810 * latter returned 0. It must be called exactly once.
2812 * \retval 0 on success
2813 * \retval -ve on failures.
2815 int lnet_lib_init(void)
2819 lnet_assert_wire_constants();
2821 /* refer to global cfs_cpt_table for now */
2822 the_lnet.ln_cpt_table = cfs_cpt_tab;
2823 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_tab);
2825 LASSERT(the_lnet.ln_cpt_number > 0);
2826 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2827 /* we are under risk of consuming all lh_cookie */
2828 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2829 "please change setting of CPT-table and retry\n",
2830 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2834 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2835 the_lnet.ln_cpt_bits++;
2837 rc = lnet_create_locks();
2839 CERROR("Can't create LNet global locks: %d\n", rc);
2843 the_lnet.ln_refcount = 0;
2844 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2845 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2847 /* The hash table size is the number of bits it takes to express the set
2848 * ln_num_routes, minus 1 (better to under estimate than over so we
2849 * don't waste memory). */
2850 if (rnet_htable_size <= 0)
2851 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2852 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2853 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2854 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2855 order_base_2(rnet_htable_size) - 1);
2857 /* All LNDs apart from the LOLND are in separate modules. They
2858 * register themselves when their module loads, and unregister
2859 * themselves when their module is unloaded. */
2860 lnet_register_lnd(&the_lolnd);
2865 * Finalize LNet library.
2867 * \pre lnet_lib_init() called with success.
2868 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2870 * As this happens at module-unload, all lnds must already be unloaded,
2871 * so they must already be unregistered.
2873 void lnet_lib_exit(void)
2877 LASSERT(the_lnet.ln_refcount == 0);
2878 lnet_unregister_lnd(&the_lolnd);
2879 for (i = 0; i < NUM_LNDS; i++)
2880 LASSERT(!the_lnet.ln_lnds[i]);
2881 lnet_destroy_locks();
2885 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2887 * Users must call this function at least once before any other functions.
2888 * For each successful call there must be a corresponding call to
2889 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2892 * The PID used by LNet may be different from the one requested.
2895 * \param requested_pid PID requested by the caller.
2897 * \return >= 0 on success, and < 0 error code on failures.
2900 LNetNIInit(lnet_pid_t requested_pid)
2902 int im_a_router = 0;
2905 struct lnet_ping_buffer *pbuf;
2906 struct lnet_handle_md ping_mdh;
2907 LIST_HEAD(net_head);
2908 struct lnet_net *net;
2910 mutex_lock(&the_lnet.ln_api_mutex);
2912 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2914 if (the_lnet.ln_refcount > 0) {
2915 rc = the_lnet.ln_refcount++;
2916 mutex_unlock(&the_lnet.ln_api_mutex);
2920 rc = lnet_prepare(requested_pid);
2922 mutex_unlock(&the_lnet.ln_api_mutex);
2926 /* create a network for Loopback network */
2927 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2930 goto err_empty_list;
2933 /* Add in the loopback NI */
2934 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2936 goto err_empty_list;
2939 if (use_tcp_bonding)
2940 CWARN("use_tcp_bonding has been removed. Use Multi-Rail and Dynamic Discovery instead, see LU-13641\n");
2942 /* If LNet is being initialized via DLC it is possible
2943 * that the user requests not to load module parameters (ones which
2944 * are supported by DLC) on initialization. Therefore, make sure not
2945 * to load networks, routes and forwarding from module parameters
2946 * in this case. On cleanup in case of failure only clean up
2947 * routes if it has been loaded */
2948 if (!the_lnet.ln_nis_from_mod_params) {
2949 rc = lnet_parse_networks(&net_head, lnet_get_networks());
2951 goto err_empty_list;
2954 ni_count = lnet_startup_lndnets(&net_head);
2957 goto err_empty_list;
2960 if (!the_lnet.ln_nis_from_mod_params) {
2961 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2963 goto err_shutdown_lndnis;
2965 rc = lnet_rtrpools_alloc(im_a_router);
2967 goto err_destroy_routes;
2970 rc = lnet_acceptor_start();
2972 goto err_destroy_routes;
2974 the_lnet.ln_refcount = 1;
2975 /* Now I may use my own API functions... */
2977 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2979 goto err_acceptor_stop;
2981 lnet_ping_target_update(pbuf, ping_mdh);
2983 the_lnet.ln_mt_handler = lnet_mt_event_handler;
2985 rc = lnet_push_target_init();
2989 rc = lnet_peer_discovery_start();
2991 goto err_destroy_push_target;
2993 rc = lnet_monitor_thr_start();
2995 goto err_stop_discovery_thr;
2998 lnet_router_debugfs_init();
3000 mutex_unlock(&the_lnet.ln_api_mutex);
3002 complete_all(&the_lnet.ln_started);
3004 /* wait for all routers to start */
3005 lnet_wait_router_start();
3009 err_stop_discovery_thr:
3010 lnet_peer_discovery_stop();
3011 err_destroy_push_target:
3012 lnet_push_target_fini();
3014 lnet_ping_target_fini();
3016 the_lnet.ln_refcount = 0;
3017 lnet_acceptor_stop();
3019 if (!the_lnet.ln_nis_from_mod_params)
3020 lnet_destroy_routes();
3021 err_shutdown_lndnis:
3022 lnet_shutdown_lndnets();
3026 mutex_unlock(&the_lnet.ln_api_mutex);
3027 while (!list_empty(&net_head)) {
3028 struct lnet_net *net;
3030 net = list_entry(net_head.next, struct lnet_net, net_list);
3031 list_del_init(&net->net_list);
3036 EXPORT_SYMBOL(LNetNIInit);
3039 * Stop LNet interfaces, routing, and forwarding.
3041 * Users must call this function once for each successful call to LNetNIInit().
3042 * Once the LNetNIFini() operation has been started, the results of pending
3043 * API operations are undefined.
3045 * \return always 0 for current implementation.
3050 mutex_lock(&the_lnet.ln_api_mutex);
3052 LASSERT(the_lnet.ln_refcount > 0);
3054 if (the_lnet.ln_refcount != 1) {
3055 the_lnet.ln_refcount--;
3057 LASSERT(!the_lnet.ln_niinit_self);
3061 lnet_router_debugfs_fini();
3062 lnet_monitor_thr_stop();
3063 lnet_peer_discovery_stop();
3064 lnet_push_target_fini();
3065 lnet_ping_target_fini();
3067 /* Teardown fns that use my own API functions BEFORE here */
3068 the_lnet.ln_refcount = 0;
3070 lnet_acceptor_stop();
3071 lnet_destroy_routes();
3072 lnet_shutdown_lndnets();
3076 mutex_unlock(&the_lnet.ln_api_mutex);
3079 EXPORT_SYMBOL(LNetNIFini);
3082 * Grabs the ni data from the ni structure and fills the out
3085 * \param[in] ni network interface structure
3086 * \param[out] cfg_ni NI config information
3087 * \param[out] tun network and LND tunables
3090 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
3091 struct lnet_ioctl_config_lnd_tunables *tun,
3092 struct lnet_ioctl_element_stats *stats,
3095 size_t min_size = 0;
3098 if (!ni || !cfg_ni || !tun || !nid_is_nid4(&ni->ni_nid))
3101 if (ni->ni_interface != NULL) {
3102 strncpy(cfg_ni->lic_ni_intf,
3104 sizeof(cfg_ni->lic_ni_intf));
3107 cfg_ni->lic_nid = lnet_nid_to_nid4(&ni->ni_nid);
3108 cfg_ni->lic_status = lnet_ni_get_status_locked(ni);
3109 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
3111 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
3114 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
3115 LNET_STATS_TYPE_SEND);
3116 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
3117 LNET_STATS_TYPE_RECV);
3118 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
3119 LNET_STATS_TYPE_DROP);
3123 * tun->lt_tun will always be present, but in order to be
3124 * backwards compatible, we need to deal with the cases when
3125 * tun->lt_tun is smaller than what the kernel has, because it
3126 * comes from an older version of a userspace program, then we'll
3127 * need to copy as much information as we have available space.
3129 min_size = tun_size - sizeof(tun->lt_cmn);
3130 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
3132 /* copy over the cpts */
3133 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
3134 ni->ni_cpts == NULL) {
3135 for (i = 0; i < ni->ni_ncpts; i++)
3136 cfg_ni->lic_cpts[i] = i;
3139 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
3140 i < LNET_MAX_SHOW_NUM_CPT;
3142 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
3144 cfg_ni->lic_ncpts = ni->ni_ncpts;
3148 * NOTE: This is a legacy function left in the code to be backwards
3149 * compatible with older userspace programs. It should eventually be
3152 * Grabs the ni data from the ni structure and fills the out
3155 * \param[in] ni network interface structure
3156 * \param[out] config config information
3159 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
3160 struct lnet_ioctl_config_data *config)
3162 struct lnet_ioctl_net_config *net_config;
3163 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
3164 size_t min_size, tunable_size = 0;
3167 if (!ni || !config || !nid_is_nid4(&ni->ni_nid))
3170 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
3174 if (!ni->ni_interface)
3177 strncpy(net_config->ni_interface,
3179 sizeof(net_config->ni_interface));
3181 config->cfg_nid = lnet_nid_to_nid4(&ni->ni_nid);
3182 config->cfg_config_u.cfg_net.net_peer_timeout =
3183 ni->ni_net->net_tunables.lct_peer_timeout;
3184 config->cfg_config_u.cfg_net.net_max_tx_credits =
3185 ni->ni_net->net_tunables.lct_max_tx_credits;
3186 config->cfg_config_u.cfg_net.net_peer_tx_credits =
3187 ni->ni_net->net_tunables.lct_peer_tx_credits;
3188 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
3189 ni->ni_net->net_tunables.lct_peer_rtr_credits;
3191 net_config->ni_status = lnet_ni_get_status_locked(ni);
3194 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
3196 for (i = 0; i < num_cpts; i++)
3197 net_config->ni_cpts[i] = ni->ni_cpts[i];
3199 config->cfg_ncpts = num_cpts;
3203 * See if user land tools sent in a newer and larger version
3204 * of struct lnet_tunables than what the kernel uses.
3206 min_size = sizeof(*config) + sizeof(*net_config);
3208 if (config->cfg_hdr.ioc_len > min_size)
3209 tunable_size = config->cfg_hdr.ioc_len - min_size;
3211 /* Don't copy too much data to user space */
3212 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
3213 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
3215 if (lnd_cfg && min_size) {
3216 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
3217 config->cfg_config_u.cfg_net.net_interface_count = 1;
3219 /* Tell user land that kernel side has less data */
3220 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
3221 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
3222 config->cfg_hdr.ioc_len -= min_size;
3228 lnet_get_ni_idx_locked(int idx)
3231 struct lnet_net *net;
3233 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3234 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3243 int lnet_get_net_healthv_locked(struct lnet_net *net)
3246 int best_healthv = 0;
3247 int healthv, ni_fatal;
3249 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3250 healthv = atomic_read(&ni->ni_healthv);
3251 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
3252 if (!ni_fatal && healthv > best_healthv)
3253 best_healthv = healthv;
3256 return best_healthv;
3260 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
3263 struct lnet_net *net = mynet;
3266 * It is possible that the net has been cleaned out while there is
3267 * a message being sent. This function accessed the net without
3268 * checking if the list is empty
3272 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
3274 if (list_empty(&net->net_ni_list))
3276 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3282 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
3283 /* if you reached the end of the ni list and the net is
3284 * specified, then there are no more nis in that net */
3288 /* we reached the end of this net ni list. move to the
3290 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
3291 /* no more nets and no more NIs. */
3294 /* get the next net */
3295 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
3297 if (list_empty(&net->net_ni_list))
3299 /* get the ni on it */
3300 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3306 if (list_empty(&prev->ni_netlist))
3309 /* there are more nis left */
3310 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3316 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3321 int idx = config->cfg_count;
3323 cpt = lnet_net_lock_current();
3325 ni = lnet_get_ni_idx_locked(idx);
3330 lnet_fill_ni_info_legacy(ni, config);
3334 lnet_net_unlock(cpt);
3339 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3340 struct lnet_ioctl_config_lnd_tunables *tun,
3341 struct lnet_ioctl_element_stats *stats,
3348 if (!cfg_ni || !tun || !stats)
3351 cpt = lnet_net_lock_current();
3353 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3358 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3362 lnet_net_unlock(cpt);
3366 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3375 cpt = lnet_net_lock_current();
3377 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3380 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3384 lnet_net_unlock(cpt);
3389 static int lnet_add_net_common(struct lnet_net *net,
3390 struct lnet_ioctl_config_lnd_tunables *tun)
3392 struct lnet_handle_md ping_mdh;
3393 struct lnet_ping_buffer *pbuf;
3394 struct lnet_remotenet *rnet;
3400 lnet_net_lock(LNET_LOCK_EX);
3401 rnet = lnet_find_rnet_locked(net->net_id);
3402 lnet_net_unlock(LNET_LOCK_EX);
3404 * make sure that the net added doesn't invalidate the current
3405 * configuration LNet is keeping
3408 CERROR("Adding net %s will invalidate routing configuration\n",
3409 libcfs_net2str(net->net_id));
3415 * make sure you calculate the correct number of slots in the ping
3416 * buffer. Since the ping info is a flattened list of all the NIs,
3417 * we should allocate enough slots to accomodate the number of NIs
3418 * which will be added.
3420 * since ni hasn't been configured yet, use
3421 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3423 net_ni_count = lnet_get_net_ni_count_pre(net);
3425 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3426 net_ni_count + lnet_get_ni_count(),
3434 memcpy(&net->net_tunables,
3435 &tun->lt_cmn, sizeof(net->net_tunables));
3437 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3439 net_id = net->net_id;
3441 rc = lnet_startup_lndnet(net,
3442 (tun) ? &tun->lt_tun : NULL);
3446 lnet_net_lock(LNET_LOCK_EX);
3447 net = lnet_get_net_locked(net_id);
3450 /* apply the UDSPs */
3451 rc = lnet_udsp_apply_policies_on_net(net);
3453 CERROR("Failed to apply UDSPs on local net %s\n",
3454 libcfs_net2str(net->net_id));
3456 /* At this point we lost track of which NI was just added, so we
3457 * just re-apply the policies on all of the NIs on this net
3459 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3460 rc = lnet_udsp_apply_policies_on_ni(ni);
3462 CERROR("Failed to apply UDSPs on ni %s\n",
3463 libcfs_nidstr(&ni->ni_nid));
3465 lnet_net_unlock(LNET_LOCK_EX);
3468 * Start the acceptor thread if this is the first network
3469 * being added that requires the thread.
3471 if (net->net_lnd->lnd_accept) {
3472 rc = lnet_acceptor_start();
3474 /* shutdown the net that we just started */
3475 CERROR("Failed to start up acceptor thread\n");
3476 lnet_shutdown_lndnet(net);
3481 lnet_net_lock(LNET_LOCK_EX);
3482 lnet_peer_net_added(net);
3483 lnet_net_unlock(LNET_LOCK_EX);
3485 lnet_ping_target_update(pbuf, ping_mdh);
3490 lnet_ping_md_unlink(pbuf, &ping_mdh);
3491 lnet_ping_buffer_decref(pbuf);
3496 lnet_set_tune_defaults(struct lnet_ioctl_config_lnd_tunables *tun)
3499 if (!tun->lt_cmn.lct_peer_timeout)
3500 tun->lt_cmn.lct_peer_timeout = DEFAULT_PEER_TIMEOUT;
3501 if (!tun->lt_cmn.lct_peer_tx_credits)
3502 tun->lt_cmn.lct_peer_tx_credits = DEFAULT_PEER_CREDITS;
3503 if (!tun->lt_cmn.lct_max_tx_credits)
3504 tun->lt_cmn.lct_max_tx_credits = DEFAULT_CREDITS;
3508 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3509 struct lnet_ioctl_config_lnd_tunables *tun)
3511 struct lnet_net *net;
3514 LIST_HEAD(net_head);
3516 rc = lnet_parse_ip2nets(&nets, ip2nets);
<