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");
81 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
82 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
84 static struct kernel_param_ops param_ops_interfaces_max = {
89 #define param_check_interfaces_max(name, p) \
90 __param_check(name, p, int)
92 #ifdef HAVE_KERNEL_PARAM_OPS
93 module_param(lnet_interfaces_max, interfaces_max, 0644);
95 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
96 ¶m_ops_interfaces_max, 0644);
98 MODULE_PARM_DESC(lnet_interfaces_max,
99 "Maximum number of interfaces in a node.");
101 unsigned lnet_peer_discovery_disabled = 0;
102 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
104 static struct kernel_param_ops param_ops_discovery_disabled = {
105 .set = discovery_set,
106 .get = param_get_int,
109 #define param_check_discovery_disabled(name, p) \
110 __param_check(name, p, int)
111 #ifdef HAVE_KERNEL_PARAM_OPS
112 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
114 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
115 ¶m_ops_discovery_disabled, 0644);
117 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
118 "Set to 1 to disable peer discovery on this node.");
120 unsigned lnet_transaction_timeout = 5;
121 module_param(lnet_transaction_timeout, uint, 0444);
122 MODULE_PARM_DESC(lnet_transaction_timeout,
123 "Time in seconds to wait for a REPLY or an ACK");
126 * This sequence number keeps track of how many times DLC was used to
127 * update the local NIs. It is incremented when a NI is added or
128 * removed and checked when sending a message to determine if there is
129 * a need to re-run the selection algorithm. See lnet_select_pathway()
130 * for more details on its usage.
132 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
134 static int lnet_ping(struct lnet_process_id id, signed long timeout,
135 struct lnet_process_id __user *ids, int n_ids);
137 static int lnet_discover(struct lnet_process_id id, __u32 force,
138 struct lnet_process_id __user *ids, int n_ids);
141 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
144 unsigned *discovery = (unsigned *)kp->arg;
146 struct lnet_ping_buffer *pbuf;
148 rc = kstrtoul(val, 0, &value);
150 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
154 value = (value) ? 1 : 0;
157 * The purpose of locking the api_mutex here is to ensure that
158 * the correct value ends up stored properly.
160 mutex_lock(&the_lnet.ln_api_mutex);
162 if (value == *discovery) {
163 mutex_unlock(&the_lnet.ln_api_mutex);
169 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
170 mutex_unlock(&the_lnet.ln_api_mutex);
174 /* tell peers that discovery setting has changed */
175 lnet_net_lock(LNET_LOCK_EX);
176 pbuf = the_lnet.ln_ping_target;
178 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
180 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
181 lnet_net_unlock(LNET_LOCK_EX);
183 lnet_push_update_to_peers(1);
185 mutex_unlock(&the_lnet.ln_api_mutex);
191 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
195 rc = kstrtoint(val, 0, &value);
197 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
201 if (value < LNET_INTERFACES_MIN) {
202 CWARN("max interfaces provided are too small, setting to %d\n",
203 LNET_INTERFACES_MAX_DEFAULT);
204 value = LNET_INTERFACES_MAX_DEFAULT;
207 *(int *)kp->arg = value;
213 lnet_get_routes(void)
219 lnet_get_networks(void)
224 if (*networks != 0 && *ip2nets != 0) {
225 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
226 "'ip2nets' but not both at once\n");
231 rc = lnet_parse_ip2nets(&nets, ip2nets);
232 return (rc == 0) ? nets : NULL;
242 lnet_init_locks(void)
244 spin_lock_init(&the_lnet.ln_eq_wait_lock);
245 spin_lock_init(&the_lnet.ln_msg_resend_lock);
246 init_waitqueue_head(&the_lnet.ln_eq_waitq);
247 init_waitqueue_head(&the_lnet.ln_rc_waitq);
248 mutex_init(&the_lnet.ln_lnd_mutex);
252 lnet_fini_locks(void)
256 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
257 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
261 lnet_descriptor_setup(void)
263 /* create specific kmem_cache for MEs and small MDs (i.e., originally
264 * allocated in <size-xxx> kmem_cache).
266 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
268 if (!lnet_mes_cachep)
271 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
272 LNET_SMALL_MD_SIZE, 0, 0,
274 if (!lnet_small_mds_cachep)
281 lnet_descriptor_cleanup(void)
284 if (lnet_small_mds_cachep) {
285 kmem_cache_destroy(lnet_small_mds_cachep);
286 lnet_small_mds_cachep = NULL;
289 if (lnet_mes_cachep) {
290 kmem_cache_destroy(lnet_mes_cachep);
291 lnet_mes_cachep = NULL;
296 lnet_create_remote_nets_table(void)
299 struct list_head *hash;
301 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
302 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
303 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
305 CERROR("Failed to create remote nets hash table\n");
309 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
310 INIT_LIST_HEAD(&hash[i]);
311 the_lnet.ln_remote_nets_hash = hash;
316 lnet_destroy_remote_nets_table(void)
320 if (the_lnet.ln_remote_nets_hash == NULL)
323 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
324 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
326 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
327 LNET_REMOTE_NETS_HASH_SIZE *
328 sizeof(the_lnet.ln_remote_nets_hash[0]));
329 the_lnet.ln_remote_nets_hash = NULL;
333 lnet_destroy_locks(void)
335 if (the_lnet.ln_res_lock != NULL) {
336 cfs_percpt_lock_free(the_lnet.ln_res_lock);
337 the_lnet.ln_res_lock = NULL;
340 if (the_lnet.ln_net_lock != NULL) {
341 cfs_percpt_lock_free(the_lnet.ln_net_lock);
342 the_lnet.ln_net_lock = NULL;
349 lnet_create_locks(void)
353 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
354 if (the_lnet.ln_res_lock == NULL)
357 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
358 if (the_lnet.ln_net_lock == NULL)
364 lnet_destroy_locks();
368 static void lnet_assert_wire_constants(void)
370 /* Wire protocol assertions generated by 'wirecheck'
371 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
372 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
373 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
376 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
377 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
378 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
379 CLASSERT(LNET_MSG_ACK == 0);
380 CLASSERT(LNET_MSG_PUT == 1);
381 CLASSERT(LNET_MSG_GET == 2);
382 CLASSERT(LNET_MSG_REPLY == 3);
383 CLASSERT(LNET_MSG_HELLO == 4);
385 /* Checks for struct lnet_handle_wire */
386 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
387 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
388 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
389 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
390 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
392 /* Checks for struct struct lnet_magicversion */
393 CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
394 CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
395 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
396 CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
397 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
398 CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
399 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
401 /* Checks for struct struct lnet_hdr */
402 CLASSERT((int)sizeof(struct lnet_hdr) == 72);
403 CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
404 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
405 CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
406 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
407 CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
408 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
409 CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
410 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
411 CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
412 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
413 CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
414 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
415 CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
416 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
419 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
420 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
421 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
422 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
423 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
424 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
427 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
428 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
429 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
430 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
431 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
432 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
433 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
434 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
435 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
436 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
439 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
440 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
441 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
442 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
443 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
444 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
445 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
446 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
447 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
448 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
451 CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
452 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
455 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
456 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
457 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
458 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
460 /* Checks for struct lnet_ni_status and related constants */
461 CLASSERT(LNET_NI_STATUS_INVALID == 0x00000000);
462 CLASSERT(LNET_NI_STATUS_UP == 0x15aac0de);
463 CLASSERT(LNET_NI_STATUS_DOWN == 0xdeadface);
465 /* Checks for struct lnet_ni_status */
466 CLASSERT((int)sizeof(struct lnet_ni_status) == 16);
467 CLASSERT((int)offsetof(struct lnet_ni_status, ns_nid) == 0);
468 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) == 8);
469 CLASSERT((int)offsetof(struct lnet_ni_status, ns_status) == 8);
470 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_status) == 4);
471 CLASSERT((int)offsetof(struct lnet_ni_status, ns_unused) == 12);
472 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) == 4);
474 /* Checks for struct lnet_ping_info and related constants */
475 CLASSERT(LNET_PROTO_PING_MAGIC == 0x70696E67);
476 CLASSERT(LNET_PING_FEAT_INVAL == 0);
477 CLASSERT(LNET_PING_FEAT_BASE == 1);
478 CLASSERT(LNET_PING_FEAT_NI_STATUS == 2);
479 CLASSERT(LNET_PING_FEAT_RTE_DISABLED == 4);
480 CLASSERT(LNET_PING_FEAT_MULTI_RAIL == 8);
481 CLASSERT(LNET_PING_FEAT_DISCOVERY == 16);
482 CLASSERT(LNET_PING_FEAT_BITS == 31);
484 /* Checks for struct lnet_ping_info */
485 CLASSERT((int)sizeof(struct lnet_ping_info) == 16);
486 CLASSERT((int)offsetof(struct lnet_ping_info, pi_magic) == 0);
487 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) == 4);
488 CLASSERT((int)offsetof(struct lnet_ping_info, pi_features) == 4);
489 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_features) == 4);
490 CLASSERT((int)offsetof(struct lnet_ping_info, pi_pid) == 8);
491 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) == 4);
492 CLASSERT((int)offsetof(struct lnet_ping_info, pi_nnis) == 12);
493 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) == 4);
494 CLASSERT((int)offsetof(struct lnet_ping_info, pi_ni) == 16);
495 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) == 0);
498 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
500 struct lnet_lnd *lnd;
501 struct list_head *tmp;
503 /* holding lnd mutex */
504 list_for_each(tmp, &the_lnet.ln_lnds) {
505 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
507 if (lnd->lnd_type == type)
514 lnet_register_lnd(struct lnet_lnd *lnd)
516 mutex_lock(&the_lnet.ln_lnd_mutex);
518 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
519 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
521 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
522 lnd->lnd_refcount = 0;
524 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
526 mutex_unlock(&the_lnet.ln_lnd_mutex);
528 EXPORT_SYMBOL(lnet_register_lnd);
531 lnet_unregister_lnd(struct lnet_lnd *lnd)
533 mutex_lock(&the_lnet.ln_lnd_mutex);
535 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
536 LASSERT(lnd->lnd_refcount == 0);
538 list_del(&lnd->lnd_list);
539 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
541 mutex_unlock(&the_lnet.ln_lnd_mutex);
543 EXPORT_SYMBOL(lnet_unregister_lnd);
546 lnet_counters_get(struct lnet_counters *counters)
548 struct lnet_counters *ctr;
551 memset(counters, 0, sizeof(*counters));
553 lnet_net_lock(LNET_LOCK_EX);
555 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
556 counters->msgs_max += ctr->msgs_max;
557 counters->msgs_alloc += ctr->msgs_alloc;
558 counters->errors += ctr->errors;
559 counters->send_count += ctr->send_count;
560 counters->recv_count += ctr->recv_count;
561 counters->route_count += ctr->route_count;
562 counters->drop_count += ctr->drop_count;
563 counters->send_length += ctr->send_length;
564 counters->recv_length += ctr->recv_length;
565 counters->route_length += ctr->route_length;
566 counters->drop_length += ctr->drop_length;
569 lnet_net_unlock(LNET_LOCK_EX);
571 EXPORT_SYMBOL(lnet_counters_get);
574 lnet_counters_reset(void)
576 struct lnet_counters *counters;
579 lnet_net_lock(LNET_LOCK_EX);
581 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
582 memset(counters, 0, sizeof(struct lnet_counters));
584 lnet_net_unlock(LNET_LOCK_EX);
588 lnet_res_type2str(int type)
593 case LNET_COOKIE_TYPE_MD:
595 case LNET_COOKIE_TYPE_ME:
597 case LNET_COOKIE_TYPE_EQ:
603 lnet_res_container_cleanup(struct lnet_res_container *rec)
607 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
610 while (!list_empty(&rec->rec_active)) {
611 struct list_head *e = rec->rec_active.next;
614 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
615 lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
617 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
618 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
620 } else { /* NB: Active MEs should be attached on portals */
627 /* Found alive MD/ME/EQ, user really should unlink/free
628 * all of them before finalize LNet, but if someone didn't,
629 * we have to recycle garbage for him */
630 CERROR("%d active elements on exit of %s container\n",
631 count, lnet_res_type2str(rec->rec_type));
634 if (rec->rec_lh_hash != NULL) {
635 LIBCFS_FREE(rec->rec_lh_hash,
636 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
637 rec->rec_lh_hash = NULL;
640 rec->rec_type = 0; /* mark it as finalized */
644 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
649 LASSERT(rec->rec_type == 0);
651 rec->rec_type = type;
652 INIT_LIST_HEAD(&rec->rec_active);
654 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
656 /* Arbitrary choice of hash table size */
657 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
658 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
659 if (rec->rec_lh_hash == NULL) {
664 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
665 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
670 CERROR("Failed to setup %s resource container\n",
671 lnet_res_type2str(type));
672 lnet_res_container_cleanup(rec);
677 lnet_res_containers_destroy(struct lnet_res_container **recs)
679 struct lnet_res_container *rec;
682 cfs_percpt_for_each(rec, i, recs)
683 lnet_res_container_cleanup(rec);
685 cfs_percpt_free(recs);
688 static struct lnet_res_container **
689 lnet_res_containers_create(int type)
691 struct lnet_res_container **recs;
692 struct lnet_res_container *rec;
696 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
698 CERROR("Failed to allocate %s resource containers\n",
699 lnet_res_type2str(type));
703 cfs_percpt_for_each(rec, i, recs) {
704 rc = lnet_res_container_setup(rec, i, type);
706 lnet_res_containers_destroy(recs);
714 struct lnet_libhandle *
715 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
717 /* ALWAYS called with lnet_res_lock held */
718 struct list_head *head;
719 struct lnet_libhandle *lh;
722 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
725 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
726 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
728 list_for_each_entry(lh, head, lh_hash_chain) {
729 if (lh->lh_cookie == cookie)
737 lnet_res_lh_initialize(struct lnet_res_container *rec,
738 struct lnet_libhandle *lh)
740 /* ALWAYS called with lnet_res_lock held */
741 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
744 lh->lh_cookie = rec->rec_lh_cookie;
745 rec->rec_lh_cookie += 1 << ibits;
747 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
749 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
752 static int lnet_unprepare(void);
755 lnet_prepare(lnet_pid_t requested_pid)
757 /* Prepare to bring up the network */
758 struct lnet_res_container **recs;
761 if (requested_pid == LNET_PID_ANY) {
762 /* Don't instantiate LNET just for me */
766 LASSERT(the_lnet.ln_refcount == 0);
768 the_lnet.ln_routing = 0;
770 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
771 the_lnet.ln_pid = requested_pid;
773 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
774 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
775 INIT_LIST_HEAD(&the_lnet.ln_nets);
776 INIT_LIST_HEAD(&the_lnet.ln_routers);
777 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
778 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
779 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
780 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
781 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
782 init_waitqueue_head(&the_lnet.ln_dc_waitq);
784 rc = lnet_descriptor_setup();
788 rc = lnet_create_remote_nets_table();
793 * NB the interface cookie in wire handles guards against delayed
794 * replies and ACKs appearing valid after reboot.
796 the_lnet.ln_interface_cookie = ktime_get_real_ns();
798 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
799 sizeof(struct lnet_counters));
800 if (the_lnet.ln_counters == NULL) {
801 CERROR("Failed to allocate counters for LNet\n");
806 rc = lnet_peer_tables_create();
810 rc = lnet_msg_containers_create();
814 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
815 LNET_COOKIE_TYPE_EQ);
819 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
825 the_lnet.ln_me_containers = recs;
827 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
833 the_lnet.ln_md_containers = recs;
835 rc = lnet_portals_create();
837 CERROR("Failed to create portals for LNet: %d\n", rc);
849 lnet_unprepare (void)
851 /* NB no LNET_LOCK since this is the last reference. All LND instances
852 * have shut down already, so it is safe to unlink and free all
853 * descriptors, even those that appear committed to a network op (eg MD
854 * with non-zero pending count) */
856 lnet_fail_nid(LNET_NID_ANY, 0);
858 LASSERT(the_lnet.ln_refcount == 0);
859 LASSERT(list_empty(&the_lnet.ln_test_peers));
860 LASSERT(list_empty(&the_lnet.ln_nets));
862 lnet_portals_destroy();
864 if (the_lnet.ln_md_containers != NULL) {
865 lnet_res_containers_destroy(the_lnet.ln_md_containers);
866 the_lnet.ln_md_containers = NULL;
869 if (the_lnet.ln_me_containers != NULL) {
870 lnet_res_containers_destroy(the_lnet.ln_me_containers);
871 the_lnet.ln_me_containers = NULL;
874 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
876 lnet_msg_containers_destroy();
878 lnet_rtrpools_free(0);
880 if (the_lnet.ln_counters != NULL) {
881 cfs_percpt_free(the_lnet.ln_counters);
882 the_lnet.ln_counters = NULL;
884 lnet_destroy_remote_nets_table();
885 lnet_descriptor_cleanup();
891 lnet_net2ni_locked(__u32 net_id, int cpt)
894 struct lnet_net *net;
896 LASSERT(cpt != LNET_LOCK_EX);
898 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
899 if (net->net_id == net_id) {
900 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
910 lnet_net2ni_addref(__u32 net)
915 ni = lnet_net2ni_locked(net, 0);
917 lnet_ni_addref_locked(ni, 0);
922 EXPORT_SYMBOL(lnet_net2ni_addref);
925 lnet_get_net_locked(__u32 net_id)
927 struct lnet_net *net;
929 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
930 if (net->net_id == net_id)
938 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
943 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
948 val = hash_long(key, LNET_CPT_BITS);
949 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
953 return (unsigned int)(key + val + (val >> 1)) % number;
957 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
959 struct lnet_net *net;
961 /* must called with hold of lnet_net_lock */
962 if (LNET_CPT_NUMBER == 1)
963 return 0; /* the only one */
966 * If NI is provided then use the CPT identified in the NI cpt
967 * list if one exists. If one doesn't exist, then that NI is
968 * associated with all CPTs and it follows that the net it belongs
969 * to is implicitly associated with all CPTs, so just hash the nid
973 if (ni->ni_cpts != NULL)
974 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
977 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
980 /* no NI provided so look at the net */
981 net = lnet_get_net_locked(LNET_NIDNET(nid));
983 if (net != NULL && net->net_cpts != NULL) {
984 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
987 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
991 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
996 if (LNET_CPT_NUMBER == 1)
997 return 0; /* the only one */
999 cpt = lnet_net_lock_current();
1001 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1003 lnet_net_unlock(cpt);
1007 EXPORT_SYMBOL(lnet_cpt_of_nid);
1010 lnet_islocalnet(__u32 net_id)
1012 struct lnet_net *net;
1016 cpt = lnet_net_lock_current();
1018 net = lnet_get_net_locked(net_id);
1020 local = net != NULL;
1022 lnet_net_unlock(cpt);
1028 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
1030 if (ni->ni_state == LNET_NI_STATE_ACTIVE ||
1031 ni->ni_state == LNET_NI_STATE_DEGRADED)
1038 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1040 struct lnet_net *net;
1043 LASSERT(cpt != LNET_LOCK_EX);
1045 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1046 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1047 if (ni->ni_nid == nid)
1056 lnet_nid2ni_addref(lnet_nid_t nid)
1061 ni = lnet_nid2ni_locked(nid, 0);
1063 lnet_ni_addref_locked(ni, 0);
1068 EXPORT_SYMBOL(lnet_nid2ni_addref);
1071 lnet_islocalnid(lnet_nid_t nid)
1076 cpt = lnet_net_lock_current();
1077 ni = lnet_nid2ni_locked(nid, cpt);
1078 lnet_net_unlock(cpt);
1084 lnet_count_acceptor_nets(void)
1086 /* Return the # of NIs that need the acceptor. */
1088 struct lnet_net *net;
1091 cpt = lnet_net_lock_current();
1092 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1093 /* all socklnd type networks should have the acceptor
1095 if (net->net_lnd->lnd_accept != NULL)
1099 lnet_net_unlock(cpt);
1104 struct lnet_ping_buffer *
1105 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1107 struct lnet_ping_buffer *pbuf;
1109 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1111 pbuf->pb_nnis = nnis;
1112 atomic_set(&pbuf->pb_refcnt, 1);
1119 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1121 LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1122 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1125 static struct lnet_ping_buffer *
1126 lnet_ping_target_create(int nnis)
1128 struct lnet_ping_buffer *pbuf;
1130 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1132 CERROR("Can't allocate ping source [%d]\n", nnis);
1136 pbuf->pb_info.pi_nnis = nnis;
1137 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1138 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1139 pbuf->pb_info.pi_features =
1140 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1146 lnet_get_net_ni_count_locked(struct lnet_net *net)
1151 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1158 lnet_get_net_ni_count_pre(struct lnet_net *net)
1163 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1170 lnet_get_ni_count(void)
1173 struct lnet_net *net;
1178 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1179 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1189 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1193 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1195 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1197 /* Loopback is guaranteed to be present */
1198 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1200 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1206 lnet_ping_target_destroy(void)
1208 struct lnet_net *net;
1211 lnet_net_lock(LNET_LOCK_EX);
1213 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1214 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1216 ni->ni_status = NULL;
1221 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1222 the_lnet.ln_ping_target = NULL;
1224 lnet_net_unlock(LNET_LOCK_EX);
1228 lnet_ping_target_event_handler(struct lnet_event *event)
1230 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1232 if (event->unlinked)
1233 lnet_ping_buffer_decref(pbuf);
1237 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1238 struct lnet_handle_md *ping_mdh,
1239 int ni_count, bool set_eq)
1241 struct lnet_process_id id = {
1242 .nid = LNET_NID_ANY,
1245 struct lnet_handle_me me_handle;
1246 struct lnet_md md = { NULL };
1250 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1251 &the_lnet.ln_ping_target_eq);
1253 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1258 *ppbuf = lnet_ping_target_create(ni_count);
1259 if (*ppbuf == NULL) {
1264 /* Ping target ME/MD */
1265 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1266 LNET_PROTO_PING_MATCHBITS, 0,
1267 LNET_UNLINK, LNET_INS_AFTER,
1270 CERROR("Can't create ping target ME: %d\n", rc);
1271 goto fail_decref_ping_buffer;
1274 /* initialize md content */
1275 md.start = &(*ppbuf)->pb_info;
1276 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1277 md.threshold = LNET_MD_THRESH_INF;
1279 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1280 LNET_MD_MANAGE_REMOTE;
1281 md.eq_handle = the_lnet.ln_ping_target_eq;
1282 md.user_ptr = *ppbuf;
1284 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, ping_mdh);
1286 CERROR("Can't attach ping target MD: %d\n", rc);
1287 goto fail_unlink_ping_me;
1289 lnet_ping_buffer_addref(*ppbuf);
1293 fail_unlink_ping_me:
1294 rc2 = LNetMEUnlink(me_handle);
1296 fail_decref_ping_buffer:
1297 LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1298 lnet_ping_buffer_decref(*ppbuf);
1302 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1309 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1310 struct lnet_handle_md *ping_mdh)
1312 sigset_t blocked = cfs_block_allsigs();
1314 LNetMDUnlink(*ping_mdh);
1315 LNetInvalidateMDHandle(ping_mdh);
1317 /* NB the MD could be busy; this just starts the unlink */
1318 while (lnet_ping_buffer_numref(pbuf) > 1) {
1319 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1320 set_current_state(TASK_UNINTERRUPTIBLE);
1321 schedule_timeout(cfs_time_seconds(1));
1324 cfs_restore_sigs(blocked);
1328 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1331 struct lnet_net *net;
1332 struct lnet_ni_status *ns;
1337 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1338 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1339 LASSERT(i < pbuf->pb_nnis);
1341 ns = &pbuf->pb_info.pi_ni[i];
1343 ns->ns_nid = ni->ni_nid;
1346 ns->ns_status = (ni->ni_status != NULL) ?
1347 ni->ni_status->ns_status :
1356 * We (ab)use the ns_status of the loopback interface to
1357 * transmit the sequence number. The first interface listed
1358 * must be the loopback interface.
1360 rc = lnet_ping_info_validate(&pbuf->pb_info);
1362 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1365 LNET_PING_BUFFER_SEQNO(pbuf) =
1366 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1370 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1371 struct lnet_handle_md ping_mdh)
1373 struct lnet_ping_buffer *old_pbuf = NULL;
1374 struct lnet_handle_md old_ping_md;
1376 /* switch the NIs to point to the new ping info created */
1377 lnet_net_lock(LNET_LOCK_EX);
1379 if (!the_lnet.ln_routing)
1380 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1381 if (!lnet_peer_discovery_disabled)
1382 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1384 /* Ensure only known feature bits have been set. */
1385 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1386 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1388 lnet_ping_target_install_locked(pbuf);
1390 if (the_lnet.ln_ping_target) {
1391 old_pbuf = the_lnet.ln_ping_target;
1392 old_ping_md = the_lnet.ln_ping_target_md;
1394 the_lnet.ln_ping_target_md = ping_mdh;
1395 the_lnet.ln_ping_target = pbuf;
1397 lnet_net_unlock(LNET_LOCK_EX);
1400 /* unlink and free the old ping info */
1401 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1402 lnet_ping_buffer_decref(old_pbuf);
1405 lnet_push_update_to_peers(0);
1409 lnet_ping_target_fini(void)
1413 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1414 &the_lnet.ln_ping_target_md);
1416 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1419 lnet_ping_target_destroy();
1422 /* Resize the push target. */
1423 int lnet_push_target_resize(void)
1425 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1426 struct lnet_md md = { NULL };
1427 struct lnet_handle_me meh;
1428 struct lnet_handle_md mdh;
1429 struct lnet_handle_md old_mdh;
1430 struct lnet_ping_buffer *pbuf;
1431 struct lnet_ping_buffer *old_pbuf;
1432 int nnis = the_lnet.ln_push_target_nnis;
1440 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1446 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1447 LNET_PROTO_PING_MATCHBITS, 0,
1448 LNET_UNLINK, LNET_INS_AFTER,
1451 CERROR("Can't create push target ME: %d\n", rc);
1452 goto fail_decref_pbuf;
1455 /* initialize md content */
1456 md.start = &pbuf->pb_info;
1457 md.length = LNET_PING_INFO_SIZE(nnis);
1458 md.threshold = LNET_MD_THRESH_INF;
1460 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1461 LNET_MD_MANAGE_REMOTE;
1463 md.eq_handle = the_lnet.ln_push_target_eq;
1465 rc = LNetMDAttach(meh, md, LNET_RETAIN, &mdh);
1467 CERROR("Can't attach push MD: %d\n", rc);
1468 goto fail_unlink_meh;
1470 lnet_ping_buffer_addref(pbuf);
1472 lnet_net_lock(LNET_LOCK_EX);
1473 old_pbuf = the_lnet.ln_push_target;
1474 old_mdh = the_lnet.ln_push_target_md;
1475 the_lnet.ln_push_target = pbuf;
1476 the_lnet.ln_push_target_md = mdh;
1477 lnet_net_unlock(LNET_LOCK_EX);
1480 LNetMDUnlink(old_mdh);
1481 lnet_ping_buffer_decref(old_pbuf);
1484 if (nnis < the_lnet.ln_push_target_nnis)
1487 CDEBUG(D_NET, "nnis %d success\n", nnis);
1494 lnet_ping_buffer_decref(pbuf);
1496 CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1500 static void lnet_push_target_event_handler(struct lnet_event *ev)
1502 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1504 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1505 lnet_swap_pinginfo(pbuf);
1507 lnet_peer_push_event(ev);
1509 lnet_ping_buffer_decref(pbuf);
1512 /* Initialize the push target. */
1513 static int lnet_push_target_init(void)
1517 if (the_lnet.ln_push_target)
1520 rc = LNetEQAlloc(0, lnet_push_target_event_handler,
1521 &the_lnet.ln_push_target_eq);
1523 CERROR("Can't allocated push target EQ: %d\n", rc);
1527 /* Start at the required minimum, we'll enlarge if required. */
1528 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1530 rc = lnet_push_target_resize();
1533 LNetEQFree(the_lnet.ln_push_target_eq);
1534 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1540 /* Clean up the push target. */
1541 static void lnet_push_target_fini(void)
1543 if (!the_lnet.ln_push_target)
1546 /* Unlink and invalidate to prevent new references. */
1547 LNetMDUnlink(the_lnet.ln_push_target_md);
1548 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1550 /* Wait for the unlink to complete. */
1551 while (lnet_ping_buffer_numref(the_lnet.ln_push_target) > 1) {
1552 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1553 set_current_state(TASK_UNINTERRUPTIBLE);
1554 schedule_timeout(cfs_time_seconds(1));
1557 lnet_ping_buffer_decref(the_lnet.ln_push_target);
1558 the_lnet.ln_push_target = NULL;
1559 the_lnet.ln_push_target_nnis = 0;
1561 LNetEQFree(the_lnet.ln_push_target_eq);
1562 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1566 lnet_ni_tq_credits(struct lnet_ni *ni)
1570 LASSERT(ni->ni_ncpts >= 1);
1572 if (ni->ni_ncpts == 1)
1573 return ni->ni_net->net_tunables.lct_max_tx_credits;
1575 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1576 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1577 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1583 lnet_ni_unlink_locked(struct lnet_ni *ni)
1585 if (!list_empty(&ni->ni_cptlist)) {
1586 list_del_init(&ni->ni_cptlist);
1587 lnet_ni_decref_locked(ni, 0);
1590 /* move it to zombie list and nobody can find it anymore */
1591 LASSERT(!list_empty(&ni->ni_netlist));
1592 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1593 lnet_ni_decref_locked(ni, 0);
1597 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1602 struct list_head *zombie_list = &net->net_ni_zombie;
1605 * Now wait for the NIs I just nuked to show up on the zombie
1606 * list and shut them down in guaranteed thread context
1609 while (!list_empty(zombie_list)) {
1613 ni = list_entry(zombie_list->next,
1614 struct lnet_ni, ni_netlist);
1615 list_del_init(&ni->ni_netlist);
1616 /* the ni should be in deleting state. If it's not it's
1618 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
1619 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1622 /* still busy, add it back to zombie list */
1623 list_add(&ni->ni_netlist, zombie_list);
1627 if (!list_empty(&ni->ni_netlist)) {
1628 lnet_net_unlock(LNET_LOCK_EX);
1630 if ((i & (-i)) == i) {
1632 "Waiting for zombie LNI %s\n",
1633 libcfs_nid2str(ni->ni_nid));
1635 set_current_state(TASK_UNINTERRUPTIBLE);
1636 schedule_timeout(cfs_time_seconds(1));
1637 lnet_net_lock(LNET_LOCK_EX);
1641 lnet_net_unlock(LNET_LOCK_EX);
1643 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1645 LASSERT(!in_interrupt());
1646 (net->net_lnd->lnd_shutdown)(ni);
1649 CDEBUG(D_LNI, "Removed LNI %s\n",
1650 libcfs_nid2str(ni->ni_nid));
1654 lnet_net_lock(LNET_LOCK_EX);
1658 /* shutdown down the NI and release refcount */
1660 lnet_shutdown_lndni(struct lnet_ni *ni)
1663 struct lnet_net *net = ni->ni_net;
1665 lnet_net_lock(LNET_LOCK_EX);
1666 ni->ni_state = LNET_NI_STATE_DELETING;
1667 lnet_ni_unlink_locked(ni);
1668 lnet_incr_dlc_seq();
1669 lnet_net_unlock(LNET_LOCK_EX);
1671 /* clear messages for this NI on the lazy portal */
1672 for (i = 0; i < the_lnet.ln_nportals; i++)
1673 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1675 lnet_net_lock(LNET_LOCK_EX);
1676 lnet_clear_zombies_nis_locked(net);
1677 lnet_net_unlock(LNET_LOCK_EX);
1681 lnet_shutdown_lndnet(struct lnet_net *net)
1685 lnet_net_lock(LNET_LOCK_EX);
1687 net->net_state = LNET_NET_STATE_DELETING;
1689 list_del_init(&net->net_list);
1691 while (!list_empty(&net->net_ni_list)) {
1692 ni = list_entry(net->net_ni_list.next,
1693 struct lnet_ni, ni_netlist);
1694 lnet_net_unlock(LNET_LOCK_EX);
1695 lnet_shutdown_lndni(ni);
1696 lnet_net_lock(LNET_LOCK_EX);
1699 lnet_net_unlock(LNET_LOCK_EX);
1701 /* Do peer table cleanup for this net */
1702 lnet_peer_tables_cleanup(net);
1704 lnet_net_lock(LNET_LOCK_EX);
1706 * decrement ref count on lnd only when the entire network goes
1709 net->net_lnd->lnd_refcount--;
1711 lnet_net_unlock(LNET_LOCK_EX);
1717 lnet_shutdown_lndnets(void)
1719 struct lnet_net *net;
1720 struct list_head resend;
1721 struct lnet_msg *msg, *tmp;
1723 INIT_LIST_HEAD(&resend);
1725 /* NB called holding the global mutex */
1727 /* All quiet on the API front */
1728 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
1729 LASSERT(the_lnet.ln_refcount == 0);
1731 lnet_net_lock(LNET_LOCK_EX);
1732 the_lnet.ln_state = LNET_STATE_STOPPING;
1734 while (!list_empty(&the_lnet.ln_nets)) {
1736 * move the nets to the zombie list to avoid them being
1737 * picked up for new work. LONET is also included in the
1738 * Nets that will be moved to the zombie list
1740 net = list_entry(the_lnet.ln_nets.next,
1741 struct lnet_net, net_list);
1742 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1745 /* Drop the cached loopback Net. */
1746 if (the_lnet.ln_loni != NULL) {
1747 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1748 the_lnet.ln_loni = NULL;
1750 lnet_net_unlock(LNET_LOCK_EX);
1752 /* iterate through the net zombie list and delete each net */
1753 while (!list_empty(&the_lnet.ln_net_zombie)) {
1754 net = list_entry(the_lnet.ln_net_zombie.next,
1755 struct lnet_net, net_list);
1756 lnet_shutdown_lndnet(net);
1759 spin_lock(&the_lnet.ln_msg_resend_lock);
1760 list_splice(&the_lnet.ln_msg_resend, &resend);
1761 spin_unlock(&the_lnet.ln_msg_resend_lock);
1763 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
1764 list_del_init(&msg->msg_list);
1765 lnet_finalize(msg, -ECANCELED);
1768 lnet_net_lock(LNET_LOCK_EX);
1769 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
1770 lnet_net_unlock(LNET_LOCK_EX);
1774 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1777 struct lnet_tx_queue *tq;
1779 struct lnet_net *net = ni->ni_net;
1781 mutex_lock(&the_lnet.ln_lnd_mutex);
1784 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1785 ni->ni_lnd_tunables_set = true;
1788 rc = (net->net_lnd->lnd_startup)(ni);
1790 mutex_unlock(&the_lnet.ln_lnd_mutex);
1793 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1794 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1795 lnet_net_lock(LNET_LOCK_EX);
1796 net->net_lnd->lnd_refcount--;
1797 lnet_net_unlock(LNET_LOCK_EX);
1801 ni->ni_state = LNET_NI_STATE_ACTIVE;
1803 /* We keep a reference on the loopback net through the loopback NI */
1804 if (net->net_lnd->lnd_type == LOLND) {
1806 LASSERT(the_lnet.ln_loni == NULL);
1807 the_lnet.ln_loni = ni;
1808 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
1809 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
1810 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
1811 ni->ni_net->net_tunables.lct_peer_timeout = 0;
1815 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
1816 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
1817 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1818 libcfs_lnd2str(net->net_lnd->lnd_type),
1819 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
1821 /* shutdown the NI since if we get here then it must've already
1824 lnet_shutdown_lndni(ni);
1828 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1829 tq->tq_credits_min =
1830 tq->tq_credits_max =
1831 tq->tq_credits = lnet_ni_tq_credits(ni);
1834 atomic_set(&ni->ni_tx_credits,
1835 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
1836 atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
1838 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1839 libcfs_nid2str(ni->ni_nid),
1840 ni->ni_net->net_tunables.lct_peer_tx_credits,
1841 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1842 ni->ni_net->net_tunables.lct_peer_rtr_credits,
1843 ni->ni_net->net_tunables.lct_peer_timeout);
1852 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
1855 struct lnet_net *net_l = NULL;
1856 struct list_head local_ni_list;
1860 struct lnet_lnd *lnd;
1862 net->net_tunables.lct_peer_timeout;
1864 net->net_tunables.lct_max_tx_credits;
1865 int peerrtrcredits =
1866 net->net_tunables.lct_peer_rtr_credits;
1868 INIT_LIST_HEAD(&local_ni_list);
1871 * make sure that this net is unique. If it isn't then
1872 * we are adding interfaces to an already existing network, and
1873 * 'net' is just a convenient way to pass in the list.
1874 * if it is unique we need to find the LND and load it if
1877 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
1878 lnd_type = LNET_NETTYP(net->net_id);
1880 mutex_lock(&the_lnet.ln_lnd_mutex);
1881 lnd = lnet_find_lnd_by_type(lnd_type);
1884 mutex_unlock(&the_lnet.ln_lnd_mutex);
1885 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1886 mutex_lock(&the_lnet.ln_lnd_mutex);
1888 lnd = lnet_find_lnd_by_type(lnd_type);
1890 mutex_unlock(&the_lnet.ln_lnd_mutex);
1891 CERROR("Can't load LND %s, module %s, rc=%d\n",
1892 libcfs_lnd2str(lnd_type),
1893 libcfs_lnd2modname(lnd_type), rc);
1894 #ifndef HAVE_MODULE_LOADING_SUPPORT
1895 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1896 "compiled with kernel module "
1897 "loading support.");
1904 lnet_net_lock(LNET_LOCK_EX);
1905 lnd->lnd_refcount++;
1906 lnet_net_unlock(LNET_LOCK_EX);
1910 mutex_unlock(&the_lnet.ln_lnd_mutex);
1916 * net_l: if the network being added is unique then net_l
1917 * will point to that network
1918 * if the network being added is not unique then
1919 * net_l points to the existing network.
1921 * When we enter the loop below, we'll pick NIs off he
1922 * network beign added and start them up, then add them to
1923 * a local ni list. Once we've successfully started all
1924 * the NIs then we join the local NI list (of started up
1925 * networks) with the net_l->net_ni_list, which should
1926 * point to the correct network to add the new ni list to
1928 * If any of the new NIs fail to start up, then we want to
1929 * iterate through the local ni list, which should include
1930 * any NIs which were successfully started up, and shut
1933 * After than we want to delete the network being added,
1934 * to avoid a memory leak.
1938 * When a network uses TCP bonding then all its interfaces
1939 * must be specified when the network is first defined: the
1940 * TCP bonding code doesn't allow for interfaces to be added
1943 if (net_l != net && net_l != NULL && use_tcp_bonding &&
1944 LNET_NETTYP(net_l->net_id) == SOCKLND) {
1949 while (!list_empty(&net->net_ni_added)) {
1950 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
1952 list_del_init(&ni->ni_netlist);
1954 /* make sure that the the NI we're about to start
1955 * up is actually unique. if it's not fail. */
1956 if (!lnet_ni_unique_net(&net_l->net_ni_list,
1957 ni->ni_interfaces[0])) {
1962 /* adjust the pointer the parent network, just in case it
1963 * the net is a duplicate */
1966 rc = lnet_startup_lndni(ni, tun);
1968 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
1969 ni->ni_net->net_lnd->lnd_query != NULL);
1975 list_add_tail(&ni->ni_netlist, &local_ni_list);
1980 lnet_net_lock(LNET_LOCK_EX);
1981 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
1982 lnet_incr_dlc_seq();
1983 lnet_net_unlock(LNET_LOCK_EX);
1985 /* if the network is not unique then we don't want to keep
1986 * it around after we're done. Free it. Otherwise add that
1987 * net to the global the_lnet.ln_nets */
1988 if (net_l != net && net_l != NULL) {
1990 * TODO - note. currently the tunables can not be updated
1995 net->net_state = LNET_NET_STATE_ACTIVE;
1997 * restore tunables after it has been overwitten by the
2000 if (peer_timeout != -1)
2001 net->net_tunables.lct_peer_timeout = peer_timeout;
2002 if (maxtxcredits != -1)
2003 net->net_tunables.lct_max_tx_credits = maxtxcredits;
2004 if (peerrtrcredits != -1)
2005 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2007 lnet_net_lock(LNET_LOCK_EX);
2008 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2009 lnet_net_unlock(LNET_LOCK_EX);
2016 * shutdown the new NIs that are being started up
2017 * free the NET being started
2019 while (!list_empty(&local_ni_list)) {
2020 ni = list_entry(local_ni_list.next, struct lnet_ni,
2023 lnet_shutdown_lndni(ni);
2033 lnet_startup_lndnets(struct list_head *netlist)
2035 struct lnet_net *net;
2040 * Change to running state before bringing up the LNDs. This
2041 * allows lnet_shutdown_lndnets() to assert that we've passed
2044 lnet_net_lock(LNET_LOCK_EX);
2045 the_lnet.ln_state = LNET_STATE_RUNNING;
2046 lnet_net_unlock(LNET_LOCK_EX);
2048 while (!list_empty(netlist)) {
2049 net = list_entry(netlist->next, struct lnet_net, net_list);
2050 list_del_init(&net->net_list);
2052 rc = lnet_startup_lndnet(net, NULL);
2062 lnet_shutdown_lndnets();
2068 * Initialize LNet library.
2070 * Automatically called at module loading time. Caller has to call
2071 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2072 * latter returned 0. It must be called exactly once.
2074 * \retval 0 on success
2075 * \retval -ve on failures.
2077 int lnet_lib_init(void)
2081 lnet_assert_wire_constants();
2083 /* refer to global cfs_cpt_table for now */
2084 the_lnet.ln_cpt_table = cfs_cpt_table;
2085 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
2087 LASSERT(the_lnet.ln_cpt_number > 0);
2088 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2089 /* we are under risk of consuming all lh_cookie */
2090 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2091 "please change setting of CPT-table and retry\n",
2092 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2096 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2097 the_lnet.ln_cpt_bits++;
2099 rc = lnet_create_locks();
2101 CERROR("Can't create LNet global locks: %d\n", rc);
2105 the_lnet.ln_refcount = 0;
2106 LNetInvalidateEQHandle(&the_lnet.ln_rc_eqh);
2107 INIT_LIST_HEAD(&the_lnet.ln_lnds);
2108 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2109 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
2110 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2111 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
2113 /* The hash table size is the number of bits it takes to express the set
2114 * ln_num_routes, minus 1 (better to under estimate than over so we
2115 * don't waste memory). */
2116 if (rnet_htable_size <= 0)
2117 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2118 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2119 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2120 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2121 order_base_2(rnet_htable_size) - 1);
2123 /* All LNDs apart from the LOLND are in separate modules. They
2124 * register themselves when their module loads, and unregister
2125 * themselves when their module is unloaded. */
2126 lnet_register_lnd(&the_lolnd);
2131 * Finalize LNet library.
2133 * \pre lnet_lib_init() called with success.
2134 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2136 void lnet_lib_exit(void)
2138 LASSERT(the_lnet.ln_refcount == 0);
2140 while (!list_empty(&the_lnet.ln_lnds))
2141 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
2142 struct lnet_lnd, lnd_list));
2143 lnet_destroy_locks();
2147 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2149 * Users must call this function at least once before any other functions.
2150 * For each successful call there must be a corresponding call to
2151 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2154 * The PID used by LNet may be different from the one requested.
2157 * \param requested_pid PID requested by the caller.
2159 * \return >= 0 on success, and < 0 error code on failures.
2162 LNetNIInit(lnet_pid_t requested_pid)
2164 int im_a_router = 0;
2167 struct lnet_ping_buffer *pbuf;
2168 struct lnet_handle_md ping_mdh;
2169 struct list_head net_head;
2170 struct lnet_net *net;
2172 INIT_LIST_HEAD(&net_head);
2174 mutex_lock(&the_lnet.ln_api_mutex);
2176 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2178 if (the_lnet.ln_refcount > 0) {
2179 rc = the_lnet.ln_refcount++;
2180 mutex_unlock(&the_lnet.ln_api_mutex);
2184 rc = lnet_prepare(requested_pid);
2186 mutex_unlock(&the_lnet.ln_api_mutex);
2190 /* create a network for Loopback network */
2191 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2194 goto err_empty_list;
2197 /* Add in the loopback NI */
2198 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2200 goto err_empty_list;
2203 /* If LNet is being initialized via DLC it is possible
2204 * that the user requests not to load module parameters (ones which
2205 * are supported by DLC) on initialization. Therefore, make sure not
2206 * to load networks, routes and forwarding from module parameters
2207 * in this case. On cleanup in case of failure only clean up
2208 * routes if it has been loaded */
2209 if (!the_lnet.ln_nis_from_mod_params) {
2210 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2213 goto err_empty_list;
2216 ni_count = lnet_startup_lndnets(&net_head);
2219 goto err_empty_list;
2222 if (!the_lnet.ln_nis_from_mod_params) {
2223 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2225 goto err_shutdown_lndnis;
2227 rc = lnet_check_routes();
2229 goto err_destroy_routes;
2231 rc = lnet_rtrpools_alloc(im_a_router);
2233 goto err_destroy_routes;
2236 rc = lnet_acceptor_start();
2238 goto err_destroy_routes;
2240 the_lnet.ln_refcount = 1;
2241 /* Now I may use my own API functions... */
2243 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2245 goto err_acceptor_stop;
2247 lnet_ping_target_update(pbuf, ping_mdh);
2249 rc = lnet_router_checker_start();
2253 rc = lnet_push_target_init();
2255 goto err_stop_router_checker;
2257 rc = lnet_peer_discovery_start();
2259 goto err_destroy_push_target;
2262 lnet_router_debugfs_init();
2264 mutex_unlock(&the_lnet.ln_api_mutex);
2268 err_destroy_push_target:
2269 lnet_push_target_fini();
2270 err_stop_router_checker:
2271 lnet_router_checker_stop();
2273 lnet_ping_target_fini();
2275 the_lnet.ln_refcount = 0;
2276 lnet_acceptor_stop();
2278 if (!the_lnet.ln_nis_from_mod_params)
2279 lnet_destroy_routes();
2280 err_shutdown_lndnis:
2281 lnet_shutdown_lndnets();
2285 mutex_unlock(&the_lnet.ln_api_mutex);
2286 while (!list_empty(&net_head)) {
2287 struct lnet_net *net;
2289 net = list_entry(net_head.next, struct lnet_net, net_list);
2290 list_del_init(&net->net_list);
2295 EXPORT_SYMBOL(LNetNIInit);
2298 * Stop LNet interfaces, routing, and forwarding.
2300 * Users must call this function once for each successful call to LNetNIInit().
2301 * Once the LNetNIFini() operation has been started, the results of pending
2302 * API operations are undefined.
2304 * \return always 0 for current implementation.
2309 mutex_lock(&the_lnet.ln_api_mutex);
2311 LASSERT(the_lnet.ln_refcount > 0);
2313 if (the_lnet.ln_refcount != 1) {
2314 the_lnet.ln_refcount--;
2316 LASSERT(!the_lnet.ln_niinit_self);
2320 lnet_router_debugfs_init();
2321 lnet_peer_discovery_stop();
2322 lnet_push_target_fini();
2323 lnet_router_checker_stop();
2324 lnet_ping_target_fini();
2326 /* Teardown fns that use my own API functions BEFORE here */
2327 the_lnet.ln_refcount = 0;
2329 lnet_acceptor_stop();
2330 lnet_destroy_routes();
2331 lnet_shutdown_lndnets();
2335 mutex_unlock(&the_lnet.ln_api_mutex);
2338 EXPORT_SYMBOL(LNetNIFini);
2341 * Grabs the ni data from the ni structure and fills the out
2344 * \param[in] ni network interface structure
2345 * \param[out] cfg_ni NI config information
2346 * \param[out] tun network and LND tunables
2349 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2350 struct lnet_ioctl_config_lnd_tunables *tun,
2351 struct lnet_ioctl_element_stats *stats,
2354 size_t min_size = 0;
2357 if (!ni || !cfg_ni || !tun)
2360 if (ni->ni_interfaces[0] != NULL) {
2361 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2362 if (ni->ni_interfaces[i] != NULL) {
2363 strncpy(cfg_ni->lic_ni_intf[i],
2364 ni->ni_interfaces[i],
2365 sizeof(cfg_ni->lic_ni_intf[i]));
2370 cfg_ni->lic_nid = ni->ni_nid;
2371 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2372 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2374 cfg_ni->lic_status = ni->ni_status->ns_status;
2375 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2376 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2378 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2381 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2382 LNET_STATS_TYPE_SEND);
2383 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2384 LNET_STATS_TYPE_RECV);
2385 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2386 LNET_STATS_TYPE_DROP);
2390 * tun->lt_tun will always be present, but in order to be
2391 * backwards compatible, we need to deal with the cases when
2392 * tun->lt_tun is smaller than what the kernel has, because it
2393 * comes from an older version of a userspace program, then we'll
2394 * need to copy as much information as we have available space.
2396 min_size = tun_size - sizeof(tun->lt_cmn);
2397 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2399 /* copy over the cpts */
2400 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2401 ni->ni_cpts == NULL) {
2402 for (i = 0; i < ni->ni_ncpts; i++)
2403 cfg_ni->lic_cpts[i] = i;
2406 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2407 i < LNET_MAX_SHOW_NUM_CPT;
2409 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2411 cfg_ni->lic_ncpts = ni->ni_ncpts;
2415 * NOTE: This is a legacy function left in the code to be backwards
2416 * compatible with older userspace programs. It should eventually be
2419 * Grabs the ni data from the ni structure and fills the out
2422 * \param[in] ni network interface structure
2423 * \param[out] config config information
2426 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2427 struct lnet_ioctl_config_data *config)
2429 struct lnet_ioctl_net_config *net_config;
2430 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2431 size_t min_size, tunable_size = 0;
2437 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2441 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2442 ARRAY_SIZE(net_config->ni_interfaces));
2444 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2445 if (!ni->ni_interfaces[i])
2448 strncpy(net_config->ni_interfaces[i],
2449 ni->ni_interfaces[i],
2450 sizeof(net_config->ni_interfaces[i]));
2453 config->cfg_nid = ni->ni_nid;
2454 config->cfg_config_u.cfg_net.net_peer_timeout =
2455 ni->ni_net->net_tunables.lct_peer_timeout;
2456 config->cfg_config_u.cfg_net.net_max_tx_credits =
2457 ni->ni_net->net_tunables.lct_max_tx_credits;
2458 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2459 ni->ni_net->net_tunables.lct_peer_tx_credits;
2460 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2461 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2463 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2464 net_config->ni_status = LNET_NI_STATUS_UP;
2466 net_config->ni_status = ni->ni_status->ns_status;
2469 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2471 for (i = 0; i < num_cpts; i++)
2472 net_config->ni_cpts[i] = ni->ni_cpts[i];
2474 config->cfg_ncpts = num_cpts;
2478 * See if user land tools sent in a newer and larger version
2479 * of struct lnet_tunables than what the kernel uses.
2481 min_size = sizeof(*config) + sizeof(*net_config);
2483 if (config->cfg_hdr.ioc_len > min_size)
2484 tunable_size = config->cfg_hdr.ioc_len - min_size;
2486 /* Don't copy too much data to user space */
2487 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2488 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2490 if (lnd_cfg && min_size) {
2491 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2492 config->cfg_config_u.cfg_net.net_interface_count = 1;
2494 /* Tell user land that kernel side has less data */
2495 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2496 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2497 config->cfg_hdr.ioc_len -= min_size;
2503 lnet_get_ni_idx_locked(int idx)
2506 struct lnet_net *net;
2508 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2509 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2519 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2522 struct lnet_net *net = mynet;
2526 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2528 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2534 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2535 /* if you reached the end of the ni list and the net is
2536 * specified, then there are no more nis in that net */
2540 /* we reached the end of this net ni list. move to the
2542 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2543 /* no more nets and no more NIs. */
2546 /* get the next net */
2547 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2549 /* get the ni on it */
2550 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2556 /* there are more nis left */
2557 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2563 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2568 int idx = config->cfg_count;
2570 cpt = lnet_net_lock_current();
2572 ni = lnet_get_ni_idx_locked(idx);
2577 lnet_fill_ni_info_legacy(ni, config);
2581 lnet_net_unlock(cpt);
2586 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2587 struct lnet_ioctl_config_lnd_tunables *tun,
2588 struct lnet_ioctl_element_stats *stats,
2595 if (!cfg_ni || !tun || !stats)
2598 cpt = lnet_net_lock_current();
2600 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2605 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
2609 lnet_net_unlock(cpt);
2613 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
2622 cpt = lnet_net_lock_current();
2624 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
2627 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
2631 lnet_net_unlock(cpt);
2636 static int lnet_add_net_common(struct lnet_net *net,
2637 struct lnet_ioctl_config_lnd_tunables *tun)
2640 struct lnet_ping_buffer *pbuf;
2641 struct lnet_handle_md ping_mdh;
2643 struct lnet_remotenet *rnet;
2645 int num_acceptor_nets;
2647 lnet_net_lock(LNET_LOCK_EX);
2648 rnet = lnet_find_rnet_locked(net->net_id);
2649 lnet_net_unlock(LNET_LOCK_EX);
2651 * make sure that the net added doesn't invalidate the current
2652 * configuration LNet is keeping
2655 CERROR("Adding net %s will invalidate routing configuration\n",
2656 libcfs_net2str(net->net_id));
2662 * make sure you calculate the correct number of slots in the ping
2663 * buffer. Since the ping info is a flattened list of all the NIs,
2664 * we should allocate enough slots to accomodate the number of NIs
2665 * which will be added.
2667 * since ni hasn't been configured yet, use
2668 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
2670 net_ni_count = lnet_get_net_ni_count_pre(net);
2672 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2673 net_ni_count + lnet_get_ni_count(),
2681 memcpy(&net->net_tunables,
2682 &tun->lt_cmn, sizeof(net->net_tunables));
2684 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
2687 * before starting this network get a count of the current TCP
2688 * networks which require the acceptor thread running. If that
2689 * count is == 0 before we start up this network, then we'd want to
2690 * start up the acceptor thread after starting up this network
2692 num_acceptor_nets = lnet_count_acceptor_nets();
2694 net_id = net->net_id;
2696 rc = lnet_startup_lndnet(net,
2697 (tun) ? &tun->lt_tun : NULL);
2701 lnet_net_lock(LNET_LOCK_EX);
2702 net = lnet_get_net_locked(net_id);
2703 lnet_net_unlock(LNET_LOCK_EX);
2708 * Start the acceptor thread if this is the first network
2709 * being added that requires the thread.
2711 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
2712 rc = lnet_acceptor_start();
2714 /* shutdown the net that we just started */
2715 CERROR("Failed to start up acceptor thread\n");
2716 lnet_shutdown_lndnet(net);
2721 lnet_net_lock(LNET_LOCK_EX);
2722 lnet_peer_net_added(net);
2723 lnet_net_unlock(LNET_LOCK_EX);
2725 lnet_ping_target_update(pbuf, ping_mdh);
2730 lnet_ping_md_unlink(pbuf, &ping_mdh);
2731 lnet_ping_buffer_decref(pbuf);
2735 static int lnet_handle_legacy_ip2nets(char *ip2nets,
2736 struct lnet_ioctl_config_lnd_tunables *tun)
2738 struct lnet_net *net;
2741 struct list_head net_head;
2743 INIT_LIST_HEAD(&net_head);
2745 rc = lnet_parse_ip2nets(&nets, ip2nets);
2749 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2753 mutex_lock(&the_lnet.ln_api_mutex);
2754 while (!list_empty(&net_head)) {
2755 net = list_entry(net_head.next, struct lnet_net, net_list);
2756 list_del_init(&net->net_list);
2757 rc = lnet_add_net_common(net, tun);
2763 mutex_unlock(&the_lnet.ln_api_mutex);
2765 while (!list_empty(&net_head)) {
2766 net = list_entry(net_head.next, struct lnet_net, net_list);
2767 list_del_init(&net->net_list);
2773 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
2775 struct lnet_net *net;
2777 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2779 __u32 net_id, lnd_type;
2781 /* get the tunables if they are available */
2782 if (conf->lic_cfg_hdr.ioc_len >=
2783 sizeof(*conf) + sizeof(*tun))
2784 tun = (struct lnet_ioctl_config_lnd_tunables *)
2787 /* handle legacy ip2nets from DLC */
2788 if (conf->lic_legacy_ip2nets[0] != '\0')
2789 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
2792 net_id = LNET_NIDNET(conf->lic_nid);
2793 lnd_type = LNET_NETTYP(net_id);
2795 if (!libcfs_isknown_lnd(lnd_type)) {
2796 CERROR("No valid net and lnd information provided\n");
2800 net = lnet_net_alloc(net_id, NULL);
2804 for (i = 0; i < conf->lic_ncpts; i++) {
2805 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
2809 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
2810 conf->lic_ni_intf[0]);
2814 mutex_lock(&the_lnet.ln_api_mutex);
2816 rc = lnet_add_net_common(net, tun);
2818 mutex_unlock(&the_lnet.ln_api_mutex);
2823 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
2825 struct lnet_net *net;
2827 __u32 net_id = LNET_NIDNET(conf->lic_nid);
2828 struct lnet_ping_buffer *pbuf;
2829 struct lnet_handle_md ping_mdh;
2834 /* don't allow userspace to shutdown the LOLND */
2835 if (LNET_NETTYP(net_id) == LOLND)
2838 mutex_lock(&the_lnet.ln_api_mutex);
2842 net = lnet_get_net_locked(net_id);
2844 CERROR("net %s not found\n",
2845 libcfs_net2str(net_id));
2850 addr = LNET_NIDADDR(conf->lic_nid);
2852 /* remove the entire net */
2853 net_count = lnet_get_net_ni_count_locked(net);
2857 /* create and link a new ping info, before removing the old one */
2858 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2859 lnet_get_ni_count() - net_count,
2862 goto unlock_api_mutex;
2864 lnet_shutdown_lndnet(net);
2866 if (lnet_count_acceptor_nets() == 0)
2867 lnet_acceptor_stop();
2869 lnet_ping_target_update(pbuf, ping_mdh);
2871 goto unlock_api_mutex;
2874 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
2876 CERROR("nid %s not found\n",
2877 libcfs_nid2str(conf->lic_nid));
2882 net_count = lnet_get_net_ni_count_locked(net);
2886 /* create and link a new ping info, before removing the old one */
2887 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2888 lnet_get_ni_count() - 1, false);
2890 goto unlock_api_mutex;
2892 lnet_shutdown_lndni(ni);
2894 if (lnet_count_acceptor_nets() == 0)
2895 lnet_acceptor_stop();
2897 lnet_ping_target_update(pbuf, ping_mdh);
2899 /* check if the net is empty and remove it if it is */
2901 lnet_shutdown_lndnet(net);
2903 goto unlock_api_mutex;
2908 mutex_unlock(&the_lnet.ln_api_mutex);
2914 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
2915 * They are only expected to be called for unique networks.
2916 * That can be as a result of older DLC library
2917 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
2920 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
2922 struct lnet_net *net;
2923 struct list_head net_head;
2925 struct lnet_ioctl_config_lnd_tunables tun;
2926 char *nets = conf->cfg_config_u.cfg_net.net_intf;
2928 INIT_LIST_HEAD(&net_head);
2930 /* Create a net/ni structures for the network string */
2931 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2933 return rc == 0 ? -EINVAL : rc;
2935 mutex_lock(&the_lnet.ln_api_mutex);
2938 rc = -EINVAL; /* only add one network per call */
2939 goto out_unlock_clean;
2942 net = list_entry(net_head.next, struct lnet_net, net_list);
2943 list_del_init(&net->net_list);
2945 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
2947 memset(&tun, 0, sizeof(tun));
2949 tun.lt_cmn.lct_peer_timeout =
2950 conf->cfg_config_u.cfg_net.net_peer_timeout;
2951 tun.lt_cmn.lct_peer_tx_credits =
2952 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
2953 tun.lt_cmn.lct_peer_rtr_credits =
2954 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
2955 tun.lt_cmn.lct_max_tx_credits =
2956 conf->cfg_config_u.cfg_net.net_max_tx_credits;
2958 rc = lnet_add_net_common(net, &tun);
2961 mutex_unlock(&the_lnet.ln_api_mutex);
2962 while (!list_empty(&net_head)) {
2963 /* net_head list is empty in success case */
2964 net = list_entry(net_head.next, struct lnet_net, net_list);
2965 list_del_init(&net->net_list);
2972 lnet_dyn_del_net(__u32 net_id)
2974 struct lnet_net *net;
2975 struct lnet_ping_buffer *pbuf;
2976 struct lnet_handle_md ping_mdh;
2980 /* don't allow userspace to shutdown the LOLND */
2981 if (LNET_NETTYP(net_id) == LOLND)
2984 mutex_lock(&the_lnet.ln_api_mutex);
2988 net = lnet_get_net_locked(net_id);
2995 net_ni_count = lnet_get_net_ni_count_locked(net);
2999 /* create and link a new ping info, before removing the old one */
3000 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3001 lnet_get_ni_count() - net_ni_count, false);
3005 lnet_shutdown_lndnet(net);
3007 if (lnet_count_acceptor_nets() == 0)
3008 lnet_acceptor_stop();
3010 lnet_ping_target_update(pbuf, ping_mdh);
3013 mutex_unlock(&the_lnet.ln_api_mutex);
3018 void lnet_incr_dlc_seq(void)
3020 atomic_inc(&lnet_dlc_seq_no);
3023 __u32 lnet_get_dlc_seq_locked(void)
3025 return atomic_read(&lnet_dlc_seq_no);
3029 * LNet ioctl handler.
3033 LNetCtl(unsigned int cmd, void *arg)
3035 struct libcfs_ioctl_data *data = arg;
3036 struct lnet_ioctl_config_data *config;
3037 struct lnet_process_id id = {0};
3041 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3042 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3045 case IOC_LIBCFS_GET_NI:
3046 rc = LNetGetId(data->ioc_count, &id);
3047 data->ioc_nid = id.nid;
3050 case IOC_LIBCFS_FAIL_NID:
3051 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3053 case IOC_LIBCFS_ADD_ROUTE:
3056 if (config->cfg_hdr.ioc_len < sizeof(*config))
3059 mutex_lock(&the_lnet.ln_api_mutex);
3060 rc = lnet_add_route(config->cfg_net,
3061 config->cfg_config_u.cfg_route.rtr_hop,
3063 config->cfg_config_u.cfg_route.
3066 rc = lnet_check_routes();
3068 lnet_del_route(config->cfg_net,
3071 mutex_unlock(&the_lnet.ln_api_mutex);
3074 case IOC_LIBCFS_DEL_ROUTE:
3077 if (config->cfg_hdr.ioc_len < sizeof(*config))
3080 mutex_lock(&the_lnet.ln_api_mutex);
3081 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3082 mutex_unlock(&the_lnet.ln_api_mutex);
3085 case IOC_LIBCFS_GET_ROUTE:
3088 if (config->cfg_hdr.ioc_len < sizeof(*config))
3091 mutex_lock(&the_lnet.ln_api_mutex);
3092 rc = lnet_get_route(config->cfg_count,
3094 &config->cfg_config_u.cfg_route.rtr_hop,
3096 &config->cfg_config_u.cfg_route.rtr_flags,
3097 &config->cfg_config_u.cfg_route.
3099 mutex_unlock(&the_lnet.ln_api_mutex);
3102 case IOC_LIBCFS_GET_LOCAL_NI: {
3103 struct lnet_ioctl_config_ni *cfg_ni;
3104 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3105 struct lnet_ioctl_element_stats *stats;
3110 /* get the tunables if they are available */
3111 if (cfg_ni->lic_cfg_hdr.ioc_len <
3112 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3115 stats = (struct lnet_ioctl_element_stats *)
3117 tun = (struct lnet_ioctl_config_lnd_tunables *)
3118 (cfg_ni->lic_bulk + sizeof(*stats));
3120 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3123 mutex_lock(&the_lnet.ln_api_mutex);
3124 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3125 mutex_unlock(&the_lnet.ln_api_mutex);
3129 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3130 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3132 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3135 mutex_lock(&the_lnet.ln_api_mutex);
3136 rc = lnet_get_ni_stats(msg_stats);
3137 mutex_unlock(&the_lnet.ln_api_mutex);
3142 case IOC_LIBCFS_GET_NET: {
3143 size_t total = sizeof(*config) +
3144 sizeof(struct lnet_ioctl_net_config);
3147 if (config->cfg_hdr.ioc_len < total)
3150 mutex_lock(&the_lnet.ln_api_mutex);
3151 rc = lnet_get_net_config(config);
3152 mutex_unlock(&the_lnet.ln_api_mutex);
3156 case IOC_LIBCFS_GET_LNET_STATS:
3158 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3160 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3163 mutex_lock(&the_lnet.ln_api_mutex);
3164 lnet_counters_get(&lnet_stats->st_cntrs);
3165 mutex_unlock(&the_lnet.ln_api_mutex);
3169 case IOC_LIBCFS_CONFIG_RTR:
3172 if (config->cfg_hdr.ioc_len < sizeof(*config))
3175 mutex_lock(&the_lnet.ln_api_mutex);
3176 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3177 rc = lnet_rtrpools_enable();
3178 mutex_unlock(&the_lnet.ln_api_mutex);
3181 lnet_rtrpools_disable();
3182 mutex_unlock(&the_lnet.ln_api_mutex);
3185 case IOC_LIBCFS_ADD_BUF:
3188 if (config->cfg_hdr.ioc_len < sizeof(*config))
3191 mutex_lock(&the_lnet.ln_api_mutex);
3192 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3194 config->cfg_config_u.cfg_buffers.
3196 config->cfg_config_u.cfg_buffers.
3198 mutex_unlock(&the_lnet.ln_api_mutex);
3201 case IOC_LIBCFS_SET_NUMA_RANGE: {
3202 struct lnet_ioctl_set_value *numa;
3204 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3206 lnet_net_lock(LNET_LOCK_EX);
3207 lnet_numa_range = numa->sv_value;
3208 lnet_net_unlock(LNET_LOCK_EX);
3212 case IOC_LIBCFS_GET_NUMA_RANGE: {
3213 struct lnet_ioctl_set_value *numa;
3215 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3217 numa->sv_value = lnet_numa_range;
3221 case IOC_LIBCFS_GET_BUF: {
3222 struct lnet_ioctl_pool_cfg *pool_cfg;
3223 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3227 if (config->cfg_hdr.ioc_len < total)
3230 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3232 mutex_lock(&the_lnet.ln_api_mutex);
3233 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3234 mutex_unlock(&the_lnet.ln_api_mutex);
3238 case IOC_LIBCFS_ADD_PEER_NI: {
3239 struct lnet_ioctl_peer_cfg *cfg = arg;
3241 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3244 mutex_lock(&the_lnet.ln_api_mutex);
3245 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3248 mutex_unlock(&the_lnet.ln_api_mutex);
3252 case IOC_LIBCFS_DEL_PEER_NI: {
3253 struct lnet_ioctl_peer_cfg *cfg = arg;
3255 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3258 mutex_lock(&the_lnet.ln_api_mutex);
3259 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3260 cfg->prcfg_cfg_nid);
3261 mutex_unlock(&the_lnet.ln_api_mutex);
3265 case IOC_LIBCFS_GET_PEER_INFO: {
3266 struct lnet_ioctl_peer *peer_info = arg;
3268 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3271 mutex_lock(&the_lnet.ln_api_mutex);
3272 rc = lnet_get_peer_ni_info(
3273 peer_info->pr_count,
3275 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3276 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3277 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3278 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3279 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3280 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3281 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3282 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3283 mutex_unlock(&the_lnet.ln_api_mutex);
3287 case IOC_LIBCFS_GET_PEER_NI: {
3288 struct lnet_ioctl_peer_cfg *cfg = arg;
3290 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3293 mutex_lock(&the_lnet.ln_api_mutex);
3294 rc = lnet_get_peer_info(cfg,
3295 (void __user *)cfg->prcfg_bulk);
3296 mutex_unlock(&the_lnet.ln_api_mutex);
3300 case IOC_LIBCFS_GET_PEER_LIST: {
3301 struct lnet_ioctl_peer_cfg *cfg = arg;
3303 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3306 mutex_lock(&the_lnet.ln_api_mutex);
3307 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3308 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3309 mutex_unlock(&the_lnet.ln_api_mutex);
3313 case IOC_LIBCFS_NOTIFY_ROUTER: {
3314 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3316 /* The deadline passed in by the user should be some time in
3317 * seconds in the future since the UNIX epoch. We have to map
3318 * that deadline to the wall clock.
3320 deadline += ktime_get_seconds();
3321 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
3325 case IOC_LIBCFS_LNET_DIST:
3326 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3327 if (rc < 0 && rc != -EHOSTUNREACH)
3330 data->ioc_u32[0] = rc;
3333 case IOC_LIBCFS_TESTPROTOCOMPAT:
3334 lnet_net_lock(LNET_LOCK_EX);
3335 the_lnet.ln_testprotocompat = data->ioc_flags;
3336 lnet_net_unlock(LNET_LOCK_EX);
3339 case IOC_LIBCFS_LNET_FAULT:
3340 return lnet_fault_ctl(data->ioc_flags, data);
3342 case IOC_LIBCFS_PING: {
3343 signed long timeout;
3345 id.nid = data->ioc_nid;
3346 id.pid = data->ioc_u32[0];
3348 /* If timeout is negative then set default of 3 minutes */
3349 if (((s32)data->ioc_u32[1] <= 0) ||
3350 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3351 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3353 timeout = msecs_to_jiffies(data->ioc_u32[1]);
3355 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3356 data->ioc_plen1 / sizeof(struct lnet_process_id));
3361 data->ioc_count = rc;
3365 case IOC_LIBCFS_PING_PEER: {
3366 struct lnet_ioctl_ping_data *ping = arg;
3367 struct lnet_peer *lp;
3368 signed long timeout;
3370 /* If timeout is negative then set default of 3 minutes */
3371 if (((s32)ping->op_param) <= 0 ||
3372 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3373 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3375 timeout = msecs_to_jiffies(ping->op_param);
3377 rc = lnet_ping(ping->ping_id, timeout,
3383 mutex_lock(&the_lnet.ln_api_mutex);
3384 lp = lnet_find_peer(ping->ping_id.nid);
3386 ping->ping_id.nid = lp->lp_primary_nid;
3387 ping->mr_info = lnet_peer_is_multi_rail(lp);
3388 lnet_peer_decref_locked(lp);
3390 mutex_unlock(&the_lnet.ln_api_mutex);
3392 ping->ping_count = rc;
3396 case IOC_LIBCFS_DISCOVER: {
3397 struct lnet_ioctl_ping_data *discover = arg;
3398 struct lnet_peer *lp;
3400 rc = lnet_discover(discover->ping_id, discover->op_param,
3402 discover->ping_count);
3406 mutex_lock(&the_lnet.ln_api_mutex);
3407 lp = lnet_find_peer(discover->ping_id.nid);
3409 discover->ping_id.nid = lp->lp_primary_nid;
3410 discover->mr_info = lnet_peer_is_multi_rail(lp);
3411 lnet_peer_decref_locked(lp);
3413 mutex_unlock(&the_lnet.ln_api_mutex);
3415 discover->ping_count = rc;
3420 ni = lnet_net2ni_addref(data->ioc_net);
3424 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3427 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
3434 EXPORT_SYMBOL(LNetCtl);
3436 void LNetDebugPeer(struct lnet_process_id id)
3438 lnet_debug_peer(id.nid);
3440 EXPORT_SYMBOL(LNetDebugPeer);
3443 * Determine if the specified peer \a nid is on the local node.
3445 * \param nid peer nid to check
3447 * \retval true If peer NID is on the local node.
3448 * \retval false If peer NID is not on the local node.
3450 bool LNetIsPeerLocal(lnet_nid_t nid)
3452 struct lnet_net *net;
3456 cpt = lnet_net_lock_current();
3457 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3458 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3459 if (ni->ni_nid == nid) {
3460 lnet_net_unlock(cpt);
3465 lnet_net_unlock(cpt);
3469 EXPORT_SYMBOL(LNetIsPeerLocal);
3472 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
3473 * Note that all interfaces share a same PID, as requested by LNetNIInit().
3475 * \param index Index of the interface to look up.
3476 * \param id On successful return, this location will hold the
3477 * struct lnet_process_id ID of the interface.
3479 * \retval 0 If an interface exists at \a index.
3480 * \retval -ENOENT If no interface has been found.
3483 LNetGetId(unsigned int index, struct lnet_process_id *id)
3486 struct lnet_net *net;
3490 LASSERT(the_lnet.ln_refcount > 0);
3492 cpt = lnet_net_lock_current();
3494 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3495 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3499 id->nid = ni->ni_nid;
3500 id->pid = the_lnet.ln_pid;
3506 lnet_net_unlock(cpt);
3509 EXPORT_SYMBOL(LNetGetId);
3511 static int lnet_ping(struct lnet_process_id id, signed long timeout,
3512 struct lnet_process_id __user *ids, int n_ids)
3514 struct lnet_handle_eq eqh;
3515 struct lnet_handle_md mdh;
3516 struct lnet_event event;
3517 struct lnet_md md = { NULL };
3521 const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
3522 struct lnet_ping_buffer *pbuf;
3523 struct lnet_process_id tmpid;
3530 /* n_ids limit is arbitrary */
3531 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
3535 * if the user buffer has more space than the lnet_interfaces_max
3536 * then only fill it up to lnet_interfaces_max
3538 if (n_ids > lnet_interfaces_max)
3539 n_ids = lnet_interfaces_max;
3541 if (id.pid == LNET_PID_ANY)
3542 id.pid = LNET_PID_LUSTRE;
3544 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
3548 /* NB 2 events max (including any unlink event) */
3549 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
3551 CERROR("Can't allocate EQ: %d\n", rc);
3552 goto fail_ping_buffer_decref;
3555 /* initialize md content */
3556 md.start = &pbuf->pb_info;
3557 md.length = LNET_PING_INFO_SIZE(n_ids);
3558 md.threshold = 2; /* GET/REPLY */
3560 md.options = LNET_MD_TRUNCATE;
3564 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
3566 CERROR("Can't bind MD: %d\n", rc);
3570 rc = LNetGet(LNET_NID_ANY, mdh, id,
3571 LNET_RESERVED_PORTAL,
3572 LNET_PROTO_PING_MATCHBITS, 0);
3575 /* Don't CERROR; this could be deliberate! */
3576 rc2 = LNetMDUnlink(mdh);
3579 /* NB must wait for the UNLINK event below... */
3581 timeout = a_long_time;
3585 /* MUST block for unlink to complete */
3587 blocked = cfs_block_allsigs();
3589 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
3592 cfs_restore_sigs(blocked);
3594 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
3595 (rc2 <= 0) ? -1 : event.type,
3596 (rc2 <= 0) ? -1 : event.status,
3597 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
3599 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
3601 if (rc2 <= 0 || event.status != 0) {
3602 /* timeout or error */
3603 if (!replied && rc == 0)
3604 rc = (rc2 < 0) ? rc2 :
3605 (rc2 == 0) ? -ETIMEDOUT :
3609 /* Ensure completion in finite time... */
3611 /* No assertion (racing with network) */
3613 timeout = a_long_time;
3614 } else if (rc2 == 0) {
3615 /* timed out waiting for unlink */
3616 CWARN("ping %s: late network completion\n",
3619 } else if (event.type == LNET_EVENT_REPLY) {
3623 } while (rc2 <= 0 || !event.unlinked);
3627 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
3634 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
3636 rc = -EPROTO; /* if I can't parse... */
3639 CERROR("%s: ping info too short %d\n",
3640 libcfs_id2str(id), nob);
3644 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
3645 lnet_swap_pinginfo(pbuf);
3646 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
3647 CERROR("%s: Unexpected magic %08x\n",
3648 libcfs_id2str(id), pbuf->pb_info.pi_magic);
3652 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
3653 CERROR("%s: ping w/o NI status: 0x%x\n",
3654 libcfs_id2str(id), pbuf->pb_info.pi_features);
3658 if (nob < LNET_PING_INFO_SIZE(0)) {
3659 CERROR("%s: Short reply %d(%d min)\n",
3661 nob, (int)LNET_PING_INFO_SIZE(0));
3665 if (pbuf->pb_info.pi_nnis < n_ids)
3666 n_ids = pbuf->pb_info.pi_nnis;
3668 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
3669 CERROR("%s: Short reply %d(%d expected)\n",
3671 nob, (int)LNET_PING_INFO_SIZE(n_ids));
3675 rc = -EFAULT; /* if I segv in copy_to_user()... */
3677 memset(&tmpid, 0, sizeof(tmpid));
3678 for (i = 0; i < n_ids; i++) {
3679 tmpid.pid = pbuf->pb_info.pi_pid;
3680 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
3681 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
3684 rc = pbuf->pb_info.pi_nnis;
3687 rc2 = LNetEQFree(eqh);
3689 CERROR("rc2 %d\n", rc2);
3692 fail_ping_buffer_decref:
3693 lnet_ping_buffer_decref(pbuf);
3698 lnet_discover(struct lnet_process_id id, __u32 force,
3699 struct lnet_process_id __user *ids, int n_ids)
3701 struct lnet_peer_ni *lpni;
3702 struct lnet_peer_ni *p;
3703 struct lnet_peer *lp;
3704 struct lnet_process_id *buf;
3708 int max_intf = lnet_interfaces_max;
3712 id.nid == LNET_NID_ANY)
3715 if (id.pid == LNET_PID_ANY)
3716 id.pid = LNET_PID_LUSTRE;
3719 * if the user buffer has more space than the max_intf
3720 * then only fill it up to max_intf
3722 if (n_ids > max_intf)
3725 buf_size = n_ids * sizeof(*buf);
3727 LIBCFS_ALLOC(buf, buf_size);
3731 cpt = lnet_net_lock_current();
3732 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
3739 * Clearing the NIDS_UPTODATE flag ensures the peer will
3740 * be discovered, provided discovery has not been disabled.
3742 lp = lpni->lpni_peer_net->lpn_peer;
3743 spin_lock(&lp->lp_lock);
3744 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
3745 /* If the force flag is set, force a PING and PUSH as well. */
3747 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
3748 spin_unlock(&lp->lp_lock);
3749 rc = lnet_discover_peer_locked(lpni, cpt, true);
3753 /* Peer may have changed. */
3754 lp = lpni->lpni_peer_net->lpn_peer;
3755 if (lp->lp_nnis < n_ids)
3756 n_ids = lp->lp_nnis;
3760 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
3761 buf[i].pid = id.pid;
3762 buf[i].nid = p->lpni_nid;
3767 lnet_net_unlock(cpt);
3770 if (copy_to_user(ids, buf, n_ids * sizeof(*buf)))
3776 lnet_peer_ni_decref_locked(lpni);
3778 lnet_net_unlock(cpt);
3780 LIBCFS_FREE(buf, buf_size);