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
42 struct lnet the_lnet; /* THE state of the network */
43 EXPORT_SYMBOL(the_lnet);
45 static char *ip2nets = "";
46 module_param(ip2nets, charp, 0444);
47 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
49 static char *networks = "";
50 module_param(networks, charp, 0444);
51 MODULE_PARM_DESC(networks, "local networks");
53 static char *routes = "";
54 module_param(routes, charp, 0444);
55 MODULE_PARM_DESC(routes, "routes to non-local networks");
57 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
58 module_param(rnet_htable_size, int, 0444);
59 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
61 static int use_tcp_bonding = false;
62 module_param(use_tcp_bonding, int, 0444);
63 MODULE_PARM_DESC(use_tcp_bonding,
64 "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
66 unsigned int lnet_numa_range = 0;
67 module_param(lnet_numa_range, uint, 0444);
68 MODULE_PARM_DESC(lnet_numa_range,
69 "NUMA range to consider during Multi-Rail selection");
71 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
72 static int intf_max_set(const char *val, struct kernel_param *kp);
73 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
74 &lnet_interfaces_max, S_IRUGO|S_IWUSR);
75 MODULE_PARM_DESC(lnet_interfaces_max,
76 "Maximum number of interfaces in a node.");
79 * This sequence number keeps track of how many times DLC was used to
80 * update the local NIs. It is incremented when a NI is added or
81 * removed and checked when sending a message to determine if there is
82 * a need to re-run the selection algorithm. See lnet_select_pathway()
83 * for more details on its usage.
85 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
87 static int lnet_ping(struct lnet_process_id id, signed long timeout,
88 struct lnet_process_id __user *ids, int n_ids);
91 intf_max_set(const char *val, struct kernel_param *kp)
95 rc = kstrtoint(val, 0, &value);
97 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
101 if (value < LNET_INTERFACES_MIN) {
102 CWARN("max interfaces provided are too small, setting to %d\n",
103 LNET_INTERFACES_MIN);
104 value = LNET_INTERFACES_MIN;
107 *(int *)kp->arg = value;
113 lnet_get_routes(void)
119 lnet_get_networks(void)
124 if (*networks != 0 && *ip2nets != 0) {
125 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
126 "'ip2nets' but not both at once\n");
131 rc = lnet_parse_ip2nets(&nets, ip2nets);
132 return (rc == 0) ? nets : NULL;
142 lnet_init_locks(void)
144 spin_lock_init(&the_lnet.ln_eq_wait_lock);
145 init_waitqueue_head(&the_lnet.ln_eq_waitq);
146 init_waitqueue_head(&the_lnet.ln_rc_waitq);
147 mutex_init(&the_lnet.ln_lnd_mutex);
148 mutex_init(&the_lnet.ln_api_mutex);
152 lnet_fini_locks(void)
156 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
157 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
161 lnet_descriptor_setup(void)
163 /* create specific kmem_cache for MEs and small MDs (i.e., originally
164 * allocated in <size-xxx> kmem_cache).
166 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
168 if (!lnet_mes_cachep)
171 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
172 LNET_SMALL_MD_SIZE, 0, 0,
174 if (!lnet_small_mds_cachep)
181 lnet_descriptor_cleanup(void)
184 if (lnet_small_mds_cachep) {
185 kmem_cache_destroy(lnet_small_mds_cachep);
186 lnet_small_mds_cachep = NULL;
189 if (lnet_mes_cachep) {
190 kmem_cache_destroy(lnet_mes_cachep);
191 lnet_mes_cachep = NULL;
196 lnet_create_remote_nets_table(void)
199 struct list_head *hash;
201 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
202 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
203 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
205 CERROR("Failed to create remote nets hash table\n");
209 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
210 INIT_LIST_HEAD(&hash[i]);
211 the_lnet.ln_remote_nets_hash = hash;
216 lnet_destroy_remote_nets_table(void)
220 if (the_lnet.ln_remote_nets_hash == NULL)
223 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
224 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
226 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
227 LNET_REMOTE_NETS_HASH_SIZE *
228 sizeof(the_lnet.ln_remote_nets_hash[0]));
229 the_lnet.ln_remote_nets_hash = NULL;
233 lnet_destroy_locks(void)
235 if (the_lnet.ln_res_lock != NULL) {
236 cfs_percpt_lock_free(the_lnet.ln_res_lock);
237 the_lnet.ln_res_lock = NULL;
240 if (the_lnet.ln_net_lock != NULL) {
241 cfs_percpt_lock_free(the_lnet.ln_net_lock);
242 the_lnet.ln_net_lock = NULL;
249 lnet_create_locks(void)
253 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
254 if (the_lnet.ln_res_lock == NULL)
257 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
258 if (the_lnet.ln_net_lock == NULL)
264 lnet_destroy_locks();
268 static void lnet_assert_wire_constants(void)
270 /* Wire protocol assertions generated by 'wirecheck'
271 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
272 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
273 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
276 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
277 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
278 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
279 CLASSERT(LNET_MSG_ACK == 0);
280 CLASSERT(LNET_MSG_PUT == 1);
281 CLASSERT(LNET_MSG_GET == 2);
282 CLASSERT(LNET_MSG_REPLY == 3);
283 CLASSERT(LNET_MSG_HELLO == 4);
285 /* Checks for struct lnet_handle_wire */
286 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
287 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
288 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
289 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
290 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
292 /* Checks for struct struct lnet_magicversion */
293 CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
294 CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
295 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
296 CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
297 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
298 CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
299 CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
301 /* Checks for struct struct lnet_hdr */
302 CLASSERT((int)sizeof(struct lnet_hdr) == 72);
303 CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
304 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
305 CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
306 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
307 CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
308 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
309 CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
310 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
311 CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
312 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
313 CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
314 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
315 CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
316 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
319 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
320 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
321 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
322 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
323 CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
324 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
327 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
328 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
329 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
330 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
331 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
332 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
333 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
334 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
335 CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
336 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
339 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
340 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
341 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
342 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
343 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
344 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
345 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
346 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
347 CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
348 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
351 CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
352 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
355 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
356 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
357 CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
358 CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
361 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
363 struct lnet_lnd *lnd;
364 struct list_head *tmp;
366 /* holding lnd mutex */
367 list_for_each(tmp, &the_lnet.ln_lnds) {
368 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
370 if (lnd->lnd_type == type)
377 lnet_register_lnd(struct lnet_lnd *lnd)
379 mutex_lock(&the_lnet.ln_lnd_mutex);
381 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
382 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
384 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
385 lnd->lnd_refcount = 0;
387 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
389 mutex_unlock(&the_lnet.ln_lnd_mutex);
391 EXPORT_SYMBOL(lnet_register_lnd);
394 lnet_unregister_lnd(struct lnet_lnd *lnd)
396 mutex_lock(&the_lnet.ln_lnd_mutex);
398 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
399 LASSERT(lnd->lnd_refcount == 0);
401 list_del(&lnd->lnd_list);
402 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
404 mutex_unlock(&the_lnet.ln_lnd_mutex);
406 EXPORT_SYMBOL(lnet_unregister_lnd);
409 lnet_counters_get(struct lnet_counters *counters)
411 struct lnet_counters *ctr;
414 memset(counters, 0, sizeof(*counters));
416 lnet_net_lock(LNET_LOCK_EX);
418 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
419 counters->msgs_max += ctr->msgs_max;
420 counters->msgs_alloc += ctr->msgs_alloc;
421 counters->errors += ctr->errors;
422 counters->send_count += ctr->send_count;
423 counters->recv_count += ctr->recv_count;
424 counters->route_count += ctr->route_count;
425 counters->drop_count += ctr->drop_count;
426 counters->send_length += ctr->send_length;
427 counters->recv_length += ctr->recv_length;
428 counters->route_length += ctr->route_length;
429 counters->drop_length += ctr->drop_length;
432 lnet_net_unlock(LNET_LOCK_EX);
434 EXPORT_SYMBOL(lnet_counters_get);
437 lnet_counters_reset(void)
439 struct lnet_counters *counters;
442 lnet_net_lock(LNET_LOCK_EX);
444 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
445 memset(counters, 0, sizeof(struct lnet_counters));
447 lnet_net_unlock(LNET_LOCK_EX);
451 lnet_res_type2str(int type)
456 case LNET_COOKIE_TYPE_MD:
458 case LNET_COOKIE_TYPE_ME:
460 case LNET_COOKIE_TYPE_EQ:
466 lnet_res_container_cleanup(struct lnet_res_container *rec)
470 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
473 while (!list_empty(&rec->rec_active)) {
474 struct list_head *e = rec->rec_active.next;
477 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
478 lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
480 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
481 lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
483 } else { /* NB: Active MEs should be attached on portals */
490 /* Found alive MD/ME/EQ, user really should unlink/free
491 * all of them before finalize LNet, but if someone didn't,
492 * we have to recycle garbage for him */
493 CERROR("%d active elements on exit of %s container\n",
494 count, lnet_res_type2str(rec->rec_type));
497 if (rec->rec_lh_hash != NULL) {
498 LIBCFS_FREE(rec->rec_lh_hash,
499 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
500 rec->rec_lh_hash = NULL;
503 rec->rec_type = 0; /* mark it as finalized */
507 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
512 LASSERT(rec->rec_type == 0);
514 rec->rec_type = type;
515 INIT_LIST_HEAD(&rec->rec_active);
517 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
519 /* Arbitrary choice of hash table size */
520 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
521 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
522 if (rec->rec_lh_hash == NULL) {
527 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
528 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
533 CERROR("Failed to setup %s resource container\n",
534 lnet_res_type2str(type));
535 lnet_res_container_cleanup(rec);
540 lnet_res_containers_destroy(struct lnet_res_container **recs)
542 struct lnet_res_container *rec;
545 cfs_percpt_for_each(rec, i, recs)
546 lnet_res_container_cleanup(rec);
548 cfs_percpt_free(recs);
551 static struct lnet_res_container **
552 lnet_res_containers_create(int type)
554 struct lnet_res_container **recs;
555 struct lnet_res_container *rec;
559 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
561 CERROR("Failed to allocate %s resource containers\n",
562 lnet_res_type2str(type));
566 cfs_percpt_for_each(rec, i, recs) {
567 rc = lnet_res_container_setup(rec, i, type);
569 lnet_res_containers_destroy(recs);
577 struct lnet_libhandle *
578 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
580 /* ALWAYS called with lnet_res_lock held */
581 struct list_head *head;
582 struct lnet_libhandle *lh;
585 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
588 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
589 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
591 list_for_each_entry(lh, head, lh_hash_chain) {
592 if (lh->lh_cookie == cookie)
600 lnet_res_lh_initialize(struct lnet_res_container *rec,
601 struct lnet_libhandle *lh)
603 /* ALWAYS called with lnet_res_lock held */
604 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
607 lh->lh_cookie = rec->rec_lh_cookie;
608 rec->rec_lh_cookie += 1 << ibits;
610 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
612 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
615 static int lnet_unprepare(void);
618 lnet_prepare(lnet_pid_t requested_pid)
620 /* Prepare to bring up the network */
621 struct lnet_res_container **recs;
624 if (requested_pid == LNET_PID_ANY) {
625 /* Don't instantiate LNET just for me */
629 LASSERT(the_lnet.ln_refcount == 0);
631 the_lnet.ln_routing = 0;
633 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
634 the_lnet.ln_pid = requested_pid;
636 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
637 INIT_LIST_HEAD(&the_lnet.ln_peers);
638 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
639 INIT_LIST_HEAD(&the_lnet.ln_nets);
640 INIT_LIST_HEAD(&the_lnet.ln_routers);
641 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
642 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
644 rc = lnet_descriptor_setup();
648 rc = lnet_create_remote_nets_table();
653 * NB the interface cookie in wire handles guards against delayed
654 * replies and ACKs appearing valid after reboot.
656 the_lnet.ln_interface_cookie = ktime_get_real_ns();
658 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
659 sizeof(struct lnet_counters));
660 if (the_lnet.ln_counters == NULL) {
661 CERROR("Failed to allocate counters for LNet\n");
666 rc = lnet_peer_tables_create();
670 rc = lnet_msg_containers_create();
674 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
675 LNET_COOKIE_TYPE_EQ);
679 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
685 the_lnet.ln_me_containers = recs;
687 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
693 the_lnet.ln_md_containers = recs;
695 rc = lnet_portals_create();
697 CERROR("Failed to create portals for LNet: %d\n", rc);
709 lnet_unprepare (void)
711 /* NB no LNET_LOCK since this is the last reference. All LND instances
712 * have shut down already, so it is safe to unlink and free all
713 * descriptors, even those that appear committed to a network op (eg MD
714 * with non-zero pending count) */
716 lnet_fail_nid(LNET_NID_ANY, 0);
718 LASSERT(the_lnet.ln_refcount == 0);
719 LASSERT(list_empty(&the_lnet.ln_test_peers));
720 LASSERT(list_empty(&the_lnet.ln_nets));
722 lnet_portals_destroy();
724 if (the_lnet.ln_md_containers != NULL) {
725 lnet_res_containers_destroy(the_lnet.ln_md_containers);
726 the_lnet.ln_md_containers = NULL;
729 if (the_lnet.ln_me_containers != NULL) {
730 lnet_res_containers_destroy(the_lnet.ln_me_containers);
731 the_lnet.ln_me_containers = NULL;
734 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
736 lnet_msg_containers_destroy();
738 lnet_rtrpools_free(0);
740 if (the_lnet.ln_counters != NULL) {
741 cfs_percpt_free(the_lnet.ln_counters);
742 the_lnet.ln_counters = NULL;
744 lnet_destroy_remote_nets_table();
745 lnet_descriptor_cleanup();
751 lnet_net2ni_locked(__u32 net_id, int cpt)
754 struct lnet_net *net;
756 LASSERT(cpt != LNET_LOCK_EX);
758 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
759 if (net->net_id == net_id) {
760 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
770 lnet_net2ni_addref(__u32 net)
775 ni = lnet_net2ni_locked(net, 0);
777 lnet_ni_addref_locked(ni, 0);
782 EXPORT_SYMBOL(lnet_net2ni_addref);
785 lnet_get_net_locked(__u32 net_id)
787 struct lnet_net *net;
789 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
790 if (net->net_id == net_id)
798 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
803 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
808 val = hash_long(key, LNET_CPT_BITS);
809 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
813 return (unsigned int)(key + val + (val >> 1)) % number;
817 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
819 struct lnet_net *net;
821 /* must called with hold of lnet_net_lock */
822 if (LNET_CPT_NUMBER == 1)
823 return 0; /* the only one */
826 * If NI is provided then use the CPT identified in the NI cpt
827 * list if one exists. If one doesn't exist, then that NI is
828 * associated with all CPTs and it follows that the net it belongs
829 * to is implicitly associated with all CPTs, so just hash the nid
833 if (ni->ni_cpts != NULL)
834 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
837 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
840 /* no NI provided so look at the net */
841 net = lnet_get_net_locked(LNET_NIDNET(nid));
843 if (net != NULL && net->net_cpts != NULL) {
844 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
847 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
851 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
856 if (LNET_CPT_NUMBER == 1)
857 return 0; /* the only one */
859 cpt = lnet_net_lock_current();
861 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
863 lnet_net_unlock(cpt);
867 EXPORT_SYMBOL(lnet_cpt_of_nid);
870 lnet_islocalnet(__u32 net_id)
872 struct lnet_net *net;
876 cpt = lnet_net_lock_current();
878 net = lnet_get_net_locked(net_id);
882 lnet_net_unlock(cpt);
888 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
890 if (ni->ni_state == LNET_NI_STATE_ACTIVE ||
891 ni->ni_state == LNET_NI_STATE_DEGRADED)
898 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
900 struct lnet_net *net;
903 LASSERT(cpt != LNET_LOCK_EX);
905 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
906 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
907 if (ni->ni_nid == nid)
916 lnet_nid2ni_addref(lnet_nid_t nid)
921 ni = lnet_nid2ni_locked(nid, 0);
923 lnet_ni_addref_locked(ni, 0);
928 EXPORT_SYMBOL(lnet_nid2ni_addref);
931 lnet_islocalnid(lnet_nid_t nid)
936 cpt = lnet_net_lock_current();
937 ni = lnet_nid2ni_locked(nid, cpt);
938 lnet_net_unlock(cpt);
944 lnet_count_acceptor_nets(void)
946 /* Return the # of NIs that need the acceptor. */
948 struct lnet_net *net;
951 cpt = lnet_net_lock_current();
952 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
953 /* all socklnd type networks should have the acceptor
955 if (net->net_lnd->lnd_accept != NULL)
959 lnet_net_unlock(cpt);
964 struct lnet_ping_buffer *
965 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
967 struct lnet_ping_buffer *pbuf;
969 LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
971 pbuf->pb_nnis = nnis;
972 atomic_set(&pbuf->pb_refcnt, 1);
979 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
981 LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
982 LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
985 static struct lnet_ping_buffer *
986 lnet_ping_target_create(int nnis)
988 struct lnet_ping_buffer *pbuf;
990 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
992 CERROR("Can't allocate ping source [%d]\n", nnis);
996 pbuf->pb_info.pi_nnis = nnis;
997 pbuf->pb_info.pi_pid = the_lnet.ln_pid;
998 pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
999 pbuf->pb_info.pi_features = LNET_PING_FEAT_NI_STATUS;
1005 lnet_get_net_ni_count_locked(struct lnet_net *net)
1010 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1017 lnet_get_net_ni_count_pre(struct lnet_net *net)
1022 list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1029 lnet_get_ni_count(void)
1032 struct lnet_net *net;
1037 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1038 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1048 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1052 if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1054 if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1056 /* Loopback is guaranteed to be present */
1057 if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1059 if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1065 lnet_ping_target_destroy(void)
1067 struct lnet_net *net;
1070 lnet_net_lock(LNET_LOCK_EX);
1072 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1073 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1075 ni->ni_status = NULL;
1080 lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1081 the_lnet.ln_ping_target = NULL;
1083 lnet_net_unlock(LNET_LOCK_EX);
1087 lnet_ping_target_event_handler(struct lnet_event *event)
1089 struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1091 if (event->unlinked)
1092 lnet_ping_buffer_decref(pbuf);
1096 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1097 struct lnet_handle_md *ping_mdh,
1098 int ni_count, bool set_eq)
1100 struct lnet_process_id id = {
1101 .nid = LNET_NID_ANY,
1104 struct lnet_handle_me me_handle;
1105 struct lnet_md md = { NULL };
1109 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1110 &the_lnet.ln_ping_target_eq);
1112 CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1117 *ppbuf = lnet_ping_target_create(ni_count);
1118 if (*ppbuf == NULL) {
1123 /* Ping target ME/MD */
1124 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1125 LNET_PROTO_PING_MATCHBITS, 0,
1126 LNET_UNLINK, LNET_INS_AFTER,
1129 CERROR("Can't create ping target ME: %d\n", rc);
1130 goto fail_decref_ping_buffer;
1133 /* initialize md content */
1134 md.start = &(*ppbuf)->pb_info;
1135 md.length = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1136 md.threshold = LNET_MD_THRESH_INF;
1138 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1139 LNET_MD_MANAGE_REMOTE;
1140 md.eq_handle = the_lnet.ln_ping_target_eq;
1141 md.user_ptr = *ppbuf;
1143 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, ping_mdh);
1145 CERROR("Can't attach ping target MD: %d\n", rc);
1146 goto fail_unlink_ping_me;
1148 lnet_ping_buffer_addref(*ppbuf);
1152 fail_unlink_ping_me:
1153 rc2 = LNetMEUnlink(me_handle);
1155 fail_decref_ping_buffer:
1156 LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1157 lnet_ping_buffer_decref(*ppbuf);
1161 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1168 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1169 struct lnet_handle_md *ping_mdh)
1171 sigset_t blocked = cfs_block_allsigs();
1173 LNetMDUnlink(*ping_mdh);
1174 LNetInvalidateMDHandle(ping_mdh);
1176 /* NB the MD could be busy; this just starts the unlink */
1177 while (lnet_ping_buffer_numref(pbuf) > 1) {
1178 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1179 set_current_state(TASK_UNINTERRUPTIBLE);
1180 schedule_timeout(cfs_time_seconds(1));
1183 cfs_restore_sigs(blocked);
1187 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1190 struct lnet_net *net;
1191 struct lnet_ni_status *ns;
1196 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1197 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1198 LASSERT(i < pbuf->pb_nnis);
1200 ns = &pbuf->pb_info.pi_ni[i];
1202 ns->ns_nid = ni->ni_nid;
1205 ns->ns_status = (ni->ni_status != NULL) ?
1206 ni->ni_status->ns_status :
1215 * We (ab)use the ns_status of the loopback interface to
1216 * transmit the sequence number. The first interface listed
1217 * must be the loopback interface.
1219 rc = lnet_ping_info_validate(&pbuf->pb_info);
1221 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1224 LNET_PING_BUFFER_SEQNO(pbuf) =
1225 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1229 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1230 struct lnet_handle_md ping_mdh)
1232 struct lnet_ping_buffer *old_pbuf = NULL;
1233 struct lnet_handle_md old_ping_md;
1235 /* switch the NIs to point to the new ping info created */
1236 lnet_net_lock(LNET_LOCK_EX);
1238 if (!the_lnet.ln_routing)
1239 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1241 /* Ensure only known feature bits have been set. */
1242 LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1243 LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1245 lnet_ping_target_install_locked(pbuf);
1247 if (the_lnet.ln_ping_target) {
1248 old_pbuf = the_lnet.ln_ping_target;
1249 old_ping_md = the_lnet.ln_ping_target_md;
1251 the_lnet.ln_ping_target_md = ping_mdh;
1252 the_lnet.ln_ping_target = pbuf;
1254 lnet_net_unlock(LNET_LOCK_EX);
1257 /* unlink and free the old ping info */
1258 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1259 lnet_ping_buffer_decref(old_pbuf);
1264 lnet_ping_target_fini(void)
1268 lnet_ping_md_unlink(the_lnet.ln_ping_target,
1269 &the_lnet.ln_ping_target_md);
1271 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1274 lnet_ping_target_destroy();
1278 lnet_ni_tq_credits(struct lnet_ni *ni)
1282 LASSERT(ni->ni_ncpts >= 1);
1284 if (ni->ni_ncpts == 1)
1285 return ni->ni_net->net_tunables.lct_max_tx_credits;
1287 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1288 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1289 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1295 lnet_ni_unlink_locked(struct lnet_ni *ni)
1297 if (!list_empty(&ni->ni_cptlist)) {
1298 list_del_init(&ni->ni_cptlist);
1299 lnet_ni_decref_locked(ni, 0);
1302 /* move it to zombie list and nobody can find it anymore */
1303 LASSERT(!list_empty(&ni->ni_netlist));
1304 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1305 lnet_ni_decref_locked(ni, 0);
1309 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1314 struct list_head *zombie_list = &net->net_ni_zombie;
1317 * Now wait for the NIs I just nuked to show up on the zombie
1318 * list and shut them down in guaranteed thread context
1321 while (!list_empty(zombie_list)) {
1325 ni = list_entry(zombie_list->next,
1326 struct lnet_ni, ni_netlist);
1327 list_del_init(&ni->ni_netlist);
1328 /* the ni should be in deleting state. If it's not it's
1330 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
1331 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1334 /* still busy, add it back to zombie list */
1335 list_add(&ni->ni_netlist, zombie_list);
1339 if (!list_empty(&ni->ni_netlist)) {
1340 lnet_net_unlock(LNET_LOCK_EX);
1342 if ((i & (-i)) == i) {
1344 "Waiting for zombie LNI %s\n",
1345 libcfs_nid2str(ni->ni_nid));
1347 set_current_state(TASK_UNINTERRUPTIBLE);
1348 schedule_timeout(cfs_time_seconds(1));
1349 lnet_net_lock(LNET_LOCK_EX);
1353 lnet_net_unlock(LNET_LOCK_EX);
1355 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1357 LASSERT(!in_interrupt());
1358 (net->net_lnd->lnd_shutdown)(ni);
1361 CDEBUG(D_LNI, "Removed LNI %s\n",
1362 libcfs_nid2str(ni->ni_nid));
1366 lnet_net_lock(LNET_LOCK_EX);
1370 /* shutdown down the NI and release refcount */
1372 lnet_shutdown_lndni(struct lnet_ni *ni)
1375 struct lnet_net *net = ni->ni_net;
1377 lnet_net_lock(LNET_LOCK_EX);
1378 ni->ni_state = LNET_NI_STATE_DELETING;
1379 lnet_ni_unlink_locked(ni);
1380 lnet_incr_dlc_seq();
1381 lnet_net_unlock(LNET_LOCK_EX);
1383 /* clear messages for this NI on the lazy portal */
1384 for (i = 0; i < the_lnet.ln_nportals; i++)
1385 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1387 lnet_net_lock(LNET_LOCK_EX);
1388 lnet_clear_zombies_nis_locked(net);
1389 lnet_net_unlock(LNET_LOCK_EX);
1393 lnet_shutdown_lndnet(struct lnet_net *net)
1397 lnet_net_lock(LNET_LOCK_EX);
1399 net->net_state = LNET_NET_STATE_DELETING;
1401 list_del_init(&net->net_list);
1403 while (!list_empty(&net->net_ni_list)) {
1404 ni = list_entry(net->net_ni_list.next,
1405 struct lnet_ni, ni_netlist);
1406 lnet_net_unlock(LNET_LOCK_EX);
1407 lnet_shutdown_lndni(ni);
1408 lnet_net_lock(LNET_LOCK_EX);
1411 lnet_net_unlock(LNET_LOCK_EX);
1413 /* Do peer table cleanup for this net */
1414 lnet_peer_tables_cleanup(net);
1416 lnet_net_lock(LNET_LOCK_EX);
1418 * decrement ref count on lnd only when the entire network goes
1421 net->net_lnd->lnd_refcount--;
1423 lnet_net_unlock(LNET_LOCK_EX);
1429 lnet_shutdown_lndnets(void)
1431 struct lnet_net *net;
1433 /* NB called holding the global mutex */
1435 /* All quiet on the API front */
1436 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
1437 LASSERT(the_lnet.ln_refcount == 0);
1439 lnet_net_lock(LNET_LOCK_EX);
1440 the_lnet.ln_state = LNET_STATE_STOPPING;
1442 while (!list_empty(&the_lnet.ln_nets)) {
1444 * move the nets to the zombie list to avoid them being
1445 * picked up for new work. LONET is also included in the
1446 * Nets that will be moved to the zombie list
1448 net = list_entry(the_lnet.ln_nets.next,
1449 struct lnet_net, net_list);
1450 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1453 /* Drop the cached loopback Net. */
1454 if (the_lnet.ln_loni != NULL) {
1455 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1456 the_lnet.ln_loni = NULL;
1458 lnet_net_unlock(LNET_LOCK_EX);
1460 /* iterate through the net zombie list and delete each net */
1461 while (!list_empty(&the_lnet.ln_net_zombie)) {
1462 net = list_entry(the_lnet.ln_net_zombie.next,
1463 struct lnet_net, net_list);
1464 lnet_shutdown_lndnet(net);
1467 lnet_net_lock(LNET_LOCK_EX);
1468 the_lnet.ln_state = LNET_STATE_SHUTDOWN;
1469 lnet_net_unlock(LNET_LOCK_EX);
1473 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1476 struct lnet_tx_queue *tq;
1478 struct lnet_net *net = ni->ni_net;
1480 mutex_lock(&the_lnet.ln_lnd_mutex);
1483 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1484 ni->ni_lnd_tunables_set = true;
1487 rc = (net->net_lnd->lnd_startup)(ni);
1489 mutex_unlock(&the_lnet.ln_lnd_mutex);
1492 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1493 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1494 lnet_net_lock(LNET_LOCK_EX);
1495 net->net_lnd->lnd_refcount--;
1496 lnet_net_unlock(LNET_LOCK_EX);
1500 ni->ni_state = LNET_NI_STATE_ACTIVE;
1502 /* We keep a reference on the loopback net through the loopback NI */
1503 if (net->net_lnd->lnd_type == LOLND) {
1505 LASSERT(the_lnet.ln_loni == NULL);
1506 the_lnet.ln_loni = ni;
1507 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
1508 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
1509 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
1510 ni->ni_net->net_tunables.lct_peer_timeout = 0;
1514 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
1515 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
1516 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1517 libcfs_lnd2str(net->net_lnd->lnd_type),
1518 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
1520 /* shutdown the NI since if we get here then it must've already
1523 lnet_shutdown_lndni(ni);
1527 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1528 tq->tq_credits_min =
1529 tq->tq_credits_max =
1530 tq->tq_credits = lnet_ni_tq_credits(ni);
1533 atomic_set(&ni->ni_tx_credits,
1534 lnet_ni_tq_credits(ni) * ni->ni_ncpts);
1536 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1537 libcfs_nid2str(ni->ni_nid),
1538 ni->ni_net->net_tunables.lct_peer_tx_credits,
1539 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1540 ni->ni_net->net_tunables.lct_peer_rtr_credits,
1541 ni->ni_net->net_tunables.lct_peer_timeout);
1550 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
1553 struct lnet_net *net_l = NULL;
1554 struct list_head local_ni_list;
1558 struct lnet_lnd *lnd;
1560 net->net_tunables.lct_peer_timeout;
1562 net->net_tunables.lct_max_tx_credits;
1563 int peerrtrcredits =
1564 net->net_tunables.lct_peer_rtr_credits;
1566 INIT_LIST_HEAD(&local_ni_list);
1569 * make sure that this net is unique. If it isn't then
1570 * we are adding interfaces to an already existing network, and
1571 * 'net' is just a convenient way to pass in the list.
1572 * if it is unique we need to find the LND and load it if
1575 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
1576 lnd_type = LNET_NETTYP(net->net_id);
1578 LASSERT(libcfs_isknown_lnd(lnd_type));
1580 mutex_lock(&the_lnet.ln_lnd_mutex);
1581 lnd = lnet_find_lnd_by_type(lnd_type);
1584 mutex_unlock(&the_lnet.ln_lnd_mutex);
1585 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1586 mutex_lock(&the_lnet.ln_lnd_mutex);
1588 lnd = lnet_find_lnd_by_type(lnd_type);
1590 mutex_unlock(&the_lnet.ln_lnd_mutex);
1591 CERROR("Can't load LND %s, module %s, rc=%d\n",
1592 libcfs_lnd2str(lnd_type),
1593 libcfs_lnd2modname(lnd_type), rc);
1594 #ifndef HAVE_MODULE_LOADING_SUPPORT
1595 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1596 "compiled with kernel module "
1597 "loading support.");
1604 lnet_net_lock(LNET_LOCK_EX);
1605 lnd->lnd_refcount++;
1606 lnet_net_unlock(LNET_LOCK_EX);
1610 mutex_unlock(&the_lnet.ln_lnd_mutex);
1616 * net_l: if the network being added is unique then net_l
1617 * will point to that network
1618 * if the network being added is not unique then
1619 * net_l points to the existing network.
1621 * When we enter the loop below, we'll pick NIs off he
1622 * network beign added and start them up, then add them to
1623 * a local ni list. Once we've successfully started all
1624 * the NIs then we join the local NI list (of started up
1625 * networks) with the net_l->net_ni_list, which should
1626 * point to the correct network to add the new ni list to
1628 * If any of the new NIs fail to start up, then we want to
1629 * iterate through the local ni list, which should include
1630 * any NIs which were successfully started up, and shut
1633 * After than we want to delete the network being added,
1634 * to avoid a memory leak.
1638 * When a network uses TCP bonding then all its interfaces
1639 * must be specified when the network is first defined: the
1640 * TCP bonding code doesn't allow for interfaces to be added
1643 if (net_l != net && net_l != NULL && use_tcp_bonding &&
1644 LNET_NETTYP(net_l->net_id) == SOCKLND) {
1649 while (!list_empty(&net->net_ni_added)) {
1650 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
1652 list_del_init(&ni->ni_netlist);
1654 /* make sure that the the NI we're about to start
1655 * up is actually unique. if it's not fail. */
1656 if (!lnet_ni_unique_net(&net_l->net_ni_list,
1657 ni->ni_interfaces[0])) {
1662 /* adjust the pointer the parent network, just in case it
1663 * the net is a duplicate */
1666 rc = lnet_startup_lndni(ni, tun);
1668 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
1669 ni->ni_net->net_lnd->lnd_query != NULL);
1675 list_add_tail(&ni->ni_netlist, &local_ni_list);
1680 lnet_net_lock(LNET_LOCK_EX);
1681 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
1682 lnet_incr_dlc_seq();
1683 lnet_net_unlock(LNET_LOCK_EX);
1685 /* if the network is not unique then we don't want to keep
1686 * it around after we're done. Free it. Otherwise add that
1687 * net to the global the_lnet.ln_nets */
1688 if (net_l != net && net_l != NULL) {
1690 * TODO - note. currently the tunables can not be updated
1695 net->net_state = LNET_NET_STATE_ACTIVE;
1697 * restore tunables after it has been overwitten by the
1700 if (peer_timeout != -1)
1701 net->net_tunables.lct_peer_timeout = peer_timeout;
1702 if (maxtxcredits != -1)
1703 net->net_tunables.lct_max_tx_credits = maxtxcredits;
1704 if (peerrtrcredits != -1)
1705 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
1707 lnet_net_lock(LNET_LOCK_EX);
1708 list_add_tail(&net->net_list, &the_lnet.ln_nets);
1709 lnet_net_unlock(LNET_LOCK_EX);
1716 * shutdown the new NIs that are being started up
1717 * free the NET being started
1719 while (!list_empty(&local_ni_list)) {
1720 ni = list_entry(local_ni_list.next, struct lnet_ni,
1723 lnet_shutdown_lndni(ni);
1733 lnet_startup_lndnets(struct list_head *netlist)
1735 struct lnet_net *net;
1740 * Change to running state before bringing up the LNDs. This
1741 * allows lnet_shutdown_lndnets() to assert that we've passed
1744 lnet_net_lock(LNET_LOCK_EX);
1745 the_lnet.ln_state = LNET_STATE_RUNNING;
1746 lnet_net_unlock(LNET_LOCK_EX);
1748 while (!list_empty(netlist)) {
1749 net = list_entry(netlist->next, struct lnet_net, net_list);
1750 list_del_init(&net->net_list);
1752 rc = lnet_startup_lndnet(net, NULL);
1762 lnet_shutdown_lndnets();
1768 * Initialize LNet library.
1770 * Automatically called at module loading time. Caller has to call
1771 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
1772 * latter returned 0. It must be called exactly once.
1774 * \retval 0 on success
1775 * \retval -ve on failures.
1777 int lnet_lib_init(void)
1781 lnet_assert_wire_constants();
1783 memset(&the_lnet, 0, sizeof(the_lnet));
1785 /* refer to global cfs_cpt_table for now */
1786 the_lnet.ln_cpt_table = cfs_cpt_table;
1787 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1789 LASSERT(the_lnet.ln_cpt_number > 0);
1790 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1791 /* we are under risk of consuming all lh_cookie */
1792 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1793 "please change setting of CPT-table and retry\n",
1794 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1798 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1799 the_lnet.ln_cpt_bits++;
1801 rc = lnet_create_locks();
1803 CERROR("Can't create LNet global locks: %d\n", rc);
1807 the_lnet.ln_refcount = 0;
1808 LNetInvalidateEQHandle(&the_lnet.ln_rc_eqh);
1809 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1810 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
1811 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1812 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1814 /* The hash table size is the number of bits it takes to express the set
1815 * ln_num_routes, minus 1 (better to under estimate than over so we
1816 * don't waste memory). */
1817 if (rnet_htable_size <= 0)
1818 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1819 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1820 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1821 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1822 order_base_2(rnet_htable_size) - 1);
1824 /* All LNDs apart from the LOLND are in separate modules. They
1825 * register themselves when their module loads, and unregister
1826 * themselves when their module is unloaded. */
1827 lnet_register_lnd(&the_lolnd);
1832 * Finalize LNet library.
1834 * \pre lnet_lib_init() called with success.
1835 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1837 void lnet_lib_exit(void)
1839 LASSERT(the_lnet.ln_refcount == 0);
1841 while (!list_empty(&the_lnet.ln_lnds))
1842 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1843 struct lnet_lnd, lnd_list));
1844 lnet_destroy_locks();
1848 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1850 * Users must call this function at least once before any other functions.
1851 * For each successful call there must be a corresponding call to
1852 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1855 * The PID used by LNet may be different from the one requested.
1858 * \param requested_pid PID requested by the caller.
1860 * \return >= 0 on success, and < 0 error code on failures.
1863 LNetNIInit(lnet_pid_t requested_pid)
1865 int im_a_router = 0;
1868 struct lnet_ping_buffer *pbuf;
1869 struct lnet_handle_md ping_mdh;
1870 struct list_head net_head;
1871 struct lnet_net *net;
1873 INIT_LIST_HEAD(&net_head);
1875 mutex_lock(&the_lnet.ln_api_mutex);
1877 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1879 if (the_lnet.ln_refcount > 0) {
1880 rc = the_lnet.ln_refcount++;
1881 mutex_unlock(&the_lnet.ln_api_mutex);
1885 rc = lnet_prepare(requested_pid);
1887 mutex_unlock(&the_lnet.ln_api_mutex);
1891 /* create a network for Loopback network */
1892 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
1895 goto err_empty_list;
1898 /* Add in the loopback NI */
1899 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
1901 goto err_empty_list;
1904 /* If LNet is being initialized via DLC it is possible
1905 * that the user requests not to load module parameters (ones which
1906 * are supported by DLC) on initialization. Therefore, make sure not
1907 * to load networks, routes and forwarding from module parameters
1908 * in this case. On cleanup in case of failure only clean up
1909 * routes if it has been loaded */
1910 if (!the_lnet.ln_nis_from_mod_params) {
1911 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
1914 goto err_empty_list;
1917 ni_count = lnet_startup_lndnets(&net_head);
1920 goto err_empty_list;
1923 if (!the_lnet.ln_nis_from_mod_params) {
1924 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1926 goto err_shutdown_lndnis;
1928 rc = lnet_check_routes();
1930 goto err_destroy_routes;
1932 rc = lnet_rtrpools_alloc(im_a_router);
1934 goto err_destroy_routes;
1937 rc = lnet_acceptor_start();
1939 goto err_destroy_routes;
1941 the_lnet.ln_refcount = 1;
1942 /* Now I may use my own API functions... */
1944 rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
1946 goto err_acceptor_stop;
1948 lnet_ping_target_update(pbuf, ping_mdh);
1950 rc = lnet_router_checker_start();
1957 mutex_unlock(&the_lnet.ln_api_mutex);
1962 lnet_ping_target_fini();
1964 the_lnet.ln_refcount = 0;
1965 lnet_acceptor_stop();
1967 if (!the_lnet.ln_nis_from_mod_params)
1968 lnet_destroy_routes();
1969 err_shutdown_lndnis:
1970 lnet_shutdown_lndnets();
1974 mutex_unlock(&the_lnet.ln_api_mutex);
1975 while (!list_empty(&net_head)) {
1976 struct lnet_net *net;
1978 net = list_entry(net_head.next, struct lnet_net, net_list);
1979 list_del_init(&net->net_list);
1984 EXPORT_SYMBOL(LNetNIInit);
1987 * Stop LNet interfaces, routing, and forwarding.
1989 * Users must call this function once for each successful call to LNetNIInit().
1990 * Once the LNetNIFini() operation has been started, the results of pending
1991 * API operations are undefined.
1993 * \return always 0 for current implementation.
1998 mutex_lock(&the_lnet.ln_api_mutex);
2000 LASSERT(the_lnet.ln_refcount > 0);
2002 if (the_lnet.ln_refcount != 1) {
2003 the_lnet.ln_refcount--;
2005 LASSERT(!the_lnet.ln_niinit_self);
2010 lnet_router_checker_stop();
2011 lnet_ping_target_fini();
2013 /* Teardown fns that use my own API functions BEFORE here */
2014 the_lnet.ln_refcount = 0;
2016 lnet_acceptor_stop();
2017 lnet_destroy_routes();
2018 lnet_shutdown_lndnets();
2022 mutex_unlock(&the_lnet.ln_api_mutex);
2025 EXPORT_SYMBOL(LNetNIFini);
2028 * Grabs the ni data from the ni structure and fills the out
2031 * \param[in] ni network interface structure
2032 * \param[out] cfg_ni NI config information
2033 * \param[out] tun network and LND tunables
2036 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2037 struct lnet_ioctl_config_lnd_tunables *tun,
2038 struct lnet_ioctl_element_stats *stats,
2041 size_t min_size = 0;
2044 if (!ni || !cfg_ni || !tun)
2047 if (ni->ni_interfaces[0] != NULL) {
2048 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2049 if (ni->ni_interfaces[i] != NULL) {
2050 strncpy(cfg_ni->lic_ni_intf[i],
2051 ni->ni_interfaces[i],
2052 sizeof(cfg_ni->lic_ni_intf[i]));
2057 cfg_ni->lic_nid = ni->ni_nid;
2058 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2059 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2061 cfg_ni->lic_status = ni->ni_status->ns_status;
2062 cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2063 cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2065 memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2068 stats->iel_send_count = atomic_read(&ni->ni_stats.send_count);
2069 stats->iel_recv_count = atomic_read(&ni->ni_stats.recv_count);
2073 * tun->lt_tun will always be present, but in order to be
2074 * backwards compatible, we need to deal with the cases when
2075 * tun->lt_tun is smaller than what the kernel has, because it
2076 * comes from an older version of a userspace program, then we'll
2077 * need to copy as much information as we have available space.
2079 min_size = tun_size - sizeof(tun->lt_cmn);
2080 memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2082 /* copy over the cpts */
2083 if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2084 ni->ni_cpts == NULL) {
2085 for (i = 0; i < ni->ni_ncpts; i++)
2086 cfg_ni->lic_cpts[i] = i;
2089 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2090 i < LNET_MAX_SHOW_NUM_CPT;
2092 cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2094 cfg_ni->lic_ncpts = ni->ni_ncpts;
2098 * NOTE: This is a legacy function left in the code to be backwards
2099 * compatible with older userspace programs. It should eventually be
2102 * Grabs the ni data from the ni structure and fills the out
2105 * \param[in] ni network interface structure
2106 * \param[out] config config information
2109 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2110 struct lnet_ioctl_config_data *config)
2112 struct lnet_ioctl_net_config *net_config;
2113 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2114 size_t min_size, tunable_size = 0;
2120 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2124 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2125 ARRAY_SIZE(net_config->ni_interfaces));
2127 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2128 if (!ni->ni_interfaces[i])
2131 strncpy(net_config->ni_interfaces[i],
2132 ni->ni_interfaces[i],
2133 sizeof(net_config->ni_interfaces[i]));
2136 config->cfg_nid = ni->ni_nid;
2137 config->cfg_config_u.cfg_net.net_peer_timeout =
2138 ni->ni_net->net_tunables.lct_peer_timeout;
2139 config->cfg_config_u.cfg_net.net_max_tx_credits =
2140 ni->ni_net->net_tunables.lct_max_tx_credits;
2141 config->cfg_config_u.cfg_net.net_peer_tx_credits =
2142 ni->ni_net->net_tunables.lct_peer_tx_credits;
2143 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2144 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2146 if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2147 net_config->ni_status = LNET_NI_STATUS_UP;
2149 net_config->ni_status = ni->ni_status->ns_status;
2152 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2154 for (i = 0; i < num_cpts; i++)
2155 net_config->ni_cpts[i] = ni->ni_cpts[i];
2157 config->cfg_ncpts = num_cpts;
2161 * See if user land tools sent in a newer and larger version
2162 * of struct lnet_tunables than what the kernel uses.
2164 min_size = sizeof(*config) + sizeof(*net_config);
2166 if (config->cfg_hdr.ioc_len > min_size)
2167 tunable_size = config->cfg_hdr.ioc_len - min_size;
2169 /* Don't copy too much data to user space */
2170 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2171 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2173 if (lnd_cfg && min_size) {
2174 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2175 config->cfg_config_u.cfg_net.net_interface_count = 1;
2177 /* Tell user land that kernel side has less data */
2178 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2179 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2180 config->cfg_hdr.ioc_len -= min_size;
2186 lnet_get_ni_idx_locked(int idx)
2189 struct lnet_net *net;
2191 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2192 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2202 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2205 struct lnet_net *net = mynet;
2209 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2211 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2217 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2218 /* if you reached the end of the ni list and the net is
2219 * specified, then there are no more nis in that net */
2223 /* we reached the end of this net ni list. move to the
2225 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2226 /* no more nets and no more NIs. */
2229 /* get the next net */
2230 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2232 /* get the ni on it */
2233 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2239 /* there are more nis left */
2240 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2246 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2251 int idx = config->cfg_count;
2253 cpt = lnet_net_lock_current();
2255 ni = lnet_get_ni_idx_locked(idx);
2260 lnet_fill_ni_info_legacy(ni, config);
2264 lnet_net_unlock(cpt);
2269 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2270 struct lnet_ioctl_config_lnd_tunables *tun,
2271 struct lnet_ioctl_element_stats *stats,
2278 if (!cfg_ni || !tun || !stats)
2281 cpt = lnet_net_lock_current();
2283 ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2288 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
2292 lnet_net_unlock(cpt);
2296 static int lnet_add_net_common(struct lnet_net *net,
2297 struct lnet_ioctl_config_lnd_tunables *tun)
2300 struct lnet_ping_buffer *pbuf;
2301 struct lnet_handle_md ping_mdh;
2303 struct lnet_remotenet *rnet;
2305 int num_acceptor_nets;
2307 lnet_net_lock(LNET_LOCK_EX);
2308 rnet = lnet_find_rnet_locked(net->net_id);
2309 lnet_net_unlock(LNET_LOCK_EX);
2311 * make sure that the net added doesn't invalidate the current
2312 * configuration LNet is keeping
2315 CERROR("Adding net %s will invalidate routing configuration\n",
2316 libcfs_net2str(net->net_id));
2322 * make sure you calculate the correct number of slots in the ping
2323 * buffer. Since the ping info is a flattened list of all the NIs,
2324 * we should allocate enough slots to accomodate the number of NIs
2325 * which will be added.
2327 * since ni hasn't been configured yet, use
2328 * lnet_get_net_ni_count_pre() which checks the net_ni_added list
2330 net_ni_count = lnet_get_net_ni_count_pre(net);
2332 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2333 net_ni_count + lnet_get_ni_count(),
2341 memcpy(&net->net_tunables,
2342 &tun->lt_cmn, sizeof(net->net_tunables));
2344 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
2347 * before starting this network get a count of the current TCP
2348 * networks which require the acceptor thread running. If that
2349 * count is == 0 before we start up this network, then we'd want to
2350 * start up the acceptor thread after starting up this network
2352 num_acceptor_nets = lnet_count_acceptor_nets();
2354 net_id = net->net_id;
2356 rc = lnet_startup_lndnet(net,
2357 (tun) ? &tun->lt_tun : NULL);
2361 lnet_net_lock(LNET_LOCK_EX);
2362 net = lnet_get_net_locked(net_id);
2363 lnet_net_unlock(LNET_LOCK_EX);
2368 * Start the acceptor thread if this is the first network
2369 * being added that requires the thread.
2371 if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
2372 rc = lnet_acceptor_start();
2374 /* shutdown the net that we just started */
2375 CERROR("Failed to start up acceptor thread\n");
2376 lnet_shutdown_lndnet(net);
2381 lnet_net_lock(LNET_LOCK_EX);
2382 lnet_peer_net_added(net);
2383 lnet_net_unlock(LNET_LOCK_EX);
2385 lnet_ping_target_update(pbuf, ping_mdh);
2390 lnet_ping_md_unlink(pbuf, &ping_mdh);
2391 lnet_ping_buffer_decref(pbuf);
2395 static int lnet_handle_legacy_ip2nets(char *ip2nets,
2396 struct lnet_ioctl_config_lnd_tunables *tun)
2398 struct lnet_net *net;
2401 struct list_head net_head;
2403 INIT_LIST_HEAD(&net_head);
2405 rc = lnet_parse_ip2nets(&nets, ip2nets);
2409 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2413 mutex_lock(&the_lnet.ln_api_mutex);
2414 while (!list_empty(&net_head)) {
2415 net = list_entry(net_head.next, struct lnet_net, net_list);
2416 list_del_init(&net->net_list);
2417 rc = lnet_add_net_common(net, tun);
2423 mutex_unlock(&the_lnet.ln_api_mutex);
2425 while (!list_empty(&net_head)) {
2426 net = list_entry(net_head.next, struct lnet_net, net_list);
2427 list_del_init(&net->net_list);
2433 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
2435 struct lnet_net *net;
2437 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2441 /* get the tunables if they are available */
2442 if (conf->lic_cfg_hdr.ioc_len >=
2443 sizeof(*conf) + sizeof(*tun))
2444 tun = (struct lnet_ioctl_config_lnd_tunables *)
2447 /* handle legacy ip2nets from DLC */
2448 if (conf->lic_legacy_ip2nets[0] != '\0')
2449 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
2452 net_id = LNET_NIDNET(conf->lic_nid);
2454 net = lnet_net_alloc(net_id, NULL);
2458 for (i = 0; i < conf->lic_ncpts; i++) {
2459 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
2463 ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
2464 conf->lic_ni_intf[0]);
2468 mutex_lock(&the_lnet.ln_api_mutex);
2470 rc = lnet_add_net_common(net, tun);
2472 mutex_unlock(&the_lnet.ln_api_mutex);
2477 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
2479 struct lnet_net *net;
2481 __u32 net_id = LNET_NIDNET(conf->lic_nid);
2482 struct lnet_ping_buffer *pbuf;
2483 struct lnet_handle_md ping_mdh;
2488 /* don't allow userspace to shutdown the LOLND */
2489 if (LNET_NETTYP(net_id) == LOLND)
2492 mutex_lock(&the_lnet.ln_api_mutex);
2496 net = lnet_get_net_locked(net_id);
2498 CERROR("net %s not found\n",
2499 libcfs_net2str(net_id));
2504 addr = LNET_NIDADDR(conf->lic_nid);
2506 /* remove the entire net */
2507 net_count = lnet_get_net_ni_count_locked(net);
2511 /* create and link a new ping info, before removing the old one */
2512 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2513 lnet_get_ni_count() - net_count,
2516 goto unlock_api_mutex;
2518 lnet_shutdown_lndnet(net);
2520 if (lnet_count_acceptor_nets() == 0)
2521 lnet_acceptor_stop();
2523 lnet_ping_target_update(pbuf, ping_mdh);
2525 goto unlock_api_mutex;
2528 ni = lnet_nid2ni_locked(conf->lic_nid, 0);
2530 CERROR("nid %s not found\n",
2531 libcfs_nid2str(conf->lic_nid));
2536 net_count = lnet_get_net_ni_count_locked(net);
2540 /* create and link a new ping info, before removing the old one */
2541 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2542 lnet_get_ni_count() - 1, false);
2544 goto unlock_api_mutex;
2546 lnet_shutdown_lndni(ni);
2548 if (lnet_count_acceptor_nets() == 0)
2549 lnet_acceptor_stop();
2551 lnet_ping_target_update(pbuf, ping_mdh);
2553 /* check if the net is empty and remove it if it is */
2555 lnet_shutdown_lndnet(net);
2557 goto unlock_api_mutex;
2562 mutex_unlock(&the_lnet.ln_api_mutex);
2568 * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
2569 * They are only expected to be called for unique networks.
2570 * That can be as a result of older DLC library
2571 * calls. Multi-Rail DLC and beyond no longer uses these APIs.
2574 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
2576 struct lnet_net *net;
2577 struct list_head net_head;
2579 struct lnet_ioctl_config_lnd_tunables tun;
2580 char *nets = conf->cfg_config_u.cfg_net.net_intf;
2582 INIT_LIST_HEAD(&net_head);
2584 /* Create a net/ni structures for the network string */
2585 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2587 return rc == 0 ? -EINVAL : rc;
2589 mutex_lock(&the_lnet.ln_api_mutex);
2592 rc = -EINVAL; /* only add one network per call */
2593 goto out_unlock_clean;
2596 net = list_entry(net_head.next, struct lnet_net, net_list);
2597 list_del_init(&net->net_list);
2599 LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
2601 memset(&tun, 0, sizeof(tun));
2603 tun.lt_cmn.lct_peer_timeout =
2604 conf->cfg_config_u.cfg_net.net_peer_timeout;
2605 tun.lt_cmn.lct_peer_tx_credits =
2606 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
2607 tun.lt_cmn.lct_peer_rtr_credits =
2608 conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
2609 tun.lt_cmn.lct_max_tx_credits =
2610 conf->cfg_config_u.cfg_net.net_max_tx_credits;
2612 rc = lnet_add_net_common(net, &tun);
2615 mutex_unlock(&the_lnet.ln_api_mutex);
2616 while (!list_empty(&net_head)) {
2617 /* net_head list is empty in success case */
2618 net = list_entry(net_head.next, struct lnet_net, net_list);
2619 list_del_init(&net->net_list);
2626 lnet_dyn_del_net(__u32 net_id)
2628 struct lnet_net *net;
2629 struct lnet_ping_buffer *pbuf;
2630 struct lnet_handle_md ping_mdh;
2634 /* don't allow userspace to shutdown the LOLND */
2635 if (LNET_NETTYP(net_id) == LOLND)
2638 mutex_lock(&the_lnet.ln_api_mutex);
2642 net = lnet_get_net_locked(net_id);
2649 net_ni_count = lnet_get_net_ni_count_locked(net);
2653 /* create and link a new ping info, before removing the old one */
2654 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2655 lnet_get_ni_count() - net_ni_count, false);
2659 lnet_shutdown_lndnet(net);
2661 if (lnet_count_acceptor_nets() == 0)
2662 lnet_acceptor_stop();
2664 lnet_ping_target_update(pbuf, ping_mdh);
2667 mutex_unlock(&the_lnet.ln_api_mutex);
2672 void lnet_incr_dlc_seq(void)
2674 atomic_inc(&lnet_dlc_seq_no);
2677 __u32 lnet_get_dlc_seq_locked(void)
2679 return atomic_read(&lnet_dlc_seq_no);
2683 * LNet ioctl handler.
2687 LNetCtl(unsigned int cmd, void *arg)
2689 struct libcfs_ioctl_data *data = arg;
2690 struct lnet_ioctl_config_data *config;
2691 struct lnet_process_id id = {0};
2695 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
2696 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
2699 case IOC_LIBCFS_GET_NI:
2700 rc = LNetGetId(data->ioc_count, &id);
2701 data->ioc_nid = id.nid;
2704 case IOC_LIBCFS_FAIL_NID:
2705 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
2707 case IOC_LIBCFS_ADD_ROUTE:
2710 if (config->cfg_hdr.ioc_len < sizeof(*config))
2713 mutex_lock(&the_lnet.ln_api_mutex);
2714 rc = lnet_add_route(config->cfg_net,
2715 config->cfg_config_u.cfg_route.rtr_hop,
2717 config->cfg_config_u.cfg_route.
2720 rc = lnet_check_routes();
2722 lnet_del_route(config->cfg_net,
2725 mutex_unlock(&the_lnet.ln_api_mutex);
2728 case IOC_LIBCFS_DEL_ROUTE:
2731 if (config->cfg_hdr.ioc_len < sizeof(*config))
2734 mutex_lock(&the_lnet.ln_api_mutex);
2735 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
2736 mutex_unlock(&the_lnet.ln_api_mutex);
2739 case IOC_LIBCFS_GET_ROUTE:
2742 if (config->cfg_hdr.ioc_len < sizeof(*config))
2745 mutex_lock(&the_lnet.ln_api_mutex);
2746 rc = lnet_get_route(config->cfg_count,
2748 &config->cfg_config_u.cfg_route.rtr_hop,
2750 &config->cfg_config_u.cfg_route.rtr_flags,
2751 &config->cfg_config_u.cfg_route.
2753 mutex_unlock(&the_lnet.ln_api_mutex);
2756 case IOC_LIBCFS_GET_LOCAL_NI: {
2757 struct lnet_ioctl_config_ni *cfg_ni;
2758 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2759 struct lnet_ioctl_element_stats *stats;
2763 /* get the tunables if they are available */
2764 if (cfg_ni->lic_cfg_hdr.ioc_len <
2765 sizeof(*cfg_ni) + sizeof(*stats)+ sizeof(*tun))
2768 stats = (struct lnet_ioctl_element_stats *)
2770 tun = (struct lnet_ioctl_config_lnd_tunables *)
2771 (cfg_ni->lic_bulk + sizeof(*stats));
2773 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
2776 mutex_lock(&the_lnet.ln_api_mutex);
2777 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
2778 mutex_unlock(&the_lnet.ln_api_mutex);
2782 case IOC_LIBCFS_GET_NET: {
2783 size_t total = sizeof(*config) +
2784 sizeof(struct lnet_ioctl_net_config);
2787 if (config->cfg_hdr.ioc_len < total)
2790 mutex_lock(&the_lnet.ln_api_mutex);
2791 rc = lnet_get_net_config(config);
2792 mutex_unlock(&the_lnet.ln_api_mutex);
2796 case IOC_LIBCFS_GET_LNET_STATS:
2798 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
2800 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
2803 mutex_lock(&the_lnet.ln_api_mutex);
2804 lnet_counters_get(&lnet_stats->st_cntrs);
2805 mutex_unlock(&the_lnet.ln_api_mutex);
2809 case IOC_LIBCFS_CONFIG_RTR:
2812 if (config->cfg_hdr.ioc_len < sizeof(*config))
2815 mutex_lock(&the_lnet.ln_api_mutex);
2816 if (config->cfg_config_u.cfg_buffers.buf_enable) {
2817 rc = lnet_rtrpools_enable();
2818 mutex_unlock(&the_lnet.ln_api_mutex);
2821 lnet_rtrpools_disable();
2822 mutex_unlock(&the_lnet.ln_api_mutex);
2825 case IOC_LIBCFS_ADD_BUF:
2828 if (config->cfg_hdr.ioc_len < sizeof(*config))
2831 mutex_lock(&the_lnet.ln_api_mutex);
2832 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
2834 config->cfg_config_u.cfg_buffers.
2836 config->cfg_config_u.cfg_buffers.
2838 mutex_unlock(&the_lnet.ln_api_mutex);
2841 case IOC_LIBCFS_SET_NUMA_RANGE: {
2842 struct lnet_ioctl_set_value *numa;
2844 if (numa->sv_hdr.ioc_len != sizeof(*numa))
2846 lnet_net_lock(LNET_LOCK_EX);
2847 lnet_numa_range = numa->sv_value;
2848 lnet_net_unlock(LNET_LOCK_EX);
2852 case IOC_LIBCFS_GET_NUMA_RANGE: {
2853 struct lnet_ioctl_set_value *numa;
2855 if (numa->sv_hdr.ioc_len != sizeof(*numa))
2857 numa->sv_value = lnet_numa_range;
2861 case IOC_LIBCFS_GET_BUF: {
2862 struct lnet_ioctl_pool_cfg *pool_cfg;
2863 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2867 if (config->cfg_hdr.ioc_len < total)
2870 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2872 mutex_lock(&the_lnet.ln_api_mutex);
2873 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2874 mutex_unlock(&the_lnet.ln_api_mutex);
2878 case IOC_LIBCFS_ADD_PEER_NI: {
2879 struct lnet_ioctl_peer_cfg *cfg = arg;
2881 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
2884 mutex_lock(&the_lnet.ln_api_mutex);
2885 rc = lnet_add_peer_ni_to_peer(cfg->prcfg_prim_nid,
2888 mutex_unlock(&the_lnet.ln_api_mutex);
2892 case IOC_LIBCFS_DEL_PEER_NI: {
2893 struct lnet_ioctl_peer_cfg *cfg = arg;
2895 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
2898 mutex_lock(&the_lnet.ln_api_mutex);
2899 rc = lnet_del_peer_ni_from_peer(cfg->prcfg_prim_nid,
2900 cfg->prcfg_cfg_nid);
2901 mutex_unlock(&the_lnet.ln_api_mutex);
2905 case IOC_LIBCFS_GET_PEER_INFO: {
2906 struct lnet_ioctl_peer *peer_info = arg;
2908 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2911 mutex_lock(&the_lnet.ln_api_mutex);
2912 rc = lnet_get_peer_ni_info(
2913 peer_info->pr_count,
2915 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2916 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2917 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2918 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2919 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2920 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2921 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
2922 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2923 mutex_unlock(&the_lnet.ln_api_mutex);
2927 case IOC_LIBCFS_GET_PEER_NI: {
2928 struct lnet_ioctl_peer_cfg *cfg = arg;
2929 struct lnet_peer_ni_credit_info __user *lpni_cri;
2930 struct lnet_ioctl_element_stats __user *lpni_stats;
2931 size_t usr_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2933 if ((cfg->prcfg_hdr.ioc_len != sizeof(*cfg)) ||
2934 (cfg->prcfg_size != usr_size))
2937 lpni_cri = cfg->prcfg_bulk;
2938 lpni_stats = cfg->prcfg_bulk + sizeof(*lpni_cri);
2940 mutex_lock(&the_lnet.ln_api_mutex);
2941 rc = lnet_get_peer_info(cfg->prcfg_count, &cfg->prcfg_prim_nid,
2942 &cfg->prcfg_cfg_nid, &cfg->prcfg_mr,
2943 lpni_cri, lpni_stats);
2944 mutex_unlock(&the_lnet.ln_api_mutex);
2948 case IOC_LIBCFS_NOTIFY_ROUTER: {
2949 unsigned long jiffies_passed;
2951 jiffies_passed = ktime_get_real_seconds() - data->ioc_u64[0];
2952 jiffies_passed = cfs_time_seconds(jiffies_passed);
2954 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2955 jiffies - jiffies_passed);
2958 case IOC_LIBCFS_LNET_DIST:
2959 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2960 if (rc < 0 && rc != -EHOSTUNREACH)
2963 data->ioc_u32[0] = rc;
2966 case IOC_LIBCFS_TESTPROTOCOMPAT:
2967 lnet_net_lock(LNET_LOCK_EX);
2968 the_lnet.ln_testprotocompat = data->ioc_flags;
2969 lnet_net_unlock(LNET_LOCK_EX);
2972 case IOC_LIBCFS_LNET_FAULT:
2973 return lnet_fault_ctl(data->ioc_flags, data);
2975 case IOC_LIBCFS_PING: {
2976 signed long timeout;
2978 id.nid = data->ioc_nid;
2979 id.pid = data->ioc_u32[0];
2981 /* Don't block longer than 2 minutes */
2982 if (data->ioc_u32[1] > 120 * MSEC_PER_SEC)
2985 /* If timestamp is negative then disable timeout */
2986 if ((s32)data->ioc_u32[1] < 0)
2987 timeout = MAX_SCHEDULE_TIMEOUT;
2989 timeout = msecs_to_jiffies(data->ioc_u32[1]);
2991 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
2992 data->ioc_plen1 / sizeof(struct lnet_process_id));
2995 data->ioc_count = rc;
3000 ni = lnet_net2ni_addref(data->ioc_net);
3004 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3007 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
3014 EXPORT_SYMBOL(LNetCtl);
3016 void LNetDebugPeer(struct lnet_process_id id)
3018 lnet_debug_peer(id.nid);
3020 EXPORT_SYMBOL(LNetDebugPeer);
3023 * Determine if the specified peer \a nid is on the local node.
3025 * \param nid peer nid to check
3027 * \retval true If peer NID is on the local node.
3028 * \retval false If peer NID is not on the local node.
3030 bool LNetIsPeerLocal(lnet_nid_t nid)
3032 struct lnet_net *net;
3036 cpt = lnet_net_lock_current();
3037 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3038 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3039 if (ni->ni_nid == nid) {
3040 lnet_net_unlock(cpt);
3045 lnet_net_unlock(cpt);
3049 EXPORT_SYMBOL(LNetIsPeerLocal);
3052 * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
3053 * Note that all interfaces share a same PID, as requested by LNetNIInit().
3055 * \param index Index of the interface to look up.
3056 * \param id On successful return, this location will hold the
3057 * struct lnet_process_id ID of the interface.
3059 * \retval 0 If an interface exists at \a index.
3060 * \retval -ENOENT If no interface has been found.
3063 LNetGetId(unsigned int index, struct lnet_process_id *id)
3066 struct lnet_net *net;
3070 LASSERT(the_lnet.ln_refcount > 0);
3072 cpt = lnet_net_lock_current();
3074 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3075 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3079 id->nid = ni->ni_nid;
3080 id->pid = the_lnet.ln_pid;
3086 lnet_net_unlock(cpt);
3089 EXPORT_SYMBOL(LNetGetId);
3091 static int lnet_ping(struct lnet_process_id id, signed long timeout,
3092 struct lnet_process_id __user *ids, int n_ids)
3094 struct lnet_handle_eq eqh;
3095 struct lnet_handle_md mdh;
3096 struct lnet_event event;
3097 struct lnet_md md = { NULL };
3101 const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
3102 struct lnet_ping_buffer *pbuf;
3103 struct lnet_process_id tmpid;
3110 /* n_ids limit is arbitrary */
3111 if (n_ids <= 0 || n_ids > lnet_interfaces_max || id.nid == LNET_NID_ANY)
3114 if (id.pid == LNET_PID_ANY)
3115 id.pid = LNET_PID_LUSTRE;
3117 pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
3121 /* NB 2 events max (including any unlink event) */
3122 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
3124 CERROR("Can't allocate EQ: %d\n", rc);
3125 goto fail_ping_buffer_decref;
3128 /* initialize md content */
3129 md.start = &pbuf->pb_info;
3130 md.length = LNET_PING_INFO_SIZE(n_ids);
3131 md.threshold = 2; /*GET/REPLY*/
3133 md.options = LNET_MD_TRUNCATE;
3137 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
3139 CERROR("Can't bind MD: %d\n", rc);
3143 rc = LNetGet(LNET_NID_ANY, mdh, id,
3144 LNET_RESERVED_PORTAL,
3145 LNET_PROTO_PING_MATCHBITS, 0);
3148 /* Don't CERROR; this could be deliberate! */
3150 rc2 = LNetMDUnlink(mdh);
3153 /* NB must wait for the UNLINK event below... */
3155 timeout = a_long_time;
3159 /* MUST block for unlink to complete */
3161 blocked = cfs_block_allsigs();
3163 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
3166 cfs_restore_sigs(blocked);
3168 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
3169 (rc2 <= 0) ? -1 : event.type,
3170 (rc2 <= 0) ? -1 : event.status,
3171 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
3173 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
3175 if (rc2 <= 0 || event.status != 0) {
3176 /* timeout or error */
3177 if (!replied && rc == 0)
3178 rc = (rc2 < 0) ? rc2 :
3179 (rc2 == 0) ? -ETIMEDOUT :
3183 /* Ensure completion in finite time... */
3185 /* No assertion (racing with network) */
3187 timeout = a_long_time;
3188 } else if (rc2 == 0) {
3189 /* timed out waiting for unlink */
3190 CWARN("ping %s: late network completion\n",
3193 } else if (event.type == LNET_EVENT_REPLY) {
3198 } while (rc2 <= 0 || !event.unlinked);
3202 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
3209 LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
3211 rc = -EPROTO; /* if I can't parse... */
3214 /* can't check magic/version */
3215 CERROR("%s: ping info too short %d\n",
3216 libcfs_id2str(id), nob);
3220 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
3221 lnet_swap_pinginfo(pbuf);
3222 } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
3223 CERROR("%s: Unexpected magic %08x\n",
3224 libcfs_id2str(id), pbuf->pb_info.pi_magic);
3228 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
3229 CERROR("%s: ping w/o NI status: 0x%x\n",
3230 libcfs_id2str(id), pbuf->pb_info.pi_features);
3234 if (nob < LNET_PING_INFO_SIZE(0)) {
3235 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
3236 nob, (int)LNET_PING_INFO_SIZE(0));
3240 if (pbuf->pb_info.pi_nnis < n_ids)
3241 n_ids = pbuf->pb_info.pi_nnis;
3243 if (nob < LNET_PING_INFO_SIZE(n_ids)) {
3244 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
3245 nob, (int)LNET_PING_INFO_SIZE(n_ids));
3249 rc = -EFAULT; /* If I SEGV... */
3251 memset(&tmpid, 0, sizeof(tmpid));
3252 for (i = 0; i < n_ids; i++) {
3253 tmpid.pid = pbuf->pb_info.pi_pid;
3254 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
3255 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
3258 rc = pbuf->pb_info.pi_nnis;
3261 rc2 = LNetEQFree(eqh);
3263 CERROR("rc2 %d\n", rc2);
3266 fail_ping_buffer_decref:
3267 lnet_ping_buffer_decref(pbuf);