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, 2016, 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
34 #include <linux/log2.h>
35 #include <linux/ktime.h>
36 #include <linux/moduleparam.h>
38 #include <lnet/lib-lnet.h>
40 #define D_LNI D_CONSOLE
43 * initialize ln_api_mutex statically, since it needs to be used in
44 * discovery_set callback. That module parameter callback can be called
45 * before module init completes. The mutex needs to be ready for use then.
47 struct lnet the_lnet = {
48 .ln_api_mutex = __MUTEX_INITIALIZER(the_lnet.ln_api_mutex),
49 }; /* THE state of the network */
50 EXPORT_SYMBOL(the_lnet);
52 static char *ip2nets = "";
53 module_param(ip2nets, charp, 0444);
54 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
56 static char *networks = "";
57 module_param(networks, charp, 0444);
58 MODULE_PARM_DESC(networks, "local networks");
60 static char *routes = "";
61 module_param(routes, charp, 0444);
62 MODULE_PARM_DESC(routes, "routes to non-local networks");
64 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
65 module_param(rnet_htable_size, int, 0444);
66 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
68 static int use_tcp_bonding = false;
69 module_param(use_tcp_bonding, int, 0444);
70 MODULE_PARM_DESC(use_tcp_bonding,
71 "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
73 unsigned int lnet_numa_range = 0;
74 module_param(lnet_numa_range, uint, 0444);
75 MODULE_PARM_DESC(lnet_numa_range,
76 "NUMA range to consider during Multi-Rail selection");
78 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
79 static int intf_max_set(const char *val, struct kernel_param *kp);
80 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
81 &lnet_interfaces_max, S_IRUGO|S_IWUSR);
82 MODULE_PARM_DESC(lnet_interfaces_max,
83 "Maximum number of interfaces in a node.");
85 unsigned lnet_peer_discovery_disabled = 0;
86 static int discovery_set(const char *val, struct kernel_param *kp);
87 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
88 &lnet_peer_discovery_disabled, S_IRUGO|S_IWUSR);
89 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
90 "Set to 1 to disable peer discovery on this node.");
93 * This sequence number keeps track of how many times DLC was used to
94 * update the local NIs. It is incremented when a NI is added or
95 * removed and checked when sending a message to determine if there is
96 * a need to re-run the selection algorithm. See lnet_select_pathway()
97 * for more details on its usage.
99 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
101 static int lnet_ping(struct lnet_process_id id, signed long timeout,
102 struct lnet_process_id __user *ids, int n_ids);
105 discovery_set(const char *val, struct kernel_param *kp)
108 unsigned *discovery = (unsigned *)kp->arg;
110 struct lnet_ping_buffer *pbuf;
112 rc = kstrtoul(val, 0, &value);
114 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
118 value = (value) ? 1 : 0;
121 * The purpose of locking the api_mutex here is to ensure that
122 * the correct value ends up stored properly.
124 mutex_lock(&the_lnet.ln_api_mutex);
126 if (value == *discovery) {
127 mutex_unlock(&the_lnet.ln_api_mutex);
133 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
134 mutex_unlock(&the_lnet.ln_api_mutex);
138 /* tell peers that discovery setting has changed */
139 lnet_net_lock(LNET_LOCK_EX);
140 pbuf = the_lnet.ln_ping_target;
142 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
144 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
145 lnet_net_unlock(LNET_LOCK_EX);
147 lnet_push_update_to_peers(1);
149 mutex_unlock(&the_lnet.ln_api_mutex);
155 intf_max_set(const char *val, struct kernel_param *kp)
159 rc = kstrtoint(val, 0, &value);
161 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
165 if (value < LNET_INTERFACES_MIN) {
166 CWARN("max interfaces provided are too small, setting to %d\n",
167 LNET_INTERFACES_MIN);
168 value = LNET_INTERFACES_MIN;
171 *(int *)kp->arg = value;
177 lnet_get_routes(void)
183 lnet_get_networks(void)
188 if (*networks != 0 && *ip2nets != 0) {
189 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
190 "'ip2nets' but not both at once\n");
195 rc = lnet_parse_ip2nets(&nets, ip2nets);
196 return (rc == 0) ? nets : NULL;
206 lnet_init_locks(void)
208 spin_lock_init(&the_lnet.ln_eq_wait_lock);
209 init_waitqueue_head(&the_lnet.ln_eq_waitq);
210 init_waitqueue_head(&the_lnet.ln_rc_waitq);
211 mutex_init(&the_lnet.ln_lnd_mutex);
215 lnet_fini_locks(void)
219 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
220 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
224 lnet_descriptor_setup(void)
226 /* create specific kmem_cache for MEs and small MDs (i.e., originally
227 * allocated in <size-xxx> kmem_cache).
229 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
231 if (!lnet_mes_cachep)
234 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
235 LNET_SMALL_MD_SIZE, 0, 0,
237 if (!lnet_small_mds_cachep)
244 lnet_descriptor_cleanup(void)
247 if (lnet_small_mds_cachep) {
248 kmem_cache_destroy(lnet_small_mds_cachep);
249 lnet_small_mds_cachep = NULL;
252 if (lnet_mes_cachep) {
253 kmem_cache_destroy(lnet_mes_cachep);
254 lnet_mes_cachep = NULL;
259 lnet_create_remote_nets_table(void)
262 struct list_head *hash;
264 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
265 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
266 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
268 CERROR("Failed to create remote nets hash table\n");
272 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
273 INIT_LIST_HEAD(&hash[i]);
274 the_lnet.ln_remote_nets_hash = hash;
279 lnet_destroy_remote_nets_table(void)
283 if (the_lnet.ln_remote_nets_hash == NULL)
286 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
287 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
289 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
290 LNET_REMOTE_NETS_HASH_SIZE *
291 sizeof(the_lnet.ln_remote_nets_hash[0]));
292 the_lnet.ln_remote_nets_hash = NULL;
296 lnet_destroy_locks(void)
298 if (the_lnet.ln_res_lock != NULL) {
299 cfs_percpt_lock_free(the_lnet.ln_res_lock);
300 the_lnet.ln_res_lock = NULL;
303 if (the_lnet.ln_net_lock != NULL) {
304 cfs_percpt_lock_free(the_lnet.ln_net_lock);
305 the_lnet.ln_net_lock = NULL;
312 lnet_create_locks(void)
316 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
317 if (the_lnet.ln_res_lock == NULL)
320 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
321 if (the_lnet.ln_net_lock == NULL)
327 lnet_destroy_locks();
331 static void lnet_assert_wire_constants(void)
333 /* Wire protocol assertions generated by 'wirecheck'
334 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
335 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
336 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
339 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
340 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
341 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
342 CLASSERT(LNET_MSG_ACK == 0);
343 CLASSERT(LNET_MSG_PUT == 1);
344 CLASSERT(LNET_MSG_GET == 2);
345 CLASSERT(LNET_MSG_REPLY == 3);
346 CLASSERT(LNET_MSG_HELLO == 4);
348 /* Checks for struct lnet_handle_wire */
349 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
350 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
351 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
352 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
353 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
355 /* Checks for struct struct lnet_magicversion */
356 CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
357 CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
358 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
359 CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
360 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
361 CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
362 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
364 /* Checks for struct struct lnet_hdr */
365 CLASSERT((int)sizeof(struct lnet_hdr) == 72);
366 CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
367 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
368 CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
369 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
370 CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
371 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
372 CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
373 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
374 CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
375 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
376 CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
377 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
378 CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
379 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
382 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
383 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
384 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
385 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
386 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
387 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
390 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
391 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
392 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
393 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
394 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
395 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
396 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
397 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
398 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
399 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
402 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
403 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
404 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
405 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
406 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
407 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
408 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
409 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
410 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
411 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
414 CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
415 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
418 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
419 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
420 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
421 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
423 /* Checks for struct lnet_ni_status and related constants */
424 CLASSERT(LNET_NI_STATUS_INVALID == 0x00000000);
425 CLASSERT(LNET_NI_STATUS_UP == 0x15aac0de);
426 CLASSERT(LNET_NI_STATUS_DOWN == 0xdeadface);
428 /* Checks for struct lnet_ni_status */
429 CLASSERT((int)sizeof(struct lnet_ni_status) == 16);
430 CLASSERT((int)offsetof(struct lnet_ni_status, ns_nid) == 0);
431 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) == 8);
432 CLASSERT((int)offsetof(struct lnet_ni_status, ns_status) == 8);
433 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_status) == 4);
434 CLASSERT((int)offsetof(struct lnet_ni_status, ns_unused) == 12);
435 CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) == 4);
437 /* Checks for struct lnet_ping_info and related constants */
438 CLASSERT(LNET_PROTO_PING_MAGIC == 0x70696E67);
439 CLASSERT(LNET_PING_FEAT_INVAL == 0);
440 CLASSERT(LNET_PING_FEAT_BASE == 1);
441 CLASSERT(LNET_PING_FEAT_NI_STATUS == 2);
442 CLASSERT(LNET_PING_FEAT_RTE_DISABLED == 4);
443 CLASSERT(LNET_PING_FEAT_MULTI_RAIL == 8);
444 CLASSERT(LNET_PING_FEAT_DISCOVERY == 16);
445 CLASSERT(LNET_PING_FEAT_BITS == 31);
447 /* Checks for struct lnet_ping_info */
448 CLASSERT((int)sizeof(struct lnet_ping_info) == 16);
449 CLASSERT((int)offsetof(struct lnet_ping_info, pi_magic) == 0);
450 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) == 4);
451 CLASSERT((int)offsetof(struct lnet_ping_info, pi_features) == 4);
452 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_features) == 4);
453 CLASSERT((int)offsetof(struct lnet_ping_info, pi_pid) == 8);
454 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) == 4);
455 CLASSERT((int)offsetof(struct lnet_ping_info, pi_nnis) == 12);
456 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) == 4);
457 CLASSERT((int)offsetof(struct lnet_ping_info, pi_ni) == 16);
458 CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) == 0);
461 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
463 struct lnet_lnd *lnd;
464 struct list_head *tmp;
466 /* holding lnd mutex */
467 list_for_each(tmp, &the_lnet.ln_lnds) {
468 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
470 if (lnd->lnd_type == type)
477 lnet_register_lnd(struct lnet_lnd *lnd)
479 mutex_lock(&the_lnet.ln_lnd_mutex);
481 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
482 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
484 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
485 lnd->lnd_refcount = 0;
487 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
489 mutex_unlock(&the_lnet.ln_lnd_mutex);
491 EXPORT_SYMBOL(lnet_register_lnd);
494 lnet_unregister_lnd(struct lnet_lnd *lnd)
496 mutex_lock(&the_lnet.ln_lnd_mutex);
498 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
499 LASSERT(lnd->lnd_refcount == 0);
501 list_del(&lnd->lnd_list);
502 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
504 mutex_unlock(&the_lnet.ln_lnd_mutex);
506 EXPORT_SYMBOL(lnet_unregister_lnd);
509 lnet_counters_get(struct lnet_counters *counters)
511 struct lnet_counters *ctr;
514 memset(counters, 0, sizeof(*counters));
516 lnet_net_lock(LNET_LOCK_EX);
518 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
519 counters->msgs_max += ctr->msgs_max;
520 counters->msgs_alloc += ctr->msgs_alloc;
521 counters->errors += ctr->errors;
522 counters->send_count += ctr->send_count;
523 counters->recv_count += ctr->recv_count;
524 counters->route_count += ctr->route_count;
525 counters->drop_count += ctr->drop_count;
526 counters->send_length += ctr->send_length;
527 counters->recv_length += ctr->recv_length;
528 counters->route_length += ctr->route_length;
529 counters->drop_length += ctr->drop_length;
532 lnet_net_unlock(LNET_LOCK_EX);
534 EXPORT_SYMBOL(lnet_counters_get);
537 lnet_counters_reset(void)
539 struct lnet_counters *counters;
542 lnet_net_lock(LNET_LOCK_EX);
544 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
545 memset(counters, 0, sizeof(struct lnet_counters));
547 lnet_net_unlock(LNET_LOCK_EX);
551 lnet_res_type2str(int type)
556 case LNET_COOKIE_TYPE_MD:
558 case LNET_COOKIE_TYPE_ME:
560 case LNET_COOKIE_TYPE_EQ:
566 lnet_res_container_cleanup(struct lnet_res_container *rec)
570 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
573 while (!list_empty(&rec->rec_active)) {
574 struct list_head *e = rec->rec_active.next;
577 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
578 lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
580 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
581 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
583 } else { /* NB: Active MEs should be attached on portals */
590 /* Found alive MD/ME/EQ, user really should unlink/free
591 * all of them before finalize LNet, but if someone didn't,
592 * we have to recycle garbage for him */
593 CERROR("%d active elements on exit of %s container\n",
594 count, lnet_res_type2str(rec->rec_type));
597 if (rec->rec_lh_hash != NULL) {
598 LIBCFS_FREE(rec->rec_lh_hash,
599 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
600 rec->rec_lh_hash = NULL;
603 rec->rec_type = 0; /* mark it as finalized */
607 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
612 LASSERT(rec->rec_type == 0);
614 rec->rec_type = type;
615 INIT_LIST_HEAD(&rec->rec_active);
617 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
619 /* Arbitrary choice of hash table size */
620 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
621 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
622 if (rec->rec_lh_hash == NULL) {
627 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
628 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
633 CERROR("Failed to setup %s resource container\n",
634 lnet_res_type2str(type));
635 lnet_res_container_cleanup(rec);
640 lnet_res_containers_destroy(struct lnet_res_container **recs)
642 struct lnet_res_container *rec;
645 cfs_percpt_for_each(rec, i, recs)
646 lnet_res_container_cleanup(rec);
648 cfs_percpt_free(recs);
651 static struct lnet_res_container **
652 lnet_res_containers_create(int type)
654 struct lnet_res_container **recs;
655 struct lnet_res_container *rec;
659 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
661 CERROR("Failed to allocate %s resource containers\n",
662 lnet_res_type2str(type));
666 cfs_percpt_for_each(rec, i, recs) {
667 rc = lnet_res_container_setup(rec, i, type);
669 lnet_res_containers_destroy(recs);
677 struct lnet_libhandle *
678 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
680 /* ALWAYS called with lnet_res_lock held */
681 struct list_head *head;
682 struct lnet_libhandle *lh;
685 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
688 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
689 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
691 list_for_each_entry(lh, head, lh_hash_chain) {
692 if (lh->lh_cookie == cookie)
700 lnet_res_lh_initialize(struct lnet_res_container *rec,
701 struct lnet_libhandle *lh)
703 /* ALWAYS called with lnet_res_lock held */
704 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
707 lh->lh_cookie = rec->rec_lh_cookie;
708 rec->rec_lh_cookie += 1 << ibits;
710 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
712 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
715 static int lnet_unprepare(void);
718 lnet_prepare(lnet_pid_t requested_pid)
720 /* Prepare to bring up the network */
721 struct lnet_res_container **recs;
724 if (requested_pid == LNET_PID_ANY) {
725 /* Don't instantiate LNET just for me */
729 LASSERT(the_lnet.ln_refcount == 0);
731 the_lnet.ln_routing = 0;
733 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
734 the_lnet.ln_pid = requested_pid;
736 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
737 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
738 INIT_LIST_HEAD(&the_lnet.ln_nets);
739 INIT_LIST_HEAD(&the_lnet.ln_routers);
740 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
741 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
742 INIT_LIST_HEAD(&the_lnet.ln_dc_request);
743 INIT_LIST_HEAD(&the_lnet.ln_dc_working);
744 INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
745 init_waitqueue_head(&the_lnet.ln_dc_waitq);
747 rc = lnet_descriptor_setup();
751 rc = lnet_create_remote_nets_table();
756 * NB the interface cookie in wire handles guards against delayed
757 * replies and ACKs appearing valid after reboot.
759 the_lnet.ln_interface_cookie = ktime_get_real_ns();
761 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
762 sizeof(struct lnet_counters));
763 if (the_lnet.ln_counters == NULL) {
764 CERROR("Failed to allocate counters for LNet\n");
769 rc = lnet_peer_tables_create();
773 rc = lnet_msg_containers_create();
777 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
778 LNET_COOKIE_TYPE_EQ);
782 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
788 the_lnet.ln_me_containers = recs;
790 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
796 the_lnet.ln_md_containers = recs;
798 rc = lnet_portals_create();
800 CERROR("Failed to create portals for LNet: %d\n", rc);
812 lnet_unprepare (void)
814 /* NB no LNET_LOCK since this is the last reference. All LND instances
815 * have shut down already, so it is safe to unlink and free all
816 * descriptors, even those that appear committed to a network op (eg MD
817 * with non-zero pending count) */
819 lnet_fail_nid(LNET_NID_ANY, 0);
821 LASSERT(the_lnet.ln_refcount == 0);
822 LASSERT(list_empty(&the_lnet.ln_test_peers));
823 LASSERT(list_empty(&the_lnet.ln_nets));
825 lnet_portals_destroy();
827 if (the_lnet.ln_md_containers != NULL) {
828 lnet_res_containers_destroy(the_lnet.ln_md_containers);
829 the_lnet.ln_md_containers = NULL;
832 if (the_lnet.ln_me_containers != NULL) {
833 lnet_res_containers_destroy(the_lnet.ln_me_containers);
834 the_lnet.ln_me_containers = NULL;
837 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
839 lnet_msg_containers_destroy();
841 lnet_rtrpools_free(0);
843 if (the_lnet.ln_counters != NULL) {
844 cfs_percpt_free(the_lnet.ln_counters);
845 the_lnet.ln_counters = NULL;
847 lnet_destroy_remote_nets_table();
848 lnet_descriptor_cleanup();
854 lnet_net2ni_locked(__u32 net_id, int cpt)
857 struct lnet_net *net;
859 LASSERT(cpt != LNET_LOCK_EX);
861 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
862 if (net->net_id == net_id) {
863 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
873 lnet_net2ni_addref(__u32 net)
878 ni = lnet_net2ni_locked(net, 0);
880 lnet_ni_addref_locked(ni, 0);
885 EXPORT_SYMBOL(lnet_net2ni_addref);
888 lnet_get_net_locked(__u32 net_id)
890 struct lnet_net *net;
892 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
893 if (net->net_id == net_id)
901 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
906 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
911 val = hash_long(key, LNET_CPT_BITS);
912 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
916 return (unsigned int)(key + val + (val >> 1)) % number;
920 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
922 struct lnet_net *net;
924 /* must called with hold of lnet_net_lock */
925 if (LNET_CPT_NUMBER == 1)
926 return 0; /* the only one */
929 * If NI is provided then use the CPT identified in the NI cpt
930 * list if one exists. If one doesn't exist, then that NI is
931 * associated with all CPTs and it follows that the net it belongs
932 * to is implicitly associated with all CPTs, so just hash the nid
936 if (ni->ni_cpts != NULL)
937 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
940 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
943 /* no NI provided so look at the net */
944 net = lnet_get_net_locked(LNET_NIDNET(nid));
946 if (net != NULL && net->net_cpts != NULL) {
947 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
950 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
954 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
959 if (LNET_CPT_NUMBER == 1)
960 return 0; /* the only one */
962 cpt = lnet_net_lock_current();
964 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
966 lnet_net_unlock(cpt);
970 EXPORT_SYMBOL(lnet_cpt_of_nid);
973 lnet_islocalnet(__u32 net_id)
975 struct lnet_net *net;
979 cpt = lnet_net_lock_current();
981 net = lnet_get_net_locked(net_id);
985 lnet_net_unlock(cpt);
991 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
993 if (ni->ni_state == LNET_NI_STATE_ACTIVE ||
994 ni->ni_state == LNET_NI_STATE_DEGRADED)
1001 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1003 struct lnet_net *net;
1006 LASSERT(cpt != LNET_LOCK_EX);
1008 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1009 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1010 if (ni->ni_nid == nid)
1019 lnet_nid2ni_addref(lnet_nid_t nid)
1024 ni = lnet_nid2ni_locked(nid, 0);
1026 lnet_ni_addref_locked(ni, 0);
1031 EXPORT_SYMBOL(lnet_nid2ni_addref);
1034 lnet_islocalnid(lnet_nid_t nid)
1039 cpt = lnet_net_lock_current();
1040 ni = lnet_nid2ni_locked(nid, cpt);
1041 lnet_net_unlock(cpt);
1047 lnet_count_acceptor_nets(void)
1049 /* Return the # of NIs that need the acceptor. */
1051 struct lnet_net *net;
1054 cpt = lnet_net_lock_current();
1055 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1056 /* all socklnd type networks should have the acceptor
1058 if (net->net_lnd->lnd_accept != NULL)
1062 lnet_net_unlock(cpt);
1067 struct lnet_ping_buffer *
1068 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1070 struct lnet_ping_buffer *pbuf;
1072 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1074 pbuf->pb_nnis = nnis;
1075 atomic_set(&pbuf->pb_refcnt, 1);
1082 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1084 LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1085 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1088 static struct lnet_ping_buffer *
1089 lnet_ping_target_create(int nnis)
1091 struct lnet_ping_buffer *pbuf;
1093 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1095 CERROR("Can't allocate ping source [%d]\n", nnis);
1099 pbuf->pb_info.pi_nnis = nnis;
1100 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1101 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1102 pbuf->pb_info.pi_features =
1103 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1109 lnet_get_net_ni_count_locked(struct lnet_net *net)
1114 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1121 lnet_get_net_ni_count_pre(struct lnet_net *net)
1126 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1133 lnet_get_ni_count(void)
1136 struct lnet_net *net;
1141 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1142 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1152 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1156 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1158 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1160 /* Loopback is guaranteed to be present */
1161 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1163 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1169 lnet_ping_target_destroy(void)
1171 struct lnet_net *net;
1174 lnet_net_lock(LNET_LOCK_EX);
1176 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1177 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1179 ni->ni_status = NULL;
1184 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1185 the_lnet.ln_ping_target = NULL;
1187 lnet_net_unlock(LNET_LOCK_EX);
1191 lnet_ping_target_event_handler(struct lnet_event *event)
1193 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1195 if (event->unlinked)
1196 lnet_ping_buffer_decref(pbuf);
1200 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1201 struct lnet_handle_md *ping_mdh,
1202 int ni_count, bool set_eq)
1204 struct lnet_process_id id = {
1205 .nid = LNET_NID_ANY,
1208 struct lnet_handle_me me_handle;
1209 struct lnet_md md = { NULL };
1213 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1214 &the_lnet.ln_ping_target_eq);
1216 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1221 *ppbuf = lnet_ping_target_create(ni_count);
1222 if (*ppbuf == NULL) {
1227 /* Ping target ME/MD */
1228 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1229 LNET_PROTO_PING_MATCHBITS, 0,
1230 LNET_UNLINK, LNET_INS_AFTER,
1233 CERROR("Can't create ping target ME: %d\n", rc);
1234 goto fail_decref_ping_buffer;
1237 /* initialize md content */
1238 md.start = &(*ppbuf)->pb_info;
1239 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1240 md.threshold = LNET_MD_THRESH_INF;
1242 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1243 LNET_MD_MANAGE_REMOTE;
1244 md.eq_handle = the_lnet.ln_ping_target_eq;
1245 md.user_ptr = *ppbuf;
1247 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, ping_mdh);
1249 CERROR("Can't attach ping target MD: %d\n", rc);
1250 goto fail_unlink_ping_me;
1252 lnet_ping_buffer_addref(*ppbuf);
1256 fail_unlink_ping_me:
1257 rc2 = LNetMEUnlink(me_handle);
1259 fail_decref_ping_buffer:
1260 LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1261 lnet_ping_buffer_decref(*ppbuf);
1265 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1272 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1273 struct lnet_handle_md *ping_mdh)
1275 sigset_t blocked = cfs_block_allsigs();
1277 LNetMDUnlink(*ping_mdh);
1278 LNetInvalidateMDHandle(ping_mdh);
1280 /* NB the MD could be busy; this just starts the unlink */
1281 while (lnet_ping_buffer_numref(pbuf) > 1) {
1282 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1283 set_current_state(TASK_UNINTERRUPTIBLE);
1284 schedule_timeout(cfs_time_seconds(1));
1287 cfs_restore_sigs(blocked);
1291 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1294 struct lnet_net *net;
1295 struct lnet_ni_status *ns;
1300 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1301 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1302 LASSERT(i < pbuf->pb_nnis);
1304 ns = &pbuf->pb_info.pi_ni[i];
1306 ns->ns_nid = ni->ni_nid;
1309 ns->ns_status = (ni->ni_status != NULL) ?
1310 ni->ni_status->ns_status :
1319 * We (ab)use the ns_status of the loopback interface to
1320 * transmit the sequence number. The first interface listed
1321 * must be the loopback interface.
1323 rc = lnet_ping_info_validate(&pbuf->pb_info);
1325 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1328 LNET_PING_BUFFER_SEQNO(pbuf) =
1329 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1333 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1334 struct lnet_handle_md ping_mdh)
1336 struct lnet_ping_buffer *old_pbuf = NULL;
1337 struct lnet_handle_md old_ping_md;
1339 /* switch the NIs to point to the new ping info created */
1340 lnet_net_lock(LNET_LOCK_EX);
1342 if (!the_lnet.ln_routing)
1343 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1344 if (!lnet_peer_discovery_disabled)
1345 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1347 /* Ensure only known feature bits have been set. */
1348 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1349 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1351 lnet_ping_target_install_locked(pbuf);
1353 if (the_lnet.ln_ping_target) {
1354 old_pbuf = the_lnet.ln_ping_target;
1355 old_ping_md = the_lnet.ln_ping_target_md;
1357 the_lnet.ln_ping_target_md = ping_mdh;
1358 the_lnet.ln_ping_target = pbuf;
1360 lnet_net_unlock(LNET_LOCK_EX);
1363 /* unlink and free the old ping info */
1364 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1365 lnet_ping_buffer_decref(old_pbuf);
1368 lnet_push_update_to_peers(0);
1372 lnet_ping_target_fini(void)
1376 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1377 &the_lnet.ln_ping_target_md);
1379 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1382 lnet_ping_target_destroy();
1385 /* Resize the push target. */
1386 int lnet_push_target_resize(void)
1388 lnet_process_id_t id = { LNET_NID_ANY, LNET_PID_ANY };
1389 lnet_md_t md = { NULL };
1390 lnet_handle_me_t meh;
1391 lnet_handle_md_t mdh;
1392 lnet_handle_md_t old_mdh;
1393 struct lnet_ping_buffer *pbuf;
1394 struct lnet_ping_buffer *old_pbuf;
1395 int nnis = the_lnet.ln_push_target_nnis;
1403 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1409 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1410 LNET_PROTO_PING_MATCHBITS, 0,
1411 LNET_UNLINK, LNET_INS_AFTER,
1414 CERROR("Can't create push target ME: %d\n", rc);
1415 goto fail_decref_pbuf;
1418 /* initialize md content */
1419 md.start = &pbuf->pb_info;
1420 md.length = LNET_PING_INFO_SIZE(nnis);
1421 md.threshold = LNET_MD_THRESH_INF;
1423 md.options = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1424 LNET_MD_MANAGE_REMOTE;
1426 md.eq_handle = the_lnet.ln_push_target_eq;
1428 rc = LNetMDAttach(meh, md, LNET_RETAIN, &mdh);
1430 CERROR("Can't attach push MD: %d\n", rc);
1431 goto fail_unlink_meh;
1433 lnet_ping_buffer_addref(pbuf);
1435 lnet_net_lock(LNET_LOCK_EX);
1436 old_pbuf = the_lnet.ln_push_target;
1437 old_mdh = the_lnet.ln_push_target_md;
1438 the_lnet.ln_push_target = pbuf;
1439 the_lnet.ln_push_target_md = mdh;
1440 lnet_net_unlock(LNET_LOCK_EX);
1443 LNetMDUnlink(old_mdh);
1444 lnet_ping_buffer_decref(old_pbuf);
1447 if (nnis < the_lnet.ln_push_target_nnis)
1450 CDEBUG(D_NET, "nnis %d success\n", nnis);
1457 lnet_ping_buffer_decref(pbuf);
1459 CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1463 static void lnet_push_target_event_handler(struct lnet_event *ev)
1465 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1467 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1468 lnet_swap_pinginfo(pbuf);
1470 lnet_peer_push_event(ev);
1472 lnet_ping_buffer_decref(pbuf);
1475 /* Initialize the push target. */
1476 static int lnet_push_target_init(void)
1480 if (the_lnet.ln_push_target)
1483 rc = LNetEQAlloc(0, lnet_push_target_event_handler,
1484 &the_lnet.ln_push_target_eq);
1486 CERROR("Can't allocated push target EQ: %d\n", rc);
1490 /* Start at the required minimum, we'll enlarge if required. */
1491 the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1493 rc = lnet_push_target_resize();
1496 LNetEQFree(the_lnet.ln_push_target_eq);
1497 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1503 /* Clean up the push target. */
1504 static void lnet_push_target_fini(void)
1506 if (!the_lnet.ln_push_target)
1509 /* Unlink and invalidate to prevent new references. */
1510 LNetMDUnlink(the_lnet.ln_push_target_md);
1511 LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1513 /* Wait for the unlink to complete. */
1514 while (lnet_ping_buffer_numref(the_lnet.ln_push_target) > 1) {
1515 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1516 set_current_state(TASK_UNINTERRUPTIBLE);
1517 schedule_timeout(cfs_time_seconds(1));
1520 lnet_ping_buffer_decref(the_lnet.ln_push_target);
1521 the_lnet.ln_push_target = NULL;
1522 the_lnet.ln_push_target_nnis = 0;
1524 LNetEQFree(the_lnet.ln_push_target_eq);
1525 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1529 lnet_ni_tq_credits(struct lnet_ni *ni)
1533 LASSERT(ni->ni_ncpts >= 1);
1535 if (ni->ni_ncpts == 1)
1536 return ni->ni_net->net_tunables.lct_max_tx_credits;
1538 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1539 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1540 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1546 lnet_ni_unlink_locked(struct lnet_ni *ni)
1548 if (!list_empty(&ni->ni_cptlist)) {
1549 list_del_init(&ni->ni_cptlist);
1550 lnet_ni_decref_locked(ni, 0);
1553 /* move it to zombie list and nobody can find it anymore */
1554 LASSERT(!list_empty(&ni->ni_netlist));
1555 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1556 lnet_ni_decref_locked(ni, 0);
1560 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1565 struct list_head *zombie_list = &net->net_ni_zombie;
1568 * Now wait for the NIs I just nuked to show up on the zombie
1569 * list and shut them down in guaranteed thread context
1572 while (!list_empty(zombie_list)) {
1576 ni = list_entry(zombie_list->next,
1577 struct lnet_ni, ni_netlist);
1578 list_del_init(&ni->ni_netlist);
1579 /* the ni should be in deleting state. If it's not it's
1581 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
1582 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1585 /* still busy, add it back to zombie list */
1586 list_add(&ni->ni_netlist, zombie_list);
1590 if (!list_empty(&ni->ni_netlist)) {
1591 lnet_net_unlock(LNET_LOCK_EX);
1593 if ((i & (-i)) == i) {
1595 "Waiting for zombie LNI %s\n",
1596 libcfs_nid2str(ni->ni_nid));
1598 set_current_state(TASK_UNINTERRUPTIBLE);
1599 schedule_timeout(cfs_time_seconds(1));
1600 lnet_net_lock(LNET_LOCK_EX);
1604 lnet_net_unlock(LNET_LOCK_EX);
1606 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1608 LASSERT(!in_interrupt());
1609 (net->net_lnd->lnd_shutdown)(ni);
1612 CDEBUG(D_LNI, "Removed LNI %s\n",
1613 libcfs_nid2str(ni->ni_nid));
1617 lnet_net_lock(LNET_LOCK_EX);
1621 /* shutdown down the NI and release refcount */
1623 lnet_shutdown_lndni(struct lnet_ni *ni)
1626 struct lnet_net *net = ni->ni_net;
1628 lnet_net_lock(LNET_LOCK_EX);
1629 ni->ni_state = LNET_NI_STATE_DELETING;
1630 lnet_ni_unlink_locked(ni);
1631 lnet_incr_dlc_seq();
1632 lnet_net_unlock(LNET_LOCK_EX);
1634 /* clear messages for this NI on the lazy portal */
1635 for (i = 0; i < the_lnet.ln_nportals; i++)
1636 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1638 lnet_net_lock(LNET_LOCK_EX);
1639 lnet_clear_zombies_nis_locked(net);
1640 lnet_net_unlock(LNET_LOCK_EX);
1644 lnet_shutdown_lndnet(struct lnet_net *net)
1648 lnet_net_lock(LNET_LOCK_EX);
1650 net->net_state = LNET_NET_STATE_DELETING;
1652 list_del_init(&net->net_list);
1654 while (!list_empty(&net->net_ni_list)) {
1655 ni = list_entry(net->net_ni_list.next,
1656 struct lnet_ni, ni_netlist);
1657 lnet_net_unlock(LNET_LOCK_EX);
1658 lnet_shutdown_lndni(ni);
1659 lnet_net_lock(LNET_LOCK_EX);
1662 lnet_net_unlock(LNET_LOCK_EX);
1664 /* Do peer table cleanup for this net */
1665 lnet_peer_tables_cleanup(net);
1667 lnet_net_lock(LNET_LOCK_EX);
1669 * decrement ref count on lnd only when the entire network goes
1672 net->net_lnd->lnd_refcount--;
1674 lnet_net_unlock(LNET_LOCK_EX);
1680 lnet_shutdown_lndnets(void)
1682 struct lnet_net *net;
1684 /* NB called holding the global mutex */
1686 /* All quiet on the API front */
1687 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
1688 LASSERT(the_lnet.ln_refcount == 0);
1690 lnet_net_lock(LNET_LOCK_EX);
1691 the_lnet.ln_state = LNET_STATE_STOPPING;
1693 while (!list_empty(&the_lnet.ln_nets)) {
1695 * move the nets to the zombie list to avoid them being
1696 * picked up for new work. LONET is also included in the
1697 * Nets that will be moved to the zombie list
1699 net = list_entry(the_lnet.ln_nets.next,
1700 struct lnet_net, net_list);
1701 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1704 /* Drop the cached loopback Net. */
1705 if (the_lnet.ln_loni != NULL) {
1706 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1707 the_lnet.ln_loni = NULL;
1709 lnet_net_unlock(LNET_LOCK_EX);
1711 /* iterate through the net zombie list and delete each net */
1712 while (!list_empty(&the_lnet.ln_net_zombie)) {
1713 net = list_entry(the_lnet.ln_net_zombie.next,
1714 struct lnet_net, net_list);
1715 lnet_shutdown_lndnet(net);
1718 lnet_net_lock(LNET_LOCK_EX);
1719 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
1720 lnet_net_unlock(LNET_LOCK_EX);
1724 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1727 struct lnet_tx_queue *tq;
1729 struct lnet_net *net = ni->ni_net;
1731 mutex_lock(&the_lnet.ln_lnd_mutex);
1734 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1735 ni->ni_lnd_tunables_set = true;
1738 rc = (net->net_lnd->lnd_startup)(ni);
1740 mutex_unlock(&the_lnet.ln_lnd_mutex);
1743 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1744 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1745 lnet_net_lock(LNET_LOCK_EX);
1746 net->net_lnd->lnd_refcount--;
1747 lnet_net_unlock(LNET_LOCK_EX);
1751 ni->ni_state = LNET_NI_STATE_ACTIVE;
1753 /* We keep a reference on the loopback net through the loopback NI */
1754 if (net->net_lnd->lnd_type == LOLND) {
1756 LASSERT(the_lnet.ln_loni == NULL);
1757 the_lnet.ln_loni = ni;
1758 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
1759 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
1760 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
1761 ni->ni_net->net_tunables.lct_peer_timeout = 0;
1765 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
1766 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
1767 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1768 libcfs_lnd2str(net->net_lnd->lnd_type),
1769 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
1771 /* shutdown the NI since if we get here then it must've already
1774 lnet_shutdown_lndni(ni);
1778 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1779 tq->tq_credits_min =
1780 tq->tq_credits_max =
1781 tq->tq_credits = lnet_ni_tq_credits(ni);
1784 atomic_set(&ni->ni_tx_credits,
1785 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
1787 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1788 libcfs_nid2str(ni->ni_nid),
1789 ni->ni_net->net_tunables.lct_peer_tx_credits,
1790 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1791 ni->ni_net->net_tunables.lct_peer_rtr_credits,
1792 ni->ni_net->net_tunables.lct_peer_timeout);
1801 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
1804 struct lnet_net *net_l = NULL;
1805 struct list_head local_ni_list;
1809 struct lnet_lnd *lnd;
1811 net->net_tunables.lct_peer_timeout;
1813 net->net_tunables.lct_max_tx_credits;
1814 int peerrtrcredits =
1815 net->net_tunables.lct_peer_rtr_credits;
1817 INIT_LIST_HEAD(&local_ni_list);
1820 * make sure that this net is unique. If it isn't then
1821 * we are adding interfaces to an already existing network, and
1822 * 'net' is just a convenient way to pass in the list.
1823 * if it is unique we need to find the LND and load it if
1826 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
1827 lnd_type = LNET_NETTYP(net->net_id);
1829 LASSERT(libcfs_isknown_lnd(lnd_type));
1831 mutex_lock(&the_lnet.ln_lnd_mutex);
1832 lnd = lnet_find_lnd_by_type(lnd_type);
1835 mutex_unlock(&the_lnet.ln_lnd_mutex);
1836 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1837 mutex_lock(&the_lnet.ln_lnd_mutex);
1839 lnd = lnet_find_lnd_by_type(lnd_type);
1841 mutex_unlock(&the_lnet.ln_lnd_mutex);
1842 CERROR("Can't load LND %s, module %s, rc=%d\n",
1843 libcfs_lnd2str(lnd_type),
1844 libcfs_lnd2modname(lnd_type), rc);
1845 #ifndef HAVE_MODULE_LOADING_SUPPORT
1846 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1847 "compiled with kernel module "
1848 "loading support.");
1855 lnet_net_lock(LNET_LOCK_EX);
1856 lnd->lnd_refcount++;
1857 lnet_net_unlock(LNET_LOCK_EX);
1861 mutex_unlock(&the_lnet.ln_lnd_mutex);
1867 * net_l: if the network being added is unique then net_l
1868 * will point to that network
1869 * if the network being added is not unique then
1870 * net_l points to the existing network.
1872 * When we enter the loop below, we'll pick NIs off he
1873 * network beign added and start them up, then add them to
1874 * a local ni list. Once we've successfully started all
1875 * the NIs then we join the local NI list (of started up
1876 * networks) with the net_l->net_ni_list, which should
1877 * point to the correct network to add the new ni list to
1879 * If any of the new NIs fail to start up, then we want to
1880 * iterate through the local ni list, which should include
1881 * any NIs which were successfully started up, and shut
1884 * After than we want to delete the network being added,
1885 * to avoid a memory leak.
1889 * When a network uses TCP bonding then all its interfaces
1890 * must be specified when the network is first defined: the
1891 * TCP bonding code doesn't allow for interfaces to be added
1894 if (net_l != net && net_l != NULL && use_tcp_bonding &&
1895 LNET_NETTYP(net_l->net_id) == SOCKLND) {
1900 while (!list_empty(&net->net_ni_added)) {
1901 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
1903 list_del_init(&ni->ni_netlist);
1905 /* make sure that the the NI we're about to start
1906 * up is actually unique. if it's not fail. */
1907 if (!lnet_ni_unique_net(&net_l->net_ni_list,
1908 ni->ni_interfaces[0])) {
1913 /* adjust the pointer the parent network, just in case it
1914 * the net is a duplicate */
1917 rc = lnet_startup_lndni(ni, tun);
1919 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
1920 ni->ni_net->net_lnd->lnd_query != NULL);
1926 list_add_tail(&ni->ni_netlist, &local_ni_list);
1931 lnet_net_lock(LNET_LOCK_EX);
1932 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
1933 lnet_incr_dlc_seq();
1934 lnet_net_unlock(LNET_LOCK_EX);
1936 /* if the network is not unique then we don't want to keep
1937 * it around after we're done. Free it. Otherwise add that
1938 * net to the global the_lnet.ln_nets */
1939 if (net_l != net && net_l != NULL) {
1941 * TODO - note. currently the tunables can not be updated
1946 net->net_state = LNET_NET_STATE_ACTIVE;
1948 * restore tunables after it has been overwitten by the
1951 if (peer_timeout != -1)
1952 net->net_tunables.lct_peer_timeout = peer_timeout;
1953 if (maxtxcredits != -1)
1954 net->net_tunables.lct_max_tx_credits = maxtxcredits;
1955 if (peerrtrcredits != -1)
1956 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
1958 lnet_net_lock(LNET_LOCK_EX);
1959 list_add_tail(&net->net_list, &the_lnet.ln_nets);
1960 lnet_net_unlock(LNET_LOCK_EX);
1967 * shutdown the new NIs that are being started up
1968 * free the NET being started
1970 while (!list_empty(&local_ni_list)) {
1971 ni = list_entry(local_ni_list.next, struct lnet_ni,
1974 lnet_shutdown_lndni(ni);
1984 lnet_startup_lndnets(struct list_head *netlist)
1986 struct lnet_net *net;
1991 * Change to running state before bringing up the LNDs. This
1992 * allows lnet_shutdown_lndnets() to assert that we've passed
1995 lnet_net_lock(LNET_LOCK_EX);
1996 the_lnet.ln_state = LNET_STATE_RUNNING;
1997 lnet_net_unlock(LNET_LOCK_EX);
1999 while (!list_empty(netlist)) {
2000 net = list_entry(netlist->next, struct lnet_net, net_list);
2001 list_del_init(&net->net_list);
2003 rc = lnet_startup_lndnet(net, NULL);
2013 lnet_shutdown_lndnets();
2019 * Initialize LNet library.
2021 * Automatically called at module loading time. Caller has to call
2022 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2023 * latter returned 0. It must be called exactly once.
2025 * \retval 0 on success
2026 * \retval -ve on failures.
2028 int lnet_lib_init(void)
2032 lnet_assert_wire_constants();
2034 /* refer to global cfs_cpt_table for now */
2035 the_lnet.ln_cpt_table = cfs_cpt_table;
2036 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
2038 LASSERT(the_lnet.ln_cpt_number > 0);
2039 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2040 /* we are under risk of consuming all lh_cookie */
2041 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2042 "please change setting of CPT-table and retry\n",
2043 the_lnet.ln_cpt_number, LNET_CPT_MAX);
2047 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2048 the_lnet.ln_cpt_bits++;
2050 rc = lnet_create_locks();
2052 CERROR("Can't create LNet global locks: %d\n", rc);
2056 the_lnet.ln_refcount = 0;
2057 LNetInvalidateEQHandle(&the_lnet.ln_rc_eqh);
2058 INIT_LIST_HEAD(&the_lnet.ln_lnds);
2059 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2060 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
2061 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
2063 /* The hash table size is the number of bits it takes to express the set
2064 * ln_num_routes, minus 1 (better to under estimate than over so we
2065 * don't waste memory). */
2066 if (rnet_htable_size <= 0)
2067 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2068 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2069 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2070 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2071 order_base_2(rnet_htable_size) - 1);
2073 /* All LNDs apart from the LOLND are in separate modules. They
2074 * register themselves when their module loads, and unregister
2075 * themselves when their module is unloaded. */
2076 lnet_register_lnd(&the_lolnd);
2081 * Finalize LNet library.
2083 * \pre lnet_lib_init() called with success.
2084 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2086 void lnet_lib_exit(void)
2088 LASSERT(the_lnet.ln_refcount == 0);
2090 while (!list_empty(&the_lnet.ln_lnds))
2091 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
2092 struct lnet_lnd, lnd_list));
2093 lnet_destroy_locks();
2097 * Set LNet PID and start LNet interfaces, routing, and forwarding.
2099 * Users must call this function at least once before any other functions.
2100 * For each successful call there must be a corresponding call to
2101 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2104 * The PID used by LNet may be different from the one requested.
2107 * \param requested_pid PID requested by the caller.
2109 * \return >= 0 on success, and < 0 error code on failures.
2112 LNetNIInit(lnet_pid_t requested_pid)
2114 int im_a_router = 0;
2117 struct lnet_ping_buffer *pbuf;
2118 struct lnet_handle_md ping_mdh;
2119 struct list_head net_head;
2120 struct lnet_net *net;
2122 INIT_LIST_HEAD(&net_head);
2124 mutex_lock(&the_lnet.ln_api_mutex);
2126 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2128 if (the_lnet.ln_refcount > 0) {
2129 rc = the_lnet.ln_refcount++;
2130 mutex_unlock(&the_lnet.ln_api_mutex);
2134 rc = lnet_prepare(requested_pid);
2136 mutex_unlock(&the_lnet.ln_api_mutex);
2140 /* create a network for Loopback network */
2141 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2144 goto err_empty_list;
2147 /* Add in the loopback NI */
2148 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2150 goto err_empty_list;
2153 /* If LNet is being initialized via DLC it is possible
2154 * that the user requests not to load module parameters (ones which
2155 * are supported by DLC) on initialization. Therefore, make sure not
2156 * to load networks, routes and forwarding from module parameters
2157 * in this case. On cleanup in case of failure only clean up
2158 * routes if it has been loaded */
2159 if (!the_lnet.ln_nis_from_mod_params) {
2160 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2163 goto err_empty_list;
2166 ni_count = lnet_startup_lndnets(&net_head);
2169 goto err_empty_list;
2172 if (!the_lnet.ln_nis_from_mod_params) {
2173 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2175 goto err_shutdown_lndnis;
2177 rc = lnet_check_routes();
2179 goto err_destroy_routes;
2181 rc = lnet_rtrpools_alloc(im_a_router);
2183 goto err_destroy_routes;
2186 rc = lnet_acceptor_start();
2188 goto err_destroy_routes;
2190 the_lnet.ln_refcount = 1;
2191 /* Now I may use my own API functions... */
2193 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2195 goto err_acceptor_stop;
2197 lnet_ping_target_update(pbuf, ping_mdh);
2199 rc = lnet_router_checker_start();
2203 rc = lnet_push_target_init();
2205 goto err_stop_router_checker;
2207 rc = lnet_peer_discovery_start();
2209 goto err_destroy_push_target;
2214 mutex_unlock(&the_lnet.ln_api_mutex);
2218 err_destroy_push_target:
2219 lnet_push_target_fini();
2220 err_stop_router_checker:
2221 lnet_router_checker_stop();
2223 lnet_ping_target_fini();
2225 the_lnet.ln_refcount = 0;
2226 lnet_acceptor_stop();
2228 if (!the_lnet.ln_nis_from_mod_params)
2229 lnet_destroy_routes();
2230 err_shutdown_lndnis:
2231 lnet_shutdown_lndnets();
2235 mutex_unlock(&the_lnet.ln_api_mutex);
2236 while (!list_empty(&net_head)) {
2237 struct lnet_net *net;
2239 net = list_entry(net_head.next, struct lnet_net, net_list);
2240 list_del_init(&net->net_list);
2245 EXPORT_SYMBOL(LNetNIInit);
2248 * Stop LNet interfaces, routing, and forwarding.
2250 * Users must call this function once for each successful call to LNetNIInit().
2251 * Once the LNetNIFini() operation has been started, the results of pending
2252 * API operations are undefined.
2254 * \return always 0 for current implementation.
2259 mutex_lock(&the_lnet.ln_api_mutex);
2261 LASSERT(the_lnet.ln_refcount > 0);
2263 if (the_lnet.ln_refcount != 1) {
2264 the_lnet.ln_refcount--;
2266 LASSERT(!the_lnet.ln_niinit_self);
2271 lnet_peer_discovery_stop();
2272 lnet_push_target_fini();
2273 lnet_router_checker_stop();
2274 lnet_ping_target_fini();
2276 /* Teardown fns that use my own API functions BEFORE here */
2277 the_lnet.ln_refcount = 0;
2279 lnet_acceptor_stop();
2280 lnet_destroy_routes();
2281 lnet_shutdown_lndnets();
2285 mutex_unlock(&the_lnet.ln_api_mutex);
2288 EXPORT_SYMBOL(LNetNIFini);
2291 * Grabs the ni data from the ni structure and fills the out
2294 * \param[in] ni network interface structure
2295 * \param[out] cfg_ni NI config information
2296 * \param[out] tun network and LND tunables
2299 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2300 struct lnet_ioctl_config_lnd_tunables *tun,
2301 struct lnet_ioctl_element_stats *stats,
2304 size_t min_size = 0;
2307 if (!ni || !cfg_ni || !tun)
2310 if (ni->ni_interfaces[0] != NULL) {
2311 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2312 if (ni->ni_interfaces[i] != NULL) {
2313 strncpy(cfg_ni->lic_ni_intf[i],
2314 ni->ni_interfaces[i],
2315 sizeof(cfg_ni->lic_ni_intf[i]));
2320 cfg_ni->lic_nid = ni->ni_nid;
2321 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2322 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2324 cfg_ni->lic_status = ni->ni_status->ns_status;
2325 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2326 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2328 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2331 stats->iel_send_count = atomic_read(&ni->ni_stats.send_count);
2332 stats->iel_recv_count = atomic_read(&ni->ni_stats.recv_count);
2336 * tun->lt_tun will always be present, but in order to be
2337 * backwards compatible, we need to deal with the cases when
2338 * tun->lt_tun is smaller than what the kernel has, because it
2339 * comes from an older version of a userspace program, then we'll
2340 * need to copy as much information as we have available space.
2342 min_size = tun_size - sizeof(tun->lt_cmn);
2343 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2345 /* copy over the cpts */
2346 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2347 ni->ni_cpts == NULL) {
2348 for (i = 0; i < ni->ni_ncpts; i++)
2349 cfg_ni->lic_cpts[i] = i;
2352 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2353 i < LNET_MAX_SHOW_NUM_CPT;
2355 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2357 cfg_ni->lic_ncpts = ni->ni_ncpts;
2361 * NOTE: This is a legacy function left in the code to be backwards
2362 * compatible with older userspace programs. It should eventually be
2365 * Grabs the ni data from the ni structure and fills the out
2368 * \param[in] ni network interface structure
2369 * \param[out] config config information
2372 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2373 struct lnet_ioctl_config_data *config)
2375 struct lnet_ioctl_net_config *net_config;
2376 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2377 size_t min_size, tunable_size = 0;
2383 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2387 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2388 ARRAY_SIZE(net_config->ni_interfaces));
2390 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2391 if (!ni->ni_interfaces[i])
2394 strncpy(net_config->ni_interfaces[i],
2395 ni->ni_interfaces[i],
2396 sizeof(net_config->ni_interfaces[i]));
2399 config->cfg_nid = ni->ni_nid;
2400 config->cfg_config_u.cfg_net.net_peer_timeout =
2401 ni->ni_net->net_tunables.lct_peer_timeout;
2402 config->cfg_config_u.cfg_net.net_max_tx_credits =
2403 ni->ni_net->net_tunables.lct_max_tx_credits;
2404 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2405 ni->ni_net->net_tunables.lct_peer_tx_credits;
2406 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2407 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2409 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2410 net_config->ni_status = LNET_NI_STATUS_UP;
2412 net_config->ni_status = ni->ni_status->ns_status;
2415 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2417 for (i = 0; i < num_cpts; i++)
2418 net_config->ni_cpts[i] = ni->ni_cpts[i];
2420 config->cfg_ncpts = num_cpts;
2424 * See if user land tools sent in a newer and larger version
2425 * of struct lnet_tunables than what the kernel uses.
2427 min_size = sizeof(*config) + sizeof(*net_config);
2429 if (config->cfg_hdr.ioc_len > min_size)
2430 tunable_size = config->cfg_hdr.ioc_len - min_size;
2432 /* Don't copy too much data to user space */
2433 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2434 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2436 if (lnd_cfg && min_size) {
2437 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2438 config->cfg_config_u.cfg_net.net_interface_count = 1;
2440 /* Tell user land that kernel side has less data */
2441 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2442 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2443 config->cfg_hdr.ioc_len -= min_size;
2449 lnet_get_ni_idx_locked(int idx)
2452 struct lnet_net *net;
2454 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2455 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2465 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2468 struct lnet_net *net = mynet;
2472 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2474 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2480 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2481 /* if you reached the end of the ni list and the net is
2482 * specified, then there are no more nis in that net */
2486 /* we reached the end of this net ni list. move to the
2488 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2489 /* no more nets and no more NIs. */
2492 /* get the next net */
2493 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2495 /* get the ni on it */
2496 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2502 /* there are more nis left */
2503 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2509 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2514 int idx = config->cfg_count;
2516 cpt = lnet_net_lock_current();
2518 ni = lnet_get_ni_idx_locked(idx);
2523 lnet_fill_ni_info_legacy(ni, config);
2527 lnet_net_unlock(cpt);
2532 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2533 struct lnet_ioctl_config_lnd_tunables *tun,
2534 struct lnet_ioctl_element_stats *stats,
2541 if (!cfg_ni || !tun || !stats)
2544 cpt = lnet_net_lock_current();
2546 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2551 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
2555 lnet_net_unlock(cpt);
2559 static int lnet_add_net_common(struct lnet_net *net,
2560 struct lnet_ioctl_config_lnd_tunables *tun)
2563 struct lnet_ping_buffer *pbuf;
2564 struct lnet_handle_md ping_mdh;
2566 struct lnet_remotenet *rnet;
2568 int num_acceptor_nets;
2570 lnet_net_lock(LNET_LOCK_EX);
2571 rnet = lnet_find_rnet_locked(net->net_id);
2572 lnet_net_unlock(LNET_LOCK_EX);
2574 * make sure that the net added doesn't invalidate the current
2575 * configuration LNet is keeping
2578 CERROR("Adding net %s will invalidate routing configuration\n",
2579 libcfs_net2str(net->net_id));
2585 * make sure you calculate the correct number of slots in the ping
2586 * buffer. Since the ping info is a flattened list of all the NIs,
2587 * we should allocate enough slots to accomodate the number of NIs
2588 * which will be added.
2590 * since ni hasn't been configured yet, use
2591 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
2593 net_ni_count = lnet_get_net_ni_count_pre(net);
2595 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2596 net_ni_count + lnet_get_ni_count(),
2604 memcpy(&net->net_tunables,
2605 &tun->lt_cmn, sizeof(net->net_tunables));
2607 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
2610 * before starting this network get a count of the current TCP
2611 * networks which require the acceptor thread running. If that
2612 * count is == 0 before we start up this network, then we'd want to
2613 * start up the acceptor thread after starting up this network
2615 num_acceptor_nets = lnet_count_acceptor_nets();
2617 net_id = net->net_id;
2619 rc = lnet_startup_lndnet(net,
2620 (tun) ? &tun->lt_tun : NULL);
2624 lnet_net_lock(LNET_LOCK_EX);
2625 net = lnet_get_net_locked(net_id);
2626 lnet_net_unlock(LNET_LOCK_EX);
2631 * Start the acceptor thread if this is the first network
2632 * being added that requires the thread.
2634 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
2635 rc = lnet_acceptor_start();
2637 /* shutdown the net that we just started */
2638 CERROR("Failed to start up acceptor thread\n");
2639 lnet_shutdown_lndnet(net);
2644 lnet_net_lock(LNET_LOCK_EX);
2645 lnet_peer_net_added(net);
2646 lnet_net_unlock(LNET_LOCK_EX);
2648 lnet_ping_target_update(pbuf, ping_mdh);
2653 lnet_ping_md_unlink(pbuf, &ping_mdh);
2654 lnet_ping_buffer_decref(pbuf);
2658 static int lnet_handle_legacy_ip2nets(char *ip2nets,
2659 struct lnet_ioctl_config_lnd_tunables *tun)
2661 struct lnet_net *net;
2664 struct list_head net_head;
2666 INIT_LIST_HEAD(&net_head);
2668 rc = lnet_parse_ip2nets(&nets, ip2nets);
2672 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2676 mutex_lock(&the_lnet.ln_api_mutex);
2677 while (!list_empty(&net_head)) {
2678 net = list_entry(net_head.next, struct lnet_net, net_list);
2679 list_del_init(&net->net_list);
2680 rc = lnet_add_net_common(net, tun);
2686 mutex_unlock(&the_lnet.ln_api_mutex);
2688 while (!list_empty(&net_head)) {
2689 net = list_entry(net_head.next, struct lnet_net, net_list);
2690 list_del_init(&net->net_list);
2696 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
2698 struct lnet_net *net;
2700 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2704 /* get the tunables if they are available */
2705 if (conf->lic_cfg_hdr.ioc_len >=
2706 sizeof(*conf) + sizeof(*tun))
2707 tun = (struct lnet_ioctl_config_lnd_tunables *)
2710 /* handle legacy ip2nets from DLC */
2711 if (conf->lic_legacy_ip2nets[0] != '\0')
2712 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
2715 net_id = LNET_NIDNET(conf->lic_nid);
2717 net = lnet_net_alloc(net_id, NULL);
2721 for (i = 0; i < conf->lic_ncpts; i++) {
2722 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
2726 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
2727 conf->lic_ni_intf[0]);
2731 mutex_lock(&the_lnet.ln_api_mutex);
2733 rc = lnet_add_net_common(net, tun);
2735 mutex_unlock(&the_lnet.ln_api_mutex);
2740 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
2742 struct lnet_net *net;
2744 __u32 net_id = LNET_NIDNET(conf->lic_nid);
2745 struct lnet_ping_buffer *pbuf;
2746 struct lnet_handle_md ping_mdh;
2751 /* don't allow userspace to shutdown the LOLND */
2752 if (LNET_NETTYP(net_id) == LOLND)
2755 mutex_lock(&the_lnet.ln_api_mutex);
2759 net = lnet_get_net_locked(net_id);
2761 CERROR("net %s not found\n",
2762 libcfs_net2str(net_id));
2767 addr = LNET_NIDADDR(conf->lic_nid);
2769 /* remove the entire net */
2770 net_count = lnet_get_net_ni_count_locked(net);
2774 /* create and link a new ping info, before removing the old one */
2775 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2776 lnet_get_ni_count() - net_count,
2779 goto unlock_api_mutex;
2781 lnet_shutdown_lndnet(net);
2783 if (lnet_count_acceptor_nets() == 0)
2784 lnet_acceptor_stop();
2786 lnet_ping_target_update(pbuf, ping_mdh);
2788 goto unlock_api_mutex;
2791 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
2793 CERROR("nid %s not found\n",
2794 libcfs_nid2str(conf->lic_nid));
2799 net_count = lnet_get_net_ni_count_locked(net);
2803 /* create and link a new ping info, before removing the old one */
2804 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2805 lnet_get_ni_count() - 1, false);
2807 goto unlock_api_mutex;
2809 lnet_shutdown_lndni(ni);
2811 if (lnet_count_acceptor_nets() == 0)
2812 lnet_acceptor_stop();
2814 lnet_ping_target_update(pbuf, ping_mdh);
2816 /* check if the net is empty and remove it if it is */
2818 lnet_shutdown_lndnet(net);
2820 goto unlock_api_mutex;
2825 mutex_unlock(&the_lnet.ln_api_mutex);
2831 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
2832 * They are only expected to be called for unique networks.
2833 * That can be as a result of older DLC library
2834 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
2837 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
2839 struct lnet_net *net;
2840 struct list_head net_head;
2842 struct lnet_ioctl_config_lnd_tunables tun;
2843 char *nets = conf->cfg_config_u.cfg_net.net_intf;
2845 INIT_LIST_HEAD(&net_head);
2847 /* Create a net/ni structures for the network string */
2848 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2850 return rc == 0 ? -EINVAL : rc;
2852 mutex_lock(&the_lnet.ln_api_mutex);
2855 rc = -EINVAL; /* only add one network per call */
2856 goto out_unlock_clean;
2859 net = list_entry(net_head.next, struct lnet_net, net_list);
2860 list_del_init(&net->net_list);
2862 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
2864 memset(&tun, 0, sizeof(tun));
2866 tun.lt_cmn.lct_peer_timeout =
2867 conf->cfg_config_u.cfg_net.net_peer_timeout;
2868 tun.lt_cmn.lct_peer_tx_credits =
2869 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
2870 tun.lt_cmn.lct_peer_rtr_credits =
2871 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
2872 tun.lt_cmn.lct_max_tx_credits =
2873 conf->cfg_config_u.cfg_net.net_max_tx_credits;
2875 rc = lnet_add_net_common(net, &tun);
2878 mutex_unlock(&the_lnet.ln_api_mutex);
2879 while (!list_empty(&net_head)) {
2880 /* net_head list is empty in success case */
2881 net = list_entry(net_head.next, struct lnet_net, net_list);
2882 list_del_init(&net->net_list);
2889 lnet_dyn_del_net(__u32 net_id)
2891 struct lnet_net *net;
2892 struct lnet_ping_buffer *pbuf;
2893 struct lnet_handle_md ping_mdh;
2897 /* don't allow userspace to shutdown the LOLND */
2898 if (LNET_NETTYP(net_id) == LOLND)
2901 mutex_lock(&the_lnet.ln_api_mutex);
2905 net = lnet_get_net_locked(net_id);
2912 net_ni_count = lnet_get_net_ni_count_locked(net);
2916 /* create and link a new ping info, before removing the old one */
2917 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2918 lnet_get_ni_count() - net_ni_count, false);
2922 lnet_shutdown_lndnet(net);
2924 if (lnet_count_acceptor_nets() == 0)
2925 lnet_acceptor_stop();
2927 lnet_ping_target_update(pbuf, ping_mdh);
2930 mutex_unlock(&the_lnet.ln_api_mutex);
2935 void lnet_incr_dlc_seq(void)
2937 atomic_inc(&lnet_dlc_seq_no);
2940 __u32 lnet_get_dlc_seq_locked(void)
2942 return atomic_read(&lnet_dlc_seq_no);
2946 * LNet ioctl handler.
2950 LNetCtl(unsigned int cmd, void *arg)
2952 struct libcfs_ioctl_data *data = arg;
2953 struct lnet_ioctl_config_data *config;
2954 struct lnet_process_id id = {0};
2958 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
2959 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
2962 case IOC_LIBCFS_GET_NI:
2963 rc = LNetGetId(data->ioc_count, &id);
2964 data->ioc_nid = id.nid;
2967 case IOC_LIBCFS_FAIL_NID:
2968 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
2970 case IOC_LIBCFS_ADD_ROUTE:
2973 if (config->cfg_hdr.ioc_len < sizeof(*config))
2976 mutex_lock(&the_lnet.ln_api_mutex);
2977 rc = lnet_add_route(config->cfg_net,
2978 config->cfg_config_u.cfg_route.rtr_hop,
2980 config->cfg_config_u.cfg_route.
2983 rc = lnet_check_routes();
2985 lnet_del_route(config->cfg_net,
2988 mutex_unlock(&the_lnet.ln_api_mutex);
2991 case IOC_LIBCFS_DEL_ROUTE:
2994 if (config->cfg_hdr.ioc_len < sizeof(*config))
2997 mutex_lock(&the_lnet.ln_api_mutex);
2998 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
2999 mutex_unlock(&the_lnet.ln_api_mutex);
3002 case IOC_LIBCFS_GET_ROUTE:
3005 if (config->cfg_hdr.ioc_len < sizeof(*config))
3008 mutex_lock(&the_lnet.ln_api_mutex);
3009 rc = lnet_get_route(config->cfg_count,
3011 &config->cfg_config_u.cfg_route.rtr_hop,
3013 &config->cfg_config_u.cfg_route.rtr_flags,
3014 &config->cfg_config_u.cfg_route.
3016 mutex_unlock(&the_lnet.ln_api_mutex);
3019 case IOC_LIBCFS_GET_LOCAL_NI: {
3020 struct lnet_ioctl_config_ni *cfg_ni;
3021 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3022 struct lnet_ioctl_element_stats *stats;
3026 /* get the tunables if they are available */
3027 if (cfg_ni->lic_cfg_hdr.ioc_len <
3028 sizeof(*cfg_ni) + sizeof(*stats)+ sizeof(*tun))
3031 stats = (struct lnet_ioctl_element_stats *)
3033 tun = (struct lnet_ioctl_config_lnd_tunables *)
3034 (cfg_ni->lic_bulk + sizeof(*stats));
3036 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3039 mutex_lock(&the_lnet.ln_api_mutex);
3040 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3041 mutex_unlock(&the_lnet.ln_api_mutex);
3045 case IOC_LIBCFS_GET_NET: {
3046 size_t total = sizeof(*config) +
3047 sizeof(struct lnet_ioctl_net_config);
3050 if (config->cfg_hdr.ioc_len < total)
3053 mutex_lock(&the_lnet.ln_api_mutex);
3054 rc = lnet_get_net_config(config);
3055 mutex_unlock(&the_lnet.ln_api_mutex);
3059 case IOC_LIBCFS_GET_LNET_STATS:
3061 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3063 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3066 mutex_lock(&the_lnet.ln_api_mutex);
3067 lnet_counters_get(&lnet_stats->st_cntrs);
3068 mutex_unlock(&the_lnet.ln_api_mutex);
3072 case IOC_LIBCFS_CONFIG_RTR:
3075 if (config->cfg_hdr.ioc_len < sizeof(*config))
3078 mutex_lock(&the_lnet.ln_api_mutex);
3079 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3080 rc = lnet_rtrpools_enable();
3081 mutex_unlock(&the_lnet.ln_api_mutex);
3084 lnet_rtrpools_disable();
3085 mutex_unlock(&the_lnet.ln_api_mutex);
3088 case IOC_LIBCFS_ADD_BUF:
3091 if (config->cfg_hdr.ioc_len < sizeof(*config))
3094 mutex_lock(&the_lnet.ln_api_mutex);
3095 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3097 config->cfg_config_u.cfg_buffers.
3099 config->cfg_config_u.cfg_buffers.
3101 mutex_unlock(&the_lnet.ln_api_mutex);
3104 case IOC_LIBCFS_SET_NUMA_RANGE: {
3105 struct lnet_ioctl_set_value *numa;
3107 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3109 lnet_net_lock(LNET_LOCK_EX);
3110 lnet_numa_range = numa->sv_value;
3111 lnet_net_unlock(LNET_LOCK_EX);
3115 case IOC_LIBCFS_GET_NUMA_RANGE: {
3116 struct lnet_ioctl_set_value *numa;
3118 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3120 numa->sv_value = lnet_numa_range;
3124 case IOC_LIBCFS_GET_BUF: {
3125 struct lnet_ioctl_pool_cfg *pool_cfg;
3126 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3130 if (config->cfg_hdr.ioc_len < total)
3133 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3135 mutex_lock(&the_lnet.ln_api_mutex);
3136 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3137 mutex_unlock(&the_lnet.ln_api_mutex);
3141 case IOC_LIBCFS_ADD_PEER_NI: {
3142 struct lnet_ioctl_peer_cfg *cfg = arg;
3144 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3147 mutex_lock(&the_lnet.ln_api_mutex);
3148 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3151 mutex_unlock(&the_lnet.ln_api_mutex);
3155 case IOC_LIBCFS_DEL_PEER_NI: {
3156 struct lnet_ioctl_peer_cfg *cfg = arg;
3158 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3161 mutex_lock(&the_lnet.ln_api_mutex);
3162 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3163 cfg->prcfg_cfg_nid);
3164 mutex_unlock(&the_lnet.ln_api_mutex);
3168 case IOC_LIBCFS_GET_PEER_INFO: {
3169 struct lnet_ioctl_peer *peer_info = arg;
3171 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3174 mutex_lock(&the_lnet.ln_api_mutex);
3175 rc = lnet_get_peer_ni_info(
3176 peer_info->pr_count,
3178 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3179 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3180 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3181 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3182 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3183 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3184 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3185 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3186 mutex_unlock(&the_lnet.ln_api_mutex);
3190 case IOC_LIBCFS_GET_PEER_NI: {
3191 struct lnet_ioctl_peer_cfg *cfg = arg;
3193 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3196 mutex_lock(&the_lnet.ln_api_mutex);
3197 rc = lnet_get_peer_info(&cfg->prcfg_prim_nid,
3198 &cfg->prcfg_cfg_nid,
3202 (void __user *)cfg->prcfg_bulk);
3203 mutex_unlock(&the_lnet.ln_api_mutex);
3207 case IOC_LIBCFS_GET_PEER_LIST: {
3208 struct lnet_ioctl_peer_cfg *cfg = arg;
3210 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3213 mutex_lock(&the_lnet.ln_api_mutex);
3214 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3215 (lnet_process_id_t __user *)cfg->prcfg_bulk);
3216 mutex_unlock(&the_lnet.ln_api_mutex);
3220 case IOC_LIBCFS_NOTIFY_ROUTER: {
3221 unsigned long jiffies_passed;
3223 jiffies_passed = ktime_get_real_seconds() - data->ioc_u64[0];
3224 jiffies_passed = cfs_time_seconds(jiffies_passed);
3226 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
3227 jiffies - jiffies_passed);
3230 case IOC_LIBCFS_LNET_DIST:
3231 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3232 if (rc < 0 && rc != -EHOSTUNREACH)
3235 data->ioc_u32[0] = rc;
3238 case IOC_LIBCFS_TESTPROTOCOMPAT:
3239 lnet_net_lock(LNET_LOCK_EX);
3240 the_lnet.ln_testprotocompat = data->ioc_flags;
3241 lnet_net_unlock(LNET_LOCK_EX);
3244 case IOC_LIBCFS_LNET_FAULT:
3245 return lnet_fault_ctl(data->ioc_flags, data);
3247 case IOC_LIBCFS_PING: {
3248 signed long timeout;
3250 id.nid = data->ioc_nid;
3251 id.pid = data->ioc_u32[0];
3253 /* If timeout is negative then set default of 3 minutes */
3254 if (((s32)data->ioc_u32[1] <= 0) ||
3255 data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3256 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3258 timeout = msecs_to_jiffies(data->ioc_u32[1]);
3260 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3261 data->ioc_plen1 / sizeof(struct lnet_process_id));
3266 data->ioc_count = rc;
3270 case IOC_LIBCFS_PING_PEER: {
3271 struct lnet_ioctl_ping_data *ping = arg;
3272 struct lnet_peer *lp;
3273 signed long timeout;
3275 /* If timeout is negative then set default of 3 minutes */
3276 if (((s32)ping->op_param) <= 0 ||
3277 ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3278 timeout = msecs_to_jiffies(DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC);
3280 timeout = msecs_to_jiffies(ping->op_param);
3282 rc = lnet_ping(ping->ping_id, timeout,
3288 lp = lnet_find_peer(ping->ping_id.nid);
3290 ping->ping_id.nid = lp->lp_primary_nid;
3291 ping->mr_info = lnet_peer_is_multi_rail(lp);
3293 ping->ping_count = rc;
3298 ni = lnet_net2ni_addref(data->ioc_net);
3302 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3305 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
3312 EXPORT_SYMBOL(LNetCtl);
3314 void LNetDebugPeer(struct lnet_process_id id)
3316 lnet_debug_peer(id.nid);
3318 EXPORT_SYMBOL(LNetDebugPeer);
3321 * Determine if the specified peer \a nid is on the local node.
3323 * \param nid peer nid to check
3325 * \retval true If peer NID is on the local node.
3326 * \retval false If peer NID is not on the local node.
3328 bool LNetIsPeerLocal(lnet_nid_t nid)
3330 struct lnet_net *net;
3334 cpt = lnet_net_lock_current();
3335 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3336 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3337 if (ni->ni_nid == nid) {
3338 lnet_net_unlock(cpt);
3343 lnet_net_unlock(cpt);
3347 EXPORT_SYMBOL(LNetIsPeerLocal);
3350 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
3351 * Note that all interfaces share a same PID, as requested by LNetNIInit().
3353 * \param index Index of the interface to look up.
3354 * \param id On successful return, this location will hold the
3355 * struct lnet_process_id ID of the interface.
3357 * \retval 0 If an interface exists at \a index.
3358 * \retval -ENOENT If no interface has been found.
3361 LNetGetId(unsigned int index, struct lnet_process_id *id)
3364 struct lnet_net *net;
3368 LASSERT(the_lnet.ln_refcount > 0);
3370 cpt = lnet_net_lock_current();
3372 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3373 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3377 id->nid = ni->ni_nid;
3378 id->pid = the_lnet.ln_pid;
3384 lnet_net_unlock(cpt);
3387 EXPORT_SYMBOL(LNetGetId);
3389 static int lnet_ping(struct lnet_process_id id, signed long timeout,
3390 struct lnet_process_id __user *ids, int n_ids)
3392 struct lnet_handle_eq eqh;
3393 struct lnet_handle_md mdh;
3394 struct lnet_event event;
3395 struct lnet_md md = { NULL };
3399 const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
3400 struct lnet_ping_buffer *pbuf;
3401 struct lnet_process_id tmpid;
3408 /* n_ids limit is arbitrary */
3409 if (n_ids <= 0 || n_ids > lnet_interfaces_max || id.nid == LNET_NID_ANY)
3412 if (id.pid == LNET_PID_ANY)
3413 id.pid = LNET_PID_LUSTRE;
3415 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
3419 /* NB 2 events max (including any unlink event) */
3420 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
3422 CERROR("Can't allocate EQ: %d\n", rc);
3423 goto fail_ping_buffer_decref;
3426 /* initialize md content */
3427 md.start = &pbuf->pb_info;
3428 md.length = LNET_PING_INFO_SIZE(n_ids);
3429 md.threshold = 2; /* GET/REPLY */
3431 md.options = LNET_MD_TRUNCATE;
3435 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
3437 CERROR("Can't bind MD: %d\n", rc);
3441 rc = LNetGet(LNET_NID_ANY, mdh, id,
3442 LNET_RESERVED_PORTAL,
3443 LNET_PROTO_PING_MATCHBITS, 0);
3446 /* Don't CERROR; this could be deliberate! */
3447 rc2 = LNetMDUnlink(mdh);
3450 /* NB must wait for the UNLINK event below... */
3452 timeout = a_long_time;
3456 /* MUST block for unlink to complete */
3458 blocked = cfs_block_allsigs();
3460 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
3463 cfs_restore_sigs(blocked);
3465 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
3466 (rc2 <= 0) ? -1 : event.type,
3467 (rc2 <= 0) ? -1 : event.status,
3468 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
3470 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
3472 if (rc2 <= 0 || event.status != 0) {
3473 /* timeout or error */
3474 if (!replied && rc == 0)
3475 rc = (rc2 < 0) ? rc2 :
3476 (rc2 == 0) ? -ETIMEDOUT :
3480 /* Ensure completion in finite time... */
3482 /* No assertion (racing with network) */
3484 timeout = a_long_time;
3485 } else if (rc2 == 0) {
3486 /* timed out waiting for unlink */
3487 CWARN("ping %s: late network completion\n",
3490 } else if (event.type == LNET_EVENT_REPLY) {
3494 } while (rc2 <= 0 || !event.unlinked);
3498 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
3505 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
3507 rc = -EPROTO; /* if I can't parse... */
3510 CERROR("%s: ping info too short %d\n",
3511 libcfs_id2str(id), nob);
3515 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
3516 lnet_swap_pinginfo(pbuf);
3517 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
3518 CERROR("%s: Unexpected magic %08x\n",
3519 libcfs_id2str(id), pbuf->pb_info.pi_magic);
3523 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
3524 CERROR("%s: ping w/o NI status: 0x%x\n",
3525 libcfs_id2str(id), pbuf->pb_info.pi_features);
3529 if (nob < LNET_PING_INFO_SIZE(0)) {
3530 CERROR("%s: Short reply %d(%d min)\n",
3532 nob, (int)LNET_PING_INFO_SIZE(0));
3536 if (pbuf->pb_info.pi_nnis < n_ids)
3537 n_ids = pbuf->pb_info.pi_nnis;
3539 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
3540 CERROR("%s: Short reply %d(%d expected)\n",
3542 nob, (int)LNET_PING_INFO_SIZE(n_ids));
3546 rc = -EFAULT; /* if I segv in copy_to_user()... */
3548 memset(&tmpid, 0, sizeof(tmpid));
3549 for (i = 0; i < n_ids; i++) {
3550 tmpid.pid = pbuf->pb_info.pi_pid;
3551 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
3552 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
3555 rc = pbuf->pb_info.pi_nnis;
3558 rc2 = LNetEQFree(eqh);
3560 CERROR("rc2 %d\n", rc2);
3563 fail_ping_buffer_decref:
3564 lnet_ping_buffer_decref(pbuf);