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 0, which means health
84 * checking is turned off by default.
86 unsigned int lnet_health_sensitivity = 0;
87 static int sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
88 static struct kernel_param_ops param_ops_health_sensitivity = {
89 .set = sensitivity_set,
92 #define param_check_health_sensitivity(name, p) \
93 __param_check(name, p, int)
94 #ifdef HAVE_KERNEL_PARAM_OPS
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");
103 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
104 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
106 static struct kernel_param_ops param_ops_interfaces_max = {
108 .get = param_get_int,
111 #define param_check_interfaces_max(name, p) \
112 __param_check(name, p, int)
114 #ifdef HAVE_KERNEL_PARAM_OPS
115 module_param(lnet_interfaces_max, interfaces_max, 0644);
117 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
118 ¶m_ops_interfaces_max, 0644);
120 MODULE_PARM_DESC(lnet_interfaces_max,
121 "Maximum number of interfaces in a node.");
123 unsigned lnet_peer_discovery_disabled = 0;
124 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
126 static struct kernel_param_ops param_ops_discovery_disabled = {
127 .set = discovery_set,
128 .get = param_get_int,
131 #define param_check_discovery_disabled(name, p) \
132 __param_check(name, p, int)
133 #ifdef HAVE_KERNEL_PARAM_OPS
134 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
136 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
137 ¶m_ops_discovery_disabled, 0644);
139 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
140 "Set to 1 to disable peer discovery on this node.");
142 unsigned lnet_transaction_timeout = 5;
143 module_param(lnet_transaction_timeout, uint, 0444);
144 MODULE_PARM_DESC(lnet_transaction_timeout,
145 "Time in seconds to wait for a REPLY or an ACK");
147 unsigned lnet_retry_count = 0;
148 module_param(lnet_retry_count, uint, 0444);
149 MODULE_PARM_DESC(lnet_retry_count,
150 "Maximum number of times to retry transmitting a message");
152 unsigned lnet_lnd_timeout = LNET_LND_DEFAULT_TIMEOUT;
155 * This sequence number keeps track of how many times DLC was used to
156 * update the local NIs. It is incremented when a NI is added or
157 * removed and checked when sending a message to determine if there is
158 * a need to re-run the selection algorithm. See lnet_select_pathway()
159 * for more details on its usage.
161 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
163 static int lnet_ping(struct lnet_process_id id, signed long timeout,
164 struct lnet_process_id __user *ids, int n_ids);
166 static int lnet_discover(struct lnet_process_id id, __u32 force,
167 struct lnet_process_id __user *ids, int n_ids);
170 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
173 unsigned *sensitivity = (unsigned *)kp->arg;
176 rc = kstrtoul(val, 0, &value);
178 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
183 * The purpose of locking the api_mutex here is to ensure that
184 * the correct value ends up stored properly.
186 mutex_lock(&the_lnet.ln_api_mutex);
188 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
189 mutex_unlock(&the_lnet.ln_api_mutex);
193 if (value == *sensitivity) {
194 mutex_unlock(&the_lnet.ln_api_mutex);
198 *sensitivity = value;
200 mutex_unlock(&the_lnet.ln_api_mutex);
206 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
209 unsigned *discovery = (unsigned *)kp->arg;
211 struct lnet_ping_buffer *pbuf;
213 rc = kstrtoul(val, 0, &value);
215 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
219 value = (value) ? 1 : 0;
222 * The purpose of locking the api_mutex here is to ensure that
223 * the correct value ends up stored properly.
225 mutex_lock(&the_lnet.ln_api_mutex);
227 if (value == *discovery) {
228 mutex_unlock(&the_lnet.ln_api_mutex);
234 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
235 mutex_unlock(&the_lnet.ln_api_mutex);
239 /* tell peers that discovery setting has changed */
240 lnet_net_lock(LNET_LOCK_EX);
241 pbuf = the_lnet.ln_ping_target;
243 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
245 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
246 lnet_net_unlock(LNET_LOCK_EX);
248 lnet_push_update_to_peers(1);
250 mutex_unlock(&the_lnet.ln_api_mutex);
256 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
260 rc = kstrtoint(val, 0, &value);
262 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
266 if (value < LNET_INTERFACES_MIN) {
267 CWARN("max interfaces provided are too small, setting to %d\n",
268 LNET_INTERFACES_MAX_DEFAULT);
269 value = LNET_INTERFACES_MAX_DEFAULT;
272 *(int *)kp->arg = value;
278 lnet_get_routes(void)
284 lnet_get_networks(void)
289 if (*networks != 0 && *ip2nets != 0) {
290 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
291 "'ip2nets' but not both at once\n");
296 rc = lnet_parse_ip2nets(&nets, ip2nets);
297 return (rc == 0) ? nets : NULL;
307 lnet_init_locks(void)
309 spin_lock_init(&the_lnet.ln_eq_wait_lock);
310 spin_lock_init(&the_lnet.ln_msg_resend_lock);
311 init_waitqueue_head(&the_lnet.ln_eq_waitq);
312 init_waitqueue_head(&the_lnet.ln_mt_waitq);
313 mutex_init(&the_lnet.ln_lnd_mutex);
317 lnet_fini_locks(void)
321 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
322 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
326 lnet_descriptor_setup(void)
328 /* create specific kmem_cache for MEs and small MDs (i.e., originally
329 * allocated in <size-xxx> kmem_cache).
331 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
333 if (!lnet_mes_cachep)
336 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
337 LNET_SMALL_MD_SIZE, 0, 0,
339 if (!lnet_small_mds_cachep)
346 lnet_descriptor_cleanup(void)
349 if (lnet_small_mds_cachep) {
350 kmem_cache_destroy(lnet_small_mds_cachep);
351 lnet_small_mds_cachep = NULL;
354 if (lnet_mes_cachep) {
355 kmem_cache_destroy(lnet_mes_cachep);
356 lnet_mes_cachep = NULL;
361 lnet_create_remote_nets_table(void)
364 struct list_head *hash;
366 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
367 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
368 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
370 CERROR("Failed to create remote nets hash table\n");
374 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
375 INIT_LIST_HEAD(&hash[i]);
376 the_lnet.ln_remote_nets_hash = hash;
381 lnet_destroy_remote_nets_table(void)
385 if (the_lnet.ln_remote_nets_hash == NULL)
388 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
389 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
391 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
392 LNET_REMOTE_NETS_HASH_SIZE *
393 sizeof(the_lnet.ln_remote_nets_hash[0]));
394 the_lnet.ln_remote_nets_hash = NULL;
398 lnet_destroy_locks(void)
400 if (the_lnet.ln_res_lock != NULL) {
401 cfs_percpt_lock_free(the_lnet.ln_res_lock);
402 the_lnet.ln_res_lock = NULL;
405 if (the_lnet.ln_net_lock != NULL) {
406 cfs_percpt_lock_free(the_lnet.ln_net_lock);
407 the_lnet.ln_net_lock = NULL;
414 lnet_create_locks(void)
418 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
419 if (the_lnet.ln_res_lock == NULL)
422 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
423 if (the_lnet.ln_net_lock == NULL)
429 lnet_destroy_locks();
433 static void lnet_assert_wire_constants(void)
435 /* Wire protocol assertions generated by 'wirecheck'
436 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
437 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
438 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
441 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
442 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
443 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
444 CLASSERT(LNET_MSG_ACK == 0);
445 CLASSERT(LNET_MSG_PUT == 1);
446 CLASSERT(LNET_MSG_GET == 2);
447 CLASSERT(LNET_MSG_REPLY == 3);
448 CLASSERT(LNET_MSG_HELLO == 4);
450 /* Checks for struct lnet_handle_wire */
451 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
452 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
453 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
454 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
455 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
457 /* Checks for struct struct lnet_magicversion */
458 CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
459 CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
460 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
461 CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
462 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
463 CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
464 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
466 /* Checks for struct struct lnet_hdr */
467 CLASSERT((int)sizeof(struct lnet_hdr) == 72);
468 CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
469 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
470 CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
471 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
472 CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
473 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
474 CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
475 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
476 CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
477 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
478 CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
479 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
480 CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
481 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
484 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
485 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
486 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
487 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
488 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
489 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
492 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
493 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
494 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
495 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
496 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
497 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
498 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
499 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
500 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
501 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
504 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
505 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
506 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
507 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
508 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
509 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
510 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
511 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
512 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
513 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
516 CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
517 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
520 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
521 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
522 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
523 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
525 /* Checks for struct lnet_ni_status and related constants */
526 CLASSERT(LNET_NI_STATUS_INVALID == 0x00000000);
527 CLASSERT(LNET_NI_STATUS_UP == 0x15aac0de);
528 CLASSERT(LNET_NI_STATUS_DOWN == 0xdeadface);
530 /* Checks for struct lnet_ni_status */
531 CLASSERT((int)sizeof(struct lnet_ni_status) == 16);
532 CLASSERT((int)offsetof(struct lnet_ni_status, ns_nid) == 0);
533 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) == 8);
534 CLASSERT((int)offsetof(struct lnet_ni_status, ns_status) == 8);
535 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_status) == 4);
536 CLASSERT((int)offsetof(struct lnet_ni_status, ns_unused) == 12);
537 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) == 4);
539 /* Checks for struct lnet_ping_info and related constants */
540 CLASSERT(LNET_PROTO_PING_MAGIC == 0x70696E67);
541 CLASSERT(LNET_PING_FEAT_INVAL == 0);
542 CLASSERT(LNET_PING_FEAT_BASE == 1);
543 CLASSERT(LNET_PING_FEAT_NI_STATUS == 2);
544 CLASSERT(LNET_PING_FEAT_RTE_DISABLED == 4);
545 CLASSERT(LNET_PING_FEAT_MULTI_RAIL == 8);
546 CLASSERT(LNET_PING_FEAT_DISCOVERY == 16);
547 CLASSERT(LNET_PING_FEAT_BITS == 31);
549 /* Checks for struct lnet_ping_info */
550 CLASSERT((int)sizeof(struct lnet_ping_info) == 16);
551 CLASSERT((int)offsetof(struct lnet_ping_info, pi_magic) == 0);
552 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) == 4);
553 CLASSERT((int)offsetof(struct lnet_ping_info, pi_features) == 4);
554 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_features) == 4);
555 CLASSERT((int)offsetof(struct lnet_ping_info, pi_pid) == 8);
556 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) == 4);
557 CLASSERT((int)offsetof(struct lnet_ping_info, pi_nnis) == 12);
558 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) == 4);
559 CLASSERT((int)offsetof(struct lnet_ping_info, pi_ni) == 16);
560 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) == 0);
563 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
565 struct lnet_lnd *lnd;
566 struct list_head *tmp;
568 /* holding lnd mutex */
569 list_for_each(tmp, &the_lnet.ln_lnds) {
570 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
572 if (lnd->lnd_type == type)
579 lnet_get_lnd_timeout(void)
581 return lnet_lnd_timeout;
583 EXPORT_SYMBOL(lnet_get_lnd_timeout);
586 lnet_register_lnd(struct lnet_lnd *lnd)
588 mutex_lock(&the_lnet.ln_lnd_mutex);
590 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
591 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
593 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
594 lnd->lnd_refcount = 0;
596 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
598 mutex_unlock(&the_lnet.ln_lnd_mutex);
600 EXPORT_SYMBOL(lnet_register_lnd);
603 lnet_unregister_lnd(struct lnet_lnd *lnd)
605 mutex_lock(&the_lnet.ln_lnd_mutex);
607 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
608 LASSERT(lnd->lnd_refcount == 0);
610 list_del(&lnd->lnd_list);
611 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
613 mutex_unlock(&the_lnet.ln_lnd_mutex);
615 EXPORT_SYMBOL(lnet_unregister_lnd);
618 lnet_counters_get(struct lnet_counters *counters)
620 struct lnet_counters *ctr;
623 memset(counters, 0, sizeof(*counters));
625 lnet_net_lock(LNET_LOCK_EX);
627 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
628 counters->msgs_max += ctr->msgs_max;
629 counters->msgs_alloc += ctr->msgs_alloc;
630 counters->errors += ctr->errors;
631 counters->send_count += ctr->send_count;
632 counters->recv_count += ctr->recv_count;
633 counters->route_count += ctr->route_count;
634 counters->drop_count += ctr->drop_count;
635 counters->send_length += ctr->send_length;
636 counters->recv_length += ctr->recv_length;
637 counters->route_length += ctr->route_length;
638 counters->drop_length += ctr->drop_length;
641 lnet_net_unlock(LNET_LOCK_EX);
643 EXPORT_SYMBOL(lnet_counters_get);
646 lnet_counters_reset(void)
648 struct lnet_counters *counters;
651 lnet_net_lock(LNET_LOCK_EX);
653 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
654 memset(counters, 0, sizeof(struct lnet_counters));
656 lnet_net_unlock(LNET_LOCK_EX);
660 lnet_res_type2str(int type)
665 case LNET_COOKIE_TYPE_MD:
667 case LNET_COOKIE_TYPE_ME:
669 case LNET_COOKIE_TYPE_EQ:
675 lnet_res_container_cleanup(struct lnet_res_container *rec)
679 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
682 while (!list_empty(&rec->rec_active)) {
683 struct list_head *e = rec->rec_active.next;
686 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
687 lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
689 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
690 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
692 } else { /* NB: Active MEs should be attached on portals */
699 /* Found alive MD/ME/EQ, user really should unlink/free
700 * all of them before finalize LNet, but if someone didn't,
701 * we have to recycle garbage for him */
702 CERROR("%d active elements on exit of %s container\n",
703 count, lnet_res_type2str(rec->rec_type));
706 if (rec->rec_lh_hash != NULL) {
707 LIBCFS_FREE(rec->rec_lh_hash,
708 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
709 rec->rec_lh_hash = NULL;
712 rec->rec_type = 0; /* mark it as finalized */
716 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
721 LASSERT(rec->rec_type == 0);
723 rec->rec_type = type;
724 INIT_LIST_HEAD(&rec->rec_active);
726 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
728 /* Arbitrary choice of hash table size */
729 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
730 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
731 if (rec->rec_lh_hash == NULL) {
736 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
737 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
742 CERROR("Failed to setup %s resource container\n",
743 lnet_res_type2str(type));
744 lnet_res_container_cleanup(rec);
749 lnet_res_containers_destroy(struct lnet_res_container **recs)
751 struct lnet_res_container *rec;
754 cfs_percpt_for_each(rec, i, recs)
755 lnet_res_container_cleanup(rec);
757 cfs_percpt_free(recs);
760 static struct lnet_res_container **
761 lnet_res_containers_create(int type)
763 struct lnet_res_container **recs;
764 struct lnet_res_container *rec;
768 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
770 CERROR("Failed to allocate %s resource containers\n",
771 lnet_res_type2str(type));
775 cfs_percpt_for_each(rec, i, recs) {
776 rc = lnet_res_container_setup(rec, i, type);
778 lnet_res_containers_destroy(recs);
786 struct lnet_libhandle *
787 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
789 /* ALWAYS called with lnet_res_lock held */
790 struct list_head *head;
791 struct lnet_libhandle *lh;
794 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
797 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
798 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
800 list_for_each_entry(lh, head, lh_hash_chain) {
801 if (lh->lh_cookie == cookie)
809 lnet_res_lh_initialize(struct lnet_res_container *rec,
810 struct lnet_libhandle *lh)
812 /* ALWAYS called with lnet_res_lock held */
813 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
816 lh->lh_cookie = rec->rec_lh_cookie;
817 rec->rec_lh_cookie += 1 << ibits;
819 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
821 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
824 static int lnet_unprepare(void);
827 lnet_prepare(lnet_pid_t requested_pid)
829 /* Prepare to bring up the network */
830 struct lnet_res_container **recs;
833 if (requested_pid == LNET_PID_ANY) {
834 /* Don't instantiate LNET just for me */
838 LASSERT(the_lnet.ln_refcount == 0);
840 the_lnet.ln_routing = 0;
842 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
843 the_lnet.ln_pid = requested_pid;
845 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
846 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
847 INIT_LIST_HEAD(&the_lnet.ln_nets);
848 INIT_LIST_HEAD(&the_lnet.ln_routers);
849 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
850 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
851 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
852 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
853 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
854 INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
855 INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
856 init_waitqueue_head(&the_lnet.ln_dc_waitq);
858 rc = lnet_descriptor_setup();
862 rc = lnet_create_remote_nets_table();
867 * NB the interface cookie in wire handles guards against delayed
868 * replies and ACKs appearing valid after reboot.
870 the_lnet.ln_interface_cookie = ktime_get_real_ns();
872 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
873 sizeof(struct lnet_counters));
874 if (the_lnet.ln_counters == NULL) {
875 CERROR("Failed to allocate counters for LNet\n");
880 rc = lnet_peer_tables_create();
884 rc = lnet_msg_containers_create();
888 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
889 LNET_COOKIE_TYPE_EQ);
893 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
899 the_lnet.ln_me_containers = recs;
901 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
907 the_lnet.ln_md_containers = recs;
909 rc = lnet_portals_create();
911 CERROR("Failed to create portals for LNet: %d\n", rc);
923 lnet_unprepare (void)
925 /* NB no LNET_LOCK since this is the last reference. All LND instances
926 * have shut down already, so it is safe to unlink and free all
927 * descriptors, even those that appear committed to a network op (eg MD
928 * with non-zero pending count) */
930 lnet_fail_nid(LNET_NID_ANY, 0);
932 LASSERT(the_lnet.ln_refcount == 0);
933 LASSERT(list_empty(&the_lnet.ln_test_peers));
934 LASSERT(list_empty(&the_lnet.ln_nets));
936 lnet_portals_destroy();
938 if (the_lnet.ln_md_containers != NULL) {
939 lnet_res_containers_destroy(the_lnet.ln_md_containers);
940 the_lnet.ln_md_containers = NULL;
943 if (the_lnet.ln_me_containers != NULL) {
944 lnet_res_containers_destroy(the_lnet.ln_me_containers);
945 the_lnet.ln_me_containers = NULL;
948 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
950 lnet_msg_containers_destroy();
952 lnet_rtrpools_free(0);
954 if (the_lnet.ln_counters != NULL) {
955 cfs_percpt_free(the_lnet.ln_counters);
956 the_lnet.ln_counters = NULL;
958 lnet_destroy_remote_nets_table();
959 lnet_descriptor_cleanup();
965 lnet_net2ni_locked(__u32 net_id, int cpt)
968 struct lnet_net *net;
970 LASSERT(cpt != LNET_LOCK_EX);
972 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
973 if (net->net_id == net_id) {
974 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
984 lnet_net2ni_addref(__u32 net)
989 ni = lnet_net2ni_locked(net, 0);
991 lnet_ni_addref_locked(ni, 0);
996 EXPORT_SYMBOL(lnet_net2ni_addref);
999 lnet_get_net_locked(__u32 net_id)
1001 struct lnet_net *net;
1003 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1004 if (net->net_id == net_id)
1012 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1017 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1022 val = hash_long(key, LNET_CPT_BITS);
1023 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1027 return (unsigned int)(key + val + (val >> 1)) % number;
1031 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1033 struct lnet_net *net;
1035 /* must called with hold of lnet_net_lock */
1036 if (LNET_CPT_NUMBER == 1)
1037 return 0; /* the only one */
1040 * If NI is provided then use the CPT identified in the NI cpt
1041 * list if one exists. If one doesn't exist, then that NI is
1042 * associated with all CPTs and it follows that the net it belongs
1043 * to is implicitly associated with all CPTs, so just hash the nid
1047 if (ni->ni_cpts != NULL)
1048 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1051 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1054 /* no NI provided so look at the net */
1055 net = lnet_get_net_locked(LNET_NIDNET(nid));
1057 if (net != NULL && net->net_cpts != NULL) {
1058 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1061 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1065 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1070 if (LNET_CPT_NUMBER == 1)
1071 return 0; /* the only one */
1073 cpt = lnet_net_lock_current();
1075 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1077 lnet_net_unlock(cpt);
1081 EXPORT_SYMBOL(lnet_cpt_of_nid);
1084 lnet_islocalnet(__u32 net_id)
1086 struct lnet_net *net;
1090 cpt = lnet_net_lock_current();
1092 net = lnet_get_net_locked(net_id);
1094 local = net != NULL;
1096 lnet_net_unlock(cpt);
1102 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
1104 if (ni->ni_state & LNET_NI_STATE_ACTIVE)
1111 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1113 struct lnet_net *net;
1116 LASSERT(cpt != LNET_LOCK_EX);
1118 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1119 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1120 if (ni->ni_nid == nid)
1129 lnet_nid2ni_addref(lnet_nid_t nid)
1134 ni = lnet_nid2ni_locked(nid, 0);
1136 lnet_ni_addref_locked(ni, 0);
1141 EXPORT_SYMBOL(lnet_nid2ni_addref);
1144 lnet_islocalnid(lnet_nid_t nid)
1149 cpt = lnet_net_lock_current();
1150 ni = lnet_nid2ni_locked(nid, cpt);
1151 lnet_net_unlock(cpt);
1157 lnet_count_acceptor_nets(void)
1159 /* Return the # of NIs that need the acceptor. */
1161 struct lnet_net *net;
1164 cpt = lnet_net_lock_current();
1165 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1166 /* all socklnd type networks should have the acceptor
1168 if (net->net_lnd->lnd_accept != NULL)
1172 lnet_net_unlock(cpt);
1177 struct lnet_ping_buffer *
1178 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1180 struct lnet_ping_buffer *pbuf;
1182 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1184 pbuf->pb_nnis = nnis;
1185 atomic_set(&pbuf->pb_refcnt, 1);
1192 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1194 LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1195 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1198 static struct lnet_ping_buffer *
1199 lnet_ping_target_create(int nnis)
1201 struct lnet_ping_buffer *pbuf;
1203 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1205 CERROR("Can't allocate ping source [%d]\n", nnis);
1209 pbuf->pb_info.pi_nnis = nnis;
1210 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1211 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1212 pbuf->pb_info.pi_features =
1213 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1219 lnet_get_net_ni_count_locked(struct lnet_net *net)
1224 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1231 lnet_get_net_ni_count_pre(struct lnet_net *net)
1236 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1243 lnet_get_ni_count(void)
1246 struct lnet_net *net;
1251 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1252 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1262 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1266 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1268 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1270 /* Loopback is guaranteed to be present */
1271 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1273 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1279 lnet_ping_target_destroy(void)
1281 struct lnet_net *net;
1284 lnet_net_lock(LNET_LOCK_EX);
1286 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1287 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1289 ni->ni_status = NULL;
1294 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1295 the_lnet.ln_ping_target = NULL;
1297 lnet_net_unlock(LNET_LOCK_EX);
1301 lnet_ping_target_event_handler(struct lnet_event *event)
1303 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1305 if (event->unlinked)
1306 lnet_ping_buffer_decref(pbuf);
1310 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1311 struct lnet_handle_md *ping_mdh,
1312 int ni_count, bool set_eq)
1314 struct lnet_process_id id = {
1315 .nid = LNET_NID_ANY,
1318 struct lnet_handle_me me_handle;
1319 struct lnet_md md = { NULL };
1323 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1324 &the_lnet.ln_ping_target_eq);
1326 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1331 *ppbuf = lnet_ping_target_create(ni_count);
1332 if (*ppbuf == NULL) {
1337 /* Ping target ME/MD */
1338 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1339 LNET_PROTO_PING_MATCHBITS, 0,
1340 LNET_UNLINK, LNET_INS_AFTER,
1343 CERROR("Can't create ping target ME: %d\n", rc);
1344 goto fail_decref_ping_buffer;
1347 /* initialize md content */
1348 md.start = &(*ppbuf)->pb_info;
1349 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1350 md.threshold = LNET_MD_THRESH_INF;
1352 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1353 LNET_MD_MANAGE_REMOTE;
1354 md.eq_handle = the_lnet.ln_ping_target_eq;
1355 md.user_ptr = *ppbuf;
1357 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, ping_mdh);
1359 CERROR("Can't attach ping target MD: %d\n", rc);
1360 goto fail_unlink_ping_me;
1362 lnet_ping_buffer_addref(*ppbuf);
1366 fail_unlink_ping_me:
1367 rc2 = LNetMEUnlink(me_handle);
1369 fail_decref_ping_buffer:
1370 LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1371 lnet_ping_buffer_decref(*ppbuf);
1375 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1382 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1383 struct lnet_handle_md *ping_mdh)
1385 sigset_t blocked = cfs_block_allsigs();
1387 LNetMDUnlink(*ping_mdh);
1388 LNetInvalidateMDHandle(ping_mdh);
1390 /* NB the MD could be busy; this just starts the unlink */
1391 while (lnet_ping_buffer_numref(pbuf) > 1) {
1392 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1393 set_current_state(TASK_UNINTERRUPTIBLE);
1394 schedule_timeout(cfs_time_seconds(1));
1397 cfs_restore_sigs(blocked);
1401 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1404 struct lnet_net *net;
1405 struct lnet_ni_status *ns;
1410 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1411 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1412 LASSERT(i < pbuf->pb_nnis);
1414 ns = &pbuf->pb_info.pi_ni[i];
1416 ns->ns_nid = ni->ni_nid;
1419 ns->ns_status = (ni->ni_status != NULL) ?
1420 ni->ni_status->ns_status :
1429 * We (ab)use the ns_status of the loopback interface to
1430 * transmit the sequence number. The first interface listed
1431 * must be the loopback interface.
1433 rc = lnet_ping_info_validate(&pbuf->pb_info);
1435 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1438 LNET_PING_BUFFER_SEQNO(pbuf) =
1439 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1443 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1444 struct lnet_handle_md ping_mdh)
1446 struct lnet_ping_buffer *old_pbuf = NULL;
1447 struct lnet_handle_md old_ping_md;
1449 /* switch the NIs to point to the new ping info created */
1450 lnet_net_lock(LNET_LOCK_EX);
1452 if (!the_lnet.ln_routing)
1453 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1454 if (!lnet_peer_discovery_disabled)
1455 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1457 /* Ensure only known feature bits have been set. */
1458 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1459 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1461 lnet_ping_target_install_locked(pbuf);
1463 if (the_lnet.ln_ping_target) {
1464 old_pbuf = the_lnet.ln_ping_target;
1465 old_ping_md = the_lnet.ln_ping_target_md;
1467 the_lnet.ln_ping_target_md = ping_mdh;
1468 the_lnet.ln_ping_target = pbuf;
1470 lnet_net_unlock(LNET_LOCK_EX);
1473 /* unlink and free the old ping info */
1474 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1475 lnet_ping_buffer_decref(old_pbuf);
1478 lnet_push_update_to_peers(0);
1482 lnet_ping_target_fini(void)
1486 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1487 &the_lnet.ln_ping_target_md);
1489 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1492 lnet_ping_target_destroy();
1495 /* Resize the push target. */
1496 int lnet_push_target_resize(void)
1498 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1499 struct lnet_md md = { NULL };
1500 struct lnet_handle_me meh;
1501 struct lnet_handle_md mdh;
1502 struct lnet_handle_md old_mdh;
1503 struct lnet_ping_buffer *pbuf;
1504 struct lnet_ping_buffer *old_pbuf;
1505 int nnis = the_lnet.ln_push_target_nnis;
1513 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1519 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1520 LNET_PROTO_PING_MATCHBITS, 0,
1521 LNET_UNLINK, LNET_INS_AFTER,
1524 CERROR("Can't create push target ME: %d\n", rc);
1525 goto fail_decref_pbuf;
1528 /* initialize md content */
1529 md.start = &pbuf->pb_info;
1530 md.length = LNET_PING_INFO_SIZE(nnis);
1531 md.threshold = LNET_MD_THRESH_INF;
1533 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1534 LNET_MD_MANAGE_REMOTE;
1536 md.eq_handle = the_lnet.ln_push_target_eq;
1538 rc = LNetMDAttach(meh, md, LNET_RETAIN, &mdh);
1540 CERROR("Can't attach push MD: %d\n", rc);
1541 goto fail_unlink_meh;
1543 lnet_ping_buffer_addref(pbuf);
1545 lnet_net_lock(LNET_LOCK_EX);
1546 old_pbuf = the_lnet.ln_push_target;
1547 old_mdh = the_lnet.ln_push_target_md;
1548 the_lnet.ln_push_target = pbuf;
1549 the_lnet.ln_push_target_md = mdh;
1550 lnet_net_unlock(LNET_LOCK_EX);
1553 LNetMDUnlink(old_mdh);
1554 lnet_ping_buffer_decref(old_pbuf);
1557 if (nnis < the_lnet.ln_push_target_nnis)
1560 CDEBUG(D_NET, "nnis %d success\n", nnis);
1567 lnet_ping_buffer_decref(pbuf);
1569 CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1573 static void lnet_push_target_event_handler(struct lnet_event *ev)
1575 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1577 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1578 lnet_swap_pinginfo(pbuf);
1580 lnet_peer_push_event(ev);
1582 lnet_ping_buffer_decref(pbuf);
1585 /* Initialize the push target. */
1586 static int lnet_push_target_init(void)
1590 if (the_lnet.ln_push_target)
1593 rc = LNetEQAlloc(0, lnet_push_target_event_handler,
1594 &the_lnet.ln_push_target_eq);
1596 CERROR("Can't allocated push target EQ: %d\n", rc);
1600 /* Start at the required minimum, we'll enlarge if required. */
1601 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1603 rc = lnet_push_target_resize();
1606 LNetEQFree(the_lnet.ln_push_target_eq);
1607 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1613 /* Clean up the push target. */
1614 static void lnet_push_target_fini(void)
1616 if (!the_lnet.ln_push_target)
1619 /* Unlink and invalidate to prevent new references. */
1620 LNetMDUnlink(the_lnet.ln_push_target_md);
1621 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1623 /* Wait for the unlink to complete. */
1624 while (lnet_ping_buffer_numref(the_lnet.ln_push_target) > 1) {
1625 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1626 set_current_state(TASK_UNINTERRUPTIBLE);
1627 schedule_timeout(cfs_time_seconds(1));
1630 lnet_ping_buffer_decref(the_lnet.ln_push_target);
1631 the_lnet.ln_push_target = NULL;
1632 the_lnet.ln_push_target_nnis = 0;
1634 LNetEQFree(the_lnet.ln_push_target_eq);
1635 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1639 lnet_ni_tq_credits(struct lnet_ni *ni)
1643 LASSERT(ni->ni_ncpts >= 1);
1645 if (ni->ni_ncpts == 1)
1646 return ni->ni_net->net_tunables.lct_max_tx_credits;
1648 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1649 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1650 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1656 lnet_ni_unlink_locked(struct lnet_ni *ni)
1658 if (!list_empty(&ni->ni_cptlist)) {
1659 list_del_init(&ni->ni_cptlist);
1660 lnet_ni_decref_locked(ni, 0);
1663 /* move it to zombie list and nobody can find it anymore */
1664 LASSERT(!list_empty(&ni->ni_netlist));
1665 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1666 lnet_ni_decref_locked(ni, 0);
1670 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1675 struct list_head *zombie_list = &net->net_ni_zombie;
1678 * Now wait for the NIs I just nuked to show up on the zombie
1679 * list and shut them down in guaranteed thread context
1682 while (!list_empty(zombie_list)) {
1686 ni = list_entry(zombie_list->next,
1687 struct lnet_ni, ni_netlist);
1688 list_del_init(&ni->ni_netlist);
1689 /* the ni should be in deleting state. If it's not it's
1691 LASSERT(ni->ni_state & LNET_NI_STATE_DELETING);
1692 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1695 /* still busy, add it back to zombie list */
1696 list_add(&ni->ni_netlist, zombie_list);
1700 if (!list_empty(&ni->ni_netlist)) {
1701 lnet_net_unlock(LNET_LOCK_EX);
1703 if ((i & (-i)) == i) {
1705 "Waiting for zombie LNI %s\n",
1706 libcfs_nid2str(ni->ni_nid));
1708 set_current_state(TASK_UNINTERRUPTIBLE);
1709 schedule_timeout(cfs_time_seconds(1));
1710 lnet_net_lock(LNET_LOCK_EX);
1714 lnet_net_unlock(LNET_LOCK_EX);
1716 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1718 LASSERT(!in_interrupt());
1719 (net->net_lnd->lnd_shutdown)(ni);
1722 CDEBUG(D_LNI, "Removed LNI %s\n",
1723 libcfs_nid2str(ni->ni_nid));
1727 lnet_net_lock(LNET_LOCK_EX);
1731 /* shutdown down the NI and release refcount */
1733 lnet_shutdown_lndni(struct lnet_ni *ni)
1736 struct lnet_net *net = ni->ni_net;
1738 lnet_net_lock(LNET_LOCK_EX);
1740 ni->ni_state |= LNET_NI_STATE_DELETING;
1741 ni->ni_state &= ~LNET_NI_STATE_ACTIVE;
1743 lnet_ni_unlink_locked(ni);
1744 lnet_incr_dlc_seq();
1745 lnet_net_unlock(LNET_LOCK_EX);
1747 /* clear messages for this NI on the lazy portal */
1748 for (i = 0; i < the_lnet.ln_nportals; i++)
1749 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1751 lnet_net_lock(LNET_LOCK_EX);
1752 lnet_clear_zombies_nis_locked(net);
1753 lnet_net_unlock(LNET_LOCK_EX);
1757 lnet_shutdown_lndnet(struct lnet_net *net)
1761 lnet_net_lock(LNET_LOCK_EX);
1763 net->net_state = LNET_NET_STATE_DELETING;
1765 list_del_init(&net->net_list);
1767 while (!list_empty(&net->net_ni_list)) {
1768 ni = list_entry(net->net_ni_list.next,
1769 struct lnet_ni, ni_netlist);
1770 lnet_net_unlock(LNET_LOCK_EX);
1771 lnet_shutdown_lndni(ni);
1772 lnet_net_lock(LNET_LOCK_EX);
1775 lnet_net_unlock(LNET_LOCK_EX);
1777 /* Do peer table cleanup for this net */
1778 lnet_peer_tables_cleanup(net);
1780 lnet_net_lock(LNET_LOCK_EX);
1782 * decrement ref count on lnd only when the entire network goes
1785 net->net_lnd->lnd_refcount--;
1787 lnet_net_unlock(LNET_LOCK_EX);
1793 lnet_shutdown_lndnets(void)
1795 struct lnet_net *net;
1796 struct list_head resend;
1797 struct lnet_msg *msg, *tmp;
1799 INIT_LIST_HEAD(&resend);
1801 /* NB called holding the global mutex */
1803 /* All quiet on the API front */
1804 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
1805 LASSERT(the_lnet.ln_refcount == 0);
1807 lnet_net_lock(LNET_LOCK_EX);
1808 the_lnet.ln_state = LNET_STATE_STOPPING;
1810 while (!list_empty(&the_lnet.ln_nets)) {
1812 * move the nets to the zombie list to avoid them being
1813 * picked up for new work. LONET is also included in the
1814 * Nets that will be moved to the zombie list
1816 net = list_entry(the_lnet.ln_nets.next,
1817 struct lnet_net, net_list);
1818 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1821 /* Drop the cached loopback Net. */
1822 if (the_lnet.ln_loni != NULL) {
1823 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1824 the_lnet.ln_loni = NULL;
1826 lnet_net_unlock(LNET_LOCK_EX);
1828 /* iterate through the net zombie list and delete each net */
1829 while (!list_empty(&the_lnet.ln_net_zombie)) {
1830 net = list_entry(the_lnet.ln_net_zombie.next,
1831 struct lnet_net, net_list);
1832 lnet_shutdown_lndnet(net);
1835 spin_lock(&the_lnet.ln_msg_resend_lock);
1836 list_splice(&the_lnet.ln_msg_resend, &resend);
1837 spin_unlock(&the_lnet.ln_msg_resend_lock);
1839 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
1840 list_del_init(&msg->msg_list);
1841 msg->msg_no_resend = true;
1842 lnet_finalize(msg, -ECANCELED);
1845 lnet_net_lock(LNET_LOCK_EX);
1846 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
1847 lnet_net_unlock(LNET_LOCK_EX);
1851 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1854 struct lnet_tx_queue *tq;
1856 struct lnet_net *net = ni->ni_net;
1858 mutex_lock(&the_lnet.ln_lnd_mutex);
1861 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1862 ni->ni_lnd_tunables_set = true;
1865 rc = (net->net_lnd->lnd_startup)(ni);
1867 mutex_unlock(&the_lnet.ln_lnd_mutex);
1870 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1871 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1872 lnet_net_lock(LNET_LOCK_EX);
1873 net->net_lnd->lnd_refcount--;
1874 lnet_net_unlock(LNET_LOCK_EX);
1879 ni->ni_state |= LNET_NI_STATE_ACTIVE;
1880 ni->ni_state &= ~LNET_NI_STATE_INIT;
1883 /* We keep a reference on the loopback net through the loopback NI */
1884 if (net->net_lnd->lnd_type == LOLND) {
1886 LASSERT(the_lnet.ln_loni == NULL);
1887 the_lnet.ln_loni = ni;
1888 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
1889 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
1890 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
1891 ni->ni_net->net_tunables.lct_peer_timeout = 0;
1895 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
1896 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
1897 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1898 libcfs_lnd2str(net->net_lnd->lnd_type),
1899 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
1901 /* shutdown the NI since if we get here then it must've already
1904 lnet_shutdown_lndni(ni);
1908 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1909 tq->tq_credits_min =
1910 tq->tq_credits_max =
1911 tq->tq_credits = lnet_ni_tq_credits(ni);
1914 atomic_set(&ni->ni_tx_credits,
1915 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
1916 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
1918 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1919 libcfs_nid2str(ni->ni_nid),
1920 ni->ni_net->net_tunables.lct_peer_tx_credits,
1921 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1922 ni->ni_net->net_tunables.lct_peer_rtr_credits,
1923 ni->ni_net->net_tunables.lct_peer_timeout);
1932 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
1935 struct lnet_net *net_l = NULL;
1936 struct list_head local_ni_list;
1940 struct lnet_lnd *lnd;
1942 net->net_tunables.lct_peer_timeout;
1944 net->net_tunables.lct_max_tx_credits;
1945 int peerrtrcredits =
1946 net->net_tunables.lct_peer_rtr_credits;
1948 INIT_LIST_HEAD(&local_ni_list);
1951 * make sure that this net is unique. If it isn't then
1952 * we are adding interfaces to an already existing network, and
1953 * 'net' is just a convenient way to pass in the list.
1954 * if it is unique we need to find the LND and load it if
1957 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
1958 lnd_type = LNET_NETTYP(net->net_id);
1960 mutex_lock(&the_lnet.ln_lnd_mutex);
1961 lnd = lnet_find_lnd_by_type(lnd_type);
1964 mutex_unlock(&the_lnet.ln_lnd_mutex);
1965 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1966 mutex_lock(&the_lnet.ln_lnd_mutex);
1968 lnd = lnet_find_lnd_by_type(lnd_type);
1970 mutex_unlock(&the_lnet.ln_lnd_mutex);
1971 CERROR("Can't load LND %s, module %s, rc=%d\n",
1972 libcfs_lnd2str(lnd_type),
1973 libcfs_lnd2modname(lnd_type), rc);
1974 #ifndef HAVE_MODULE_LOADING_SUPPORT
1975 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1976 "compiled with kernel module "
1977 "loading support.");
1984 lnet_net_lock(LNET_LOCK_EX);
1985 lnd->lnd_refcount++;
1986 lnet_net_unlock(LNET_LOCK_EX);
1990 mutex_unlock(&the_lnet.ln_lnd_mutex);
1996 * net_l: if the network being added is unique then net_l
1997 * will point to that network
1998 * if the network being added is not unique then
1999 * net_l points to the existing network.
2001 * When we enter the loop below, we'll pick NIs off he
2002 * network beign added and start them up, then add them to
2003 * a local ni list. Once we've successfully started all
2004 * the NIs then we join the local NI list (of started up
2005 * networks) with the net_l->net_ni_list, which should
2006 * point to the correct network to add the new ni list to
2008 * If any of the new NIs fail to start up, then we want to
2009 * iterate through the local ni list, which should include
2010 * any NIs which were successfully started up, and shut
2013 * After than we want to delete the network being added,
2014 * to avoid a memory leak.
2018 * When a network uses TCP bonding then all its interfaces
2019 * must be specified when the network is first defined: the
2020 * TCP bonding code doesn't allow for interfaces to be added
2023 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2024 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2029 while (!list_empty(&net->net_ni_added)) {
2030 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2032 list_del_init(&ni->ni_netlist);
2034 /* make sure that the the NI we're about to start
2035 * up is actually unique. if it's not fail. */
2036 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2037 ni->ni_interfaces[0])) {
2042 /* adjust the pointer the parent network, just in case it
2043 * the net is a duplicate */
2046 rc = lnet_startup_lndni(ni, tun);
2048 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
2049 ni->ni_net->net_lnd->lnd_query != NULL);
2055 list_add_tail(&ni->ni_netlist, &local_ni_list);
2060 lnet_net_lock(LNET_LOCK_EX);
2061 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2062 lnet_incr_dlc_seq();
2063 lnet_net_unlock(LNET_LOCK_EX);
2065 /* if the network is not unique then we don't want to keep
2066 * it around after we're done. Free it. Otherwise add that
2067 * net to the global the_lnet.ln_nets */
2068 if (net_l != net && net_l != NULL) {
2070 * TODO - note. currently the tunables can not be updated
2075 net->net_state = LNET_NET_STATE_ACTIVE;
2077 * restore tunables after it has been overwitten by the
2080 if (peer_timeout != -1)
2081 net->net_tunables.lct_peer_timeout = peer_timeout;
2082 if (maxtxcredits != -1)
2083 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2084 if (peerrtrcredits != -1)
2085 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2087 lnet_net_lock(LNET_LOCK_EX);
2088 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2089 lnet_net_unlock(LNET_LOCK_EX);
2096 * shutdown the new NIs that are being started up
2097 * free the NET being started
2099 while (!list_empty(&local_ni_list)) {
2100 ni = list_entry(local_ni_list.next, struct lnet_ni,
2103 lnet_shutdown_lndni(ni);
2113 lnet_startup_lndnets(struct list_head *netlist)
2115 struct lnet_net *net;
2120 * Change to running state before bringing up the LNDs. This
2121 * allows lnet_shutdown_lndnets() to assert that we've passed
2124 lnet_net_lock(LNET_LOCK_EX);
2125 the_lnet.ln_state = LNET_STATE_RUNNING;
2126 lnet_net_unlock(LNET_LOCK_EX);
2128 while (!list_empty(netlist)) {
2129 net = list_entry(netlist->next, struct lnet_net, net_list);
2130 list_del_init(&net->net_list);
2132 rc = lnet_startup_lndnet(net, NULL);
2142 lnet_shutdown_lndnets();
2148 * Initialize LNet library.
2150 * Automatically called at module loading time. Caller has to call
2151 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2152 * latter returned 0. It must be called exactly once.
2154 * \retval 0 on success
2155 * \retval -ve on failures.
2157 int lnet_lib_init(void)
2161 lnet_assert_wire_constants();
2163 /* refer to global cfs_cpt_table for now */
2164 the_lnet.ln_cpt_table = cfs_cpt_table;
2165 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
2167 LASSERT(the_lnet.ln_cpt_number > 0);
2168 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2169 /* we are under risk of consuming all lh_cookie */
2170 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2171 "please change setting of CPT-table and retry\n",
2172 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2176 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2177 the_lnet.ln_cpt_bits++;
2179 rc = lnet_create_locks();
2181 CERROR("Can't create LNet global locks: %d\n", rc);
2185 the_lnet.ln_refcount = 0;
2186 LNetInvalidateEQHandle(&the_lnet.ln_rc_eqh);
2187 INIT_LIST_HEAD(&the_lnet.ln_lnds);
2188 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2189 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
2190 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2191 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
2193 /* The hash table size is the number of bits it takes to express the set
2194 * ln_num_routes, minus 1 (better to under estimate than over so we
2195 * don't waste memory). */
2196 if (rnet_htable_size <= 0)
2197 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2198 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2199 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2200 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2201 order_base_2(rnet_htable_size) - 1);
2203 /* All LNDs apart from the LOLND are in separate modules. They
2204 * register themselves when their module loads, and unregister
2205 * themselves when their module is unloaded. */
2206 lnet_register_lnd(&the_lolnd);
2211 * Finalize LNet library.
2213 * \pre lnet_lib_init() called with success.
2214 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2216 void lnet_lib_exit(void)
2218 LASSERT(the_lnet.ln_refcount == 0);
2220 while (!list_empty(&the_lnet.ln_lnds))
2221 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
2222 struct lnet_lnd, lnd_list));
2223 lnet_destroy_locks();
2227 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2229 * Users must call this function at least once before any other functions.
2230 * For each successful call there must be a corresponding call to
2231 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2234 * The PID used by LNet may be different from the one requested.
2237 * \param requested_pid PID requested by the caller.
2239 * \return >= 0 on success, and < 0 error code on failures.
2242 LNetNIInit(lnet_pid_t requested_pid)
2244 int im_a_router = 0;
2247 struct lnet_ping_buffer *pbuf;
2248 struct lnet_handle_md ping_mdh;
2249 struct list_head net_head;
2250 struct lnet_net *net;
2252 INIT_LIST_HEAD(&net_head);
2254 mutex_lock(&the_lnet.ln_api_mutex);
2256 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2258 if (the_lnet.ln_refcount > 0) {
2259 rc = the_lnet.ln_refcount++;
2260 mutex_unlock(&the_lnet.ln_api_mutex);
2264 rc = lnet_prepare(requested_pid);
2266 mutex_unlock(&the_lnet.ln_api_mutex);
2270 /* create a network for Loopback network */
2271 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2274 goto err_empty_list;
2277 /* Add in the loopback NI */
2278 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2280 goto err_empty_list;
2283 /* If LNet is being initialized via DLC it is possible
2284 * that the user requests not to load module parameters (ones which
2285 * are supported by DLC) on initialization. Therefore, make sure not
2286 * to load networks, routes and forwarding from module parameters
2287 * in this case. On cleanup in case of failure only clean up
2288 * routes if it has been loaded */
2289 if (!the_lnet.ln_nis_from_mod_params) {
2290 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2293 goto err_empty_list;
2296 ni_count = lnet_startup_lndnets(&net_head);
2299 goto err_empty_list;
2302 if (!the_lnet.ln_nis_from_mod_params) {
2303 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2305 goto err_shutdown_lndnis;
2307 rc = lnet_check_routes();
2309 goto err_destroy_routes;
2311 rc = lnet_rtrpools_alloc(im_a_router);
2313 goto err_destroy_routes;
2316 rc = lnet_acceptor_start();
2318 goto err_destroy_routes;
2320 the_lnet.ln_refcount = 1;
2321 /* Now I may use my own API functions... */
2323 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2325 goto err_acceptor_stop;
2327 lnet_ping_target_update(pbuf, ping_mdh);
2329 rc = lnet_monitor_thr_start();
2333 rc = lnet_push_target_init();
2335 goto err_stop_monitor_thr;
2337 rc = lnet_peer_discovery_start();
2339 goto err_destroy_push_target;
2342 lnet_router_debugfs_init();
2344 mutex_unlock(&the_lnet.ln_api_mutex);
2348 err_destroy_push_target:
2349 lnet_push_target_fini();
2350 err_stop_monitor_thr:
2351 lnet_monitor_thr_stop();
2353 lnet_ping_target_fini();
2355 the_lnet.ln_refcount = 0;
2356 lnet_acceptor_stop();
2358 if (!the_lnet.ln_nis_from_mod_params)
2359 lnet_destroy_routes();
2360 err_shutdown_lndnis:
2361 lnet_shutdown_lndnets();
2365 mutex_unlock(&the_lnet.ln_api_mutex);
2366 while (!list_empty(&net_head)) {
2367 struct lnet_net *net;
2369 net = list_entry(net_head.next, struct lnet_net, net_list);
2370 list_del_init(&net->net_list);
2375 EXPORT_SYMBOL(LNetNIInit);
2378 * Stop LNet interfaces, routing, and forwarding.
2380 * Users must call this function once for each successful call to LNetNIInit().
2381 * Once the LNetNIFini() operation has been started, the results of pending
2382 * API operations are undefined.
2384 * \return always 0 for current implementation.
2389 mutex_lock(&the_lnet.ln_api_mutex);
2391 LASSERT(the_lnet.ln_refcount > 0);
2393 if (the_lnet.ln_refcount != 1) {
2394 the_lnet.ln_refcount--;
2396 LASSERT(!the_lnet.ln_niinit_self);
2400 lnet_router_debugfs_init();
2401 lnet_peer_discovery_stop();
2402 lnet_push_target_fini();
2403 lnet_monitor_thr_stop();
2404 lnet_ping_target_fini();
2406 /* Teardown fns that use my own API functions BEFORE here */
2407 the_lnet.ln_refcount = 0;
2409 lnet_acceptor_stop();
2410 lnet_destroy_routes();
2411 lnet_shutdown_lndnets();
2415 mutex_unlock(&the_lnet.ln_api_mutex);
2418 EXPORT_SYMBOL(LNetNIFini);
2421 * Grabs the ni data from the ni structure and fills the out
2424 * \param[in] ni network interface structure
2425 * \param[out] cfg_ni NI config information
2426 * \param[out] tun network and LND tunables
2429 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2430 struct lnet_ioctl_config_lnd_tunables *tun,
2431 struct lnet_ioctl_element_stats *stats,
2434 size_t min_size = 0;
2437 if (!ni || !cfg_ni || !tun)
2440 if (ni->ni_interfaces[0] != NULL) {
2441 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2442 if (ni->ni_interfaces[i] != NULL) {
2443 strncpy(cfg_ni->lic_ni_intf[i],
2444 ni->ni_interfaces[i],
2445 sizeof(cfg_ni->lic_ni_intf[i]));
2450 cfg_ni->lic_nid = ni->ni_nid;
2451 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2452 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2454 cfg_ni->lic_status = ni->ni_status->ns_status;
2455 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2456 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2458 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2461 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2462 LNET_STATS_TYPE_SEND);
2463 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2464 LNET_STATS_TYPE_RECV);
2465 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2466 LNET_STATS_TYPE_DROP);
2470 * tun->lt_tun will always be present, but in order to be
2471 * backwards compatible, we need to deal with the cases when
2472 * tun->lt_tun is smaller than what the kernel has, because it
2473 * comes from an older version of a userspace program, then we'll
2474 * need to copy as much information as we have available space.
2476 min_size = tun_size - sizeof(tun->lt_cmn);
2477 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2479 /* copy over the cpts */
2480 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2481 ni->ni_cpts == NULL) {
2482 for (i = 0; i < ni->ni_ncpts; i++)
2483 cfg_ni->lic_cpts[i] = i;
2486 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2487 i < LNET_MAX_SHOW_NUM_CPT;
2489 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2491 cfg_ni->lic_ncpts = ni->ni_ncpts;
2495 * NOTE: This is a legacy function left in the code to be backwards
2496 * compatible with older userspace programs. It should eventually be
2499 * Grabs the ni data from the ni structure and fills the out
2502 * \param[in] ni network interface structure
2503 * \param[out] config config information
2506 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2507 struct lnet_ioctl_config_data *config)
2509 struct lnet_ioctl_net_config *net_config;
2510 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2511 size_t min_size, tunable_size = 0;
2517 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2521 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2522 ARRAY_SIZE(net_config->ni_interfaces));
2524 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2525 if (!ni->ni_interfaces[i])
2528 strncpy(net_config->ni_interfaces[i],
2529 ni->ni_interfaces[i],
2530 sizeof(net_config->ni_interfaces[i]));
2533 config->cfg_nid = ni->ni_nid;
2534 config->cfg_config_u.cfg_net.net_peer_timeout =
2535 ni->ni_net->net_tunables.lct_peer_timeout;
2536 config->cfg_config_u.cfg_net.net_max_tx_credits =
2537 ni->ni_net->net_tunables.lct_max_tx_credits;
2538 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2539 ni->ni_net->net_tunables.lct_peer_tx_credits;
2540 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2541 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2543 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2544 net_config->ni_status = LNET_NI_STATUS_UP;
2546 net_config->ni_status = ni->ni_status->ns_status;
2549 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2551 for (i = 0; i < num_cpts; i++)
2552 net_config->ni_cpts[i] = ni->ni_cpts[i];
2554 config->cfg_ncpts = num_cpts;
2558 * See if user land tools sent in a newer and larger version
2559 * of struct lnet_tunables than what the kernel uses.
2561 min_size = sizeof(*config) + sizeof(*net_config);
2563 if (config->cfg_hdr.ioc_len > min_size)
2564 tunable_size = config->cfg_hdr.ioc_len - min_size;
2566 /* Don't copy too much data to user space */
2567 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2568 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2570 if (lnd_cfg && min_size) {
2571 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2572 config->cfg_config_u.cfg_net.net_interface_count = 1;
2574 /* Tell user land that kernel side has less data */
2575 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2576 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2577 config->cfg_hdr.ioc_len -= min_size;
2583 lnet_get_ni_idx_locked(int idx)
2586 struct lnet_net *net;
2588 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2589 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2599 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2602 struct lnet_net *net = mynet;
2605 * It is possible that the net has been cleaned out while there is
2606 * a message being sent. This function accessed the net without
2607 * checking if the list is empty
2611 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2613 if (list_empty(&net->net_ni_list))
2615 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2621 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2622 /* if you reached the end of the ni list and the net is
2623 * specified, then there are no more nis in that net */
2627 /* we reached the end of this net ni list. move to the
2629 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2630 /* no more nets and no more NIs. */
2633 /* get the next net */
2634 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2636 if (list_empty(&net->net_ni_list))
2638 /* get the ni on it */
2639 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2645 if (list_empty(&prev->ni_netlist))
2648 /* there are more nis left */
2649 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2655 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2660 int idx = config->cfg_count;
2662 cpt = lnet_net_lock_current();
2664 ni = lnet_get_ni_idx_locked(idx);
2669 lnet_fill_ni_info_legacy(ni, config);
2673 lnet_net_unlock(cpt);
2678 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2679 struct lnet_ioctl_config_lnd_tunables *tun,
2680 struct lnet_ioctl_element_stats *stats,
2687 if (!cfg_ni || !tun || !stats)
2690 cpt = lnet_net_lock_current();
2692 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2697 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
2701 lnet_net_unlock(cpt);
2705 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
2714 cpt = lnet_net_lock_current();
2716 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
2719 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
2723 lnet_net_unlock(cpt);
2728 static int lnet_add_net_common(struct lnet_net *net,
2729 struct lnet_ioctl_config_lnd_tunables *tun)
2732 struct lnet_ping_buffer *pbuf;
2733 struct lnet_handle_md ping_mdh;
2735 struct lnet_remotenet *rnet;
2737 int num_acceptor_nets;
2739 lnet_net_lock(LNET_LOCK_EX);
2740 rnet = lnet_find_rnet_locked(net->net_id);
2741 lnet_net_unlock(LNET_LOCK_EX);
2743 * make sure that the net added doesn't invalidate the current
2744 * configuration LNet is keeping
2747 CERROR("Adding net %s will invalidate routing configuration\n",
2748 libcfs_net2str(net->net_id));
2754 * make sure you calculate the correct number of slots in the ping
2755 * buffer. Since the ping info is a flattened list of all the NIs,
2756 * we should allocate enough slots to accomodate the number of NIs
2757 * which will be added.
2759 * since ni hasn't been configured yet, use
2760 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
2762 net_ni_count = lnet_get_net_ni_count_pre(net);
2764 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2765 net_ni_count + lnet_get_ni_count(),
2773 memcpy(&net->net_tunables,
2774 &tun->lt_cmn, sizeof(net->net_tunables));
2776 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
2779 * before starting this network get a count of the current TCP
2780 * networks which require the acceptor thread running. If that
2781 * count is == 0 before we start up this network, then we'd want to
2782 * start up the acceptor thread after starting up this network
2784 num_acceptor_nets = lnet_count_acceptor_nets();
2786 net_id = net->net_id;
2788 rc = lnet_startup_lndnet(net,
2789 (tun) ? &tun->lt_tun : NULL);
2793 lnet_net_lock(LNET_LOCK_EX);
2794 net = lnet_get_net_locked(net_id);
2795 lnet_net_unlock(LNET_LOCK_EX);
2800 * Start the acceptor thread if this is the first network
2801 * being added that requires the thread.
2803 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
2804 rc = lnet_acceptor_start();
2806 /* shutdown the net that we just started */
2807 CERROR("Failed to start up acceptor thread\n");
2808 lnet_shutdown_lndnet(net);
2813 lnet_net_lock(LNET_LOCK_EX);
2814 lnet_peer_net_added(net);
2815 lnet_net_unlock(LNET_LOCK_EX);
2817 lnet_ping_target_update(pbuf, ping_mdh);
2822 lnet_ping_md_unlink(pbuf, &ping_mdh);
2823 lnet_ping_buffer_decref(pbuf);
2827 static int lnet_handle_legacy_ip2nets(char *ip2nets,
2828 struct lnet_ioctl_config_lnd_tunables *tun)
2830 struct lnet_net *net;
2833 struct list_head net_head;
2835 INIT_LIST_HEAD(&net_head);
2837 rc = lnet_parse_ip2nets(&nets, ip2nets);
2841 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2845 mutex_lock(&the_lnet.ln_api_mutex);
2846 while (!list_empty(&net_head)) {
2847 net = list_entry(net_head.next, struct lnet_net, net_list);
2848 list_del_init(&net->net_list);
2849 rc = lnet_add_net_common(net, tun);
2855 mutex_unlock(&the_lnet.ln_api_mutex);
2857 while (!list_empty(&net_head)) {
2858 net = list_entry(net_head.next, struct lnet_net, net_list);
2859 list_del_init(&net->net_list);
2865 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
2867 struct lnet_net *net;
2869 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2871 __u32 net_id, lnd_type;
2873 /* get the tunables if they are available */
2874 if (conf->lic_cfg_hdr.ioc_len >=
2875 sizeof(*conf) + sizeof(*tun))
2876 tun = (struct lnet_ioctl_config_lnd_tunables *)
2879 /* handle legacy ip2nets from DLC */
2880 if (conf->lic_legacy_ip2nets[0] != '\0')
2881 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
2884 net_id = LNET_NIDNET(conf->lic_nid);
2885 lnd_type = LNET_NETTYP(net_id);
2887 if (!libcfs_isknown_lnd(lnd_type)) {
2888 CERROR("No valid net and lnd information provided\n");
2892 net = lnet_net_alloc(net_id, NULL);
2896 for (i = 0; i < conf->lic_ncpts; i++) {
2897 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
2901 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
2902 conf->lic_ni_intf[0]);
2906 mutex_lock(&the_lnet.ln_api_mutex);
2908 rc = lnet_add_net_common(net, tun);
2910 mutex_unlock(&the_lnet.ln_api_mutex);
2915 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
2917 struct lnet_net *net;
2919 __u32 net_id = LNET_NIDNET(conf->lic_nid);
2920 struct lnet_ping_buffer *pbuf;
2921 struct lnet_handle_md ping_mdh;
2926 /* don't allow userspace to shutdown the LOLND */
2927 if (LNET_NETTYP(net_id) == LOLND)
2930 mutex_lock(&the_lnet.ln_api_mutex);
2934 net = lnet_get_net_locked(net_id);
2936 CERROR("net %s not found\n",
2937 libcfs_net2str(net_id));
2942 addr = LNET_NIDADDR(conf->lic_nid);
2944 /* remove the entire net */
2945 net_count = lnet_get_net_ni_count_locked(net);
2949 /* create and link a new ping info, before removing the old one */
2950 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2951 lnet_get_ni_count() - net_count,
2954 goto unlock_api_mutex;
2956 lnet_shutdown_lndnet(net);
2958 if (lnet_count_acceptor_nets() == 0)
2959 lnet_acceptor_stop();
2961 lnet_ping_target_update(pbuf, ping_mdh);
2963 goto unlock_api_mutex;
2966 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
2968 CERROR("nid %s not found\n",
2969 libcfs_nid2str(conf->lic_nid));
2974 net_count = lnet_get_net_ni_count_locked(net);
2978 /* create and link a new ping info, before removing the old one */
2979 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2980 lnet_get_ni_count() - 1, false);
2982 goto unlock_api_mutex;
2984 lnet_shutdown_lndni(ni);
2986 if (lnet_count_acceptor_nets() == 0)
2987 lnet_acceptor_stop();
2989 lnet_ping_target_update(pbuf, ping_mdh);
2991 /* check if the net is empty and remove it if it is */
2993 lnet_shutdown_lndnet(net);
2995 goto unlock_api_mutex;
3000 mutex_unlock(&the_lnet.ln_api_mutex);
3006 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3007 * They are only expected to be called for unique networks.
3008 * That can be as a result of older DLC library
3009 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3012 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3014 struct lnet_net *net;
3015 struct list_head net_head;
3017 struct lnet_ioctl_config_lnd_tunables tun;
3018 char *nets = conf->cfg_config_u.cfg_net.net_intf;
3020 INIT_LIST_HEAD(&net_head);
3022 /* Create a net/ni structures for the network string */
3023 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3025 return rc == 0 ? -EINVAL : rc;
3027 mutex_lock(&the_lnet.ln_api_mutex);
3030 rc = -EINVAL; /* only add one network per call */
3031 goto out_unlock_clean;
3034 net = list_entry(net_head.next, struct lnet_net, net_list);
3035 list_del_init(&net->net_list);
3037 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3039 memset(&tun, 0, sizeof(tun));
3041 tun.lt_cmn.lct_peer_timeout =
3042 conf->cfg_config_u.cfg_net.net_peer_timeout;
3043 tun.lt_cmn.lct_peer_tx_credits =
3044 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3045 tun.lt_cmn.lct_peer_rtr_credits =
3046 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3047 tun.lt_cmn.lct_max_tx_credits =
3048 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3050 rc = lnet_add_net_common(net, &tun);
3053 mutex_unlock(&the_lnet.ln_api_mutex);
3054 while (!list_empty(&net_head)) {
3055 /* net_head list is empty in success case */
3056 net = list_entry(net_head.next, struct lnet_net, net_list);
3057 list_del_init(&net->net_list);
3064 lnet_dyn_del_net(__u32 net_id)
3066 struct lnet_net *net;
3067 struct lnet_ping_buffer *pbuf;
3068 struct lnet_handle_md ping_mdh;
3072 /* don't allow userspace to shutdown the LOLND */
3073 if (LNET_NETTYP(net_id) == LOLND)
3076 mutex_lock(&the_lnet.ln_api_mutex);
3080 net = lnet_get_net_locked(net_id);
3087 net_ni_count = lnet_get_net_ni_count_locked(net);
3091 /* create and link a new ping info, before removing the old one */
3092 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3093 lnet_get_ni_count() - net_ni_count, false);
3097 lnet_shutdown_lndnet(net);
3099 if (lnet_count_acceptor_nets() == 0)
3100 lnet_acceptor_stop();
3102 lnet_ping_target_update(pbuf, ping_mdh);
3105 mutex_unlock(&the_lnet.ln_api_mutex);
3110 void lnet_incr_dlc_seq(void)
3112 atomic_inc(&lnet_dlc_seq_no);
3115 __u32 lnet_get_dlc_seq_locked(void)
3117 return atomic_read(&lnet_dlc_seq_no);
3121 * LNet ioctl handler.
3125 LNetCtl(unsigned int cmd, void *arg)
3127 struct libcfs_ioctl_data *data = arg;
3128 struct lnet_ioctl_config_data *config;
3129 struct lnet_process_id id = {0};
3133 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3134 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3137 case IOC_LIBCFS_GET_NI:
3138 rc = LNetGetId(data->ioc_count, &id);
3139 data->ioc_nid = id.nid;
3142 case IOC_LIBCFS_FAIL_NID:
3143 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3145 case IOC_LIBCFS_ADD_ROUTE:
3148 if (config->cfg_hdr.ioc_len < sizeof(*config))
3151 mutex_lock(&the_lnet.ln_api_mutex);
3152 rc = lnet_add_route(config->cfg_net,
3153 config->cfg_config_u.cfg_route.rtr_hop,
3155 config->cfg_config_u.cfg_route.
3158 rc = lnet_check_routes();
3160 lnet_del_route(config->cfg_net,
3163 mutex_unlock(&the_lnet.ln_api_mutex);
3166 case IOC_LIBCFS_DEL_ROUTE:
3169 if (config->cfg_hdr.ioc_len < sizeof(*config))
3172 mutex_lock(&the_lnet.ln_api_mutex);
3173 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3174 mutex_unlock(&the_lnet.ln_api_mutex);
3177 case IOC_LIBCFS_GET_ROUTE:
3180 if (config->cfg_hdr.ioc_len < sizeof(*config))
3183 mutex_lock(&the_lnet.ln_api_mutex);
3184 rc = lnet_get_route(config->cfg_count,
3186 &config->cfg_config_u.cfg_route.rtr_hop,
3188 &config->cfg_config_u.cfg_route.rtr_flags,
3189 &config->cfg_config_u.cfg_route.
3191 mutex_unlock(&the_lnet.ln_api_mutex);
3194 case IOC_LIBCFS_GET_LOCAL_NI: {
3195 struct lnet_ioctl_config_ni *cfg_ni;
3196 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3197 struct lnet_ioctl_element_stats *stats;
3202 /* get the tunables if they are available */
3203 if (cfg_ni->lic_cfg_hdr.ioc_len <
3204 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3207 stats = (struct lnet_ioctl_element_stats *)
3209 tun = (struct lnet_ioctl_config_lnd_tunables *)
3210 (cfg_ni->lic_bulk + sizeof(*stats));
3212 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3215 mutex_lock(&the_lnet.ln_api_mutex);
3216 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3217 mutex_unlock(&the_lnet.ln_api_mutex);
3221 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3222 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3224 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3227 mutex_lock(&the_lnet.ln_api_mutex);
3228 rc = lnet_get_ni_stats(msg_stats);
3229 mutex_unlock(&the_lnet.ln_api_mutex);
3234 case IOC_LIBCFS_GET_NET: {
3235 size_t total = sizeof(*config) +
3236 sizeof(struct lnet_ioctl_net_config);
3239 if (config->cfg_hdr.ioc_len < total)
3242 mutex_lock(&the_lnet.ln_api_mutex);
3243 rc = lnet_get_net_config(config);
3244 mutex_unlock(&the_lnet.ln_api_mutex);
3248 case IOC_LIBCFS_GET_LNET_STATS:
3250 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3252 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3255 mutex_lock(&the_lnet.ln_api_mutex);
3256 lnet_counters_get(&lnet_stats->st_cntrs);
3257 mutex_unlock(&the_lnet.ln_api_mutex);
3261 case IOC_LIBCFS_CONFIG_RTR:
3264 if (config->cfg_hdr.ioc_len < sizeof(*config))
3267 mutex_lock(&the_lnet.ln_api_mutex);
3268 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3269 rc = lnet_rtrpools_enable();
3270 mutex_unlock(&the_lnet.ln_api_mutex);
3273 lnet_rtrpools_disable();
3274 mutex_unlock(&the_lnet.ln_api_mutex);
3277 case IOC_LIBCFS_ADD_BUF:
3280 if (config->cfg_hdr.ioc_len < sizeof(*config))
3283 mutex_lock(&the_lnet.ln_api_mutex);
3284 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3286 config->cfg_config_u.cfg_buffers.
3288 config->cfg_config_u.cfg_buffers.
3290 mutex_unlock(&the_lnet.ln_api_mutex);
3293 case IOC_LIBCFS_SET_NUMA_RANGE: {
3294 struct lnet_ioctl_set_value *numa;
3296 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3298 lnet_net_lock(LNET_LOCK_EX);
3299 lnet_numa_range = numa->sv_value;
3300 lnet_net_unlock(LNET_LOCK_EX);
3304 case IOC_LIBCFS_GET_NUMA_RANGE: {
3305 struct lnet_ioctl_set_value *numa;
3307 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3309 numa->sv_value = lnet_numa_range;
3313 case IOC_LIBCFS_GET_BUF: {
3314 struct lnet_ioctl_pool_cfg *pool_cfg;
3315 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3319 if (config->cfg_hdr.ioc_len < total)
3322 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3324 mutex_lock(&the_lnet.ln_api_mutex);
3325 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3326 mutex_unlock(&the_lnet.ln_api_mutex);
3330 case IOC_LIBCFS_ADD_PEER_NI: {
3331 struct lnet_ioctl_peer_cfg *cfg = arg;
3333 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3336 mutex_lock(&the_lnet.ln_api_mutex);
3337 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3340 mutex_unlock(&the_lnet.ln_api_mutex);
3344 case IOC_LIBCFS_DEL_PEER_NI: {
3345 struct lnet_ioctl_peer_cfg *cfg = arg;
3347 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3350 mutex_lock(&the_lnet.ln_api_mutex);
3351 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3352 cfg->prcfg_cfg_nid);
3353 mutex_unlock(&the_lnet.ln_api_mutex);
3357 case IOC_LIBCFS_GET_PEER_INFO: {
3358 struct lnet_ioctl_peer *peer_info = arg;
3360 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3363 mutex_lock(&the_lnet.ln_api_mutex);
3364 rc = lnet_get_peer_ni_info(
3365 peer_info->pr_count,
3367 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3368 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3369 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3370 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3371 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3372 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3373 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3374 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3375 mutex_unlock(&the_lnet.ln_api_mutex);
3379 case IOC_LIBCFS_GET_PEER_NI: {
3380 struct lnet_ioctl_peer_cfg *cfg = arg;
3382 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3385 mutex_lock(&the_lnet.ln_api_mutex);
3386 rc = lnet_get_peer_info(cfg,
3387 (void __user *)cfg->prcfg_bulk);
3388 mutex_unlock(&the_lnet.ln_api_mutex);
3392 case IOC_LIBCFS_GET_PEER_LIST: {
3393 struct lnet_ioctl_peer_cfg *cfg = arg;
3395 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3398 mutex_lock(&the_lnet.ln_api_mutex);
3399 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3400 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3401 mutex_unlock(&the_lnet.ln_api_mutex);
3405 case IOC_LIBCFS_NOTIFY_ROUTER: {
3406 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3408 /* The deadline passed in by the user should be some time in
3409 * seconds in the future since the UNIX epoch. We have to map
3410 * that deadline to the wall clock.
3412 deadline += ktime_get_seconds();
3413 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
3417 case IOC_LIBCFS_LNET_DIST:
3418 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3419 if (rc < 0 && rc != -EHOSTUNREACH)
3422 data->ioc_u32[0] = rc;
3425 case IOC_LIBCFS_TESTPROTOCOMPAT:
3426 lnet_net_lock(LNET_LOCK_EX);
3427 the_lnet.ln_testprotocompat = data->ioc_flags;
3428 lnet_net_unlock(LNET_LOCK_EX);
3431 case IOC_LIBCFS_LNET_FAULT:
3432 return lnet_fault_ctl(data->ioc_flags, data);
3434 case IOC_LIBCFS_PING: {
3435 signed long timeout;
3437 id.nid = data->ioc_nid;
3438 id.pid = data->ioc_u32[0];
3440 /* If timeout is negative then set default of 3 minutes */
3441 if (((s32)data->ioc_u32[1] <= 0) ||
3442 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3443 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3445 timeout = msecs_to_jiffies(data->ioc_u32[1]);
3447 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3448 data->ioc_plen1 / sizeof(struct lnet_process_id));
3453 data->ioc_count = rc;
3457 case IOC_LIBCFS_PING_PEER: {
3458 struct lnet_ioctl_ping_data *ping = arg;
3459 struct lnet_peer *lp;
3460 signed long timeout;
3462 /* If timeout is negative then set default of 3 minutes */
3463 if (((s32)ping->op_param) <= 0 ||
3464 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3465 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3467 timeout = msecs_to_jiffies(ping->op_param);
3469 rc = lnet_ping(ping->ping_id, timeout,
3475 mutex_lock(&the_lnet.ln_api_mutex);
3476 lp = lnet_find_peer(ping->ping_id.nid);
3478 ping->ping_id.nid = lp->lp_primary_nid;
3479 ping->mr_info = lnet_peer_is_multi_rail(lp);
3480 lnet_peer_decref_locked(lp);
3482 mutex_unlock(&the_lnet.ln_api_mutex);
3484 ping->ping_count = rc;
3488 case IOC_LIBCFS_DISCOVER: {
3489 struct lnet_ioctl_ping_data *discover = arg;
3490 struct lnet_peer *lp;
3492 rc = lnet_discover(discover->ping_id, discover->op_param,
3494 discover->ping_count);
3498 mutex_lock(&the_lnet.ln_api_mutex);
3499 lp = lnet_find_peer(discover->ping_id.nid);
3501 discover->ping_id.nid = lp->lp_primary_nid;
3502 discover->mr_info = lnet_peer_is_multi_rail(lp);
3503 lnet_peer_decref_locked(lp);
3505 mutex_unlock(&the_lnet.ln_api_mutex);
3507 discover->ping_count = rc;
3512 ni = lnet_net2ni_addref(data->ioc_net);
3516 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3519 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
3526 EXPORT_SYMBOL(LNetCtl);
3528 void LNetDebugPeer(struct lnet_process_id id)
3530 lnet_debug_peer(id.nid);
3532 EXPORT_SYMBOL(LNetDebugPeer);
3535 * Determine if the specified peer \a nid is on the local node.
3537 * \param nid peer nid to check
3539 * \retval true If peer NID is on the local node.
3540 * \retval false If peer NID is not on the local node.
3542 bool LNetIsPeerLocal(lnet_nid_t nid)
3544 struct lnet_net *net;
3548 cpt = lnet_net_lock_current();
3549 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3550 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3551 if (ni->ni_nid == nid) {
3552 lnet_net_unlock(cpt);
3557 lnet_net_unlock(cpt);
3561 EXPORT_SYMBOL(LNetIsPeerLocal);
3564 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
3565 * Note that all interfaces share a same PID, as requested by LNetNIInit().
3567 * \param index Index of the interface to look up.
3568 * \param id On successful return, this location will hold the
3569 * struct lnet_process_id ID of the interface.
3571 * \retval 0 If an interface exists at \a index.
3572 * \retval -ENOENT If no interface has been found.
3575 LNetGetId(unsigned int index, struct lnet_process_id *id)
3578 struct lnet_net *net;
3582 LASSERT(the_lnet.ln_refcount > 0);
3584 cpt = lnet_net_lock_current();
3586 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3587 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3591 id->nid = ni->ni_nid;
3592 id->pid = the_lnet.ln_pid;
3598 lnet_net_unlock(cpt);
3601 EXPORT_SYMBOL(LNetGetId);
3603 static int lnet_ping(struct lnet_process_id id, signed long timeout,
3604 struct lnet_process_id __user *ids, int n_ids)
3606 struct lnet_handle_eq eqh;
3607 struct lnet_handle_md mdh;
3608 struct lnet_event event;
3609 struct lnet_md md = { NULL };
3613 const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
3614 struct lnet_ping_buffer *pbuf;
3615 struct lnet_process_id tmpid;
3622 /* n_ids limit is arbitrary */
3623 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
3627 * if the user buffer has more space than the lnet_interfaces_max
3628 * then only fill it up to lnet_interfaces_max
3630 if (n_ids > lnet_interfaces_max)
3631 n_ids = lnet_interfaces_max;
3633 if (id.pid == LNET_PID_ANY)
3634 id.pid = LNET_PID_LUSTRE;
3636 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
3640 /* NB 2 events max (including any unlink event) */
3641 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
3643 CERROR("Can't allocate EQ: %d\n", rc);
3644 goto fail_ping_buffer_decref;
3647 /* initialize md content */
3648 md.start = &pbuf->pb_info;
3649 md.length = LNET_PING_INFO_SIZE(n_ids);
3650 md.threshold = 2; /* GET/REPLY */
3652 md.options = LNET_MD_TRUNCATE;
3656 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
3658 CERROR("Can't bind MD: %d\n", rc);
3662 rc = LNetGet(LNET_NID_ANY, mdh, id,
3663 LNET_RESERVED_PORTAL,
3664 LNET_PROTO_PING_MATCHBITS, 0, false);
3667 /* Don't CERROR; this could be deliberate! */
3668 rc2 = LNetMDUnlink(mdh);
3671 /* NB must wait for the UNLINK event below... */
3673 timeout = a_long_time;
3677 /* MUST block for unlink to complete */
3679 blocked = cfs_block_allsigs();
3681 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
3684 cfs_restore_sigs(blocked);
3686 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
3687 (rc2 <= 0) ? -1 : event.type,
3688 (rc2 <= 0) ? -1 : event.status,
3689 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
3691 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
3693 if (rc2 <= 0 || event.status != 0) {
3694 /* timeout or error */
3695 if (!replied && rc == 0)
3696 rc = (rc2 < 0) ? rc2 :
3697 (rc2 == 0) ? -ETIMEDOUT :
3701 /* Ensure completion in finite time... */
3703 /* No assertion (racing with network) */
3705 timeout = a_long_time;
3706 } else if (rc2 == 0) {
3707 /* timed out waiting for unlink */
3708 CWARN("ping %s: late network completion\n",
3711 } else if (event.type == LNET_EVENT_REPLY) {
3715 } while (rc2 <= 0 || !event.unlinked);
3719 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
3726 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
3728 rc = -EPROTO; /* if I can't parse... */
3731 CERROR("%s: ping info too short %d\n",
3732 libcfs_id2str(id), nob);
3736 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
3737 lnet_swap_pinginfo(pbuf);
3738 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
3739 CERROR("%s: Unexpected magic %08x\n",
3740 libcfs_id2str(id), pbuf->pb_info.pi_magic);
3744 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
3745 CERROR("%s: ping w/o NI status: 0x%x\n",
3746 libcfs_id2str(id), pbuf->pb_info.pi_features);
3750 if (nob < LNET_PING_INFO_SIZE(0)) {
3751 CERROR("%s: Short reply %d(%d min)\n",
3753 nob, (int)LNET_PING_INFO_SIZE(0));
3757 if (pbuf->pb_info.pi_nnis < n_ids)
3758 n_ids = pbuf->pb_info.pi_nnis;
3760 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
3761 CERROR("%s: Short reply %d(%d expected)\n",
3763 nob, (int)LNET_PING_INFO_SIZE(n_ids));
3767 rc = -EFAULT; /* if I segv in copy_to_user()... */
3769 memset(&tmpid, 0, sizeof(tmpid));
3770 for (i = 0; i < n_ids; i++) {
3771 tmpid.pid = pbuf->pb_info.pi_pid;
3772 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
3773 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
3776 rc = pbuf->pb_info.pi_nnis;
3779 rc2 = LNetEQFree(eqh);
3781 CERROR("rc2 %d\n", rc2);
3784 fail_ping_buffer_decref:
3785 lnet_ping_buffer_decref(pbuf);
3790 lnet_discover(struct lnet_process_id id, __u32 force,
3791 struct lnet_process_id __user *ids, int n_ids)
3793 struct lnet_peer_ni *lpni;
3794 struct lnet_peer_ni *p;
3795 struct lnet_peer *lp;
3796 struct lnet_process_id *buf;
3800 int max_intf = lnet_interfaces_max;
3804 id.nid == LNET_NID_ANY)
3807 if (id.pid == LNET_PID_ANY)
3808 id.pid = LNET_PID_LUSTRE;
3811 * if the user buffer has more space than the max_intf
3812 * then only fill it up to max_intf
3814 if (n_ids > max_intf)
3817 buf_size = n_ids * sizeof(*buf);
3819 LIBCFS_ALLOC(buf, buf_size);
3823 cpt = lnet_net_lock_current();
3824 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
3831 * Clearing the NIDS_UPTODATE flag ensures the peer will
3832 * be discovered, provided discovery has not been disabled.
3834 lp = lpni->lpni_peer_net->lpn_peer;
3835 spin_lock(&lp->lp_lock);
3836 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
3837 /* If the force flag is set, force a PING and PUSH as well. */
3839 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
3840 spin_unlock(&lp->lp_lock);
3841 rc = lnet_discover_peer_locked(lpni, cpt, true);
3845 /* Peer may have changed. */
3846 lp = lpni->lpni_peer_net->lpn_peer;
3847 if (lp->lp_nnis < n_ids)
3848 n_ids = lp->lp_nnis;
3852 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
3853 buf[i].pid = id.pid;
3854 buf[i].nid = p->lpni_nid;
3859 lnet_net_unlock(cpt);
3862 if (copy_to_user(ids, buf, n_ids * sizeof(*buf)))
3868 lnet_peer_ni_decref_locked(lpni);
3870 lnet_net_unlock(cpt);
3872 LIBCFS_FREE(buf, buf_size);