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");
148 * This sequence number keeps track of how many times DLC was used to
149 * update the local NIs. It is incremented when a NI is added or
150 * removed and checked when sending a message to determine if there is
151 * a need to re-run the selection algorithm. See lnet_select_pathway()
152 * for more details on its usage.
154 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
156 static int lnet_ping(struct lnet_process_id id, signed long timeout,
157 struct lnet_process_id __user *ids, int n_ids);
159 static int lnet_discover(struct lnet_process_id id, __u32 force,
160 struct lnet_process_id __user *ids, int n_ids);
163 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
166 unsigned *sensitivity = (unsigned *)kp->arg;
169 rc = kstrtoul(val, 0, &value);
171 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
176 * The purpose of locking the api_mutex here is to ensure that
177 * the correct value ends up stored properly.
179 mutex_lock(&the_lnet.ln_api_mutex);
181 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
182 mutex_unlock(&the_lnet.ln_api_mutex);
186 if (value == *sensitivity) {
187 mutex_unlock(&the_lnet.ln_api_mutex);
191 *sensitivity = value;
193 mutex_unlock(&the_lnet.ln_api_mutex);
199 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
202 unsigned *discovery = (unsigned *)kp->arg;
204 struct lnet_ping_buffer *pbuf;
206 rc = kstrtoul(val, 0, &value);
208 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
212 value = (value) ? 1 : 0;
215 * The purpose of locking the api_mutex here is to ensure that
216 * the correct value ends up stored properly.
218 mutex_lock(&the_lnet.ln_api_mutex);
220 if (value == *discovery) {
221 mutex_unlock(&the_lnet.ln_api_mutex);
227 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
228 mutex_unlock(&the_lnet.ln_api_mutex);
232 /* tell peers that discovery setting has changed */
233 lnet_net_lock(LNET_LOCK_EX);
234 pbuf = the_lnet.ln_ping_target;
236 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
238 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
239 lnet_net_unlock(LNET_LOCK_EX);
241 lnet_push_update_to_peers(1);
243 mutex_unlock(&the_lnet.ln_api_mutex);
249 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
253 rc = kstrtoint(val, 0, &value);
255 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
259 if (value < LNET_INTERFACES_MIN) {
260 CWARN("max interfaces provided are too small, setting to %d\n",
261 LNET_INTERFACES_MAX_DEFAULT);
262 value = LNET_INTERFACES_MAX_DEFAULT;
265 *(int *)kp->arg = value;
271 lnet_get_routes(void)
277 lnet_get_networks(void)
282 if (*networks != 0 && *ip2nets != 0) {
283 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
284 "'ip2nets' but not both at once\n");
289 rc = lnet_parse_ip2nets(&nets, ip2nets);
290 return (rc == 0) ? nets : NULL;
300 lnet_init_locks(void)
302 spin_lock_init(&the_lnet.ln_eq_wait_lock);
303 spin_lock_init(&the_lnet.ln_msg_resend_lock);
304 init_waitqueue_head(&the_lnet.ln_eq_waitq);
305 init_waitqueue_head(&the_lnet.ln_rc_waitq);
306 mutex_init(&the_lnet.ln_lnd_mutex);
310 lnet_fini_locks(void)
314 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
315 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
319 lnet_descriptor_setup(void)
321 /* create specific kmem_cache for MEs and small MDs (i.e., originally
322 * allocated in <size-xxx> kmem_cache).
324 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
326 if (!lnet_mes_cachep)
329 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
330 LNET_SMALL_MD_SIZE, 0, 0,
332 if (!lnet_small_mds_cachep)
339 lnet_descriptor_cleanup(void)
342 if (lnet_small_mds_cachep) {
343 kmem_cache_destroy(lnet_small_mds_cachep);
344 lnet_small_mds_cachep = NULL;
347 if (lnet_mes_cachep) {
348 kmem_cache_destroy(lnet_mes_cachep);
349 lnet_mes_cachep = NULL;
354 lnet_create_remote_nets_table(void)
357 struct list_head *hash;
359 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
360 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
361 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
363 CERROR("Failed to create remote nets hash table\n");
367 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
368 INIT_LIST_HEAD(&hash[i]);
369 the_lnet.ln_remote_nets_hash = hash;
374 lnet_destroy_remote_nets_table(void)
378 if (the_lnet.ln_remote_nets_hash == NULL)
381 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
382 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
384 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
385 LNET_REMOTE_NETS_HASH_SIZE *
386 sizeof(the_lnet.ln_remote_nets_hash[0]));
387 the_lnet.ln_remote_nets_hash = NULL;
391 lnet_destroy_locks(void)
393 if (the_lnet.ln_res_lock != NULL) {
394 cfs_percpt_lock_free(the_lnet.ln_res_lock);
395 the_lnet.ln_res_lock = NULL;
398 if (the_lnet.ln_net_lock != NULL) {
399 cfs_percpt_lock_free(the_lnet.ln_net_lock);
400 the_lnet.ln_net_lock = NULL;
407 lnet_create_locks(void)
411 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
412 if (the_lnet.ln_res_lock == NULL)
415 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
416 if (the_lnet.ln_net_lock == NULL)
422 lnet_destroy_locks();
426 static void lnet_assert_wire_constants(void)
428 /* Wire protocol assertions generated by 'wirecheck'
429 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
430 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
431 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
434 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
435 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
436 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
437 CLASSERT(LNET_MSG_ACK == 0);
438 CLASSERT(LNET_MSG_PUT == 1);
439 CLASSERT(LNET_MSG_GET == 2);
440 CLASSERT(LNET_MSG_REPLY == 3);
441 CLASSERT(LNET_MSG_HELLO == 4);
443 /* Checks for struct lnet_handle_wire */
444 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
445 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
446 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
447 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
448 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
450 /* Checks for struct struct lnet_magicversion */
451 CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
452 CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
453 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
454 CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
455 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
456 CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
457 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
459 /* Checks for struct struct lnet_hdr */
460 CLASSERT((int)sizeof(struct lnet_hdr) == 72);
461 CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
462 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
463 CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
464 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
465 CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
466 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
467 CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
468 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
469 CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
470 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
471 CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
472 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
473 CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
474 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
477 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
478 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
479 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
480 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
481 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
482 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
485 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
486 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
487 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
488 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
489 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
490 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
491 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
492 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
493 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
494 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
497 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
498 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
499 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
500 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
501 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
502 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
503 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
504 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
505 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
506 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
509 CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
510 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
513 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
514 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
515 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
516 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
518 /* Checks for struct lnet_ni_status and related constants */
519 CLASSERT(LNET_NI_STATUS_INVALID == 0x00000000);
520 CLASSERT(LNET_NI_STATUS_UP == 0x15aac0de);
521 CLASSERT(LNET_NI_STATUS_DOWN == 0xdeadface);
523 /* Checks for struct lnet_ni_status */
524 CLASSERT((int)sizeof(struct lnet_ni_status) == 16);
525 CLASSERT((int)offsetof(struct lnet_ni_status, ns_nid) == 0);
526 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) == 8);
527 CLASSERT((int)offsetof(struct lnet_ni_status, ns_status) == 8);
528 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_status) == 4);
529 CLASSERT((int)offsetof(struct lnet_ni_status, ns_unused) == 12);
530 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) == 4);
532 /* Checks for struct lnet_ping_info and related constants */
533 CLASSERT(LNET_PROTO_PING_MAGIC == 0x70696E67);
534 CLASSERT(LNET_PING_FEAT_INVAL == 0);
535 CLASSERT(LNET_PING_FEAT_BASE == 1);
536 CLASSERT(LNET_PING_FEAT_NI_STATUS == 2);
537 CLASSERT(LNET_PING_FEAT_RTE_DISABLED == 4);
538 CLASSERT(LNET_PING_FEAT_MULTI_RAIL == 8);
539 CLASSERT(LNET_PING_FEAT_DISCOVERY == 16);
540 CLASSERT(LNET_PING_FEAT_BITS == 31);
542 /* Checks for struct lnet_ping_info */
543 CLASSERT((int)sizeof(struct lnet_ping_info) == 16);
544 CLASSERT((int)offsetof(struct lnet_ping_info, pi_magic) == 0);
545 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) == 4);
546 CLASSERT((int)offsetof(struct lnet_ping_info, pi_features) == 4);
547 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_features) == 4);
548 CLASSERT((int)offsetof(struct lnet_ping_info, pi_pid) == 8);
549 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) == 4);
550 CLASSERT((int)offsetof(struct lnet_ping_info, pi_nnis) == 12);
551 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) == 4);
552 CLASSERT((int)offsetof(struct lnet_ping_info, pi_ni) == 16);
553 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) == 0);
556 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
558 struct lnet_lnd *lnd;
559 struct list_head *tmp;
561 /* holding lnd mutex */
562 list_for_each(tmp, &the_lnet.ln_lnds) {
563 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
565 if (lnd->lnd_type == type)
572 lnet_register_lnd(struct lnet_lnd *lnd)
574 mutex_lock(&the_lnet.ln_lnd_mutex);
576 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
577 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
579 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
580 lnd->lnd_refcount = 0;
582 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
584 mutex_unlock(&the_lnet.ln_lnd_mutex);
586 EXPORT_SYMBOL(lnet_register_lnd);
589 lnet_unregister_lnd(struct lnet_lnd *lnd)
591 mutex_lock(&the_lnet.ln_lnd_mutex);
593 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
594 LASSERT(lnd->lnd_refcount == 0);
596 list_del(&lnd->lnd_list);
597 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
599 mutex_unlock(&the_lnet.ln_lnd_mutex);
601 EXPORT_SYMBOL(lnet_unregister_lnd);
604 lnet_counters_get(struct lnet_counters *counters)
606 struct lnet_counters *ctr;
609 memset(counters, 0, sizeof(*counters));
611 lnet_net_lock(LNET_LOCK_EX);
613 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
614 counters->msgs_max += ctr->msgs_max;
615 counters->msgs_alloc += ctr->msgs_alloc;
616 counters->errors += ctr->errors;
617 counters->send_count += ctr->send_count;
618 counters->recv_count += ctr->recv_count;
619 counters->route_count += ctr->route_count;
620 counters->drop_count += ctr->drop_count;
621 counters->send_length += ctr->send_length;
622 counters->recv_length += ctr->recv_length;
623 counters->route_length += ctr->route_length;
624 counters->drop_length += ctr->drop_length;
627 lnet_net_unlock(LNET_LOCK_EX);
629 EXPORT_SYMBOL(lnet_counters_get);
632 lnet_counters_reset(void)
634 struct lnet_counters *counters;
637 lnet_net_lock(LNET_LOCK_EX);
639 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
640 memset(counters, 0, sizeof(struct lnet_counters));
642 lnet_net_unlock(LNET_LOCK_EX);
646 lnet_res_type2str(int type)
651 case LNET_COOKIE_TYPE_MD:
653 case LNET_COOKIE_TYPE_ME:
655 case LNET_COOKIE_TYPE_EQ:
661 lnet_res_container_cleanup(struct lnet_res_container *rec)
665 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
668 while (!list_empty(&rec->rec_active)) {
669 struct list_head *e = rec->rec_active.next;
672 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
673 lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
675 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
676 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
678 } else { /* NB: Active MEs should be attached on portals */
685 /* Found alive MD/ME/EQ, user really should unlink/free
686 * all of them before finalize LNet, but if someone didn't,
687 * we have to recycle garbage for him */
688 CERROR("%d active elements on exit of %s container\n",
689 count, lnet_res_type2str(rec->rec_type));
692 if (rec->rec_lh_hash != NULL) {
693 LIBCFS_FREE(rec->rec_lh_hash,
694 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
695 rec->rec_lh_hash = NULL;
698 rec->rec_type = 0; /* mark it as finalized */
702 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
707 LASSERT(rec->rec_type == 0);
709 rec->rec_type = type;
710 INIT_LIST_HEAD(&rec->rec_active);
712 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
714 /* Arbitrary choice of hash table size */
715 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
716 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
717 if (rec->rec_lh_hash == NULL) {
722 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
723 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
728 CERROR("Failed to setup %s resource container\n",
729 lnet_res_type2str(type));
730 lnet_res_container_cleanup(rec);
735 lnet_res_containers_destroy(struct lnet_res_container **recs)
737 struct lnet_res_container *rec;
740 cfs_percpt_for_each(rec, i, recs)
741 lnet_res_container_cleanup(rec);
743 cfs_percpt_free(recs);
746 static struct lnet_res_container **
747 lnet_res_containers_create(int type)
749 struct lnet_res_container **recs;
750 struct lnet_res_container *rec;
754 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
756 CERROR("Failed to allocate %s resource containers\n",
757 lnet_res_type2str(type));
761 cfs_percpt_for_each(rec, i, recs) {
762 rc = lnet_res_container_setup(rec, i, type);
764 lnet_res_containers_destroy(recs);
772 struct lnet_libhandle *
773 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
775 /* ALWAYS called with lnet_res_lock held */
776 struct list_head *head;
777 struct lnet_libhandle *lh;
780 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
783 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
784 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
786 list_for_each_entry(lh, head, lh_hash_chain) {
787 if (lh->lh_cookie == cookie)
795 lnet_res_lh_initialize(struct lnet_res_container *rec,
796 struct lnet_libhandle *lh)
798 /* ALWAYS called with lnet_res_lock held */
799 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
802 lh->lh_cookie = rec->rec_lh_cookie;
803 rec->rec_lh_cookie += 1 << ibits;
805 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
807 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
810 static int lnet_unprepare(void);
813 lnet_prepare(lnet_pid_t requested_pid)
815 /* Prepare to bring up the network */
816 struct lnet_res_container **recs;
819 if (requested_pid == LNET_PID_ANY) {
820 /* Don't instantiate LNET just for me */
824 LASSERT(the_lnet.ln_refcount == 0);
826 the_lnet.ln_routing = 0;
828 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
829 the_lnet.ln_pid = requested_pid;
831 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
832 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
833 INIT_LIST_HEAD(&the_lnet.ln_nets);
834 INIT_LIST_HEAD(&the_lnet.ln_routers);
835 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
836 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
837 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
838 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
839 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
840 init_waitqueue_head(&the_lnet.ln_dc_waitq);
842 rc = lnet_descriptor_setup();
846 rc = lnet_create_remote_nets_table();
851 * NB the interface cookie in wire handles guards against delayed
852 * replies and ACKs appearing valid after reboot.
854 the_lnet.ln_interface_cookie = ktime_get_real_ns();
856 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
857 sizeof(struct lnet_counters));
858 if (the_lnet.ln_counters == NULL) {
859 CERROR("Failed to allocate counters for LNet\n");
864 rc = lnet_peer_tables_create();
868 rc = lnet_msg_containers_create();
872 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
873 LNET_COOKIE_TYPE_EQ);
877 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
883 the_lnet.ln_me_containers = recs;
885 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
891 the_lnet.ln_md_containers = recs;
893 rc = lnet_portals_create();
895 CERROR("Failed to create portals for LNet: %d\n", rc);
907 lnet_unprepare (void)
909 /* NB no LNET_LOCK since this is the last reference. All LND instances
910 * have shut down already, so it is safe to unlink and free all
911 * descriptors, even those that appear committed to a network op (eg MD
912 * with non-zero pending count) */
914 lnet_fail_nid(LNET_NID_ANY, 0);
916 LASSERT(the_lnet.ln_refcount == 0);
917 LASSERT(list_empty(&the_lnet.ln_test_peers));
918 LASSERT(list_empty(&the_lnet.ln_nets));
920 lnet_portals_destroy();
922 if (the_lnet.ln_md_containers != NULL) {
923 lnet_res_containers_destroy(the_lnet.ln_md_containers);
924 the_lnet.ln_md_containers = NULL;
927 if (the_lnet.ln_me_containers != NULL) {
928 lnet_res_containers_destroy(the_lnet.ln_me_containers);
929 the_lnet.ln_me_containers = NULL;
932 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
934 lnet_msg_containers_destroy();
936 lnet_rtrpools_free(0);
938 if (the_lnet.ln_counters != NULL) {
939 cfs_percpt_free(the_lnet.ln_counters);
940 the_lnet.ln_counters = NULL;
942 lnet_destroy_remote_nets_table();
943 lnet_descriptor_cleanup();
949 lnet_net2ni_locked(__u32 net_id, int cpt)
952 struct lnet_net *net;
954 LASSERT(cpt != LNET_LOCK_EX);
956 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
957 if (net->net_id == net_id) {
958 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
968 lnet_net2ni_addref(__u32 net)
973 ni = lnet_net2ni_locked(net, 0);
975 lnet_ni_addref_locked(ni, 0);
980 EXPORT_SYMBOL(lnet_net2ni_addref);
983 lnet_get_net_locked(__u32 net_id)
985 struct lnet_net *net;
987 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
988 if (net->net_id == net_id)
996 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1001 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1006 val = hash_long(key, LNET_CPT_BITS);
1007 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1011 return (unsigned int)(key + val + (val >> 1)) % number;
1015 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1017 struct lnet_net *net;
1019 /* must called with hold of lnet_net_lock */
1020 if (LNET_CPT_NUMBER == 1)
1021 return 0; /* the only one */
1024 * If NI is provided then use the CPT identified in the NI cpt
1025 * list if one exists. If one doesn't exist, then that NI is
1026 * associated with all CPTs and it follows that the net it belongs
1027 * to is implicitly associated with all CPTs, so just hash the nid
1031 if (ni->ni_cpts != NULL)
1032 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1035 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1038 /* no NI provided so look at the net */
1039 net = lnet_get_net_locked(LNET_NIDNET(nid));
1041 if (net != NULL && net->net_cpts != NULL) {
1042 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1045 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1049 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1054 if (LNET_CPT_NUMBER == 1)
1055 return 0; /* the only one */
1057 cpt = lnet_net_lock_current();
1059 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1061 lnet_net_unlock(cpt);
1065 EXPORT_SYMBOL(lnet_cpt_of_nid);
1068 lnet_islocalnet(__u32 net_id)
1070 struct lnet_net *net;
1074 cpt = lnet_net_lock_current();
1076 net = lnet_get_net_locked(net_id);
1078 local = net != NULL;
1080 lnet_net_unlock(cpt);
1086 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
1088 if (ni->ni_state == LNET_NI_STATE_ACTIVE ||
1089 ni->ni_state == LNET_NI_STATE_DEGRADED)
1096 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1098 struct lnet_net *net;
1101 LASSERT(cpt != LNET_LOCK_EX);
1103 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1104 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1105 if (ni->ni_nid == nid)
1114 lnet_nid2ni_addref(lnet_nid_t nid)
1119 ni = lnet_nid2ni_locked(nid, 0);
1121 lnet_ni_addref_locked(ni, 0);
1126 EXPORT_SYMBOL(lnet_nid2ni_addref);
1129 lnet_islocalnid(lnet_nid_t nid)
1134 cpt = lnet_net_lock_current();
1135 ni = lnet_nid2ni_locked(nid, cpt);
1136 lnet_net_unlock(cpt);
1142 lnet_count_acceptor_nets(void)
1144 /* Return the # of NIs that need the acceptor. */
1146 struct lnet_net *net;
1149 cpt = lnet_net_lock_current();
1150 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1151 /* all socklnd type networks should have the acceptor
1153 if (net->net_lnd->lnd_accept != NULL)
1157 lnet_net_unlock(cpt);
1162 struct lnet_ping_buffer *
1163 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1165 struct lnet_ping_buffer *pbuf;
1167 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1169 pbuf->pb_nnis = nnis;
1170 atomic_set(&pbuf->pb_refcnt, 1);
1177 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1179 LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1180 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1183 static struct lnet_ping_buffer *
1184 lnet_ping_target_create(int nnis)
1186 struct lnet_ping_buffer *pbuf;
1188 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1190 CERROR("Can't allocate ping source [%d]\n", nnis);
1194 pbuf->pb_info.pi_nnis = nnis;
1195 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1196 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1197 pbuf->pb_info.pi_features =
1198 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1204 lnet_get_net_ni_count_locked(struct lnet_net *net)
1209 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1216 lnet_get_net_ni_count_pre(struct lnet_net *net)
1221 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1228 lnet_get_ni_count(void)
1231 struct lnet_net *net;
1236 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1237 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1247 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1251 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1253 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1255 /* Loopback is guaranteed to be present */
1256 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1258 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1264 lnet_ping_target_destroy(void)
1266 struct lnet_net *net;
1269 lnet_net_lock(LNET_LOCK_EX);
1271 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1272 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1274 ni->ni_status = NULL;
1279 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1280 the_lnet.ln_ping_target = NULL;
1282 lnet_net_unlock(LNET_LOCK_EX);
1286 lnet_ping_target_event_handler(struct lnet_event *event)
1288 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1290 if (event->unlinked)
1291 lnet_ping_buffer_decref(pbuf);
1295 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1296 struct lnet_handle_md *ping_mdh,
1297 int ni_count, bool set_eq)
1299 struct lnet_process_id id = {
1300 .nid = LNET_NID_ANY,
1303 struct lnet_handle_me me_handle;
1304 struct lnet_md md = { NULL };
1308 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1309 &the_lnet.ln_ping_target_eq);
1311 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1316 *ppbuf = lnet_ping_target_create(ni_count);
1317 if (*ppbuf == NULL) {
1322 /* Ping target ME/MD */
1323 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1324 LNET_PROTO_PING_MATCHBITS, 0,
1325 LNET_UNLINK, LNET_INS_AFTER,
1328 CERROR("Can't create ping target ME: %d\n", rc);
1329 goto fail_decref_ping_buffer;
1332 /* initialize md content */
1333 md.start = &(*ppbuf)->pb_info;
1334 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1335 md.threshold = LNET_MD_THRESH_INF;
1337 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1338 LNET_MD_MANAGE_REMOTE;
1339 md.eq_handle = the_lnet.ln_ping_target_eq;
1340 md.user_ptr = *ppbuf;
1342 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, ping_mdh);
1344 CERROR("Can't attach ping target MD: %d\n", rc);
1345 goto fail_unlink_ping_me;
1347 lnet_ping_buffer_addref(*ppbuf);
1351 fail_unlink_ping_me:
1352 rc2 = LNetMEUnlink(me_handle);
1354 fail_decref_ping_buffer:
1355 LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1356 lnet_ping_buffer_decref(*ppbuf);
1360 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1367 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1368 struct lnet_handle_md *ping_mdh)
1370 sigset_t blocked = cfs_block_allsigs();
1372 LNetMDUnlink(*ping_mdh);
1373 LNetInvalidateMDHandle(ping_mdh);
1375 /* NB the MD could be busy; this just starts the unlink */
1376 while (lnet_ping_buffer_numref(pbuf) > 1) {
1377 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1378 set_current_state(TASK_UNINTERRUPTIBLE);
1379 schedule_timeout(cfs_time_seconds(1));
1382 cfs_restore_sigs(blocked);
1386 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1389 struct lnet_net *net;
1390 struct lnet_ni_status *ns;
1395 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1396 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1397 LASSERT(i < pbuf->pb_nnis);
1399 ns = &pbuf->pb_info.pi_ni[i];
1401 ns->ns_nid = ni->ni_nid;
1404 ns->ns_status = (ni->ni_status != NULL) ?
1405 ni->ni_status->ns_status :
1414 * We (ab)use the ns_status of the loopback interface to
1415 * transmit the sequence number. The first interface listed
1416 * must be the loopback interface.
1418 rc = lnet_ping_info_validate(&pbuf->pb_info);
1420 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1423 LNET_PING_BUFFER_SEQNO(pbuf) =
1424 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1428 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1429 struct lnet_handle_md ping_mdh)
1431 struct lnet_ping_buffer *old_pbuf = NULL;
1432 struct lnet_handle_md old_ping_md;
1434 /* switch the NIs to point to the new ping info created */
1435 lnet_net_lock(LNET_LOCK_EX);
1437 if (!the_lnet.ln_routing)
1438 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1439 if (!lnet_peer_discovery_disabled)
1440 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1442 /* Ensure only known feature bits have been set. */
1443 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1444 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1446 lnet_ping_target_install_locked(pbuf);
1448 if (the_lnet.ln_ping_target) {
1449 old_pbuf = the_lnet.ln_ping_target;
1450 old_ping_md = the_lnet.ln_ping_target_md;
1452 the_lnet.ln_ping_target_md = ping_mdh;
1453 the_lnet.ln_ping_target = pbuf;
1455 lnet_net_unlock(LNET_LOCK_EX);
1458 /* unlink and free the old ping info */
1459 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1460 lnet_ping_buffer_decref(old_pbuf);
1463 lnet_push_update_to_peers(0);
1467 lnet_ping_target_fini(void)
1471 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1472 &the_lnet.ln_ping_target_md);
1474 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1477 lnet_ping_target_destroy();
1480 /* Resize the push target. */
1481 int lnet_push_target_resize(void)
1483 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1484 struct lnet_md md = { NULL };
1485 struct lnet_handle_me meh;
1486 struct lnet_handle_md mdh;
1487 struct lnet_handle_md old_mdh;
1488 struct lnet_ping_buffer *pbuf;
1489 struct lnet_ping_buffer *old_pbuf;
1490 int nnis = the_lnet.ln_push_target_nnis;
1498 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1504 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1505 LNET_PROTO_PING_MATCHBITS, 0,
1506 LNET_UNLINK, LNET_INS_AFTER,
1509 CERROR("Can't create push target ME: %d\n", rc);
1510 goto fail_decref_pbuf;
1513 /* initialize md content */
1514 md.start = &pbuf->pb_info;
1515 md.length = LNET_PING_INFO_SIZE(nnis);
1516 md.threshold = LNET_MD_THRESH_INF;
1518 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1519 LNET_MD_MANAGE_REMOTE;
1521 md.eq_handle = the_lnet.ln_push_target_eq;
1523 rc = LNetMDAttach(meh, md, LNET_RETAIN, &mdh);
1525 CERROR("Can't attach push MD: %d\n", rc);
1526 goto fail_unlink_meh;
1528 lnet_ping_buffer_addref(pbuf);
1530 lnet_net_lock(LNET_LOCK_EX);
1531 old_pbuf = the_lnet.ln_push_target;
1532 old_mdh = the_lnet.ln_push_target_md;
1533 the_lnet.ln_push_target = pbuf;
1534 the_lnet.ln_push_target_md = mdh;
1535 lnet_net_unlock(LNET_LOCK_EX);
1538 LNetMDUnlink(old_mdh);
1539 lnet_ping_buffer_decref(old_pbuf);
1542 if (nnis < the_lnet.ln_push_target_nnis)
1545 CDEBUG(D_NET, "nnis %d success\n", nnis);
1552 lnet_ping_buffer_decref(pbuf);
1554 CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1558 static void lnet_push_target_event_handler(struct lnet_event *ev)
1560 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1562 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1563 lnet_swap_pinginfo(pbuf);
1565 lnet_peer_push_event(ev);
1567 lnet_ping_buffer_decref(pbuf);
1570 /* Initialize the push target. */
1571 static int lnet_push_target_init(void)
1575 if (the_lnet.ln_push_target)
1578 rc = LNetEQAlloc(0, lnet_push_target_event_handler,
1579 &the_lnet.ln_push_target_eq);
1581 CERROR("Can't allocated push target EQ: %d\n", rc);
1585 /* Start at the required minimum, we'll enlarge if required. */
1586 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1588 rc = lnet_push_target_resize();
1591 LNetEQFree(the_lnet.ln_push_target_eq);
1592 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1598 /* Clean up the push target. */
1599 static void lnet_push_target_fini(void)
1601 if (!the_lnet.ln_push_target)
1604 /* Unlink and invalidate to prevent new references. */
1605 LNetMDUnlink(the_lnet.ln_push_target_md);
1606 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1608 /* Wait for the unlink to complete. */
1609 while (lnet_ping_buffer_numref(the_lnet.ln_push_target) > 1) {
1610 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1611 set_current_state(TASK_UNINTERRUPTIBLE);
1612 schedule_timeout(cfs_time_seconds(1));
1615 lnet_ping_buffer_decref(the_lnet.ln_push_target);
1616 the_lnet.ln_push_target = NULL;
1617 the_lnet.ln_push_target_nnis = 0;
1619 LNetEQFree(the_lnet.ln_push_target_eq);
1620 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1624 lnet_ni_tq_credits(struct lnet_ni *ni)
1628 LASSERT(ni->ni_ncpts >= 1);
1630 if (ni->ni_ncpts == 1)
1631 return ni->ni_net->net_tunables.lct_max_tx_credits;
1633 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1634 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1635 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1641 lnet_ni_unlink_locked(struct lnet_ni *ni)
1643 if (!list_empty(&ni->ni_cptlist)) {
1644 list_del_init(&ni->ni_cptlist);
1645 lnet_ni_decref_locked(ni, 0);
1648 /* move it to zombie list and nobody can find it anymore */
1649 LASSERT(!list_empty(&ni->ni_netlist));
1650 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1651 lnet_ni_decref_locked(ni, 0);
1655 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1660 struct list_head *zombie_list = &net->net_ni_zombie;
1663 * Now wait for the NIs I just nuked to show up on the zombie
1664 * list and shut them down in guaranteed thread context
1667 while (!list_empty(zombie_list)) {
1671 ni = list_entry(zombie_list->next,
1672 struct lnet_ni, ni_netlist);
1673 list_del_init(&ni->ni_netlist);
1674 /* the ni should be in deleting state. If it's not it's
1676 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
1677 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1680 /* still busy, add it back to zombie list */
1681 list_add(&ni->ni_netlist, zombie_list);
1685 if (!list_empty(&ni->ni_netlist)) {
1686 lnet_net_unlock(LNET_LOCK_EX);
1688 if ((i & (-i)) == i) {
1690 "Waiting for zombie LNI %s\n",
1691 libcfs_nid2str(ni->ni_nid));
1693 set_current_state(TASK_UNINTERRUPTIBLE);
1694 schedule_timeout(cfs_time_seconds(1));
1695 lnet_net_lock(LNET_LOCK_EX);
1699 lnet_net_unlock(LNET_LOCK_EX);
1701 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1703 LASSERT(!in_interrupt());
1704 (net->net_lnd->lnd_shutdown)(ni);
1707 CDEBUG(D_LNI, "Removed LNI %s\n",
1708 libcfs_nid2str(ni->ni_nid));
1712 lnet_net_lock(LNET_LOCK_EX);
1716 /* shutdown down the NI and release refcount */
1718 lnet_shutdown_lndni(struct lnet_ni *ni)
1721 struct lnet_net *net = ni->ni_net;
1723 lnet_net_lock(LNET_LOCK_EX);
1724 ni->ni_state = LNET_NI_STATE_DELETING;
1725 lnet_ni_unlink_locked(ni);
1726 lnet_incr_dlc_seq();
1727 lnet_net_unlock(LNET_LOCK_EX);
1729 /* clear messages for this NI on the lazy portal */
1730 for (i = 0; i < the_lnet.ln_nportals; i++)
1731 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1733 lnet_net_lock(LNET_LOCK_EX);
1734 lnet_clear_zombies_nis_locked(net);
1735 lnet_net_unlock(LNET_LOCK_EX);
1739 lnet_shutdown_lndnet(struct lnet_net *net)
1743 lnet_net_lock(LNET_LOCK_EX);
1745 net->net_state = LNET_NET_STATE_DELETING;
1747 list_del_init(&net->net_list);
1749 while (!list_empty(&net->net_ni_list)) {
1750 ni = list_entry(net->net_ni_list.next,
1751 struct lnet_ni, ni_netlist);
1752 lnet_net_unlock(LNET_LOCK_EX);
1753 lnet_shutdown_lndni(ni);
1754 lnet_net_lock(LNET_LOCK_EX);
1757 lnet_net_unlock(LNET_LOCK_EX);
1759 /* Do peer table cleanup for this net */
1760 lnet_peer_tables_cleanup(net);
1762 lnet_net_lock(LNET_LOCK_EX);
1764 * decrement ref count on lnd only when the entire network goes
1767 net->net_lnd->lnd_refcount--;
1769 lnet_net_unlock(LNET_LOCK_EX);
1775 lnet_shutdown_lndnets(void)
1777 struct lnet_net *net;
1778 struct list_head resend;
1779 struct lnet_msg *msg, *tmp;
1781 INIT_LIST_HEAD(&resend);
1783 /* NB called holding the global mutex */
1785 /* All quiet on the API front */
1786 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
1787 LASSERT(the_lnet.ln_refcount == 0);
1789 lnet_net_lock(LNET_LOCK_EX);
1790 the_lnet.ln_state = LNET_STATE_STOPPING;
1792 while (!list_empty(&the_lnet.ln_nets)) {
1794 * move the nets to the zombie list to avoid them being
1795 * picked up for new work. LONET is also included in the
1796 * Nets that will be moved to the zombie list
1798 net = list_entry(the_lnet.ln_nets.next,
1799 struct lnet_net, net_list);
1800 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1803 /* Drop the cached loopback Net. */
1804 if (the_lnet.ln_loni != NULL) {
1805 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1806 the_lnet.ln_loni = NULL;
1808 lnet_net_unlock(LNET_LOCK_EX);
1810 /* iterate through the net zombie list and delete each net */
1811 while (!list_empty(&the_lnet.ln_net_zombie)) {
1812 net = list_entry(the_lnet.ln_net_zombie.next,
1813 struct lnet_net, net_list);
1814 lnet_shutdown_lndnet(net);
1817 spin_lock(&the_lnet.ln_msg_resend_lock);
1818 list_splice(&the_lnet.ln_msg_resend, &resend);
1819 spin_unlock(&the_lnet.ln_msg_resend_lock);
1821 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
1822 list_del_init(&msg->msg_list);
1823 lnet_finalize(msg, -ECANCELED);
1826 lnet_net_lock(LNET_LOCK_EX);
1827 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
1828 lnet_net_unlock(LNET_LOCK_EX);
1832 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1835 struct lnet_tx_queue *tq;
1837 struct lnet_net *net = ni->ni_net;
1839 mutex_lock(&the_lnet.ln_lnd_mutex);
1842 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1843 ni->ni_lnd_tunables_set = true;
1846 rc = (net->net_lnd->lnd_startup)(ni);
1848 mutex_unlock(&the_lnet.ln_lnd_mutex);
1851 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1852 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1853 lnet_net_lock(LNET_LOCK_EX);
1854 net->net_lnd->lnd_refcount--;
1855 lnet_net_unlock(LNET_LOCK_EX);
1859 ni->ni_state = LNET_NI_STATE_ACTIVE;
1861 /* We keep a reference on the loopback net through the loopback NI */
1862 if (net->net_lnd->lnd_type == LOLND) {
1864 LASSERT(the_lnet.ln_loni == NULL);
1865 the_lnet.ln_loni = ni;
1866 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
1867 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
1868 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
1869 ni->ni_net->net_tunables.lct_peer_timeout = 0;
1873 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
1874 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
1875 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1876 libcfs_lnd2str(net->net_lnd->lnd_type),
1877 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
1879 /* shutdown the NI since if we get here then it must've already
1882 lnet_shutdown_lndni(ni);
1886 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1887 tq->tq_credits_min =
1888 tq->tq_credits_max =
1889 tq->tq_credits = lnet_ni_tq_credits(ni);
1892 atomic_set(&ni->ni_tx_credits,
1893 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
1894 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
1896 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1897 libcfs_nid2str(ni->ni_nid),
1898 ni->ni_net->net_tunables.lct_peer_tx_credits,
1899 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1900 ni->ni_net->net_tunables.lct_peer_rtr_credits,
1901 ni->ni_net->net_tunables.lct_peer_timeout);
1910 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
1913 struct lnet_net *net_l = NULL;
1914 struct list_head local_ni_list;
1918 struct lnet_lnd *lnd;
1920 net->net_tunables.lct_peer_timeout;
1922 net->net_tunables.lct_max_tx_credits;
1923 int peerrtrcredits =
1924 net->net_tunables.lct_peer_rtr_credits;
1926 INIT_LIST_HEAD(&local_ni_list);
1929 * make sure that this net is unique. If it isn't then
1930 * we are adding interfaces to an already existing network, and
1931 * 'net' is just a convenient way to pass in the list.
1932 * if it is unique we need to find the LND and load it if
1935 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
1936 lnd_type = LNET_NETTYP(net->net_id);
1938 mutex_lock(&the_lnet.ln_lnd_mutex);
1939 lnd = lnet_find_lnd_by_type(lnd_type);
1942 mutex_unlock(&the_lnet.ln_lnd_mutex);
1943 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1944 mutex_lock(&the_lnet.ln_lnd_mutex);
1946 lnd = lnet_find_lnd_by_type(lnd_type);
1948 mutex_unlock(&the_lnet.ln_lnd_mutex);
1949 CERROR("Can't load LND %s, module %s, rc=%d\n",
1950 libcfs_lnd2str(lnd_type),
1951 libcfs_lnd2modname(lnd_type), rc);
1952 #ifndef HAVE_MODULE_LOADING_SUPPORT
1953 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1954 "compiled with kernel module "
1955 "loading support.");
1962 lnet_net_lock(LNET_LOCK_EX);
1963 lnd->lnd_refcount++;
1964 lnet_net_unlock(LNET_LOCK_EX);
1968 mutex_unlock(&the_lnet.ln_lnd_mutex);
1974 * net_l: if the network being added is unique then net_l
1975 * will point to that network
1976 * if the network being added is not unique then
1977 * net_l points to the existing network.
1979 * When we enter the loop below, we'll pick NIs off he
1980 * network beign added and start them up, then add them to
1981 * a local ni list. Once we've successfully started all
1982 * the NIs then we join the local NI list (of started up
1983 * networks) with the net_l->net_ni_list, which should
1984 * point to the correct network to add the new ni list to
1986 * If any of the new NIs fail to start up, then we want to
1987 * iterate through the local ni list, which should include
1988 * any NIs which were successfully started up, and shut
1991 * After than we want to delete the network being added,
1992 * to avoid a memory leak.
1996 * When a network uses TCP bonding then all its interfaces
1997 * must be specified when the network is first defined: the
1998 * TCP bonding code doesn't allow for interfaces to be added
2001 if (net_l != net && net_l != NULL && use_tcp_bonding &&
2002 LNET_NETTYP(net_l->net_id) == SOCKLND) {
2007 while (!list_empty(&net->net_ni_added)) {
2008 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2010 list_del_init(&ni->ni_netlist);
2012 /* make sure that the the NI we're about to start
2013 * up is actually unique. if it's not fail. */
2014 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2015 ni->ni_interfaces[0])) {
2020 /* adjust the pointer the parent network, just in case it
2021 * the net is a duplicate */
2024 rc = lnet_startup_lndni(ni, tun);
2026 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
2027 ni->ni_net->net_lnd->lnd_query != NULL);
2033 list_add_tail(&ni->ni_netlist, &local_ni_list);
2038 lnet_net_lock(LNET_LOCK_EX);
2039 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2040 lnet_incr_dlc_seq();
2041 lnet_net_unlock(LNET_LOCK_EX);
2043 /* if the network is not unique then we don't want to keep
2044 * it around after we're done. Free it. Otherwise add that
2045 * net to the global the_lnet.ln_nets */
2046 if (net_l != net && net_l != NULL) {
2048 * TODO - note. currently the tunables can not be updated
2053 net->net_state = LNET_NET_STATE_ACTIVE;
2055 * restore tunables after it has been overwitten by the
2058 if (peer_timeout != -1)
2059 net->net_tunables.lct_peer_timeout = peer_timeout;
2060 if (maxtxcredits != -1)
2061 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2062 if (peerrtrcredits != -1)
2063 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2065 lnet_net_lock(LNET_LOCK_EX);
2066 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2067 lnet_net_unlock(LNET_LOCK_EX);
2074 * shutdown the new NIs that are being started up
2075 * free the NET being started
2077 while (!list_empty(&local_ni_list)) {
2078 ni = list_entry(local_ni_list.next, struct lnet_ni,
2081 lnet_shutdown_lndni(ni);
2091 lnet_startup_lndnets(struct list_head *netlist)
2093 struct lnet_net *net;
2098 * Change to running state before bringing up the LNDs. This
2099 * allows lnet_shutdown_lndnets() to assert that we've passed
2102 lnet_net_lock(LNET_LOCK_EX);
2103 the_lnet.ln_state = LNET_STATE_RUNNING;
2104 lnet_net_unlock(LNET_LOCK_EX);
2106 while (!list_empty(netlist)) {
2107 net = list_entry(netlist->next, struct lnet_net, net_list);
2108 list_del_init(&net->net_list);
2110 rc = lnet_startup_lndnet(net, NULL);
2120 lnet_shutdown_lndnets();
2126 * Initialize LNet library.
2128 * Automatically called at module loading time. Caller has to call
2129 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2130 * latter returned 0. It must be called exactly once.
2132 * \retval 0 on success
2133 * \retval -ve on failures.
2135 int lnet_lib_init(void)
2139 lnet_assert_wire_constants();
2141 /* refer to global cfs_cpt_table for now */
2142 the_lnet.ln_cpt_table = cfs_cpt_table;
2143 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
2145 LASSERT(the_lnet.ln_cpt_number > 0);
2146 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2147 /* we are under risk of consuming all lh_cookie */
2148 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2149 "please change setting of CPT-table and retry\n",
2150 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2154 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2155 the_lnet.ln_cpt_bits++;
2157 rc = lnet_create_locks();
2159 CERROR("Can't create LNet global locks: %d\n", rc);
2163 the_lnet.ln_refcount = 0;
2164 LNetInvalidateEQHandle(&the_lnet.ln_rc_eqh);
2165 INIT_LIST_HEAD(&the_lnet.ln_lnds);
2166 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2167 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
2168 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2169 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
2171 /* The hash table size is the number of bits it takes to express the set
2172 * ln_num_routes, minus 1 (better to under estimate than over so we
2173 * don't waste memory). */
2174 if (rnet_htable_size <= 0)
2175 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2176 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2177 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2178 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2179 order_base_2(rnet_htable_size) - 1);
2181 /* All LNDs apart from the LOLND are in separate modules. They
2182 * register themselves when their module loads, and unregister
2183 * themselves when their module is unloaded. */
2184 lnet_register_lnd(&the_lolnd);
2189 * Finalize LNet library.
2191 * \pre lnet_lib_init() called with success.
2192 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2194 void lnet_lib_exit(void)
2196 LASSERT(the_lnet.ln_refcount == 0);
2198 while (!list_empty(&the_lnet.ln_lnds))
2199 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
2200 struct lnet_lnd, lnd_list));
2201 lnet_destroy_locks();
2205 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2207 * Users must call this function at least once before any other functions.
2208 * For each successful call there must be a corresponding call to
2209 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2212 * The PID used by LNet may be different from the one requested.
2215 * \param requested_pid PID requested by the caller.
2217 * \return >= 0 on success, and < 0 error code on failures.
2220 LNetNIInit(lnet_pid_t requested_pid)
2222 int im_a_router = 0;
2225 struct lnet_ping_buffer *pbuf;
2226 struct lnet_handle_md ping_mdh;
2227 struct list_head net_head;
2228 struct lnet_net *net;
2230 INIT_LIST_HEAD(&net_head);
2232 mutex_lock(&the_lnet.ln_api_mutex);
2234 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2236 if (the_lnet.ln_refcount > 0) {
2237 rc = the_lnet.ln_refcount++;
2238 mutex_unlock(&the_lnet.ln_api_mutex);
2242 rc = lnet_prepare(requested_pid);
2244 mutex_unlock(&the_lnet.ln_api_mutex);
2248 /* create a network for Loopback network */
2249 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2252 goto err_empty_list;
2255 /* Add in the loopback NI */
2256 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2258 goto err_empty_list;
2261 /* If LNet is being initialized via DLC it is possible
2262 * that the user requests not to load module parameters (ones which
2263 * are supported by DLC) on initialization. Therefore, make sure not
2264 * to load networks, routes and forwarding from module parameters
2265 * in this case. On cleanup in case of failure only clean up
2266 * routes if it has been loaded */
2267 if (!the_lnet.ln_nis_from_mod_params) {
2268 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2271 goto err_empty_list;
2274 ni_count = lnet_startup_lndnets(&net_head);
2277 goto err_empty_list;
2280 if (!the_lnet.ln_nis_from_mod_params) {
2281 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2283 goto err_shutdown_lndnis;
2285 rc = lnet_check_routes();
2287 goto err_destroy_routes;
2289 rc = lnet_rtrpools_alloc(im_a_router);
2291 goto err_destroy_routes;
2294 rc = lnet_acceptor_start();
2296 goto err_destroy_routes;
2298 the_lnet.ln_refcount = 1;
2299 /* Now I may use my own API functions... */
2301 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2303 goto err_acceptor_stop;
2305 lnet_ping_target_update(pbuf, ping_mdh);
2307 rc = lnet_router_checker_start();
2311 rc = lnet_push_target_init();
2313 goto err_stop_router_checker;
2315 rc = lnet_peer_discovery_start();
2317 goto err_destroy_push_target;
2320 lnet_router_debugfs_init();
2322 mutex_unlock(&the_lnet.ln_api_mutex);
2326 err_destroy_push_target:
2327 lnet_push_target_fini();
2328 err_stop_router_checker:
2329 lnet_router_checker_stop();
2331 lnet_ping_target_fini();
2333 the_lnet.ln_refcount = 0;
2334 lnet_acceptor_stop();
2336 if (!the_lnet.ln_nis_from_mod_params)
2337 lnet_destroy_routes();
2338 err_shutdown_lndnis:
2339 lnet_shutdown_lndnets();
2343 mutex_unlock(&the_lnet.ln_api_mutex);
2344 while (!list_empty(&net_head)) {
2345 struct lnet_net *net;
2347 net = list_entry(net_head.next, struct lnet_net, net_list);
2348 list_del_init(&net->net_list);
2353 EXPORT_SYMBOL(LNetNIInit);
2356 * Stop LNet interfaces, routing, and forwarding.
2358 * Users must call this function once for each successful call to LNetNIInit().
2359 * Once the LNetNIFini() operation has been started, the results of pending
2360 * API operations are undefined.
2362 * \return always 0 for current implementation.
2367 mutex_lock(&the_lnet.ln_api_mutex);
2369 LASSERT(the_lnet.ln_refcount > 0);
2371 if (the_lnet.ln_refcount != 1) {
2372 the_lnet.ln_refcount--;
2374 LASSERT(!the_lnet.ln_niinit_self);
2378 lnet_router_debugfs_init();
2379 lnet_peer_discovery_stop();
2380 lnet_push_target_fini();
2381 lnet_router_checker_stop();
2382 lnet_ping_target_fini();
2384 /* Teardown fns that use my own API functions BEFORE here */
2385 the_lnet.ln_refcount = 0;
2387 lnet_acceptor_stop();
2388 lnet_destroy_routes();
2389 lnet_shutdown_lndnets();
2393 mutex_unlock(&the_lnet.ln_api_mutex);
2396 EXPORT_SYMBOL(LNetNIFini);
2399 * Grabs the ni data from the ni structure and fills the out
2402 * \param[in] ni network interface structure
2403 * \param[out] cfg_ni NI config information
2404 * \param[out] tun network and LND tunables
2407 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2408 struct lnet_ioctl_config_lnd_tunables *tun,
2409 struct lnet_ioctl_element_stats *stats,
2412 size_t min_size = 0;
2415 if (!ni || !cfg_ni || !tun)
2418 if (ni->ni_interfaces[0] != NULL) {
2419 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2420 if (ni->ni_interfaces[i] != NULL) {
2421 strncpy(cfg_ni->lic_ni_intf[i],
2422 ni->ni_interfaces[i],
2423 sizeof(cfg_ni->lic_ni_intf[i]));
2428 cfg_ni->lic_nid = ni->ni_nid;
2429 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2430 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2432 cfg_ni->lic_status = ni->ni_status->ns_status;
2433 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2434 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2436 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2439 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2440 LNET_STATS_TYPE_SEND);
2441 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2442 LNET_STATS_TYPE_RECV);
2443 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2444 LNET_STATS_TYPE_DROP);
2448 * tun->lt_tun will always be present, but in order to be
2449 * backwards compatible, we need to deal with the cases when
2450 * tun->lt_tun is smaller than what the kernel has, because it
2451 * comes from an older version of a userspace program, then we'll
2452 * need to copy as much information as we have available space.
2454 min_size = tun_size - sizeof(tun->lt_cmn);
2455 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2457 /* copy over the cpts */
2458 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2459 ni->ni_cpts == NULL) {
2460 for (i = 0; i < ni->ni_ncpts; i++)
2461 cfg_ni->lic_cpts[i] = i;
2464 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2465 i < LNET_MAX_SHOW_NUM_CPT;
2467 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2469 cfg_ni->lic_ncpts = ni->ni_ncpts;
2473 * NOTE: This is a legacy function left in the code to be backwards
2474 * compatible with older userspace programs. It should eventually be
2477 * Grabs the ni data from the ni structure and fills the out
2480 * \param[in] ni network interface structure
2481 * \param[out] config config information
2484 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2485 struct lnet_ioctl_config_data *config)
2487 struct lnet_ioctl_net_config *net_config;
2488 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2489 size_t min_size, tunable_size = 0;
2495 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2499 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2500 ARRAY_SIZE(net_config->ni_interfaces));
2502 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2503 if (!ni->ni_interfaces[i])
2506 strncpy(net_config->ni_interfaces[i],
2507 ni->ni_interfaces[i],
2508 sizeof(net_config->ni_interfaces[i]));
2511 config->cfg_nid = ni->ni_nid;
2512 config->cfg_config_u.cfg_net.net_peer_timeout =
2513 ni->ni_net->net_tunables.lct_peer_timeout;
2514 config->cfg_config_u.cfg_net.net_max_tx_credits =
2515 ni->ni_net->net_tunables.lct_max_tx_credits;
2516 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2517 ni->ni_net->net_tunables.lct_peer_tx_credits;
2518 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2519 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2521 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2522 net_config->ni_status = LNET_NI_STATUS_UP;
2524 net_config->ni_status = ni->ni_status->ns_status;
2527 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2529 for (i = 0; i < num_cpts; i++)
2530 net_config->ni_cpts[i] = ni->ni_cpts[i];
2532 config->cfg_ncpts = num_cpts;
2536 * See if user land tools sent in a newer and larger version
2537 * of struct lnet_tunables than what the kernel uses.
2539 min_size = sizeof(*config) + sizeof(*net_config);
2541 if (config->cfg_hdr.ioc_len > min_size)
2542 tunable_size = config->cfg_hdr.ioc_len - min_size;
2544 /* Don't copy too much data to user space */
2545 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2546 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2548 if (lnd_cfg && min_size) {
2549 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2550 config->cfg_config_u.cfg_net.net_interface_count = 1;
2552 /* Tell user land that kernel side has less data */
2553 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2554 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2555 config->cfg_hdr.ioc_len -= min_size;
2561 lnet_get_ni_idx_locked(int idx)
2564 struct lnet_net *net;
2566 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2567 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2577 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2580 struct lnet_net *net = mynet;
2584 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2586 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2592 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2593 /* if you reached the end of the ni list and the net is
2594 * specified, then there are no more nis in that net */
2598 /* we reached the end of this net ni list. move to the
2600 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2601 /* no more nets and no more NIs. */
2604 /* get the next net */
2605 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2607 /* get the ni on it */
2608 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2614 /* there are more nis left */
2615 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2621 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2626 int idx = config->cfg_count;
2628 cpt = lnet_net_lock_current();
2630 ni = lnet_get_ni_idx_locked(idx);
2635 lnet_fill_ni_info_legacy(ni, config);
2639 lnet_net_unlock(cpt);
2644 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2645 struct lnet_ioctl_config_lnd_tunables *tun,
2646 struct lnet_ioctl_element_stats *stats,
2653 if (!cfg_ni || !tun || !stats)
2656 cpt = lnet_net_lock_current();
2658 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2663 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
2667 lnet_net_unlock(cpt);
2671 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
2680 cpt = lnet_net_lock_current();
2682 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
2685 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
2689 lnet_net_unlock(cpt);
2694 static int lnet_add_net_common(struct lnet_net *net,
2695 struct lnet_ioctl_config_lnd_tunables *tun)
2698 struct lnet_ping_buffer *pbuf;
2699 struct lnet_handle_md ping_mdh;
2701 struct lnet_remotenet *rnet;
2703 int num_acceptor_nets;
2705 lnet_net_lock(LNET_LOCK_EX);
2706 rnet = lnet_find_rnet_locked(net->net_id);
2707 lnet_net_unlock(LNET_LOCK_EX);
2709 * make sure that the net added doesn't invalidate the current
2710 * configuration LNet is keeping
2713 CERROR("Adding net %s will invalidate routing configuration\n",
2714 libcfs_net2str(net->net_id));
2720 * make sure you calculate the correct number of slots in the ping
2721 * buffer. Since the ping info is a flattened list of all the NIs,
2722 * we should allocate enough slots to accomodate the number of NIs
2723 * which will be added.
2725 * since ni hasn't been configured yet, use
2726 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
2728 net_ni_count = lnet_get_net_ni_count_pre(net);
2730 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2731 net_ni_count + lnet_get_ni_count(),
2739 memcpy(&net->net_tunables,
2740 &tun->lt_cmn, sizeof(net->net_tunables));
2742 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
2745 * before starting this network get a count of the current TCP
2746 * networks which require the acceptor thread running. If that
2747 * count is == 0 before we start up this network, then we'd want to
2748 * start up the acceptor thread after starting up this network
2750 num_acceptor_nets = lnet_count_acceptor_nets();
2752 net_id = net->net_id;
2754 rc = lnet_startup_lndnet(net,
2755 (tun) ? &tun->lt_tun : NULL);
2759 lnet_net_lock(LNET_LOCK_EX);
2760 net = lnet_get_net_locked(net_id);
2761 lnet_net_unlock(LNET_LOCK_EX);
2766 * Start the acceptor thread if this is the first network
2767 * being added that requires the thread.
2769 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
2770 rc = lnet_acceptor_start();
2772 /* shutdown the net that we just started */
2773 CERROR("Failed to start up acceptor thread\n");
2774 lnet_shutdown_lndnet(net);
2779 lnet_net_lock(LNET_LOCK_EX);
2780 lnet_peer_net_added(net);
2781 lnet_net_unlock(LNET_LOCK_EX);
2783 lnet_ping_target_update(pbuf, ping_mdh);
2788 lnet_ping_md_unlink(pbuf, &ping_mdh);
2789 lnet_ping_buffer_decref(pbuf);
2793 static int lnet_handle_legacy_ip2nets(char *ip2nets,
2794 struct lnet_ioctl_config_lnd_tunables *tun)
2796 struct lnet_net *net;
2799 struct list_head net_head;
2801 INIT_LIST_HEAD(&net_head);
2803 rc = lnet_parse_ip2nets(&nets, ip2nets);
2807 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2811 mutex_lock(&the_lnet.ln_api_mutex);
2812 while (!list_empty(&net_head)) {
2813 net = list_entry(net_head.next, struct lnet_net, net_list);
2814 list_del_init(&net->net_list);
2815 rc = lnet_add_net_common(net, tun);
2821 mutex_unlock(&the_lnet.ln_api_mutex);
2823 while (!list_empty(&net_head)) {
2824 net = list_entry(net_head.next, struct lnet_net, net_list);
2825 list_del_init(&net->net_list);
2831 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
2833 struct lnet_net *net;
2835 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2837 __u32 net_id, lnd_type;
2839 /* get the tunables if they are available */
2840 if (conf->lic_cfg_hdr.ioc_len >=
2841 sizeof(*conf) + sizeof(*tun))
2842 tun = (struct lnet_ioctl_config_lnd_tunables *)
2845 /* handle legacy ip2nets from DLC */
2846 if (conf->lic_legacy_ip2nets[0] != '\0')
2847 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
2850 net_id = LNET_NIDNET(conf->lic_nid);
2851 lnd_type = LNET_NETTYP(net_id);
2853 if (!libcfs_isknown_lnd(lnd_type)) {
2854 CERROR("No valid net and lnd information provided\n");
2858 net = lnet_net_alloc(net_id, NULL);
2862 for (i = 0; i < conf->lic_ncpts; i++) {
2863 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
2867 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
2868 conf->lic_ni_intf[0]);
2872 mutex_lock(&the_lnet.ln_api_mutex);
2874 rc = lnet_add_net_common(net, tun);
2876 mutex_unlock(&the_lnet.ln_api_mutex);
2881 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
2883 struct lnet_net *net;
2885 __u32 net_id = LNET_NIDNET(conf->lic_nid);
2886 struct lnet_ping_buffer *pbuf;
2887 struct lnet_handle_md ping_mdh;
2892 /* don't allow userspace to shutdown the LOLND */
2893 if (LNET_NETTYP(net_id) == LOLND)
2896 mutex_lock(&the_lnet.ln_api_mutex);
2900 net = lnet_get_net_locked(net_id);
2902 CERROR("net %s not found\n",
2903 libcfs_net2str(net_id));
2908 addr = LNET_NIDADDR(conf->lic_nid);
2910 /* remove the entire net */
2911 net_count = lnet_get_net_ni_count_locked(net);
2915 /* create and link a new ping info, before removing the old one */
2916 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2917 lnet_get_ni_count() - net_count,
2920 goto unlock_api_mutex;
2922 lnet_shutdown_lndnet(net);
2924 if (lnet_count_acceptor_nets() == 0)
2925 lnet_acceptor_stop();
2927 lnet_ping_target_update(pbuf, ping_mdh);
2929 goto unlock_api_mutex;
2932 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
2934 CERROR("nid %s not found\n",
2935 libcfs_nid2str(conf->lic_nid));
2940 net_count = lnet_get_net_ni_count_locked(net);
2944 /* create and link a new ping info, before removing the old one */
2945 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2946 lnet_get_ni_count() - 1, false);
2948 goto unlock_api_mutex;
2950 lnet_shutdown_lndni(ni);
2952 if (lnet_count_acceptor_nets() == 0)
2953 lnet_acceptor_stop();
2955 lnet_ping_target_update(pbuf, ping_mdh);
2957 /* check if the net is empty and remove it if it is */
2959 lnet_shutdown_lndnet(net);
2961 goto unlock_api_mutex;
2966 mutex_unlock(&the_lnet.ln_api_mutex);
2972 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
2973 * They are only expected to be called for unique networks.
2974 * That can be as a result of older DLC library
2975 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
2978 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
2980 struct lnet_net *net;
2981 struct list_head net_head;
2983 struct lnet_ioctl_config_lnd_tunables tun;
2984 char *nets = conf->cfg_config_u.cfg_net.net_intf;
2986 INIT_LIST_HEAD(&net_head);
2988 /* Create a net/ni structures for the network string */
2989 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2991 return rc == 0 ? -EINVAL : rc;
2993 mutex_lock(&the_lnet.ln_api_mutex);
2996 rc = -EINVAL; /* only add one network per call */
2997 goto out_unlock_clean;
3000 net = list_entry(net_head.next, struct lnet_net, net_list);
3001 list_del_init(&net->net_list);
3003 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3005 memset(&tun, 0, sizeof(tun));
3007 tun.lt_cmn.lct_peer_timeout =
3008 conf->cfg_config_u.cfg_net.net_peer_timeout;
3009 tun.lt_cmn.lct_peer_tx_credits =
3010 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3011 tun.lt_cmn.lct_peer_rtr_credits =
3012 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3013 tun.lt_cmn.lct_max_tx_credits =
3014 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3016 rc = lnet_add_net_common(net, &tun);
3019 mutex_unlock(&the_lnet.ln_api_mutex);
3020 while (!list_empty(&net_head)) {
3021 /* net_head list is empty in success case */
3022 net = list_entry(net_head.next, struct lnet_net, net_list);
3023 list_del_init(&net->net_list);
3030 lnet_dyn_del_net(__u32 net_id)
3032 struct lnet_net *net;
3033 struct lnet_ping_buffer *pbuf;
3034 struct lnet_handle_md ping_mdh;
3038 /* don't allow userspace to shutdown the LOLND */
3039 if (LNET_NETTYP(net_id) == LOLND)
3042 mutex_lock(&the_lnet.ln_api_mutex);
3046 net = lnet_get_net_locked(net_id);
3053 net_ni_count = lnet_get_net_ni_count_locked(net);
3057 /* create and link a new ping info, before removing the old one */
3058 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3059 lnet_get_ni_count() - net_ni_count, false);
3063 lnet_shutdown_lndnet(net);
3065 if (lnet_count_acceptor_nets() == 0)
3066 lnet_acceptor_stop();
3068 lnet_ping_target_update(pbuf, ping_mdh);
3071 mutex_unlock(&the_lnet.ln_api_mutex);
3076 void lnet_incr_dlc_seq(void)
3078 atomic_inc(&lnet_dlc_seq_no);
3081 __u32 lnet_get_dlc_seq_locked(void)
3083 return atomic_read(&lnet_dlc_seq_no);
3087 * LNet ioctl handler.
3091 LNetCtl(unsigned int cmd, void *arg)
3093 struct libcfs_ioctl_data *data = arg;
3094 struct lnet_ioctl_config_data *config;
3095 struct lnet_process_id id = {0};
3099 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3100 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3103 case IOC_LIBCFS_GET_NI:
3104 rc = LNetGetId(data->ioc_count, &id);
3105 data->ioc_nid = id.nid;
3108 case IOC_LIBCFS_FAIL_NID:
3109 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3111 case IOC_LIBCFS_ADD_ROUTE:
3114 if (config->cfg_hdr.ioc_len < sizeof(*config))
3117 mutex_lock(&the_lnet.ln_api_mutex);
3118 rc = lnet_add_route(config->cfg_net,
3119 config->cfg_config_u.cfg_route.rtr_hop,
3121 config->cfg_config_u.cfg_route.
3124 rc = lnet_check_routes();
3126 lnet_del_route(config->cfg_net,
3129 mutex_unlock(&the_lnet.ln_api_mutex);
3132 case IOC_LIBCFS_DEL_ROUTE:
3135 if (config->cfg_hdr.ioc_len < sizeof(*config))
3138 mutex_lock(&the_lnet.ln_api_mutex);
3139 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3140 mutex_unlock(&the_lnet.ln_api_mutex);
3143 case IOC_LIBCFS_GET_ROUTE:
3146 if (config->cfg_hdr.ioc_len < sizeof(*config))
3149 mutex_lock(&the_lnet.ln_api_mutex);
3150 rc = lnet_get_route(config->cfg_count,
3152 &config->cfg_config_u.cfg_route.rtr_hop,
3154 &config->cfg_config_u.cfg_route.rtr_flags,
3155 &config->cfg_config_u.cfg_route.
3157 mutex_unlock(&the_lnet.ln_api_mutex);
3160 case IOC_LIBCFS_GET_LOCAL_NI: {
3161 struct lnet_ioctl_config_ni *cfg_ni;
3162 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3163 struct lnet_ioctl_element_stats *stats;
3168 /* get the tunables if they are available */
3169 if (cfg_ni->lic_cfg_hdr.ioc_len <
3170 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3173 stats = (struct lnet_ioctl_element_stats *)
3175 tun = (struct lnet_ioctl_config_lnd_tunables *)
3176 (cfg_ni->lic_bulk + sizeof(*stats));
3178 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3181 mutex_lock(&the_lnet.ln_api_mutex);
3182 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3183 mutex_unlock(&the_lnet.ln_api_mutex);
3187 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3188 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3190 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3193 mutex_lock(&the_lnet.ln_api_mutex);
3194 rc = lnet_get_ni_stats(msg_stats);
3195 mutex_unlock(&the_lnet.ln_api_mutex);
3200 case IOC_LIBCFS_GET_NET: {
3201 size_t total = sizeof(*config) +
3202 sizeof(struct lnet_ioctl_net_config);
3205 if (config->cfg_hdr.ioc_len < total)
3208 mutex_lock(&the_lnet.ln_api_mutex);
3209 rc = lnet_get_net_config(config);
3210 mutex_unlock(&the_lnet.ln_api_mutex);
3214 case IOC_LIBCFS_GET_LNET_STATS:
3216 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3218 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3221 mutex_lock(&the_lnet.ln_api_mutex);
3222 lnet_counters_get(&lnet_stats->st_cntrs);
3223 mutex_unlock(&the_lnet.ln_api_mutex);
3227 case IOC_LIBCFS_CONFIG_RTR:
3230 if (config->cfg_hdr.ioc_len < sizeof(*config))
3233 mutex_lock(&the_lnet.ln_api_mutex);
3234 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3235 rc = lnet_rtrpools_enable();
3236 mutex_unlock(&the_lnet.ln_api_mutex);
3239 lnet_rtrpools_disable();
3240 mutex_unlock(&the_lnet.ln_api_mutex);
3243 case IOC_LIBCFS_ADD_BUF:
3246 if (config->cfg_hdr.ioc_len < sizeof(*config))
3249 mutex_lock(&the_lnet.ln_api_mutex);
3250 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3252 config->cfg_config_u.cfg_buffers.
3254 config->cfg_config_u.cfg_buffers.
3256 mutex_unlock(&the_lnet.ln_api_mutex);
3259 case IOC_LIBCFS_SET_NUMA_RANGE: {
3260 struct lnet_ioctl_set_value *numa;
3262 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3264 lnet_net_lock(LNET_LOCK_EX);
3265 lnet_numa_range = numa->sv_value;
3266 lnet_net_unlock(LNET_LOCK_EX);
3270 case IOC_LIBCFS_GET_NUMA_RANGE: {
3271 struct lnet_ioctl_set_value *numa;
3273 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3275 numa->sv_value = lnet_numa_range;
3279 case IOC_LIBCFS_GET_BUF: {
3280 struct lnet_ioctl_pool_cfg *pool_cfg;
3281 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3285 if (config->cfg_hdr.ioc_len < total)
3288 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3290 mutex_lock(&the_lnet.ln_api_mutex);
3291 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3292 mutex_unlock(&the_lnet.ln_api_mutex);
3296 case IOC_LIBCFS_ADD_PEER_NI: {
3297 struct lnet_ioctl_peer_cfg *cfg = arg;
3299 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3302 mutex_lock(&the_lnet.ln_api_mutex);
3303 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3306 mutex_unlock(&the_lnet.ln_api_mutex);
3310 case IOC_LIBCFS_DEL_PEER_NI: {
3311 struct lnet_ioctl_peer_cfg *cfg = arg;
3313 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3316 mutex_lock(&the_lnet.ln_api_mutex);
3317 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3318 cfg->prcfg_cfg_nid);
3319 mutex_unlock(&the_lnet.ln_api_mutex);
3323 case IOC_LIBCFS_GET_PEER_INFO: {
3324 struct lnet_ioctl_peer *peer_info = arg;
3326 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3329 mutex_lock(&the_lnet.ln_api_mutex);
3330 rc = lnet_get_peer_ni_info(
3331 peer_info->pr_count,
3333 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3334 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3335 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3336 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3337 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3338 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3339 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3340 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3341 mutex_unlock(&the_lnet.ln_api_mutex);
3345 case IOC_LIBCFS_GET_PEER_NI: {
3346 struct lnet_ioctl_peer_cfg *cfg = arg;
3348 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3351 mutex_lock(&the_lnet.ln_api_mutex);
3352 rc = lnet_get_peer_info(cfg,
3353 (void __user *)cfg->prcfg_bulk);
3354 mutex_unlock(&the_lnet.ln_api_mutex);
3358 case IOC_LIBCFS_GET_PEER_LIST: {
3359 struct lnet_ioctl_peer_cfg *cfg = arg;
3361 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3364 mutex_lock(&the_lnet.ln_api_mutex);
3365 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3366 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3367 mutex_unlock(&the_lnet.ln_api_mutex);
3371 case IOC_LIBCFS_NOTIFY_ROUTER: {
3372 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3374 /* The deadline passed in by the user should be some time in
3375 * seconds in the future since the UNIX epoch. We have to map
3376 * that deadline to the wall clock.
3378 deadline += ktime_get_seconds();
3379 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
3383 case IOC_LIBCFS_LNET_DIST:
3384 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3385 if (rc < 0 && rc != -EHOSTUNREACH)
3388 data->ioc_u32[0] = rc;
3391 case IOC_LIBCFS_TESTPROTOCOMPAT:
3392 lnet_net_lock(LNET_LOCK_EX);
3393 the_lnet.ln_testprotocompat = data->ioc_flags;
3394 lnet_net_unlock(LNET_LOCK_EX);
3397 case IOC_LIBCFS_LNET_FAULT:
3398 return lnet_fault_ctl(data->ioc_flags, data);
3400 case IOC_LIBCFS_PING: {
3401 signed long timeout;
3403 id.nid = data->ioc_nid;
3404 id.pid = data->ioc_u32[0];
3406 /* If timeout is negative then set default of 3 minutes */
3407 if (((s32)data->ioc_u32[1] <= 0) ||
3408 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3409 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3411 timeout = msecs_to_jiffies(data->ioc_u32[1]);
3413 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3414 data->ioc_plen1 / sizeof(struct lnet_process_id));
3419 data->ioc_count = rc;
3423 case IOC_LIBCFS_PING_PEER: {
3424 struct lnet_ioctl_ping_data *ping = arg;
3425 struct lnet_peer *lp;
3426 signed long timeout;
3428 /* If timeout is negative then set default of 3 minutes */
3429 if (((s32)ping->op_param) <= 0 ||
3430 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3431 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3433 timeout = msecs_to_jiffies(ping->op_param);
3435 rc = lnet_ping(ping->ping_id, timeout,
3441 mutex_lock(&the_lnet.ln_api_mutex);
3442 lp = lnet_find_peer(ping->ping_id.nid);
3444 ping->ping_id.nid = lp->lp_primary_nid;
3445 ping->mr_info = lnet_peer_is_multi_rail(lp);
3446 lnet_peer_decref_locked(lp);
3448 mutex_unlock(&the_lnet.ln_api_mutex);
3450 ping->ping_count = rc;
3454 case IOC_LIBCFS_DISCOVER: {
3455 struct lnet_ioctl_ping_data *discover = arg;
3456 struct lnet_peer *lp;
3458 rc = lnet_discover(discover->ping_id, discover->op_param,
3460 discover->ping_count);
3464 mutex_lock(&the_lnet.ln_api_mutex);
3465 lp = lnet_find_peer(discover->ping_id.nid);
3467 discover->ping_id.nid = lp->lp_primary_nid;
3468 discover->mr_info = lnet_peer_is_multi_rail(lp);
3469 lnet_peer_decref_locked(lp);
3471 mutex_unlock(&the_lnet.ln_api_mutex);
3473 discover->ping_count = rc;
3478 ni = lnet_net2ni_addref(data->ioc_net);
3482 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3485 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
3492 EXPORT_SYMBOL(LNetCtl);
3494 void LNetDebugPeer(struct lnet_process_id id)
3496 lnet_debug_peer(id.nid);
3498 EXPORT_SYMBOL(LNetDebugPeer);
3501 * Determine if the specified peer \a nid is on the local node.
3503 * \param nid peer nid to check
3505 * \retval true If peer NID is on the local node.
3506 * \retval false If peer NID is not on the local node.
3508 bool LNetIsPeerLocal(lnet_nid_t nid)
3510 struct lnet_net *net;
3514 cpt = lnet_net_lock_current();
3515 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3516 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3517 if (ni->ni_nid == nid) {
3518 lnet_net_unlock(cpt);
3523 lnet_net_unlock(cpt);
3527 EXPORT_SYMBOL(LNetIsPeerLocal);
3530 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
3531 * Note that all interfaces share a same PID, as requested by LNetNIInit().
3533 * \param index Index of the interface to look up.
3534 * \param id On successful return, this location will hold the
3535 * struct lnet_process_id ID of the interface.
3537 * \retval 0 If an interface exists at \a index.
3538 * \retval -ENOENT If no interface has been found.
3541 LNetGetId(unsigned int index, struct lnet_process_id *id)
3544 struct lnet_net *net;
3548 LASSERT(the_lnet.ln_refcount > 0);
3550 cpt = lnet_net_lock_current();
3552 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3553 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3557 id->nid = ni->ni_nid;
3558 id->pid = the_lnet.ln_pid;
3564 lnet_net_unlock(cpt);
3567 EXPORT_SYMBOL(LNetGetId);
3569 static int lnet_ping(struct lnet_process_id id, signed long timeout,
3570 struct lnet_process_id __user *ids, int n_ids)
3572 struct lnet_handle_eq eqh;
3573 struct lnet_handle_md mdh;
3574 struct lnet_event event;
3575 struct lnet_md md = { NULL };
3579 const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
3580 struct lnet_ping_buffer *pbuf;
3581 struct lnet_process_id tmpid;
3588 /* n_ids limit is arbitrary */
3589 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
3593 * if the user buffer has more space than the lnet_interfaces_max
3594 * then only fill it up to lnet_interfaces_max
3596 if (n_ids > lnet_interfaces_max)
3597 n_ids = lnet_interfaces_max;
3599 if (id.pid == LNET_PID_ANY)
3600 id.pid = LNET_PID_LUSTRE;
3602 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
3606 /* NB 2 events max (including any unlink event) */
3607 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
3609 CERROR("Can't allocate EQ: %d\n", rc);
3610 goto fail_ping_buffer_decref;
3613 /* initialize md content */
3614 md.start = &pbuf->pb_info;
3615 md.length = LNET_PING_INFO_SIZE(n_ids);
3616 md.threshold = 2; /* GET/REPLY */
3618 md.options = LNET_MD_TRUNCATE;
3622 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
3624 CERROR("Can't bind MD: %d\n", rc);
3628 rc = LNetGet(LNET_NID_ANY, mdh, id,
3629 LNET_RESERVED_PORTAL,
3630 LNET_PROTO_PING_MATCHBITS, 0);
3633 /* Don't CERROR; this could be deliberate! */
3634 rc2 = LNetMDUnlink(mdh);
3637 /* NB must wait for the UNLINK event below... */
3639 timeout = a_long_time;
3643 /* MUST block for unlink to complete */
3645 blocked = cfs_block_allsigs();
3647 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
3650 cfs_restore_sigs(blocked);
3652 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
3653 (rc2 <= 0) ? -1 : event.type,
3654 (rc2 <= 0) ? -1 : event.status,
3655 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
3657 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
3659 if (rc2 <= 0 || event.status != 0) {
3660 /* timeout or error */
3661 if (!replied && rc == 0)
3662 rc = (rc2 < 0) ? rc2 :
3663 (rc2 == 0) ? -ETIMEDOUT :
3667 /* Ensure completion in finite time... */
3669 /* No assertion (racing with network) */
3671 timeout = a_long_time;
3672 } else if (rc2 == 0) {
3673 /* timed out waiting for unlink */
3674 CWARN("ping %s: late network completion\n",
3677 } else if (event.type == LNET_EVENT_REPLY) {
3681 } while (rc2 <= 0 || !event.unlinked);
3685 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
3692 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
3694 rc = -EPROTO; /* if I can't parse... */
3697 CERROR("%s: ping info too short %d\n",
3698 libcfs_id2str(id), nob);
3702 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
3703 lnet_swap_pinginfo(pbuf);
3704 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
3705 CERROR("%s: Unexpected magic %08x\n",
3706 libcfs_id2str(id), pbuf->pb_info.pi_magic);
3710 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
3711 CERROR("%s: ping w/o NI status: 0x%x\n",
3712 libcfs_id2str(id), pbuf->pb_info.pi_features);
3716 if (nob < LNET_PING_INFO_SIZE(0)) {
3717 CERROR("%s: Short reply %d(%d min)\n",
3719 nob, (int)LNET_PING_INFO_SIZE(0));
3723 if (pbuf->pb_info.pi_nnis < n_ids)
3724 n_ids = pbuf->pb_info.pi_nnis;
3726 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
3727 CERROR("%s: Short reply %d(%d expected)\n",
3729 nob, (int)LNET_PING_INFO_SIZE(n_ids));
3733 rc = -EFAULT; /* if I segv in copy_to_user()... */
3735 memset(&tmpid, 0, sizeof(tmpid));
3736 for (i = 0; i < n_ids; i++) {
3737 tmpid.pid = pbuf->pb_info.pi_pid;
3738 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
3739 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
3742 rc = pbuf->pb_info.pi_nnis;
3745 rc2 = LNetEQFree(eqh);
3747 CERROR("rc2 %d\n", rc2);
3750 fail_ping_buffer_decref:
3751 lnet_ping_buffer_decref(pbuf);
3756 lnet_discover(struct lnet_process_id id, __u32 force,
3757 struct lnet_process_id __user *ids, int n_ids)
3759 struct lnet_peer_ni *lpni;
3760 struct lnet_peer_ni *p;
3761 struct lnet_peer *lp;
3762 struct lnet_process_id *buf;
3766 int max_intf = lnet_interfaces_max;
3770 id.nid == LNET_NID_ANY)
3773 if (id.pid == LNET_PID_ANY)
3774 id.pid = LNET_PID_LUSTRE;
3777 * if the user buffer has more space than the max_intf
3778 * then only fill it up to max_intf
3780 if (n_ids > max_intf)
3783 buf_size = n_ids * sizeof(*buf);
3785 LIBCFS_ALLOC(buf, buf_size);
3789 cpt = lnet_net_lock_current();
3790 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
3797 * Clearing the NIDS_UPTODATE flag ensures the peer will
3798 * be discovered, provided discovery has not been disabled.
3800 lp = lpni->lpni_peer_net->lpn_peer;
3801 spin_lock(&lp->lp_lock);
3802 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
3803 /* If the force flag is set, force a PING and PUSH as well. */
3805 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
3806 spin_unlock(&lp->lp_lock);
3807 rc = lnet_discover_peer_locked(lpni, cpt, true);
3811 /* Peer may have changed. */
3812 lp = lpni->lpni_peer_net->lpn_peer;
3813 if (lp->lp_nnis < n_ids)
3814 n_ids = lp->lp_nnis;
3818 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
3819 buf[i].pid = id.pid;
3820 buf[i].nid = p->lpni_nid;
3825 lnet_net_unlock(cpt);
3828 if (copy_to_user(ids, buf, n_ids * sizeof(*buf)))
3834 lnet_peer_ni_decref_locked(lpni);
3836 lnet_net_unlock(cpt);
3838 LIBCFS_FREE(buf, buf_size);