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, struct kernel_param *kp);
83 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
84 &lnet_interfaces_max, S_IRUGO|S_IWUSR);
85 MODULE_PARM_DESC(lnet_interfaces_max,
86 "Maximum number of interfaces in a node.");
88 unsigned lnet_peer_discovery_disabled = 0;
89 static int discovery_set(const char *val, struct kernel_param *kp);
90 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
91 &lnet_peer_discovery_disabled, S_IRUGO|S_IWUSR);
92 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
93 "Set to 1 to disable peer discovery on this node.");
96 * This sequence number keeps track of how many times DLC was used to
97 * update the local NIs. It is incremented when a NI is added or
98 * removed and checked when sending a message to determine if there is
99 * a need to re-run the selection algorithm. See lnet_select_pathway()
100 * for more details on its usage.
102 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
104 static int lnet_ping(struct lnet_process_id id, signed long timeout,
105 struct lnet_process_id __user *ids, int n_ids);
107 static int lnet_discover(struct lnet_process_id id, __u32 force,
108 struct lnet_process_id __user *ids, int n_ids);
111 discovery_set(const char *val, struct kernel_param *kp)
114 unsigned *discovery = (unsigned *)kp->arg;
116 struct lnet_ping_buffer *pbuf;
118 rc = kstrtoul(val, 0, &value);
120 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
124 value = (value) ? 1 : 0;
127 * The purpose of locking the api_mutex here is to ensure that
128 * the correct value ends up stored properly.
130 mutex_lock(&the_lnet.ln_api_mutex);
132 if (value == *discovery) {
133 mutex_unlock(&the_lnet.ln_api_mutex);
139 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
140 mutex_unlock(&the_lnet.ln_api_mutex);
144 /* tell peers that discovery setting has changed */
145 lnet_net_lock(LNET_LOCK_EX);
146 pbuf = the_lnet.ln_ping_target;
148 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
150 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
151 lnet_net_unlock(LNET_LOCK_EX);
153 lnet_push_update_to_peers(1);
155 mutex_unlock(&the_lnet.ln_api_mutex);
161 intf_max_set(const char *val, struct kernel_param *kp)
165 rc = kstrtoint(val, 0, &value);
167 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
171 if (value < LNET_INTERFACES_MIN) {
172 CWARN("max interfaces provided are too small, setting to %d\n",
173 LNET_INTERFACES_MAX_DEFAULT);
174 value = LNET_INTERFACES_MAX_DEFAULT;
177 *(int *)kp->arg = value;
183 lnet_get_routes(void)
189 lnet_get_networks(void)
194 if (*networks != 0 && *ip2nets != 0) {
195 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
196 "'ip2nets' but not both at once\n");
201 rc = lnet_parse_ip2nets(&nets, ip2nets);
202 return (rc == 0) ? nets : NULL;
212 lnet_init_locks(void)
214 spin_lock_init(&the_lnet.ln_eq_wait_lock);
215 spin_lock_init(&the_lnet.ln_msg_resend_lock);
216 init_waitqueue_head(&the_lnet.ln_eq_waitq);
217 init_waitqueue_head(&the_lnet.ln_rc_waitq);
218 mutex_init(&the_lnet.ln_lnd_mutex);
222 lnet_fini_locks(void)
226 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
227 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
231 lnet_descriptor_setup(void)
233 /* create specific kmem_cache for MEs and small MDs (i.e., originally
234 * allocated in <size-xxx> kmem_cache).
236 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
238 if (!lnet_mes_cachep)
241 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
242 LNET_SMALL_MD_SIZE, 0, 0,
244 if (!lnet_small_mds_cachep)
251 lnet_descriptor_cleanup(void)
254 if (lnet_small_mds_cachep) {
255 kmem_cache_destroy(lnet_small_mds_cachep);
256 lnet_small_mds_cachep = NULL;
259 if (lnet_mes_cachep) {
260 kmem_cache_destroy(lnet_mes_cachep);
261 lnet_mes_cachep = NULL;
266 lnet_create_remote_nets_table(void)
269 struct list_head *hash;
271 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
272 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
273 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
275 CERROR("Failed to create remote nets hash table\n");
279 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
280 INIT_LIST_HEAD(&hash[i]);
281 the_lnet.ln_remote_nets_hash = hash;
286 lnet_destroy_remote_nets_table(void)
290 if (the_lnet.ln_remote_nets_hash == NULL)
293 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
294 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
296 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
297 LNET_REMOTE_NETS_HASH_SIZE *
298 sizeof(the_lnet.ln_remote_nets_hash[0]));
299 the_lnet.ln_remote_nets_hash = NULL;
303 lnet_destroy_locks(void)
305 if (the_lnet.ln_res_lock != NULL) {
306 cfs_percpt_lock_free(the_lnet.ln_res_lock);
307 the_lnet.ln_res_lock = NULL;
310 if (the_lnet.ln_net_lock != NULL) {
311 cfs_percpt_lock_free(the_lnet.ln_net_lock);
312 the_lnet.ln_net_lock = NULL;
319 lnet_create_locks(void)
323 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
324 if (the_lnet.ln_res_lock == NULL)
327 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
328 if (the_lnet.ln_net_lock == NULL)
334 lnet_destroy_locks();
338 static void lnet_assert_wire_constants(void)
340 /* Wire protocol assertions generated by 'wirecheck'
341 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
342 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
343 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
346 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
347 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
348 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
349 CLASSERT(LNET_MSG_ACK == 0);
350 CLASSERT(LNET_MSG_PUT == 1);
351 CLASSERT(LNET_MSG_GET == 2);
352 CLASSERT(LNET_MSG_REPLY == 3);
353 CLASSERT(LNET_MSG_HELLO == 4);
355 /* Checks for struct lnet_handle_wire */
356 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
357 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
358 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
359 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
360 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
362 /* Checks for struct struct lnet_magicversion */
363 CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
364 CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
365 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
366 CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
367 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
368 CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
369 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
371 /* Checks for struct struct lnet_hdr */
372 CLASSERT((int)sizeof(struct lnet_hdr) == 72);
373 CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
374 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
375 CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
376 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
377 CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
378 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
379 CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
380 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
381 CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
382 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
383 CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
384 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
385 CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
386 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
389 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
390 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
391 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
392 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
393 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
394 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
397 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
398 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
399 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
400 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
401 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
402 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
403 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
404 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
405 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
406 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
409 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
410 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
411 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
412 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
413 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
414 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
415 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
416 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
417 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
418 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
421 CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
422 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
425 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
426 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
427 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
428 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
430 /* Checks for struct lnet_ni_status and related constants */
431 CLASSERT(LNET_NI_STATUS_INVALID == 0x00000000);
432 CLASSERT(LNET_NI_STATUS_UP == 0x15aac0de);
433 CLASSERT(LNET_NI_STATUS_DOWN == 0xdeadface);
435 /* Checks for struct lnet_ni_status */
436 CLASSERT((int)sizeof(struct lnet_ni_status) == 16);
437 CLASSERT((int)offsetof(struct lnet_ni_status, ns_nid) == 0);
438 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) == 8);
439 CLASSERT((int)offsetof(struct lnet_ni_status, ns_status) == 8);
440 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_status) == 4);
441 CLASSERT((int)offsetof(struct lnet_ni_status, ns_unused) == 12);
442 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) == 4);
444 /* Checks for struct lnet_ping_info and related constants */
445 CLASSERT(LNET_PROTO_PING_MAGIC == 0x70696E67);
446 CLASSERT(LNET_PING_FEAT_INVAL == 0);
447 CLASSERT(LNET_PING_FEAT_BASE == 1);
448 CLASSERT(LNET_PING_FEAT_NI_STATUS == 2);
449 CLASSERT(LNET_PING_FEAT_RTE_DISABLED == 4);
450 CLASSERT(LNET_PING_FEAT_MULTI_RAIL == 8);
451 CLASSERT(LNET_PING_FEAT_DISCOVERY == 16);
452 CLASSERT(LNET_PING_FEAT_BITS == 31);
454 /* Checks for struct lnet_ping_info */
455 CLASSERT((int)sizeof(struct lnet_ping_info) == 16);
456 CLASSERT((int)offsetof(struct lnet_ping_info, pi_magic) == 0);
457 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) == 4);
458 CLASSERT((int)offsetof(struct lnet_ping_info, pi_features) == 4);
459 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_features) == 4);
460 CLASSERT((int)offsetof(struct lnet_ping_info, pi_pid) == 8);
461 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) == 4);
462 CLASSERT((int)offsetof(struct lnet_ping_info, pi_nnis) == 12);
463 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) == 4);
464 CLASSERT((int)offsetof(struct lnet_ping_info, pi_ni) == 16);
465 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) == 0);
468 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
470 struct lnet_lnd *lnd;
471 struct list_head *tmp;
473 /* holding lnd mutex */
474 list_for_each(tmp, &the_lnet.ln_lnds) {
475 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
477 if (lnd->lnd_type == type)
484 lnet_register_lnd(struct lnet_lnd *lnd)
486 mutex_lock(&the_lnet.ln_lnd_mutex);
488 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
489 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
491 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
492 lnd->lnd_refcount = 0;
494 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
496 mutex_unlock(&the_lnet.ln_lnd_mutex);
498 EXPORT_SYMBOL(lnet_register_lnd);
501 lnet_unregister_lnd(struct lnet_lnd *lnd)
503 mutex_lock(&the_lnet.ln_lnd_mutex);
505 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
506 LASSERT(lnd->lnd_refcount == 0);
508 list_del(&lnd->lnd_list);
509 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
511 mutex_unlock(&the_lnet.ln_lnd_mutex);
513 EXPORT_SYMBOL(lnet_unregister_lnd);
516 lnet_counters_get(struct lnet_counters *counters)
518 struct lnet_counters *ctr;
521 memset(counters, 0, sizeof(*counters));
523 lnet_net_lock(LNET_LOCK_EX);
525 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
526 counters->msgs_max += ctr->msgs_max;
527 counters->msgs_alloc += ctr->msgs_alloc;
528 counters->errors += ctr->errors;
529 counters->send_count += ctr->send_count;
530 counters->recv_count += ctr->recv_count;
531 counters->route_count += ctr->route_count;
532 counters->drop_count += ctr->drop_count;
533 counters->send_length += ctr->send_length;
534 counters->recv_length += ctr->recv_length;
535 counters->route_length += ctr->route_length;
536 counters->drop_length += ctr->drop_length;
539 lnet_net_unlock(LNET_LOCK_EX);
541 EXPORT_SYMBOL(lnet_counters_get);
544 lnet_counters_reset(void)
546 struct lnet_counters *counters;
549 lnet_net_lock(LNET_LOCK_EX);
551 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
552 memset(counters, 0, sizeof(struct lnet_counters));
554 lnet_net_unlock(LNET_LOCK_EX);
558 lnet_res_type2str(int type)
563 case LNET_COOKIE_TYPE_MD:
565 case LNET_COOKIE_TYPE_ME:
567 case LNET_COOKIE_TYPE_EQ:
573 lnet_res_container_cleanup(struct lnet_res_container *rec)
577 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
580 while (!list_empty(&rec->rec_active)) {
581 struct list_head *e = rec->rec_active.next;
584 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
585 lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
587 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
588 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
590 } else { /* NB: Active MEs should be attached on portals */
597 /* Found alive MD/ME/EQ, user really should unlink/free
598 * all of them before finalize LNet, but if someone didn't,
599 * we have to recycle garbage for him */
600 CERROR("%d active elements on exit of %s container\n",
601 count, lnet_res_type2str(rec->rec_type));
604 if (rec->rec_lh_hash != NULL) {
605 LIBCFS_FREE(rec->rec_lh_hash,
606 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
607 rec->rec_lh_hash = NULL;
610 rec->rec_type = 0; /* mark it as finalized */
614 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
619 LASSERT(rec->rec_type == 0);
621 rec->rec_type = type;
622 INIT_LIST_HEAD(&rec->rec_active);
624 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
626 /* Arbitrary choice of hash table size */
627 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
628 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
629 if (rec->rec_lh_hash == NULL) {
634 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
635 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
640 CERROR("Failed to setup %s resource container\n",
641 lnet_res_type2str(type));
642 lnet_res_container_cleanup(rec);
647 lnet_res_containers_destroy(struct lnet_res_container **recs)
649 struct lnet_res_container *rec;
652 cfs_percpt_for_each(rec, i, recs)
653 lnet_res_container_cleanup(rec);
655 cfs_percpt_free(recs);
658 static struct lnet_res_container **
659 lnet_res_containers_create(int type)
661 struct lnet_res_container **recs;
662 struct lnet_res_container *rec;
666 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
668 CERROR("Failed to allocate %s resource containers\n",
669 lnet_res_type2str(type));
673 cfs_percpt_for_each(rec, i, recs) {
674 rc = lnet_res_container_setup(rec, i, type);
676 lnet_res_containers_destroy(recs);
684 struct lnet_libhandle *
685 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
687 /* ALWAYS called with lnet_res_lock held */
688 struct list_head *head;
689 struct lnet_libhandle *lh;
692 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
695 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
696 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
698 list_for_each_entry(lh, head, lh_hash_chain) {
699 if (lh->lh_cookie == cookie)
707 lnet_res_lh_initialize(struct lnet_res_container *rec,
708 struct lnet_libhandle *lh)
710 /* ALWAYS called with lnet_res_lock held */
711 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
714 lh->lh_cookie = rec->rec_lh_cookie;
715 rec->rec_lh_cookie += 1 << ibits;
717 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
719 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
722 static int lnet_unprepare(void);
725 lnet_prepare(lnet_pid_t requested_pid)
727 /* Prepare to bring up the network */
728 struct lnet_res_container **recs;
731 if (requested_pid == LNET_PID_ANY) {
732 /* Don't instantiate LNET just for me */
736 LASSERT(the_lnet.ln_refcount == 0);
738 the_lnet.ln_routing = 0;
740 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
741 the_lnet.ln_pid = requested_pid;
743 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
744 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
745 INIT_LIST_HEAD(&the_lnet.ln_nets);
746 INIT_LIST_HEAD(&the_lnet.ln_routers);
747 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
748 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
749 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
750 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
751 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
752 init_waitqueue_head(&the_lnet.ln_dc_waitq);
754 rc = lnet_descriptor_setup();
758 rc = lnet_create_remote_nets_table();
763 * NB the interface cookie in wire handles guards against delayed
764 * replies and ACKs appearing valid after reboot.
766 the_lnet.ln_interface_cookie = ktime_get_real_ns();
768 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
769 sizeof(struct lnet_counters));
770 if (the_lnet.ln_counters == NULL) {
771 CERROR("Failed to allocate counters for LNet\n");
776 rc = lnet_peer_tables_create();
780 rc = lnet_msg_containers_create();
784 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
785 LNET_COOKIE_TYPE_EQ);
789 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
795 the_lnet.ln_me_containers = recs;
797 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
803 the_lnet.ln_md_containers = recs;
805 rc = lnet_portals_create();
807 CERROR("Failed to create portals for LNet: %d\n", rc);
819 lnet_unprepare (void)
821 /* NB no LNET_LOCK since this is the last reference. All LND instances
822 * have shut down already, so it is safe to unlink and free all
823 * descriptors, even those that appear committed to a network op (eg MD
824 * with non-zero pending count) */
826 lnet_fail_nid(LNET_NID_ANY, 0);
828 LASSERT(the_lnet.ln_refcount == 0);
829 LASSERT(list_empty(&the_lnet.ln_test_peers));
830 LASSERT(list_empty(&the_lnet.ln_nets));
832 lnet_portals_destroy();
834 if (the_lnet.ln_md_containers != NULL) {
835 lnet_res_containers_destroy(the_lnet.ln_md_containers);
836 the_lnet.ln_md_containers = NULL;
839 if (the_lnet.ln_me_containers != NULL) {
840 lnet_res_containers_destroy(the_lnet.ln_me_containers);
841 the_lnet.ln_me_containers = NULL;
844 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
846 lnet_msg_containers_destroy();
848 lnet_rtrpools_free(0);
850 if (the_lnet.ln_counters != NULL) {
851 cfs_percpt_free(the_lnet.ln_counters);
852 the_lnet.ln_counters = NULL;
854 lnet_destroy_remote_nets_table();
855 lnet_descriptor_cleanup();
861 lnet_net2ni_locked(__u32 net_id, int cpt)
864 struct lnet_net *net;
866 LASSERT(cpt != LNET_LOCK_EX);
868 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
869 if (net->net_id == net_id) {
870 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
880 lnet_net2ni_addref(__u32 net)
885 ni = lnet_net2ni_locked(net, 0);
887 lnet_ni_addref_locked(ni, 0);
892 EXPORT_SYMBOL(lnet_net2ni_addref);
895 lnet_get_net_locked(__u32 net_id)
897 struct lnet_net *net;
899 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
900 if (net->net_id == net_id)
908 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
913 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
918 val = hash_long(key, LNET_CPT_BITS);
919 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
923 return (unsigned int)(key + val + (val >> 1)) % number;
927 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
929 struct lnet_net *net;
931 /* must called with hold of lnet_net_lock */
932 if (LNET_CPT_NUMBER == 1)
933 return 0; /* the only one */
936 * If NI is provided then use the CPT identified in the NI cpt
937 * list if one exists. If one doesn't exist, then that NI is
938 * associated with all CPTs and it follows that the net it belongs
939 * to is implicitly associated with all CPTs, so just hash the nid
943 if (ni->ni_cpts != NULL)
944 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
947 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
950 /* no NI provided so look at the net */
951 net = lnet_get_net_locked(LNET_NIDNET(nid));
953 if (net != NULL && net->net_cpts != NULL) {
954 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
957 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
961 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
966 if (LNET_CPT_NUMBER == 1)
967 return 0; /* the only one */
969 cpt = lnet_net_lock_current();
971 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
973 lnet_net_unlock(cpt);
977 EXPORT_SYMBOL(lnet_cpt_of_nid);
980 lnet_islocalnet(__u32 net_id)
982 struct lnet_net *net;
986 cpt = lnet_net_lock_current();
988 net = lnet_get_net_locked(net_id);
992 lnet_net_unlock(cpt);
998 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
1000 if (ni->ni_state == LNET_NI_STATE_ACTIVE ||
1001 ni->ni_state == LNET_NI_STATE_DEGRADED)
1008 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1010 struct lnet_net *net;
1013 LASSERT(cpt != LNET_LOCK_EX);
1015 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1016 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1017 if (ni->ni_nid == nid)
1026 lnet_nid2ni_addref(lnet_nid_t nid)
1031 ni = lnet_nid2ni_locked(nid, 0);
1033 lnet_ni_addref_locked(ni, 0);
1038 EXPORT_SYMBOL(lnet_nid2ni_addref);
1041 lnet_islocalnid(lnet_nid_t nid)
1046 cpt = lnet_net_lock_current();
1047 ni = lnet_nid2ni_locked(nid, cpt);
1048 lnet_net_unlock(cpt);
1054 lnet_count_acceptor_nets(void)
1056 /* Return the # of NIs that need the acceptor. */
1058 struct lnet_net *net;
1061 cpt = lnet_net_lock_current();
1062 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1063 /* all socklnd type networks should have the acceptor
1065 if (net->net_lnd->lnd_accept != NULL)
1069 lnet_net_unlock(cpt);
1074 struct lnet_ping_buffer *
1075 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1077 struct lnet_ping_buffer *pbuf;
1079 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1081 pbuf->pb_nnis = nnis;
1082 atomic_set(&pbuf->pb_refcnt, 1);
1089 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1091 LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1092 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1095 static struct lnet_ping_buffer *
1096 lnet_ping_target_create(int nnis)
1098 struct lnet_ping_buffer *pbuf;
1100 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1102 CERROR("Can't allocate ping source [%d]\n", nnis);
1106 pbuf->pb_info.pi_nnis = nnis;
1107 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1108 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1109 pbuf->pb_info.pi_features =
1110 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1116 lnet_get_net_ni_count_locked(struct lnet_net *net)
1121 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1128 lnet_get_net_ni_count_pre(struct lnet_net *net)
1133 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1140 lnet_get_ni_count(void)
1143 struct lnet_net *net;
1148 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1149 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1159 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1163 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1165 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1167 /* Loopback is guaranteed to be present */
1168 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1170 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1176 lnet_ping_target_destroy(void)
1178 struct lnet_net *net;
1181 lnet_net_lock(LNET_LOCK_EX);
1183 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1184 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1186 ni->ni_status = NULL;
1191 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1192 the_lnet.ln_ping_target = NULL;
1194 lnet_net_unlock(LNET_LOCK_EX);
1198 lnet_ping_target_event_handler(struct lnet_event *event)
1200 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1202 if (event->unlinked)
1203 lnet_ping_buffer_decref(pbuf);
1207 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1208 struct lnet_handle_md *ping_mdh,
1209 int ni_count, bool set_eq)
1211 struct lnet_process_id id = {
1212 .nid = LNET_NID_ANY,
1215 struct lnet_handle_me me_handle;
1216 struct lnet_md md = { NULL };
1220 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1221 &the_lnet.ln_ping_target_eq);
1223 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1228 *ppbuf = lnet_ping_target_create(ni_count);
1229 if (*ppbuf == NULL) {
1234 /* Ping target ME/MD */
1235 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1236 LNET_PROTO_PING_MATCHBITS, 0,
1237 LNET_UNLINK, LNET_INS_AFTER,
1240 CERROR("Can't create ping target ME: %d\n", rc);
1241 goto fail_decref_ping_buffer;
1244 /* initialize md content */
1245 md.start = &(*ppbuf)->pb_info;
1246 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1247 md.threshold = LNET_MD_THRESH_INF;
1249 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1250 LNET_MD_MANAGE_REMOTE;
1251 md.eq_handle = the_lnet.ln_ping_target_eq;
1252 md.user_ptr = *ppbuf;
1254 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, ping_mdh);
1256 CERROR("Can't attach ping target MD: %d\n", rc);
1257 goto fail_unlink_ping_me;
1259 lnet_ping_buffer_addref(*ppbuf);
1263 fail_unlink_ping_me:
1264 rc2 = LNetMEUnlink(me_handle);
1266 fail_decref_ping_buffer:
1267 LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1268 lnet_ping_buffer_decref(*ppbuf);
1272 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1279 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1280 struct lnet_handle_md *ping_mdh)
1282 sigset_t blocked = cfs_block_allsigs();
1284 LNetMDUnlink(*ping_mdh);
1285 LNetInvalidateMDHandle(ping_mdh);
1287 /* NB the MD could be busy; this just starts the unlink */
1288 while (lnet_ping_buffer_numref(pbuf) > 1) {
1289 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1290 set_current_state(TASK_UNINTERRUPTIBLE);
1291 schedule_timeout(cfs_time_seconds(1));
1294 cfs_restore_sigs(blocked);
1298 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1301 struct lnet_net *net;
1302 struct lnet_ni_status *ns;
1307 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1308 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1309 LASSERT(i < pbuf->pb_nnis);
1311 ns = &pbuf->pb_info.pi_ni[i];
1313 ns->ns_nid = ni->ni_nid;
1316 ns->ns_status = (ni->ni_status != NULL) ?
1317 ni->ni_status->ns_status :
1326 * We (ab)use the ns_status of the loopback interface to
1327 * transmit the sequence number. The first interface listed
1328 * must be the loopback interface.
1330 rc = lnet_ping_info_validate(&pbuf->pb_info);
1332 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1335 LNET_PING_BUFFER_SEQNO(pbuf) =
1336 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1340 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1341 struct lnet_handle_md ping_mdh)
1343 struct lnet_ping_buffer *old_pbuf = NULL;
1344 struct lnet_handle_md old_ping_md;
1346 /* switch the NIs to point to the new ping info created */
1347 lnet_net_lock(LNET_LOCK_EX);
1349 if (!the_lnet.ln_routing)
1350 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1351 if (!lnet_peer_discovery_disabled)
1352 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1354 /* Ensure only known feature bits have been set. */
1355 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1356 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1358 lnet_ping_target_install_locked(pbuf);
1360 if (the_lnet.ln_ping_target) {
1361 old_pbuf = the_lnet.ln_ping_target;
1362 old_ping_md = the_lnet.ln_ping_target_md;
1364 the_lnet.ln_ping_target_md = ping_mdh;
1365 the_lnet.ln_ping_target = pbuf;
1367 lnet_net_unlock(LNET_LOCK_EX);
1370 /* unlink and free the old ping info */
1371 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1372 lnet_ping_buffer_decref(old_pbuf);
1375 lnet_push_update_to_peers(0);
1379 lnet_ping_target_fini(void)
1383 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1384 &the_lnet.ln_ping_target_md);
1386 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1389 lnet_ping_target_destroy();
1392 /* Resize the push target. */
1393 int lnet_push_target_resize(void)
1395 struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1396 struct lnet_md md = { NULL };
1397 struct lnet_handle_me meh;
1398 struct lnet_handle_md mdh;
1399 struct lnet_handle_md old_mdh;
1400 struct lnet_ping_buffer *pbuf;
1401 struct lnet_ping_buffer *old_pbuf;
1402 int nnis = the_lnet.ln_push_target_nnis;
1410 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1416 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1417 LNET_PROTO_PING_MATCHBITS, 0,
1418 LNET_UNLINK, LNET_INS_AFTER,
1421 CERROR("Can't create push target ME: %d\n", rc);
1422 goto fail_decref_pbuf;
1425 /* initialize md content */
1426 md.start = &pbuf->pb_info;
1427 md.length = LNET_PING_INFO_SIZE(nnis);
1428 md.threshold = LNET_MD_THRESH_INF;
1430 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1431 LNET_MD_MANAGE_REMOTE;
1433 md.eq_handle = the_lnet.ln_push_target_eq;
1435 rc = LNetMDAttach(meh, md, LNET_RETAIN, &mdh);
1437 CERROR("Can't attach push MD: %d\n", rc);
1438 goto fail_unlink_meh;
1440 lnet_ping_buffer_addref(pbuf);
1442 lnet_net_lock(LNET_LOCK_EX);
1443 old_pbuf = the_lnet.ln_push_target;
1444 old_mdh = the_lnet.ln_push_target_md;
1445 the_lnet.ln_push_target = pbuf;
1446 the_lnet.ln_push_target_md = mdh;
1447 lnet_net_unlock(LNET_LOCK_EX);
1450 LNetMDUnlink(old_mdh);
1451 lnet_ping_buffer_decref(old_pbuf);
1454 if (nnis < the_lnet.ln_push_target_nnis)
1457 CDEBUG(D_NET, "nnis %d success\n", nnis);
1464 lnet_ping_buffer_decref(pbuf);
1466 CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1470 static void lnet_push_target_event_handler(struct lnet_event *ev)
1472 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1474 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1475 lnet_swap_pinginfo(pbuf);
1477 lnet_peer_push_event(ev);
1479 lnet_ping_buffer_decref(pbuf);
1482 /* Initialize the push target. */
1483 static int lnet_push_target_init(void)
1487 if (the_lnet.ln_push_target)
1490 rc = LNetEQAlloc(0, lnet_push_target_event_handler,
1491 &the_lnet.ln_push_target_eq);
1493 CERROR("Can't allocated push target EQ: %d\n", rc);
1497 /* Start at the required minimum, we'll enlarge if required. */
1498 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1500 rc = lnet_push_target_resize();
1503 LNetEQFree(the_lnet.ln_push_target_eq);
1504 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1510 /* Clean up the push target. */
1511 static void lnet_push_target_fini(void)
1513 if (!the_lnet.ln_push_target)
1516 /* Unlink and invalidate to prevent new references. */
1517 LNetMDUnlink(the_lnet.ln_push_target_md);
1518 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1520 /* Wait for the unlink to complete. */
1521 while (lnet_ping_buffer_numref(the_lnet.ln_push_target) > 1) {
1522 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1523 set_current_state(TASK_UNINTERRUPTIBLE);
1524 schedule_timeout(cfs_time_seconds(1));
1527 lnet_ping_buffer_decref(the_lnet.ln_push_target);
1528 the_lnet.ln_push_target = NULL;
1529 the_lnet.ln_push_target_nnis = 0;
1531 LNetEQFree(the_lnet.ln_push_target_eq);
1532 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1536 lnet_ni_tq_credits(struct lnet_ni *ni)
1540 LASSERT(ni->ni_ncpts >= 1);
1542 if (ni->ni_ncpts == 1)
1543 return ni->ni_net->net_tunables.lct_max_tx_credits;
1545 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1546 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1547 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1553 lnet_ni_unlink_locked(struct lnet_ni *ni)
1555 if (!list_empty(&ni->ni_cptlist)) {
1556 list_del_init(&ni->ni_cptlist);
1557 lnet_ni_decref_locked(ni, 0);
1560 /* move it to zombie list and nobody can find it anymore */
1561 LASSERT(!list_empty(&ni->ni_netlist));
1562 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1563 lnet_ni_decref_locked(ni, 0);
1567 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1572 struct list_head *zombie_list = &net->net_ni_zombie;
1575 * Now wait for the NIs I just nuked to show up on the zombie
1576 * list and shut them down in guaranteed thread context
1579 while (!list_empty(zombie_list)) {
1583 ni = list_entry(zombie_list->next,
1584 struct lnet_ni, ni_netlist);
1585 list_del_init(&ni->ni_netlist);
1586 /* the ni should be in deleting state. If it's not it's
1588 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
1589 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1592 /* still busy, add it back to zombie list */
1593 list_add(&ni->ni_netlist, zombie_list);
1597 if (!list_empty(&ni->ni_netlist)) {
1598 lnet_net_unlock(LNET_LOCK_EX);
1600 if ((i & (-i)) == i) {
1602 "Waiting for zombie LNI %s\n",
1603 libcfs_nid2str(ni->ni_nid));
1605 set_current_state(TASK_UNINTERRUPTIBLE);
1606 schedule_timeout(cfs_time_seconds(1));
1607 lnet_net_lock(LNET_LOCK_EX);
1611 lnet_net_unlock(LNET_LOCK_EX);
1613 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1615 LASSERT(!in_interrupt());
1616 (net->net_lnd->lnd_shutdown)(ni);
1619 CDEBUG(D_LNI, "Removed LNI %s\n",
1620 libcfs_nid2str(ni->ni_nid));
1624 lnet_net_lock(LNET_LOCK_EX);
1628 /* shutdown down the NI and release refcount */
1630 lnet_shutdown_lndni(struct lnet_ni *ni)
1633 struct lnet_net *net = ni->ni_net;
1635 lnet_net_lock(LNET_LOCK_EX);
1636 ni->ni_state = LNET_NI_STATE_DELETING;
1637 lnet_ni_unlink_locked(ni);
1638 lnet_incr_dlc_seq();
1639 lnet_net_unlock(LNET_LOCK_EX);
1641 /* clear messages for this NI on the lazy portal */
1642 for (i = 0; i < the_lnet.ln_nportals; i++)
1643 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1645 lnet_net_lock(LNET_LOCK_EX);
1646 lnet_clear_zombies_nis_locked(net);
1647 lnet_net_unlock(LNET_LOCK_EX);
1651 lnet_shutdown_lndnet(struct lnet_net *net)
1655 lnet_net_lock(LNET_LOCK_EX);
1657 net->net_state = LNET_NET_STATE_DELETING;
1659 list_del_init(&net->net_list);
1661 while (!list_empty(&net->net_ni_list)) {
1662 ni = list_entry(net->net_ni_list.next,
1663 struct lnet_ni, ni_netlist);
1664 lnet_net_unlock(LNET_LOCK_EX);
1665 lnet_shutdown_lndni(ni);
1666 lnet_net_lock(LNET_LOCK_EX);
1669 lnet_net_unlock(LNET_LOCK_EX);
1671 /* Do peer table cleanup for this net */
1672 lnet_peer_tables_cleanup(net);
1674 lnet_net_lock(LNET_LOCK_EX);
1676 * decrement ref count on lnd only when the entire network goes
1679 net->net_lnd->lnd_refcount--;
1681 lnet_net_unlock(LNET_LOCK_EX);
1687 lnet_shutdown_lndnets(void)
1689 struct lnet_net *net;
1690 struct list_head resend;
1691 struct lnet_msg *msg, *tmp;
1693 INIT_LIST_HEAD(&resend);
1695 /* NB called holding the global mutex */
1697 /* All quiet on the API front */
1698 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
1699 LASSERT(the_lnet.ln_refcount == 0);
1701 lnet_net_lock(LNET_LOCK_EX);
1702 the_lnet.ln_state = LNET_STATE_STOPPING;
1704 while (!list_empty(&the_lnet.ln_nets)) {
1706 * move the nets to the zombie list to avoid them being
1707 * picked up for new work. LONET is also included in the
1708 * Nets that will be moved to the zombie list
1710 net = list_entry(the_lnet.ln_nets.next,
1711 struct lnet_net, net_list);
1712 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1715 /* Drop the cached loopback Net. */
1716 if (the_lnet.ln_loni != NULL) {
1717 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1718 the_lnet.ln_loni = NULL;
1720 lnet_net_unlock(LNET_LOCK_EX);
1722 /* iterate through the net zombie list and delete each net */
1723 while (!list_empty(&the_lnet.ln_net_zombie)) {
1724 net = list_entry(the_lnet.ln_net_zombie.next,
1725 struct lnet_net, net_list);
1726 lnet_shutdown_lndnet(net);
1729 spin_lock(&the_lnet.ln_msg_resend_lock);
1730 list_splice(&the_lnet.ln_msg_resend, &resend);
1731 spin_unlock(&the_lnet.ln_msg_resend_lock);
1733 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
1734 list_del_init(&msg->msg_list);
1735 lnet_finalize(msg, -ECANCELED);
1738 lnet_net_lock(LNET_LOCK_EX);
1739 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
1740 lnet_net_unlock(LNET_LOCK_EX);
1744 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1747 struct lnet_tx_queue *tq;
1749 struct lnet_net *net = ni->ni_net;
1751 mutex_lock(&the_lnet.ln_lnd_mutex);
1754 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1755 ni->ni_lnd_tunables_set = true;
1758 rc = (net->net_lnd->lnd_startup)(ni);
1760 mutex_unlock(&the_lnet.ln_lnd_mutex);
1763 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1764 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1765 lnet_net_lock(LNET_LOCK_EX);
1766 net->net_lnd->lnd_refcount--;
1767 lnet_net_unlock(LNET_LOCK_EX);
1771 ni->ni_state = LNET_NI_STATE_ACTIVE;
1773 /* We keep a reference on the loopback net through the loopback NI */
1774 if (net->net_lnd->lnd_type == LOLND) {
1776 LASSERT(the_lnet.ln_loni == NULL);
1777 the_lnet.ln_loni = ni;
1778 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
1779 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
1780 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
1781 ni->ni_net->net_tunables.lct_peer_timeout = 0;
1785 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
1786 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
1787 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1788 libcfs_lnd2str(net->net_lnd->lnd_type),
1789 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
1791 /* shutdown the NI since if we get here then it must've already
1794 lnet_shutdown_lndni(ni);
1798 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1799 tq->tq_credits_min =
1800 tq->tq_credits_max =
1801 tq->tq_credits = lnet_ni_tq_credits(ni);
1804 atomic_set(&ni->ni_tx_credits,
1805 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
1807 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1808 libcfs_nid2str(ni->ni_nid),
1809 ni->ni_net->net_tunables.lct_peer_tx_credits,
1810 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1811 ni->ni_net->net_tunables.lct_peer_rtr_credits,
1812 ni->ni_net->net_tunables.lct_peer_timeout);
1821 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
1824 struct lnet_net *net_l = NULL;
1825 struct list_head local_ni_list;
1829 struct lnet_lnd *lnd;
1831 net->net_tunables.lct_peer_timeout;
1833 net->net_tunables.lct_max_tx_credits;
1834 int peerrtrcredits =
1835 net->net_tunables.lct_peer_rtr_credits;
1837 INIT_LIST_HEAD(&local_ni_list);
1840 * make sure that this net is unique. If it isn't then
1841 * we are adding interfaces to an already existing network, and
1842 * 'net' is just a convenient way to pass in the list.
1843 * if it is unique we need to find the LND and load it if
1846 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
1847 lnd_type = LNET_NETTYP(net->net_id);
1849 LASSERT(libcfs_isknown_lnd(lnd_type));
1851 mutex_lock(&the_lnet.ln_lnd_mutex);
1852 lnd = lnet_find_lnd_by_type(lnd_type);
1855 mutex_unlock(&the_lnet.ln_lnd_mutex);
1856 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1857 mutex_lock(&the_lnet.ln_lnd_mutex);
1859 lnd = lnet_find_lnd_by_type(lnd_type);
1861 mutex_unlock(&the_lnet.ln_lnd_mutex);
1862 CERROR("Can't load LND %s, module %s, rc=%d\n",
1863 libcfs_lnd2str(lnd_type),
1864 libcfs_lnd2modname(lnd_type), rc);
1865 #ifndef HAVE_MODULE_LOADING_SUPPORT
1866 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1867 "compiled with kernel module "
1868 "loading support.");
1875 lnet_net_lock(LNET_LOCK_EX);
1876 lnd->lnd_refcount++;
1877 lnet_net_unlock(LNET_LOCK_EX);
1881 mutex_unlock(&the_lnet.ln_lnd_mutex);
1887 * net_l: if the network being added is unique then net_l
1888 * will point to that network
1889 * if the network being added is not unique then
1890 * net_l points to the existing network.
1892 * When we enter the loop below, we'll pick NIs off he
1893 * network beign added and start them up, then add them to
1894 * a local ni list. Once we've successfully started all
1895 * the NIs then we join the local NI list (of started up
1896 * networks) with the net_l->net_ni_list, which should
1897 * point to the correct network to add the new ni list to
1899 * If any of the new NIs fail to start up, then we want to
1900 * iterate through the local ni list, which should include
1901 * any NIs which were successfully started up, and shut
1904 * After than we want to delete the network being added,
1905 * to avoid a memory leak.
1909 * When a network uses TCP bonding then all its interfaces
1910 * must be specified when the network is first defined: the
1911 * TCP bonding code doesn't allow for interfaces to be added
1914 if (net_l != net && net_l != NULL && use_tcp_bonding &&
1915 LNET_NETTYP(net_l->net_id) == SOCKLND) {
1920 while (!list_empty(&net->net_ni_added)) {
1921 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
1923 list_del_init(&ni->ni_netlist);
1925 /* make sure that the the NI we're about to start
1926 * up is actually unique. if it's not fail. */
1927 if (!lnet_ni_unique_net(&net_l->net_ni_list,
1928 ni->ni_interfaces[0])) {
1933 /* adjust the pointer the parent network, just in case it
1934 * the net is a duplicate */
1937 rc = lnet_startup_lndni(ni, tun);
1939 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
1940 ni->ni_net->net_lnd->lnd_query != NULL);
1946 list_add_tail(&ni->ni_netlist, &local_ni_list);
1951 lnet_net_lock(LNET_LOCK_EX);
1952 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
1953 lnet_incr_dlc_seq();
1954 lnet_net_unlock(LNET_LOCK_EX);
1956 /* if the network is not unique then we don't want to keep
1957 * it around after we're done. Free it. Otherwise add that
1958 * net to the global the_lnet.ln_nets */
1959 if (net_l != net && net_l != NULL) {
1961 * TODO - note. currently the tunables can not be updated
1966 net->net_state = LNET_NET_STATE_ACTIVE;
1968 * restore tunables after it has been overwitten by the
1971 if (peer_timeout != -1)
1972 net->net_tunables.lct_peer_timeout = peer_timeout;
1973 if (maxtxcredits != -1)
1974 net->net_tunables.lct_max_tx_credits = maxtxcredits;
1975 if (peerrtrcredits != -1)
1976 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
1978 lnet_net_lock(LNET_LOCK_EX);
1979 list_add_tail(&net->net_list, &the_lnet.ln_nets);
1980 lnet_net_unlock(LNET_LOCK_EX);
1987 * shutdown the new NIs that are being started up
1988 * free the NET being started
1990 while (!list_empty(&local_ni_list)) {
1991 ni = list_entry(local_ni_list.next, struct lnet_ni,
1994 lnet_shutdown_lndni(ni);
2004 lnet_startup_lndnets(struct list_head *netlist)
2006 struct lnet_net *net;
2011 * Change to running state before bringing up the LNDs. This
2012 * allows lnet_shutdown_lndnets() to assert that we've passed
2015 lnet_net_lock(LNET_LOCK_EX);
2016 the_lnet.ln_state = LNET_STATE_RUNNING;
2017 lnet_net_unlock(LNET_LOCK_EX);
2019 while (!list_empty(netlist)) {
2020 net = list_entry(netlist->next, struct lnet_net, net_list);
2021 list_del_init(&net->net_list);
2023 rc = lnet_startup_lndnet(net, NULL);
2033 lnet_shutdown_lndnets();
2039 * Initialize LNet library.
2041 * Automatically called at module loading time. Caller has to call
2042 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2043 * latter returned 0. It must be called exactly once.
2045 * \retval 0 on success
2046 * \retval -ve on failures.
2048 int lnet_lib_init(void)
2052 lnet_assert_wire_constants();
2054 /* refer to global cfs_cpt_table for now */
2055 the_lnet.ln_cpt_table = cfs_cpt_table;
2056 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
2058 LASSERT(the_lnet.ln_cpt_number > 0);
2059 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2060 /* we are under risk of consuming all lh_cookie */
2061 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2062 "please change setting of CPT-table and retry\n",
2063 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2067 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2068 the_lnet.ln_cpt_bits++;
2070 rc = lnet_create_locks();
2072 CERROR("Can't create LNet global locks: %d\n", rc);
2076 the_lnet.ln_refcount = 0;
2077 LNetInvalidateEQHandle(&the_lnet.ln_rc_eqh);
2078 INIT_LIST_HEAD(&the_lnet.ln_lnds);
2079 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2080 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
2081 INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2082 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
2084 /* The hash table size is the number of bits it takes to express the set
2085 * ln_num_routes, minus 1 (better to under estimate than over so we
2086 * don't waste memory). */
2087 if (rnet_htable_size <= 0)
2088 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2089 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2090 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2091 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2092 order_base_2(rnet_htable_size) - 1);
2094 /* All LNDs apart from the LOLND are in separate modules. They
2095 * register themselves when their module loads, and unregister
2096 * themselves when their module is unloaded. */
2097 lnet_register_lnd(&the_lolnd);
2102 * Finalize LNet library.
2104 * \pre lnet_lib_init() called with success.
2105 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2107 void lnet_lib_exit(void)
2109 LASSERT(the_lnet.ln_refcount == 0);
2111 while (!list_empty(&the_lnet.ln_lnds))
2112 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
2113 struct lnet_lnd, lnd_list));
2114 lnet_destroy_locks();
2118 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2120 * Users must call this function at least once before any other functions.
2121 * For each successful call there must be a corresponding call to
2122 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2125 * The PID used by LNet may be different from the one requested.
2128 * \param requested_pid PID requested by the caller.
2130 * \return >= 0 on success, and < 0 error code on failures.
2133 LNetNIInit(lnet_pid_t requested_pid)
2135 int im_a_router = 0;
2138 struct lnet_ping_buffer *pbuf;
2139 struct lnet_handle_md ping_mdh;
2140 struct list_head net_head;
2141 struct lnet_net *net;
2143 INIT_LIST_HEAD(&net_head);
2145 mutex_lock(&the_lnet.ln_api_mutex);
2147 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2149 if (the_lnet.ln_refcount > 0) {
2150 rc = the_lnet.ln_refcount++;
2151 mutex_unlock(&the_lnet.ln_api_mutex);
2155 rc = lnet_prepare(requested_pid);
2157 mutex_unlock(&the_lnet.ln_api_mutex);
2161 /* create a network for Loopback network */
2162 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2165 goto err_empty_list;
2168 /* Add in the loopback NI */
2169 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2171 goto err_empty_list;
2174 /* If LNet is being initialized via DLC it is possible
2175 * that the user requests not to load module parameters (ones which
2176 * are supported by DLC) on initialization. Therefore, make sure not
2177 * to load networks, routes and forwarding from module parameters
2178 * in this case. On cleanup in case of failure only clean up
2179 * routes if it has been loaded */
2180 if (!the_lnet.ln_nis_from_mod_params) {
2181 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2184 goto err_empty_list;
2187 ni_count = lnet_startup_lndnets(&net_head);
2190 goto err_empty_list;
2193 if (!the_lnet.ln_nis_from_mod_params) {
2194 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2196 goto err_shutdown_lndnis;
2198 rc = lnet_check_routes();
2200 goto err_destroy_routes;
2202 rc = lnet_rtrpools_alloc(im_a_router);
2204 goto err_destroy_routes;
2207 rc = lnet_acceptor_start();
2209 goto err_destroy_routes;
2211 the_lnet.ln_refcount = 1;
2212 /* Now I may use my own API functions... */
2214 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2216 goto err_acceptor_stop;
2218 lnet_ping_target_update(pbuf, ping_mdh);
2220 rc = lnet_router_checker_start();
2224 rc = lnet_push_target_init();
2226 goto err_stop_router_checker;
2228 rc = lnet_peer_discovery_start();
2230 goto err_destroy_push_target;
2233 lnet_router_debugfs_init();
2235 mutex_unlock(&the_lnet.ln_api_mutex);
2239 err_destroy_push_target:
2240 lnet_push_target_fini();
2241 err_stop_router_checker:
2242 lnet_router_checker_stop();
2244 lnet_ping_target_fini();
2246 the_lnet.ln_refcount = 0;
2247 lnet_acceptor_stop();
2249 if (!the_lnet.ln_nis_from_mod_params)
2250 lnet_destroy_routes();
2251 err_shutdown_lndnis:
2252 lnet_shutdown_lndnets();
2256 mutex_unlock(&the_lnet.ln_api_mutex);
2257 while (!list_empty(&net_head)) {
2258 struct lnet_net *net;
2260 net = list_entry(net_head.next, struct lnet_net, net_list);
2261 list_del_init(&net->net_list);
2266 EXPORT_SYMBOL(LNetNIInit);
2269 * Stop LNet interfaces, routing, and forwarding.
2271 * Users must call this function once for each successful call to LNetNIInit().
2272 * Once the LNetNIFini() operation has been started, the results of pending
2273 * API operations are undefined.
2275 * \return always 0 for current implementation.
2280 mutex_lock(&the_lnet.ln_api_mutex);
2282 LASSERT(the_lnet.ln_refcount > 0);
2284 if (the_lnet.ln_refcount != 1) {
2285 the_lnet.ln_refcount--;
2287 LASSERT(!the_lnet.ln_niinit_self);
2291 lnet_router_debugfs_init();
2292 lnet_peer_discovery_stop();
2293 lnet_push_target_fini();
2294 lnet_router_checker_stop();
2295 lnet_ping_target_fini();
2297 /* Teardown fns that use my own API functions BEFORE here */
2298 the_lnet.ln_refcount = 0;
2300 lnet_acceptor_stop();
2301 lnet_destroy_routes();
2302 lnet_shutdown_lndnets();
2306 mutex_unlock(&the_lnet.ln_api_mutex);
2309 EXPORT_SYMBOL(LNetNIFini);
2312 * Grabs the ni data from the ni structure and fills the out
2315 * \param[in] ni network interface structure
2316 * \param[out] cfg_ni NI config information
2317 * \param[out] tun network and LND tunables
2320 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2321 struct lnet_ioctl_config_lnd_tunables *tun,
2322 struct lnet_ioctl_element_stats *stats,
2325 size_t min_size = 0;
2328 if (!ni || !cfg_ni || !tun)
2331 if (ni->ni_interfaces[0] != NULL) {
2332 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2333 if (ni->ni_interfaces[i] != NULL) {
2334 strncpy(cfg_ni->lic_ni_intf[i],
2335 ni->ni_interfaces[i],
2336 sizeof(cfg_ni->lic_ni_intf[i]));
2341 cfg_ni->lic_nid = ni->ni_nid;
2342 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2343 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2345 cfg_ni->lic_status = ni->ni_status->ns_status;
2346 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2347 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2349 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2352 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2353 LNET_STATS_TYPE_SEND);
2354 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2355 LNET_STATS_TYPE_RECV);
2356 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2357 LNET_STATS_TYPE_DROP);
2361 * tun->lt_tun will always be present, but in order to be
2362 * backwards compatible, we need to deal with the cases when
2363 * tun->lt_tun is smaller than what the kernel has, because it
2364 * comes from an older version of a userspace program, then we'll
2365 * need to copy as much information as we have available space.
2367 min_size = tun_size - sizeof(tun->lt_cmn);
2368 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2370 /* copy over the cpts */
2371 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2372 ni->ni_cpts == NULL) {
2373 for (i = 0; i < ni->ni_ncpts; i++)
2374 cfg_ni->lic_cpts[i] = i;
2377 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2378 i < LNET_MAX_SHOW_NUM_CPT;
2380 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2382 cfg_ni->lic_ncpts = ni->ni_ncpts;
2386 * NOTE: This is a legacy function left in the code to be backwards
2387 * compatible with older userspace programs. It should eventually be
2390 * Grabs the ni data from the ni structure and fills the out
2393 * \param[in] ni network interface structure
2394 * \param[out] config config information
2397 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2398 struct lnet_ioctl_config_data *config)
2400 struct lnet_ioctl_net_config *net_config;
2401 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2402 size_t min_size, tunable_size = 0;
2408 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2412 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2413 ARRAY_SIZE(net_config->ni_interfaces));
2415 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2416 if (!ni->ni_interfaces[i])
2419 strncpy(net_config->ni_interfaces[i],
2420 ni->ni_interfaces[i],
2421 sizeof(net_config->ni_interfaces[i]));
2424 config->cfg_nid = ni->ni_nid;
2425 config->cfg_config_u.cfg_net.net_peer_timeout =
2426 ni->ni_net->net_tunables.lct_peer_timeout;
2427 config->cfg_config_u.cfg_net.net_max_tx_credits =
2428 ni->ni_net->net_tunables.lct_max_tx_credits;
2429 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2430 ni->ni_net->net_tunables.lct_peer_tx_credits;
2431 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2432 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2434 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2435 net_config->ni_status = LNET_NI_STATUS_UP;
2437 net_config->ni_status = ni->ni_status->ns_status;
2440 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2442 for (i = 0; i < num_cpts; i++)
2443 net_config->ni_cpts[i] = ni->ni_cpts[i];
2445 config->cfg_ncpts = num_cpts;
2449 * See if user land tools sent in a newer and larger version
2450 * of struct lnet_tunables than what the kernel uses.
2452 min_size = sizeof(*config) + sizeof(*net_config);
2454 if (config->cfg_hdr.ioc_len > min_size)
2455 tunable_size = config->cfg_hdr.ioc_len - min_size;
2457 /* Don't copy too much data to user space */
2458 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2459 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2461 if (lnd_cfg && min_size) {
2462 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2463 config->cfg_config_u.cfg_net.net_interface_count = 1;
2465 /* Tell user land that kernel side has less data */
2466 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2467 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2468 config->cfg_hdr.ioc_len -= min_size;
2474 lnet_get_ni_idx_locked(int idx)
2477 struct lnet_net *net;
2479 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2480 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2490 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2493 struct lnet_net *net = mynet;
2497 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2499 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2505 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2506 /* if you reached the end of the ni list and the net is
2507 * specified, then there are no more nis in that net */
2511 /* we reached the end of this net ni list. move to the
2513 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2514 /* no more nets and no more NIs. */
2517 /* get the next net */
2518 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2520 /* get the ni on it */
2521 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2527 /* there are more nis left */
2528 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2534 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2539 int idx = config->cfg_count;
2541 cpt = lnet_net_lock_current();
2543 ni = lnet_get_ni_idx_locked(idx);
2548 lnet_fill_ni_info_legacy(ni, config);
2552 lnet_net_unlock(cpt);
2557 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2558 struct lnet_ioctl_config_lnd_tunables *tun,
2559 struct lnet_ioctl_element_stats *stats,
2566 if (!cfg_ni || !tun || !stats)
2569 cpt = lnet_net_lock_current();
2571 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2576 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
2580 lnet_net_unlock(cpt);
2584 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
2593 cpt = lnet_net_lock_current();
2595 ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
2598 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
2602 lnet_net_unlock(cpt);
2607 static int lnet_add_net_common(struct lnet_net *net,
2608 struct lnet_ioctl_config_lnd_tunables *tun)
2611 struct lnet_ping_buffer *pbuf;
2612 struct lnet_handle_md ping_mdh;
2614 struct lnet_remotenet *rnet;
2616 int num_acceptor_nets;
2618 lnet_net_lock(LNET_LOCK_EX);
2619 rnet = lnet_find_rnet_locked(net->net_id);
2620 lnet_net_unlock(LNET_LOCK_EX);
2622 * make sure that the net added doesn't invalidate the current
2623 * configuration LNet is keeping
2626 CERROR("Adding net %s will invalidate routing configuration\n",
2627 libcfs_net2str(net->net_id));
2633 * make sure you calculate the correct number of slots in the ping
2634 * buffer. Since the ping info is a flattened list of all the NIs,
2635 * we should allocate enough slots to accomodate the number of NIs
2636 * which will be added.
2638 * since ni hasn't been configured yet, use
2639 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
2641 net_ni_count = lnet_get_net_ni_count_pre(net);
2643 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2644 net_ni_count + lnet_get_ni_count(),
2652 memcpy(&net->net_tunables,
2653 &tun->lt_cmn, sizeof(net->net_tunables));
2655 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
2658 * before starting this network get a count of the current TCP
2659 * networks which require the acceptor thread running. If that
2660 * count is == 0 before we start up this network, then we'd want to
2661 * start up the acceptor thread after starting up this network
2663 num_acceptor_nets = lnet_count_acceptor_nets();
2665 net_id = net->net_id;
2667 rc = lnet_startup_lndnet(net,
2668 (tun) ? &tun->lt_tun : NULL);
2672 lnet_net_lock(LNET_LOCK_EX);
2673 net = lnet_get_net_locked(net_id);
2674 lnet_net_unlock(LNET_LOCK_EX);
2679 * Start the acceptor thread if this is the first network
2680 * being added that requires the thread.
2682 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
2683 rc = lnet_acceptor_start();
2685 /* shutdown the net that we just started */
2686 CERROR("Failed to start up acceptor thread\n");
2687 lnet_shutdown_lndnet(net);
2692 lnet_net_lock(LNET_LOCK_EX);
2693 lnet_peer_net_added(net);
2694 lnet_net_unlock(LNET_LOCK_EX);
2696 lnet_ping_target_update(pbuf, ping_mdh);
2701 lnet_ping_md_unlink(pbuf, &ping_mdh);
2702 lnet_ping_buffer_decref(pbuf);
2706 static int lnet_handle_legacy_ip2nets(char *ip2nets,
2707 struct lnet_ioctl_config_lnd_tunables *tun)
2709 struct lnet_net *net;
2712 struct list_head net_head;
2714 INIT_LIST_HEAD(&net_head);
2716 rc = lnet_parse_ip2nets(&nets, ip2nets);
2720 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2724 mutex_lock(&the_lnet.ln_api_mutex);
2725 while (!list_empty(&net_head)) {
2726 net = list_entry(net_head.next, struct lnet_net, net_list);
2727 list_del_init(&net->net_list);
2728 rc = lnet_add_net_common(net, tun);
2734 mutex_unlock(&the_lnet.ln_api_mutex);
2736 while (!list_empty(&net_head)) {
2737 net = list_entry(net_head.next, struct lnet_net, net_list);
2738 list_del_init(&net->net_list);
2744 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
2746 struct lnet_net *net;
2748 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2752 /* get the tunables if they are available */
2753 if (conf->lic_cfg_hdr.ioc_len >=
2754 sizeof(*conf) + sizeof(*tun))
2755 tun = (struct lnet_ioctl_config_lnd_tunables *)
2758 /* handle legacy ip2nets from DLC */
2759 if (conf->lic_legacy_ip2nets[0] != '\0')
2760 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
2763 net_id = LNET_NIDNET(conf->lic_nid);
2765 net = lnet_net_alloc(net_id, NULL);
2769 for (i = 0; i < conf->lic_ncpts; i++) {
2770 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
2774 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
2775 conf->lic_ni_intf[0]);
2779 mutex_lock(&the_lnet.ln_api_mutex);
2781 rc = lnet_add_net_common(net, tun);
2783 mutex_unlock(&the_lnet.ln_api_mutex);
2788 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
2790 struct lnet_net *net;
2792 __u32 net_id = LNET_NIDNET(conf->lic_nid);
2793 struct lnet_ping_buffer *pbuf;
2794 struct lnet_handle_md ping_mdh;
2799 /* don't allow userspace to shutdown the LOLND */
2800 if (LNET_NETTYP(net_id) == LOLND)
2803 mutex_lock(&the_lnet.ln_api_mutex);
2807 net = lnet_get_net_locked(net_id);
2809 CERROR("net %s not found\n",
2810 libcfs_net2str(net_id));
2815 addr = LNET_NIDADDR(conf->lic_nid);
2817 /* remove the entire net */
2818 net_count = lnet_get_net_ni_count_locked(net);
2822 /* create and link a new ping info, before removing the old one */
2823 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2824 lnet_get_ni_count() - net_count,
2827 goto unlock_api_mutex;
2829 lnet_shutdown_lndnet(net);
2831 if (lnet_count_acceptor_nets() == 0)
2832 lnet_acceptor_stop();
2834 lnet_ping_target_update(pbuf, ping_mdh);
2836 goto unlock_api_mutex;
2839 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
2841 CERROR("nid %s not found\n",
2842 libcfs_nid2str(conf->lic_nid));
2847 net_count = lnet_get_net_ni_count_locked(net);
2851 /* create and link a new ping info, before removing the old one */
2852 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2853 lnet_get_ni_count() - 1, false);
2855 goto unlock_api_mutex;
2857 lnet_shutdown_lndni(ni);
2859 if (lnet_count_acceptor_nets() == 0)
2860 lnet_acceptor_stop();
2862 lnet_ping_target_update(pbuf, ping_mdh);
2864 /* check if the net is empty and remove it if it is */
2866 lnet_shutdown_lndnet(net);
2868 goto unlock_api_mutex;
2873 mutex_unlock(&the_lnet.ln_api_mutex);
2879 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
2880 * They are only expected to be called for unique networks.
2881 * That can be as a result of older DLC library
2882 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
2885 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
2887 struct lnet_net *net;
2888 struct list_head net_head;
2890 struct lnet_ioctl_config_lnd_tunables tun;
2891 char *nets = conf->cfg_config_u.cfg_net.net_intf;
2893 INIT_LIST_HEAD(&net_head);
2895 /* Create a net/ni structures for the network string */
2896 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2898 return rc == 0 ? -EINVAL : rc;
2900 mutex_lock(&the_lnet.ln_api_mutex);
2903 rc = -EINVAL; /* only add one network per call */
2904 goto out_unlock_clean;
2907 net = list_entry(net_head.next, struct lnet_net, net_list);
2908 list_del_init(&net->net_list);
2910 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
2912 memset(&tun, 0, sizeof(tun));
2914 tun.lt_cmn.lct_peer_timeout =
2915 conf->cfg_config_u.cfg_net.net_peer_timeout;
2916 tun.lt_cmn.lct_peer_tx_credits =
2917 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
2918 tun.lt_cmn.lct_peer_rtr_credits =
2919 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
2920 tun.lt_cmn.lct_max_tx_credits =
2921 conf->cfg_config_u.cfg_net.net_max_tx_credits;
2923 rc = lnet_add_net_common(net, &tun);
2926 mutex_unlock(&the_lnet.ln_api_mutex);
2927 while (!list_empty(&net_head)) {
2928 /* net_head list is empty in success case */
2929 net = list_entry(net_head.next, struct lnet_net, net_list);
2930 list_del_init(&net->net_list);
2937 lnet_dyn_del_net(__u32 net_id)
2939 struct lnet_net *net;
2940 struct lnet_ping_buffer *pbuf;
2941 struct lnet_handle_md ping_mdh;
2945 /* don't allow userspace to shutdown the LOLND */
2946 if (LNET_NETTYP(net_id) == LOLND)
2949 mutex_lock(&the_lnet.ln_api_mutex);
2953 net = lnet_get_net_locked(net_id);
2960 net_ni_count = lnet_get_net_ni_count_locked(net);
2964 /* create and link a new ping info, before removing the old one */
2965 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2966 lnet_get_ni_count() - net_ni_count, false);
2970 lnet_shutdown_lndnet(net);
2972 if (lnet_count_acceptor_nets() == 0)
2973 lnet_acceptor_stop();
2975 lnet_ping_target_update(pbuf, ping_mdh);
2978 mutex_unlock(&the_lnet.ln_api_mutex);
2983 void lnet_incr_dlc_seq(void)
2985 atomic_inc(&lnet_dlc_seq_no);
2988 __u32 lnet_get_dlc_seq_locked(void)
2990 return atomic_read(&lnet_dlc_seq_no);
2994 * LNet ioctl handler.
2998 LNetCtl(unsigned int cmd, void *arg)
3000 struct libcfs_ioctl_data *data = arg;
3001 struct lnet_ioctl_config_data *config;
3002 struct lnet_process_id id = {0};
3006 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3007 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3010 case IOC_LIBCFS_GET_NI:
3011 rc = LNetGetId(data->ioc_count, &id);
3012 data->ioc_nid = id.nid;
3015 case IOC_LIBCFS_FAIL_NID:
3016 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3018 case IOC_LIBCFS_ADD_ROUTE:
3021 if (config->cfg_hdr.ioc_len < sizeof(*config))
3024 mutex_lock(&the_lnet.ln_api_mutex);
3025 rc = lnet_add_route(config->cfg_net,
3026 config->cfg_config_u.cfg_route.rtr_hop,
3028 config->cfg_config_u.cfg_route.
3031 rc = lnet_check_routes();
3033 lnet_del_route(config->cfg_net,
3036 mutex_unlock(&the_lnet.ln_api_mutex);
3039 case IOC_LIBCFS_DEL_ROUTE:
3042 if (config->cfg_hdr.ioc_len < sizeof(*config))
3045 mutex_lock(&the_lnet.ln_api_mutex);
3046 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3047 mutex_unlock(&the_lnet.ln_api_mutex);
3050 case IOC_LIBCFS_GET_ROUTE:
3053 if (config->cfg_hdr.ioc_len < sizeof(*config))
3056 mutex_lock(&the_lnet.ln_api_mutex);
3057 rc = lnet_get_route(config->cfg_count,
3059 &config->cfg_config_u.cfg_route.rtr_hop,
3061 &config->cfg_config_u.cfg_route.rtr_flags,
3062 &config->cfg_config_u.cfg_route.
3064 mutex_unlock(&the_lnet.ln_api_mutex);
3067 case IOC_LIBCFS_GET_LOCAL_NI: {
3068 struct lnet_ioctl_config_ni *cfg_ni;
3069 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3070 struct lnet_ioctl_element_stats *stats;
3075 /* get the tunables if they are available */
3076 if (cfg_ni->lic_cfg_hdr.ioc_len <
3077 sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3080 stats = (struct lnet_ioctl_element_stats *)
3082 tun = (struct lnet_ioctl_config_lnd_tunables *)
3083 (cfg_ni->lic_bulk + sizeof(*stats));
3085 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3088 mutex_lock(&the_lnet.ln_api_mutex);
3089 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3090 mutex_unlock(&the_lnet.ln_api_mutex);
3094 case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3095 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3097 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3100 mutex_lock(&the_lnet.ln_api_mutex);
3101 rc = lnet_get_ni_stats(msg_stats);
3102 mutex_unlock(&the_lnet.ln_api_mutex);
3107 case IOC_LIBCFS_GET_NET: {
3108 size_t total = sizeof(*config) +
3109 sizeof(struct lnet_ioctl_net_config);
3112 if (config->cfg_hdr.ioc_len < total)
3115 mutex_lock(&the_lnet.ln_api_mutex);
3116 rc = lnet_get_net_config(config);
3117 mutex_unlock(&the_lnet.ln_api_mutex);
3121 case IOC_LIBCFS_GET_LNET_STATS:
3123 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3125 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3128 mutex_lock(&the_lnet.ln_api_mutex);
3129 lnet_counters_get(&lnet_stats->st_cntrs);
3130 mutex_unlock(&the_lnet.ln_api_mutex);
3134 case IOC_LIBCFS_CONFIG_RTR:
3137 if (config->cfg_hdr.ioc_len < sizeof(*config))
3140 mutex_lock(&the_lnet.ln_api_mutex);
3141 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3142 rc = lnet_rtrpools_enable();
3143 mutex_unlock(&the_lnet.ln_api_mutex);
3146 lnet_rtrpools_disable();
3147 mutex_unlock(&the_lnet.ln_api_mutex);
3150 case IOC_LIBCFS_ADD_BUF:
3153 if (config->cfg_hdr.ioc_len < sizeof(*config))
3156 mutex_lock(&the_lnet.ln_api_mutex);
3157 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3159 config->cfg_config_u.cfg_buffers.
3161 config->cfg_config_u.cfg_buffers.
3163 mutex_unlock(&the_lnet.ln_api_mutex);
3166 case IOC_LIBCFS_SET_NUMA_RANGE: {
3167 struct lnet_ioctl_set_value *numa;
3169 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3171 lnet_net_lock(LNET_LOCK_EX);
3172 lnet_numa_range = numa->sv_value;
3173 lnet_net_unlock(LNET_LOCK_EX);
3177 case IOC_LIBCFS_GET_NUMA_RANGE: {
3178 struct lnet_ioctl_set_value *numa;
3180 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3182 numa->sv_value = lnet_numa_range;
3186 case IOC_LIBCFS_GET_BUF: {
3187 struct lnet_ioctl_pool_cfg *pool_cfg;
3188 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3192 if (config->cfg_hdr.ioc_len < total)
3195 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3197 mutex_lock(&the_lnet.ln_api_mutex);
3198 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3199 mutex_unlock(&the_lnet.ln_api_mutex);
3203 case IOC_LIBCFS_ADD_PEER_NI: {
3204 struct lnet_ioctl_peer_cfg *cfg = arg;
3206 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3209 mutex_lock(&the_lnet.ln_api_mutex);
3210 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3213 mutex_unlock(&the_lnet.ln_api_mutex);
3217 case IOC_LIBCFS_DEL_PEER_NI: {
3218 struct lnet_ioctl_peer_cfg *cfg = arg;
3220 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3223 mutex_lock(&the_lnet.ln_api_mutex);
3224 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3225 cfg->prcfg_cfg_nid);
3226 mutex_unlock(&the_lnet.ln_api_mutex);
3230 case IOC_LIBCFS_GET_PEER_INFO: {
3231 struct lnet_ioctl_peer *peer_info = arg;
3233 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3236 mutex_lock(&the_lnet.ln_api_mutex);
3237 rc = lnet_get_peer_ni_info(
3238 peer_info->pr_count,
3240 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3241 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3242 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3243 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3244 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3245 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3246 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3247 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3248 mutex_unlock(&the_lnet.ln_api_mutex);
3252 case IOC_LIBCFS_GET_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_get_peer_info(cfg,
3260 (void __user *)cfg->prcfg_bulk);
3261 mutex_unlock(&the_lnet.ln_api_mutex);
3265 case IOC_LIBCFS_GET_PEER_LIST: {
3266 struct lnet_ioctl_peer_cfg *cfg = arg;
3268 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3271 mutex_lock(&the_lnet.ln_api_mutex);
3272 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3273 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3274 mutex_unlock(&the_lnet.ln_api_mutex);
3278 case IOC_LIBCFS_NOTIFY_ROUTER: {
3279 unsigned long jiffies_passed;
3281 jiffies_passed = ktime_get_real_seconds() - data->ioc_u64[0];
3282 jiffies_passed = cfs_time_seconds(jiffies_passed);
3284 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
3285 jiffies - jiffies_passed);
3288 case IOC_LIBCFS_LNET_DIST:
3289 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3290 if (rc < 0 && rc != -EHOSTUNREACH)
3293 data->ioc_u32[0] = rc;
3296 case IOC_LIBCFS_TESTPROTOCOMPAT:
3297 lnet_net_lock(LNET_LOCK_EX);
3298 the_lnet.ln_testprotocompat = data->ioc_flags;
3299 lnet_net_unlock(LNET_LOCK_EX);
3302 case IOC_LIBCFS_LNET_FAULT:
3303 return lnet_fault_ctl(data->ioc_flags, data);
3305 case IOC_LIBCFS_PING: {
3306 signed long timeout;
3308 id.nid = data->ioc_nid;
3309 id.pid = data->ioc_u32[0];
3311 /* If timeout is negative then set default of 3 minutes */
3312 if (((s32)data->ioc_u32[1] <= 0) ||
3313 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3314 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3316 timeout = msecs_to_jiffies(data->ioc_u32[1]);
3318 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3319 data->ioc_plen1 / sizeof(struct lnet_process_id));
3324 data->ioc_count = rc;
3328 case IOC_LIBCFS_PING_PEER: {
3329 struct lnet_ioctl_ping_data *ping = arg;
3330 struct lnet_peer *lp;
3331 signed long timeout;
3333 /* If timeout is negative then set default of 3 minutes */
3334 if (((s32)ping->op_param) <= 0 ||
3335 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3336 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3338 timeout = msecs_to_jiffies(ping->op_param);
3340 rc = lnet_ping(ping->ping_id, timeout,
3346 mutex_lock(&the_lnet.ln_api_mutex);
3347 lp = lnet_find_peer(ping->ping_id.nid);
3349 ping->ping_id.nid = lp->lp_primary_nid;
3350 ping->mr_info = lnet_peer_is_multi_rail(lp);
3351 lnet_peer_decref_locked(lp);
3353 mutex_unlock(&the_lnet.ln_api_mutex);
3355 ping->ping_count = rc;
3359 case IOC_LIBCFS_DISCOVER: {
3360 struct lnet_ioctl_ping_data *discover = arg;
3361 struct lnet_peer *lp;
3363 rc = lnet_discover(discover->ping_id, discover->op_param,
3365 discover->ping_count);
3369 mutex_lock(&the_lnet.ln_api_mutex);
3370 lp = lnet_find_peer(discover->ping_id.nid);
3372 discover->ping_id.nid = lp->lp_primary_nid;
3373 discover->mr_info = lnet_peer_is_multi_rail(lp);
3374 lnet_peer_decref_locked(lp);
3376 mutex_unlock(&the_lnet.ln_api_mutex);
3378 discover->ping_count = rc;
3383 ni = lnet_net2ni_addref(data->ioc_net);
3387 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3390 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
3397 EXPORT_SYMBOL(LNetCtl);
3399 void LNetDebugPeer(struct lnet_process_id id)
3401 lnet_debug_peer(id.nid);
3403 EXPORT_SYMBOL(LNetDebugPeer);
3406 * Determine if the specified peer \a nid is on the local node.
3408 * \param nid peer nid to check
3410 * \retval true If peer NID is on the local node.
3411 * \retval false If peer NID is not on the local node.
3413 bool LNetIsPeerLocal(lnet_nid_t nid)
3415 struct lnet_net *net;
3419 cpt = lnet_net_lock_current();
3420 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3421 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3422 if (ni->ni_nid == nid) {
3423 lnet_net_unlock(cpt);
3428 lnet_net_unlock(cpt);
3432 EXPORT_SYMBOL(LNetIsPeerLocal);
3435 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
3436 * Note that all interfaces share a same PID, as requested by LNetNIInit().
3438 * \param index Index of the interface to look up.
3439 * \param id On successful return, this location will hold the
3440 * struct lnet_process_id ID of the interface.
3442 * \retval 0 If an interface exists at \a index.
3443 * \retval -ENOENT If no interface has been found.
3446 LNetGetId(unsigned int index, struct lnet_process_id *id)
3449 struct lnet_net *net;
3453 LASSERT(the_lnet.ln_refcount > 0);
3455 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) {
3462 id->nid = ni->ni_nid;
3463 id->pid = the_lnet.ln_pid;
3469 lnet_net_unlock(cpt);
3472 EXPORT_SYMBOL(LNetGetId);
3474 static int lnet_ping(struct lnet_process_id id, signed long timeout,
3475 struct lnet_process_id __user *ids, int n_ids)
3477 struct lnet_handle_eq eqh;
3478 struct lnet_handle_md mdh;
3479 struct lnet_event event;
3480 struct lnet_md md = { NULL };
3484 const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
3485 struct lnet_ping_buffer *pbuf;
3486 struct lnet_process_id tmpid;
3493 /* n_ids limit is arbitrary */
3494 if (n_ids <= 0 || id.nid == LNET_NID_ANY)
3498 * if the user buffer has more space than the lnet_interfaces_max
3499 * then only fill it up to lnet_interfaces_max
3501 if (n_ids > lnet_interfaces_max)
3502 n_ids = lnet_interfaces_max;
3504 if (id.pid == LNET_PID_ANY)
3505 id.pid = LNET_PID_LUSTRE;
3507 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
3511 /* NB 2 events max (including any unlink event) */
3512 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
3514 CERROR("Can't allocate EQ: %d\n", rc);
3515 goto fail_ping_buffer_decref;
3518 /* initialize md content */
3519 md.start = &pbuf->pb_info;
3520 md.length = LNET_PING_INFO_SIZE(n_ids);
3521 md.threshold = 2; /* GET/REPLY */
3523 md.options = LNET_MD_TRUNCATE;
3527 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
3529 CERROR("Can't bind MD: %d\n", rc);
3533 rc = LNetGet(LNET_NID_ANY, mdh, id,
3534 LNET_RESERVED_PORTAL,
3535 LNET_PROTO_PING_MATCHBITS, 0);
3538 /* Don't CERROR; this could be deliberate! */
3539 rc2 = LNetMDUnlink(mdh);
3542 /* NB must wait for the UNLINK event below... */
3544 timeout = a_long_time;
3548 /* MUST block for unlink to complete */
3550 blocked = cfs_block_allsigs();
3552 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
3555 cfs_restore_sigs(blocked);
3557 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
3558 (rc2 <= 0) ? -1 : event.type,
3559 (rc2 <= 0) ? -1 : event.status,
3560 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
3562 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
3564 if (rc2 <= 0 || event.status != 0) {
3565 /* timeout or error */
3566 if (!replied && rc == 0)
3567 rc = (rc2 < 0) ? rc2 :
3568 (rc2 == 0) ? -ETIMEDOUT :
3572 /* Ensure completion in finite time... */
3574 /* No assertion (racing with network) */
3576 timeout = a_long_time;
3577 } else if (rc2 == 0) {
3578 /* timed out waiting for unlink */
3579 CWARN("ping %s: late network completion\n",
3582 } else if (event.type == LNET_EVENT_REPLY) {
3586 } while (rc2 <= 0 || !event.unlinked);
3590 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
3597 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
3599 rc = -EPROTO; /* if I can't parse... */
3602 CERROR("%s: ping info too short %d\n",
3603 libcfs_id2str(id), nob);
3607 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
3608 lnet_swap_pinginfo(pbuf);
3609 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
3610 CERROR("%s: Unexpected magic %08x\n",
3611 libcfs_id2str(id), pbuf->pb_info.pi_magic);
3615 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
3616 CERROR("%s: ping w/o NI status: 0x%x\n",
3617 libcfs_id2str(id), pbuf->pb_info.pi_features);
3621 if (nob < LNET_PING_INFO_SIZE(0)) {
3622 CERROR("%s: Short reply %d(%d min)\n",
3624 nob, (int)LNET_PING_INFO_SIZE(0));
3628 if (pbuf->pb_info.pi_nnis < n_ids)
3629 n_ids = pbuf->pb_info.pi_nnis;
3631 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
3632 CERROR("%s: Short reply %d(%d expected)\n",
3634 nob, (int)LNET_PING_INFO_SIZE(n_ids));
3638 rc = -EFAULT; /* if I segv in copy_to_user()... */
3640 memset(&tmpid, 0, sizeof(tmpid));
3641 for (i = 0; i < n_ids; i++) {
3642 tmpid.pid = pbuf->pb_info.pi_pid;
3643 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
3644 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
3647 rc = pbuf->pb_info.pi_nnis;
3650 rc2 = LNetEQFree(eqh);
3652 CERROR("rc2 %d\n", rc2);
3655 fail_ping_buffer_decref:
3656 lnet_ping_buffer_decref(pbuf);
3661 lnet_discover(struct lnet_process_id id, __u32 force,
3662 struct lnet_process_id __user *ids, int n_ids)
3664 struct lnet_peer_ni *lpni;
3665 struct lnet_peer_ni *p;
3666 struct lnet_peer *lp;
3667 struct lnet_process_id *buf;
3671 int max_intf = lnet_interfaces_max;
3675 id.nid == LNET_NID_ANY)
3678 if (id.pid == LNET_PID_ANY)
3679 id.pid = LNET_PID_LUSTRE;
3682 * if the user buffer has more space than the max_intf
3683 * then only fill it up to max_intf
3685 if (n_ids > max_intf)
3688 buf_size = n_ids * sizeof(*buf);
3690 LIBCFS_ALLOC(buf, buf_size);
3694 cpt = lnet_net_lock_current();
3695 lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
3702 * Clearing the NIDS_UPTODATE flag ensures the peer will
3703 * be discovered, provided discovery has not been disabled.
3705 lp = lpni->lpni_peer_net->lpn_peer;
3706 spin_lock(&lp->lp_lock);
3707 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
3708 /* If the force flag is set, force a PING and PUSH as well. */
3710 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
3711 spin_unlock(&lp->lp_lock);
3712 rc = lnet_discover_peer_locked(lpni, cpt, true);
3716 /* Peer may have changed. */
3717 lp = lpni->lpni_peer_net->lpn_peer;
3718 if (lp->lp_nnis < n_ids)
3719 n_ids = lp->lp_nnis;
3723 while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
3724 buf[i].pid = id.pid;
3725 buf[i].nid = p->lpni_nid;
3730 lnet_net_unlock(cpt);
3733 if (copy_to_user(ids, buf, n_ids * sizeof(*buf)))
3739 lnet_peer_ni_decref_locked(lpni);
3741 lnet_net_unlock(cpt);
3743 LIBCFS_FREE(buf, buf_size);