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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/log2.h>
39 #include <linux/ktime.h>
41 #include "../../include/linux/lnet/lib-lnet.h"
42 #include "../../include/linux/lnet/lib-dlc.h"
44 #define D_LNI D_CONSOLE
46 lnet_t the_lnet
; /* THE state of the network */
47 EXPORT_SYMBOL(the_lnet
);
49 static char *ip2nets
= "";
50 module_param(ip2nets
, charp
, 0444);
51 MODULE_PARM_DESC(ip2nets
, "LNET network <- IP table");
53 static char *networks
= "";
54 module_param(networks
, charp
, 0444);
55 MODULE_PARM_DESC(networks
, "local networks");
57 static char *routes
= "";
58 module_param(routes
, charp
, 0444);
59 MODULE_PARM_DESC(routes
, "routes to non-local networks");
61 static int rnet_htable_size
= LNET_REMOTE_NETS_HASH_DEFAULT
;
62 module_param(rnet_htable_size
, int, 0444);
63 MODULE_PARM_DESC(rnet_htable_size
, "size of remote network hash table");
65 static int lnet_ping(lnet_process_id_t id
, int timeout_ms
,
66 lnet_process_id_t __user
*ids
, int n_ids
);
75 lnet_get_networks(void)
80 if (*networks
&& *ip2nets
) {
81 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or 'ip2nets' but not both at once\n");
86 rc
= lnet_parse_ip2nets(&nets
, ip2nets
);
87 return !rc
? nets
: NULL
;
99 spin_lock_init(&the_lnet
.ln_eq_wait_lock
);
100 init_waitqueue_head(&the_lnet
.ln_eq_waitq
);
101 mutex_init(&the_lnet
.ln_lnd_mutex
);
102 mutex_init(&the_lnet
.ln_api_mutex
);
106 lnet_create_remote_nets_table(void)
109 struct list_head
*hash
;
111 LASSERT(!the_lnet
.ln_remote_nets_hash
);
112 LASSERT(the_lnet
.ln_remote_nets_hbits
> 0);
113 LIBCFS_ALLOC(hash
, LNET_REMOTE_NETS_HASH_SIZE
* sizeof(*hash
));
115 CERROR("Failed to create remote nets hash table\n");
119 for (i
= 0; i
< LNET_REMOTE_NETS_HASH_SIZE
; i
++)
120 INIT_LIST_HEAD(&hash
[i
]);
121 the_lnet
.ln_remote_nets_hash
= hash
;
126 lnet_destroy_remote_nets_table(void)
130 if (!the_lnet
.ln_remote_nets_hash
)
133 for (i
= 0; i
< LNET_REMOTE_NETS_HASH_SIZE
; i
++)
134 LASSERT(list_empty(&the_lnet
.ln_remote_nets_hash
[i
]));
136 LIBCFS_FREE(the_lnet
.ln_remote_nets_hash
,
137 LNET_REMOTE_NETS_HASH_SIZE
*
138 sizeof(the_lnet
.ln_remote_nets_hash
[0]));
139 the_lnet
.ln_remote_nets_hash
= NULL
;
143 lnet_destroy_locks(void)
145 if (the_lnet
.ln_res_lock
) {
146 cfs_percpt_lock_free(the_lnet
.ln_res_lock
);
147 the_lnet
.ln_res_lock
= NULL
;
150 if (the_lnet
.ln_net_lock
) {
151 cfs_percpt_lock_free(the_lnet
.ln_net_lock
);
152 the_lnet
.ln_net_lock
= NULL
;
157 lnet_create_locks(void)
161 the_lnet
.ln_res_lock
= cfs_percpt_lock_alloc(lnet_cpt_table());
162 if (!the_lnet
.ln_res_lock
)
165 the_lnet
.ln_net_lock
= cfs_percpt_lock_alloc(lnet_cpt_table());
166 if (!the_lnet
.ln_net_lock
)
172 lnet_destroy_locks();
176 static void lnet_assert_wire_constants(void)
179 * Wire protocol assertions generated by 'wirecheck'
180 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
181 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
182 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
186 CLASSERT(LNET_PROTO_TCP_MAGIC
== 0xeebc0ded);
187 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR
== 1);
188 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR
== 0);
189 CLASSERT(LNET_MSG_ACK
== 0);
190 CLASSERT(LNET_MSG_PUT
== 1);
191 CLASSERT(LNET_MSG_GET
== 2);
192 CLASSERT(LNET_MSG_REPLY
== 3);
193 CLASSERT(LNET_MSG_HELLO
== 4);
195 /* Checks for struct ptl_handle_wire_t */
196 CLASSERT((int)sizeof(lnet_handle_wire_t
) == 16);
197 CLASSERT((int)offsetof(lnet_handle_wire_t
, wh_interface_cookie
) == 0);
198 CLASSERT((int)sizeof(((lnet_handle_wire_t
*)0)->wh_interface_cookie
) == 8);
199 CLASSERT((int)offsetof(lnet_handle_wire_t
, wh_object_cookie
) == 8);
200 CLASSERT((int)sizeof(((lnet_handle_wire_t
*)0)->wh_object_cookie
) == 8);
202 /* Checks for struct lnet_magicversion_t */
203 CLASSERT((int)sizeof(lnet_magicversion_t
) == 8);
204 CLASSERT((int)offsetof(lnet_magicversion_t
, magic
) == 0);
205 CLASSERT((int)sizeof(((lnet_magicversion_t
*)0)->magic
) == 4);
206 CLASSERT((int)offsetof(lnet_magicversion_t
, version_major
) == 4);
207 CLASSERT((int)sizeof(((lnet_magicversion_t
*)0)->version_major
) == 2);
208 CLASSERT((int)offsetof(lnet_magicversion_t
, version_minor
) == 6);
209 CLASSERT((int)sizeof(((lnet_magicversion_t
*)0)->version_minor
) == 2);
211 /* Checks for struct lnet_hdr_t */
212 CLASSERT((int)sizeof(lnet_hdr_t
) == 72);
213 CLASSERT((int)offsetof(lnet_hdr_t
, dest_nid
) == 0);
214 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->dest_nid
) == 8);
215 CLASSERT((int)offsetof(lnet_hdr_t
, src_nid
) == 8);
216 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->src_nid
) == 8);
217 CLASSERT((int)offsetof(lnet_hdr_t
, dest_pid
) == 16);
218 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->dest_pid
) == 4);
219 CLASSERT((int)offsetof(lnet_hdr_t
, src_pid
) == 20);
220 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->src_pid
) == 4);
221 CLASSERT((int)offsetof(lnet_hdr_t
, type
) == 24);
222 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->type
) == 4);
223 CLASSERT((int)offsetof(lnet_hdr_t
, payload_length
) == 28);
224 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->payload_length
) == 4);
225 CLASSERT((int)offsetof(lnet_hdr_t
, msg
) == 32);
226 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
) == 40);
229 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.ack
.dst_wmd
) == 32);
230 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.ack
.dst_wmd
) == 16);
231 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.ack
.match_bits
) == 48);
232 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.ack
.match_bits
) == 8);
233 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.ack
.mlength
) == 56);
234 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.ack
.mlength
) == 4);
237 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.put
.ack_wmd
) == 32);
238 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.put
.ack_wmd
) == 16);
239 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.put
.match_bits
) == 48);
240 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.put
.match_bits
) == 8);
241 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.put
.hdr_data
) == 56);
242 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.put
.hdr_data
) == 8);
243 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.put
.ptl_index
) == 64);
244 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.put
.ptl_index
) == 4);
245 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.put
.offset
) == 68);
246 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.put
.offset
) == 4);
249 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.get
.return_wmd
) == 32);
250 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.get
.return_wmd
) == 16);
251 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.get
.match_bits
) == 48);
252 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.get
.match_bits
) == 8);
253 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.get
.ptl_index
) == 56);
254 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.get
.ptl_index
) == 4);
255 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.get
.src_offset
) == 60);
256 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.get
.src_offset
) == 4);
257 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.get
.sink_length
) == 64);
258 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.get
.sink_length
) == 4);
261 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.reply
.dst_wmd
) == 32);
262 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.reply
.dst_wmd
) == 16);
265 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.hello
.incarnation
) == 32);
266 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.hello
.incarnation
) == 8);
267 CLASSERT((int)offsetof(lnet_hdr_t
, msg
.hello
.type
) == 40);
268 CLASSERT((int)sizeof(((lnet_hdr_t
*)0)->msg
.hello
.type
) == 4);
272 lnet_find_lnd_by_type(__u32 type
)
275 struct list_head
*tmp
;
277 /* holding lnd mutex */
278 list_for_each(tmp
, &the_lnet
.ln_lnds
) {
279 lnd
= list_entry(tmp
, lnd_t
, lnd_list
);
281 if (lnd
->lnd_type
== type
)
289 lnet_register_lnd(lnd_t
*lnd
)
291 mutex_lock(&the_lnet
.ln_lnd_mutex
);
293 LASSERT(the_lnet
.ln_init
);
294 LASSERT(libcfs_isknown_lnd(lnd
->lnd_type
));
295 LASSERT(!lnet_find_lnd_by_type(lnd
->lnd_type
));
297 list_add_tail(&lnd
->lnd_list
, &the_lnet
.ln_lnds
);
298 lnd
->lnd_refcount
= 0;
300 CDEBUG(D_NET
, "%s LND registered\n", libcfs_lnd2str(lnd
->lnd_type
));
302 mutex_unlock(&the_lnet
.ln_lnd_mutex
);
304 EXPORT_SYMBOL(lnet_register_lnd
);
307 lnet_unregister_lnd(lnd_t
*lnd
)
309 mutex_lock(&the_lnet
.ln_lnd_mutex
);
311 LASSERT(the_lnet
.ln_init
);
312 LASSERT(lnet_find_lnd_by_type(lnd
->lnd_type
) == lnd
);
313 LASSERT(!lnd
->lnd_refcount
);
315 list_del(&lnd
->lnd_list
);
316 CDEBUG(D_NET
, "%s LND unregistered\n", libcfs_lnd2str(lnd
->lnd_type
));
318 mutex_unlock(&the_lnet
.ln_lnd_mutex
);
320 EXPORT_SYMBOL(lnet_unregister_lnd
);
323 lnet_counters_get(lnet_counters_t
*counters
)
325 lnet_counters_t
*ctr
;
328 memset(counters
, 0, sizeof(*counters
));
330 lnet_net_lock(LNET_LOCK_EX
);
332 cfs_percpt_for_each(ctr
, i
, the_lnet
.ln_counters
) {
333 counters
->msgs_max
+= ctr
->msgs_max
;
334 counters
->msgs_alloc
+= ctr
->msgs_alloc
;
335 counters
->errors
+= ctr
->errors
;
336 counters
->send_count
+= ctr
->send_count
;
337 counters
->recv_count
+= ctr
->recv_count
;
338 counters
->route_count
+= ctr
->route_count
;
339 counters
->drop_count
+= ctr
->drop_count
;
340 counters
->send_length
+= ctr
->send_length
;
341 counters
->recv_length
+= ctr
->recv_length
;
342 counters
->route_length
+= ctr
->route_length
;
343 counters
->drop_length
+= ctr
->drop_length
;
345 lnet_net_unlock(LNET_LOCK_EX
);
347 EXPORT_SYMBOL(lnet_counters_get
);
350 lnet_counters_reset(void)
352 lnet_counters_t
*counters
;
355 lnet_net_lock(LNET_LOCK_EX
);
357 cfs_percpt_for_each(counters
, i
, the_lnet
.ln_counters
)
358 memset(counters
, 0, sizeof(lnet_counters_t
));
360 lnet_net_unlock(LNET_LOCK_EX
);
364 lnet_res_type2str(int type
)
369 case LNET_COOKIE_TYPE_MD
:
371 case LNET_COOKIE_TYPE_ME
:
373 case LNET_COOKIE_TYPE_EQ
:
379 lnet_res_container_cleanup(struct lnet_res_container
*rec
)
383 if (!rec
->rec_type
) /* not set yet, it's uninitialized */
386 while (!list_empty(&rec
->rec_active
)) {
387 struct list_head
*e
= rec
->rec_active
.next
;
390 if (rec
->rec_type
== LNET_COOKIE_TYPE_EQ
) {
391 lnet_eq_free(list_entry(e
, lnet_eq_t
, eq_list
));
393 } else if (rec
->rec_type
== LNET_COOKIE_TYPE_MD
) {
394 lnet_md_free(list_entry(e
, lnet_libmd_t
, md_list
));
396 } else { /* NB: Active MEs should be attached on portals */
404 * Found alive MD/ME/EQ, user really should unlink/free
405 * all of them before finalize LNet, but if someone didn't,
406 * we have to recycle garbage for him
408 CERROR("%d active elements on exit of %s container\n",
409 count
, lnet_res_type2str(rec
->rec_type
));
412 if (rec
->rec_lh_hash
) {
413 LIBCFS_FREE(rec
->rec_lh_hash
,
414 LNET_LH_HASH_SIZE
* sizeof(rec
->rec_lh_hash
[0]));
415 rec
->rec_lh_hash
= NULL
;
418 rec
->rec_type
= 0; /* mark it as finalized */
422 lnet_res_container_setup(struct lnet_res_container
*rec
, int cpt
, int type
)
427 LASSERT(!rec
->rec_type
);
429 rec
->rec_type
= type
;
430 INIT_LIST_HEAD(&rec
->rec_active
);
431 rec
->rec_lh_cookie
= (cpt
<< LNET_COOKIE_TYPE_BITS
) | type
;
433 /* Arbitrary choice of hash table size */
434 LIBCFS_CPT_ALLOC(rec
->rec_lh_hash
, lnet_cpt_table(), cpt
,
435 LNET_LH_HASH_SIZE
* sizeof(rec
->rec_lh_hash
[0]));
436 if (!rec
->rec_lh_hash
) {
441 for (i
= 0; i
< LNET_LH_HASH_SIZE
; i
++)
442 INIT_LIST_HEAD(&rec
->rec_lh_hash
[i
]);
447 CERROR("Failed to setup %s resource container\n",
448 lnet_res_type2str(type
));
449 lnet_res_container_cleanup(rec
);
454 lnet_res_containers_destroy(struct lnet_res_container
**recs
)
456 struct lnet_res_container
*rec
;
459 cfs_percpt_for_each(rec
, i
, recs
)
460 lnet_res_container_cleanup(rec
);
462 cfs_percpt_free(recs
);
465 static struct lnet_res_container
**
466 lnet_res_containers_create(int type
)
468 struct lnet_res_container
**recs
;
469 struct lnet_res_container
*rec
;
473 recs
= cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec
));
475 CERROR("Failed to allocate %s resource containers\n",
476 lnet_res_type2str(type
));
480 cfs_percpt_for_each(rec
, i
, recs
) {
481 rc
= lnet_res_container_setup(rec
, i
, type
);
483 lnet_res_containers_destroy(recs
);
492 lnet_res_lh_lookup(struct lnet_res_container
*rec
, __u64 cookie
)
494 /* ALWAYS called with lnet_res_lock held */
495 struct list_head
*head
;
496 lnet_libhandle_t
*lh
;
499 if ((cookie
& LNET_COOKIE_MASK
) != rec
->rec_type
)
502 hash
= cookie
>> (LNET_COOKIE_TYPE_BITS
+ LNET_CPT_BITS
);
503 head
= &rec
->rec_lh_hash
[hash
& LNET_LH_HASH_MASK
];
505 list_for_each_entry(lh
, head
, lh_hash_chain
) {
506 if (lh
->lh_cookie
== cookie
)
514 lnet_res_lh_initialize(struct lnet_res_container
*rec
, lnet_libhandle_t
*lh
)
516 /* ALWAYS called with lnet_res_lock held */
517 unsigned int ibits
= LNET_COOKIE_TYPE_BITS
+ LNET_CPT_BITS
;
520 lh
->lh_cookie
= rec
->rec_lh_cookie
;
521 rec
->rec_lh_cookie
+= 1 << ibits
;
523 hash
= (lh
->lh_cookie
>> ibits
) & LNET_LH_HASH_MASK
;
525 list_add(&lh
->lh_hash_chain
, &rec
->rec_lh_hash
[hash
]);
528 static int lnet_unprepare(void);
531 lnet_prepare(lnet_pid_t requested_pid
)
533 /* Prepare to bring up the network */
534 struct lnet_res_container
**recs
;
537 if (requested_pid
== LNET_PID_ANY
) {
538 /* Don't instantiate LNET just for me */
542 LASSERT(!the_lnet
.ln_refcount
);
544 the_lnet
.ln_routing
= 0;
546 LASSERT(!(requested_pid
& LNET_PID_USERFLAG
));
547 the_lnet
.ln_pid
= requested_pid
;
549 INIT_LIST_HEAD(&the_lnet
.ln_test_peers
);
550 INIT_LIST_HEAD(&the_lnet
.ln_nis
);
551 INIT_LIST_HEAD(&the_lnet
.ln_nis_cpt
);
552 INIT_LIST_HEAD(&the_lnet
.ln_nis_zombie
);
553 INIT_LIST_HEAD(&the_lnet
.ln_routers
);
555 rc
= lnet_create_remote_nets_table();
559 * NB the interface cookie in wire handles guards against delayed
560 * replies and ACKs appearing valid after reboot.
562 the_lnet
.ln_interface_cookie
= ktime_get_ns();
564 the_lnet
.ln_counters
= cfs_percpt_alloc(lnet_cpt_table(),
565 sizeof(lnet_counters_t
));
566 if (!the_lnet
.ln_counters
) {
567 CERROR("Failed to allocate counters for LNet\n");
572 rc
= lnet_peer_tables_create();
576 rc
= lnet_msg_containers_create();
580 rc
= lnet_res_container_setup(&the_lnet
.ln_eq_container
, 0,
581 LNET_COOKIE_TYPE_EQ
);
585 recs
= lnet_res_containers_create(LNET_COOKIE_TYPE_ME
);
591 the_lnet
.ln_me_containers
= recs
;
593 recs
= lnet_res_containers_create(LNET_COOKIE_TYPE_MD
);
599 the_lnet
.ln_md_containers
= recs
;
601 rc
= lnet_portals_create();
603 CERROR("Failed to create portals for LNet: %d\n", rc
);
618 * NB no LNET_LOCK since this is the last reference. All LND instances
619 * have shut down already, so it is safe to unlink and free all
620 * descriptors, even those that appear committed to a network op (eg MD
621 * with non-zero pending count)
623 lnet_fail_nid(LNET_NID_ANY
, 0);
625 LASSERT(!the_lnet
.ln_refcount
);
626 LASSERT(list_empty(&the_lnet
.ln_test_peers
));
627 LASSERT(list_empty(&the_lnet
.ln_nis
));
628 LASSERT(list_empty(&the_lnet
.ln_nis_cpt
));
629 LASSERT(list_empty(&the_lnet
.ln_nis_zombie
));
631 lnet_portals_destroy();
633 if (the_lnet
.ln_md_containers
) {
634 lnet_res_containers_destroy(the_lnet
.ln_md_containers
);
635 the_lnet
.ln_md_containers
= NULL
;
638 if (the_lnet
.ln_me_containers
) {
639 lnet_res_containers_destroy(the_lnet
.ln_me_containers
);
640 the_lnet
.ln_me_containers
= NULL
;
643 lnet_res_container_cleanup(&the_lnet
.ln_eq_container
);
645 lnet_msg_containers_destroy();
646 lnet_peer_tables_destroy();
647 lnet_rtrpools_free(0);
649 if (the_lnet
.ln_counters
) {
650 cfs_percpt_free(the_lnet
.ln_counters
);
651 the_lnet
.ln_counters
= NULL
;
653 lnet_destroy_remote_nets_table();
659 lnet_net2ni_locked(__u32 net
, int cpt
)
661 struct list_head
*tmp
;
664 LASSERT(cpt
!= LNET_LOCK_EX
);
666 list_for_each(tmp
, &the_lnet
.ln_nis
) {
667 ni
= list_entry(tmp
, lnet_ni_t
, ni_list
);
669 if (LNET_NIDNET(ni
->ni_nid
) == net
) {
670 lnet_ni_addref_locked(ni
, cpt
);
679 lnet_net2ni(__u32 net
)
684 ni
= lnet_net2ni_locked(net
, 0);
689 EXPORT_SYMBOL(lnet_net2ni
);
692 lnet_nid_cpt_hash(lnet_nid_t nid
, unsigned int number
)
697 LASSERT(number
>= 1 && number
<= LNET_CPT_NUMBER
);
702 val
= hash_long(key
, LNET_CPT_BITS
);
703 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
707 return (unsigned int)(key
+ val
+ (val
>> 1)) % number
;
711 lnet_cpt_of_nid_locked(lnet_nid_t nid
)
715 /* must called with hold of lnet_net_lock */
716 if (LNET_CPT_NUMBER
== 1)
717 return 0; /* the only one */
719 /* take lnet_net_lock(any) would be OK */
720 if (!list_empty(&the_lnet
.ln_nis_cpt
)) {
721 list_for_each_entry(ni
, &the_lnet
.ln_nis_cpt
, ni_cptlist
) {
722 if (LNET_NIDNET(ni
->ni_nid
) != LNET_NIDNET(nid
))
725 LASSERT(ni
->ni_cpts
);
726 return ni
->ni_cpts
[lnet_nid_cpt_hash
727 (nid
, ni
->ni_ncpts
)];
731 return lnet_nid_cpt_hash(nid
, LNET_CPT_NUMBER
);
735 lnet_cpt_of_nid(lnet_nid_t nid
)
740 if (LNET_CPT_NUMBER
== 1)
741 return 0; /* the only one */
743 if (list_empty(&the_lnet
.ln_nis_cpt
))
744 return lnet_nid_cpt_hash(nid
, LNET_CPT_NUMBER
);
746 cpt
= lnet_net_lock_current();
747 cpt2
= lnet_cpt_of_nid_locked(nid
);
748 lnet_net_unlock(cpt
);
752 EXPORT_SYMBOL(lnet_cpt_of_nid
);
755 lnet_islocalnet(__u32 net
)
760 cpt
= lnet_net_lock_current();
762 ni
= lnet_net2ni_locked(net
, cpt
);
764 lnet_ni_decref_locked(ni
, cpt
);
766 lnet_net_unlock(cpt
);
772 lnet_nid2ni_locked(lnet_nid_t nid
, int cpt
)
775 struct list_head
*tmp
;
777 LASSERT(cpt
!= LNET_LOCK_EX
);
779 list_for_each(tmp
, &the_lnet
.ln_nis
) {
780 ni
= list_entry(tmp
, lnet_ni_t
, ni_list
);
782 if (ni
->ni_nid
== nid
) {
783 lnet_ni_addref_locked(ni
, cpt
);
792 lnet_islocalnid(lnet_nid_t nid
)
797 cpt
= lnet_net_lock_current();
798 ni
= lnet_nid2ni_locked(nid
, cpt
);
800 lnet_ni_decref_locked(ni
, cpt
);
801 lnet_net_unlock(cpt
);
807 lnet_count_acceptor_nis(void)
809 /* Return the # of NIs that need the acceptor. */
811 struct list_head
*tmp
;
815 cpt
= lnet_net_lock_current();
816 list_for_each(tmp
, &the_lnet
.ln_nis
) {
817 ni
= list_entry(tmp
, lnet_ni_t
, ni_list
);
819 if (ni
->ni_lnd
->lnd_accept
)
823 lnet_net_unlock(cpt
);
828 static lnet_ping_info_t
*
829 lnet_ping_info_create(int num_ni
)
831 lnet_ping_info_t
*ping_info
;
834 infosz
= offsetof(lnet_ping_info_t
, pi_ni
[num_ni
]);
835 LIBCFS_ALLOC(ping_info
, infosz
);
837 CERROR("Can't allocate ping info[%d]\n", num_ni
);
841 ping_info
->pi_nnis
= num_ni
;
842 ping_info
->pi_pid
= the_lnet
.ln_pid
;
843 ping_info
->pi_magic
= LNET_PROTO_PING_MAGIC
;
844 ping_info
->pi_features
= LNET_PING_FEAT_NI_STATUS
;
850 lnet_get_ni_count(void)
857 list_for_each_entry(ni
, &the_lnet
.ln_nis
, ni_list
)
866 lnet_ping_info_free(lnet_ping_info_t
*pinfo
)
869 offsetof(lnet_ping_info_t
,
870 pi_ni
[pinfo
->pi_nnis
]));
874 lnet_ping_info_destroy(void)
878 lnet_net_lock(LNET_LOCK_EX
);
880 list_for_each_entry(ni
, &the_lnet
.ln_nis
, ni_list
) {
882 ni
->ni_status
= NULL
;
886 lnet_ping_info_free(the_lnet
.ln_ping_info
);
887 the_lnet
.ln_ping_info
= NULL
;
889 lnet_net_unlock(LNET_LOCK_EX
);
893 lnet_ping_event_handler(lnet_event_t
*event
)
895 lnet_ping_info_t
*pinfo
= event
->md
.user_ptr
;
898 pinfo
->pi_features
= LNET_PING_FEAT_INVAL
;
902 lnet_ping_info_setup(lnet_ping_info_t
**ppinfo
, lnet_handle_md_t
*md_handle
,
903 int ni_count
, bool set_eq
)
905 lnet_process_id_t id
= {LNET_NID_ANY
, LNET_PID_ANY
};
906 lnet_handle_me_t me_handle
;
907 lnet_md_t md
= { NULL
};
911 rc
= LNetEQAlloc(0, lnet_ping_event_handler
,
912 &the_lnet
.ln_ping_target_eq
);
914 CERROR("Can't allocate ping EQ: %d\n", rc
);
919 *ppinfo
= lnet_ping_info_create(ni_count
);
925 rc
= LNetMEAttach(LNET_RESERVED_PORTAL
, id
,
926 LNET_PROTO_PING_MATCHBITS
, 0,
927 LNET_UNLINK
, LNET_INS_AFTER
,
930 CERROR("Can't create ping ME: %d\n", rc
);
934 /* initialize md content */
936 md
.length
= offsetof(lnet_ping_info_t
,
937 pi_ni
[(*ppinfo
)->pi_nnis
]);
938 md
.threshold
= LNET_MD_THRESH_INF
;
940 md
.options
= LNET_MD_OP_GET
| LNET_MD_TRUNCATE
|
941 LNET_MD_MANAGE_REMOTE
;
943 md
.eq_handle
= the_lnet
.ln_ping_target_eq
;
944 md
.user_ptr
= *ppinfo
;
946 rc
= LNetMDAttach(me_handle
, md
, LNET_RETAIN
, md_handle
);
948 CERROR("Can't attach ping MD: %d\n", rc
);
955 rc2
= LNetMEUnlink(me_handle
);
958 lnet_ping_info_free(*ppinfo
);
962 LNetEQFree(the_lnet
.ln_ping_target_eq
);
967 lnet_ping_md_unlink(lnet_ping_info_t
*pinfo
, lnet_handle_md_t
*md_handle
)
969 sigset_t blocked
= cfs_block_allsigs();
971 LNetMDUnlink(*md_handle
);
972 LNetInvalidateHandle(md_handle
);
974 /* NB md could be busy; this just starts the unlink */
975 while (pinfo
->pi_features
!= LNET_PING_FEAT_INVAL
) {
976 CDEBUG(D_NET
, "Still waiting for ping MD to unlink\n");
977 set_current_state(TASK_UNINTERRUPTIBLE
);
978 schedule_timeout(cfs_time_seconds(1));
981 cfs_restore_sigs(blocked
);
985 lnet_ping_info_install_locked(lnet_ping_info_t
*ping_info
)
987 lnet_ni_status_t
*ns
;
991 list_for_each_entry(ni
, &the_lnet
.ln_nis
, ni_list
) {
992 LASSERT(i
< ping_info
->pi_nnis
);
994 ns
= &ping_info
->pi_ni
[i
];
996 ns
->ns_nid
= ni
->ni_nid
;
999 ns
->ns_status
= (ni
->ni_status
) ?
1000 ni
->ni_status
->ns_status
: LNET_NI_STATUS_UP
;
1009 lnet_ping_target_update(lnet_ping_info_t
*pinfo
, lnet_handle_md_t md_handle
)
1011 lnet_ping_info_t
*old_pinfo
= NULL
;
1012 lnet_handle_md_t old_md
;
1014 /* switch the NIs to point to the new ping info created */
1015 lnet_net_lock(LNET_LOCK_EX
);
1017 if (!the_lnet
.ln_routing
)
1018 pinfo
->pi_features
|= LNET_PING_FEAT_RTE_DISABLED
;
1019 lnet_ping_info_install_locked(pinfo
);
1021 if (the_lnet
.ln_ping_info
) {
1022 old_pinfo
= the_lnet
.ln_ping_info
;
1023 old_md
= the_lnet
.ln_ping_target_md
;
1025 the_lnet
.ln_ping_target_md
= md_handle
;
1026 the_lnet
.ln_ping_info
= pinfo
;
1028 lnet_net_unlock(LNET_LOCK_EX
);
1031 /* unlink the old ping info */
1032 lnet_ping_md_unlink(old_pinfo
, &old_md
);
1033 lnet_ping_info_free(old_pinfo
);
1038 lnet_ping_target_fini(void)
1042 lnet_ping_md_unlink(the_lnet
.ln_ping_info
,
1043 &the_lnet
.ln_ping_target_md
);
1045 rc
= LNetEQFree(the_lnet
.ln_ping_target_eq
);
1048 lnet_ping_info_destroy();
1052 lnet_ni_tq_credits(lnet_ni_t
*ni
)
1056 LASSERT(ni
->ni_ncpts
>= 1);
1058 if (ni
->ni_ncpts
== 1)
1059 return ni
->ni_maxtxcredits
;
1061 credits
= ni
->ni_maxtxcredits
/ ni
->ni_ncpts
;
1062 credits
= max(credits
, 8 * ni
->ni_peertxcredits
);
1063 credits
= min(credits
, ni
->ni_maxtxcredits
);
1069 lnet_ni_unlink_locked(lnet_ni_t
*ni
)
1071 if (!list_empty(&ni
->ni_cptlist
)) {
1072 list_del_init(&ni
->ni_cptlist
);
1073 lnet_ni_decref_locked(ni
, 0);
1076 /* move it to zombie list and nobody can find it anymore */
1077 LASSERT(!list_empty(&ni
->ni_list
));
1078 list_move(&ni
->ni_list
, &the_lnet
.ln_nis_zombie
);
1079 lnet_ni_decref_locked(ni
, 0); /* drop ln_nis' ref */
1083 lnet_clear_zombies_nis_locked(void)
1090 * Now wait for the NI's I just nuked to show up on ln_zombie_nis
1091 * and shut them down in guaranteed thread context
1094 while (!list_empty(&the_lnet
.ln_nis_zombie
)) {
1098 ni
= list_entry(the_lnet
.ln_nis_zombie
.next
,
1099 lnet_ni_t
, ni_list
);
1100 list_del_init(&ni
->ni_list
);
1101 cfs_percpt_for_each(ref
, j
, ni
->ni_refs
) {
1104 /* still busy, add it back to zombie list */
1105 list_add(&ni
->ni_list
, &the_lnet
.ln_nis_zombie
);
1109 if (!list_empty(&ni
->ni_list
)) {
1110 lnet_net_unlock(LNET_LOCK_EX
);
1112 if ((i
& (-i
)) == i
) {
1113 CDEBUG(D_WARNING
, "Waiting for zombie LNI %s\n",
1114 libcfs_nid2str(ni
->ni_nid
));
1116 set_current_state(TASK_UNINTERRUPTIBLE
);
1117 schedule_timeout(cfs_time_seconds(1));
1118 lnet_net_lock(LNET_LOCK_EX
);
1122 ni
->ni_lnd
->lnd_refcount
--;
1123 lnet_net_unlock(LNET_LOCK_EX
);
1125 islo
= ni
->ni_lnd
->lnd_type
== LOLND
;
1127 LASSERT(!in_interrupt());
1128 ni
->ni_lnd
->lnd_shutdown(ni
);
1131 * can't deref lnd anymore now; it might have unregistered
1135 CDEBUG(D_LNI
, "Removed LNI %s\n",
1136 libcfs_nid2str(ni
->ni_nid
));
1141 lnet_net_lock(LNET_LOCK_EX
);
1146 lnet_shutdown_lndnis(void)
1151 /* NB called holding the global mutex */
1153 /* All quiet on the API front */
1154 LASSERT(!the_lnet
.ln_shutdown
);
1155 LASSERT(!the_lnet
.ln_refcount
);
1156 LASSERT(list_empty(&the_lnet
.ln_nis_zombie
));
1158 lnet_net_lock(LNET_LOCK_EX
);
1159 the_lnet
.ln_shutdown
= 1; /* flag shutdown */
1161 /* Unlink NIs from the global table */
1162 while (!list_empty(&the_lnet
.ln_nis
)) {
1163 ni
= list_entry(the_lnet
.ln_nis
.next
,
1164 lnet_ni_t
, ni_list
);
1165 lnet_ni_unlink_locked(ni
);
1168 /* Drop the cached eqwait NI. */
1169 if (the_lnet
.ln_eq_waitni
) {
1170 lnet_ni_decref_locked(the_lnet
.ln_eq_waitni
, 0);
1171 the_lnet
.ln_eq_waitni
= NULL
;
1174 /* Drop the cached loopback NI. */
1175 if (the_lnet
.ln_loni
) {
1176 lnet_ni_decref_locked(the_lnet
.ln_loni
, 0);
1177 the_lnet
.ln_loni
= NULL
;
1180 lnet_net_unlock(LNET_LOCK_EX
);
1183 * Clear lazy portals and drop delayed messages which hold refs
1184 * on their lnet_msg_t::msg_rxpeer
1186 for (i
= 0; i
< the_lnet
.ln_nportals
; i
++)
1187 LNetClearLazyPortal(i
);
1190 * Clear the peer table and wait for all peers to go (they hold refs on
1193 lnet_peer_tables_cleanup(NULL
);
1195 lnet_net_lock(LNET_LOCK_EX
);
1197 lnet_clear_zombies_nis_locked();
1198 the_lnet
.ln_shutdown
= 0;
1199 lnet_net_unlock(LNET_LOCK_EX
);
1202 /* shutdown down the NI and release refcount */
1204 lnet_shutdown_lndni(struct lnet_ni
*ni
)
1206 lnet_net_lock(LNET_LOCK_EX
);
1207 lnet_ni_unlink_locked(ni
);
1208 lnet_net_unlock(LNET_LOCK_EX
);
1210 /* Do peer table cleanup for this ni */
1211 lnet_peer_tables_cleanup(ni
);
1213 lnet_net_lock(LNET_LOCK_EX
);
1214 lnet_clear_zombies_nis_locked();
1215 lnet_net_unlock(LNET_LOCK_EX
);
1219 lnet_startup_lndni(struct lnet_ni
*ni
, __s32 peer_timeout
,
1220 __s32 peer_cr
, __s32 peer_buf_cr
, __s32 credits
)
1225 struct lnet_tx_queue
*tq
;
1228 lnd_type
= LNET_NETTYP(LNET_NIDNET(ni
->ni_nid
));
1230 LASSERT(libcfs_isknown_lnd(lnd_type
));
1232 if (lnd_type
== CIBLND
|| lnd_type
== OPENIBLND
||
1233 lnd_type
== IIBLND
|| lnd_type
== VIBLND
) {
1234 CERROR("LND %s obsoleted\n", libcfs_lnd2str(lnd_type
));
1238 /* Make sure this new NI is unique. */
1239 lnet_net_lock(LNET_LOCK_EX
);
1240 rc
= lnet_net_unique(LNET_NIDNET(ni
->ni_nid
), &the_lnet
.ln_nis
);
1241 lnet_net_unlock(LNET_LOCK_EX
);
1243 if (lnd_type
== LOLND
) {
1248 CERROR("Net %s is not unique\n",
1249 libcfs_net2str(LNET_NIDNET(ni
->ni_nid
)));
1254 mutex_lock(&the_lnet
.ln_lnd_mutex
);
1255 lnd
= lnet_find_lnd_by_type(lnd_type
);
1258 mutex_unlock(&the_lnet
.ln_lnd_mutex
);
1259 rc
= request_module("%s", libcfs_lnd2modname(lnd_type
));
1260 mutex_lock(&the_lnet
.ln_lnd_mutex
);
1262 lnd
= lnet_find_lnd_by_type(lnd_type
);
1264 mutex_unlock(&the_lnet
.ln_lnd_mutex
);
1265 CERROR("Can't load LND %s, module %s, rc=%d\n",
1266 libcfs_lnd2str(lnd_type
),
1267 libcfs_lnd2modname(lnd_type
), rc
);
1273 lnet_net_lock(LNET_LOCK_EX
);
1274 lnd
->lnd_refcount
++;
1275 lnet_net_unlock(LNET_LOCK_EX
);
1279 rc
= lnd
->lnd_startup(ni
);
1281 mutex_unlock(&the_lnet
.ln_lnd_mutex
);
1284 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1285 rc
, libcfs_lnd2str(lnd
->lnd_type
));
1286 lnet_net_lock(LNET_LOCK_EX
);
1287 lnd
->lnd_refcount
--;
1288 lnet_net_unlock(LNET_LOCK_EX
);
1293 * If given some LND tunable parameters, parse those now to
1294 * override the values in the NI structure.
1296 if (peer_buf_cr
>= 0)
1297 ni
->ni_peerrtrcredits
= peer_buf_cr
;
1298 if (peer_timeout
>= 0)
1299 ni
->ni_peertimeout
= peer_timeout
;
1302 * Note: For now, don't allow the user to change
1303 * peertxcredits as this number is used in the
1304 * IB LND to control queue depth.
1305 * if (peer_cr != -1)
1306 * ni->ni_peertxcredits = peer_cr;
1309 ni
->ni_maxtxcredits
= credits
;
1311 LASSERT(ni
->ni_peertimeout
<= 0 || lnd
->lnd_query
);
1313 lnet_net_lock(LNET_LOCK_EX
);
1314 /* refcount for ln_nis */
1315 lnet_ni_addref_locked(ni
, 0);
1316 list_add_tail(&ni
->ni_list
, &the_lnet
.ln_nis
);
1318 lnet_ni_addref_locked(ni
, 0);
1319 list_add_tail(&ni
->ni_cptlist
, &the_lnet
.ln_nis_cpt
);
1322 lnet_net_unlock(LNET_LOCK_EX
);
1324 if (lnd
->lnd_type
== LOLND
) {
1326 LASSERT(!the_lnet
.ln_loni
);
1327 the_lnet
.ln_loni
= ni
;
1331 if (!ni
->ni_peertxcredits
|| !ni
->ni_maxtxcredits
) {
1332 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1333 libcfs_lnd2str(lnd
->lnd_type
),
1334 !ni
->ni_peertxcredits
?
1337 * shutdown the NI since if we get here then it must've already
1340 lnet_shutdown_lndni(ni
);
1344 cfs_percpt_for_each(tq
, i
, ni
->ni_tx_queues
) {
1345 tq
->tq_credits_min
=
1346 tq
->tq_credits_max
=
1347 tq
->tq_credits
= lnet_ni_tq_credits(ni
);
1350 CDEBUG(D_LNI
, "Added LNI %s [%d/%d/%d/%d]\n",
1351 libcfs_nid2str(ni
->ni_nid
), ni
->ni_peertxcredits
,
1352 lnet_ni_tq_credits(ni
) * LNET_CPT_NUMBER
,
1353 ni
->ni_peerrtrcredits
, ni
->ni_peertimeout
);
1362 lnet_startup_lndnis(struct list_head
*nilist
)
1369 while (!list_empty(nilist
)) {
1370 ni
= list_entry(nilist
->next
, lnet_ni_t
, ni_list
);
1371 list_del(&ni
->ni_list
);
1372 rc
= lnet_startup_lndni(ni
, -1, -1, -1, -1);
1380 if (the_lnet
.ln_eq_waitni
&& ni_count
> 1) {
1381 lnd_type
= the_lnet
.ln_eq_waitni
->ni_lnd
->lnd_type
;
1382 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network\n",
1383 libcfs_lnd2str(lnd_type
));
1390 lnet_shutdown_lndnis();
1396 * Initialize LNet library.
1398 * Only userspace program needs to call this function - it's automatically
1399 * called in the kernel at module loading time. Caller has to call lnet_fini()
1400 * after a call to lnet_init(), if and only if the latter returned 0. It must
1401 * be called exactly once.
1403 * \return 0 on success, and -ve on failures.
1410 lnet_assert_wire_constants();
1411 LASSERT(!the_lnet
.ln_init
);
1413 memset(&the_lnet
, 0, sizeof(the_lnet
));
1415 /* refer to global cfs_cpt_table for now */
1416 the_lnet
.ln_cpt_table
= cfs_cpt_table
;
1417 the_lnet
.ln_cpt_number
= cfs_cpt_number(cfs_cpt_table
);
1419 LASSERT(the_lnet
.ln_cpt_number
> 0);
1420 if (the_lnet
.ln_cpt_number
> LNET_CPT_MAX
) {
1421 /* we are under risk of consuming all lh_cookie */
1422 CERROR("Can't have %d CPTs for LNet (max allowed is %d), please change setting of CPT-table and retry\n",
1423 the_lnet
.ln_cpt_number
, LNET_CPT_MAX
);
1427 while ((1 << the_lnet
.ln_cpt_bits
) < the_lnet
.ln_cpt_number
)
1428 the_lnet
.ln_cpt_bits
++;
1430 rc
= lnet_create_locks();
1432 CERROR("Can't create LNet global locks: %d\n", rc
);
1436 the_lnet
.ln_refcount
= 0;
1437 the_lnet
.ln_init
= 1;
1438 LNetInvalidateHandle(&the_lnet
.ln_rc_eqh
);
1439 INIT_LIST_HEAD(&the_lnet
.ln_lnds
);
1440 INIT_LIST_HEAD(&the_lnet
.ln_rcd_zombie
);
1441 INIT_LIST_HEAD(&the_lnet
.ln_rcd_deathrow
);
1444 * The hash table size is the number of bits it takes to express the set
1445 * ln_num_routes, minus 1 (better to under estimate than over so we
1446 * don't waste memory).
1448 if (rnet_htable_size
<= 0)
1449 rnet_htable_size
= LNET_REMOTE_NETS_HASH_DEFAULT
;
1450 else if (rnet_htable_size
> LNET_REMOTE_NETS_HASH_MAX
)
1451 rnet_htable_size
= LNET_REMOTE_NETS_HASH_MAX
;
1452 the_lnet
.ln_remote_nets_hbits
= max_t(int, 1,
1453 order_base_2(rnet_htable_size
) - 1);
1456 * All LNDs apart from the LOLND are in separate modules. They
1457 * register themselves when their module loads, and unregister
1458 * themselves when their module is unloaded.
1460 lnet_register_lnd(&the_lolnd
);
1465 * Finalize LNet library.
1467 * Only userspace program needs to call this function. It can be called
1470 * \pre lnet_init() called with success.
1471 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1476 LASSERT(the_lnet
.ln_init
);
1477 LASSERT(!the_lnet
.ln_refcount
);
1479 while (!list_empty(&the_lnet
.ln_lnds
))
1480 lnet_unregister_lnd(list_entry(the_lnet
.ln_lnds
.next
,
1482 lnet_destroy_locks();
1484 the_lnet
.ln_init
= 0;
1488 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1490 * Userspace program should call this after a successful call to lnet_init().
1491 * Users must call this function at least once before any other functions.
1492 * For each successful call there must be a corresponding call to
1493 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1496 * The PID used by LNet may be different from the one requested.
1499 * \param requested_pid PID requested by the caller.
1501 * \return >= 0 on success, and < 0 error code on failures.
1504 LNetNIInit(lnet_pid_t requested_pid
)
1506 int im_a_router
= 0;
1509 lnet_ping_info_t
*pinfo
;
1510 lnet_handle_md_t md_handle
;
1511 struct list_head net_head
;
1513 INIT_LIST_HEAD(&net_head
);
1515 mutex_lock(&the_lnet
.ln_api_mutex
);
1517 LASSERT(the_lnet
.ln_init
);
1518 CDEBUG(D_OTHER
, "refs %d\n", the_lnet
.ln_refcount
);
1520 if (the_lnet
.ln_refcount
> 0) {
1521 rc
= the_lnet
.ln_refcount
++;
1522 mutex_unlock(&the_lnet
.ln_api_mutex
);
1526 rc
= lnet_prepare(requested_pid
);
1528 mutex_unlock(&the_lnet
.ln_api_mutex
);
1532 /* Add in the loopback network */
1533 if (!lnet_ni_alloc(LNET_MKNET(LOLND
, 0), NULL
, &net_head
)) {
1535 goto err_empty_list
;
1539 * If LNet is being initialized via DLC it is possible
1540 * that the user requests not to load module parameters (ones which
1541 * are supported by DLC) on initialization. Therefore, make sure not
1542 * to load networks, routes and forwarding from module parameters
1543 * in this case. On cleanup in case of failure only clean up
1544 * routes if it has been loaded
1546 if (!the_lnet
.ln_nis_from_mod_params
) {
1547 rc
= lnet_parse_networks(&net_head
, lnet_get_networks());
1549 goto err_empty_list
;
1552 ni_count
= lnet_startup_lndnis(&net_head
);
1555 goto err_empty_list
;
1558 if (!the_lnet
.ln_nis_from_mod_params
) {
1559 rc
= lnet_parse_routes(lnet_get_routes(), &im_a_router
);
1561 goto err_shutdown_lndnis
;
1563 rc
= lnet_check_routes();
1565 goto err_destory_routes
;
1567 rc
= lnet_rtrpools_alloc(im_a_router
);
1569 goto err_destory_routes
;
1572 rc
= lnet_acceptor_start();
1574 goto err_destory_routes
;
1576 the_lnet
.ln_refcount
= 1;
1577 /* Now I may use my own API functions... */
1579 rc
= lnet_ping_info_setup(&pinfo
, &md_handle
, ni_count
, true);
1581 goto err_acceptor_stop
;
1583 lnet_ping_target_update(pinfo
, md_handle
);
1585 rc
= lnet_router_checker_start();
1589 lnet_router_debugfs_init();
1591 mutex_unlock(&the_lnet
.ln_api_mutex
);
1596 lnet_ping_target_fini();
1598 the_lnet
.ln_refcount
= 0;
1599 lnet_acceptor_stop();
1601 if (!the_lnet
.ln_nis_from_mod_params
)
1602 lnet_destroy_routes();
1603 err_shutdown_lndnis
:
1604 lnet_shutdown_lndnis();
1608 mutex_unlock(&the_lnet
.ln_api_mutex
);
1609 while (!list_empty(&net_head
)) {
1612 ni
= list_entry(net_head
.next
, struct lnet_ni
, ni_list
);
1613 list_del_init(&ni
->ni_list
);
1618 EXPORT_SYMBOL(LNetNIInit
);
1621 * Stop LNet interfaces, routing, and forwarding.
1623 * Users must call this function once for each successful call to LNetNIInit().
1624 * Once the LNetNIFini() operation has been started, the results of pending
1625 * API operations are undefined.
1627 * \return always 0 for current implementation.
1632 mutex_lock(&the_lnet
.ln_api_mutex
);
1634 LASSERT(the_lnet
.ln_init
);
1635 LASSERT(the_lnet
.ln_refcount
> 0);
1637 if (the_lnet
.ln_refcount
!= 1) {
1638 the_lnet
.ln_refcount
--;
1640 LASSERT(!the_lnet
.ln_niinit_self
);
1642 lnet_router_debugfs_fini();
1643 lnet_router_checker_stop();
1644 lnet_ping_target_fini();
1646 /* Teardown fns that use my own API functions BEFORE here */
1647 the_lnet
.ln_refcount
= 0;
1649 lnet_acceptor_stop();
1650 lnet_destroy_routes();
1651 lnet_shutdown_lndnis();
1655 mutex_unlock(&the_lnet
.ln_api_mutex
);
1658 EXPORT_SYMBOL(LNetNIFini
);
1661 * Grabs the ni data from the ni structure and fills the out
1664 * \param[in] ni network interface structure
1665 * \param[out] cpt_count the number of cpts the ni is on
1666 * \param[out] nid Network Interface ID
1667 * \param[out] peer_timeout NI peer timeout
1668 * \param[out] peer_tx_crdits NI peer transmit credits
1669 * \param[out] peer_rtr_credits NI peer router credits
1670 * \param[out] max_tx_credits NI max transmit credit
1671 * \param[out] net_config Network configuration
1674 lnet_fill_ni_info(struct lnet_ni
*ni
, __u32
*cpt_count
, __u64
*nid
,
1675 int *peer_timeout
, int *peer_tx_credits
,
1676 int *peer_rtr_credits
, int *max_tx_credits
,
1677 struct lnet_ioctl_net_config
*net_config
)
1687 BUILD_BUG_ON(ARRAY_SIZE(ni
->ni_interfaces
) !=
1688 ARRAY_SIZE(net_config
->ni_interfaces
));
1690 for (i
= 0; i
< ARRAY_SIZE(ni
->ni_interfaces
); i
++) {
1691 if (!ni
->ni_interfaces
[i
])
1694 strncpy(net_config
->ni_interfaces
[i
],
1695 ni
->ni_interfaces
[i
],
1696 sizeof(net_config
->ni_interfaces
[i
]));
1700 *peer_timeout
= ni
->ni_peertimeout
;
1701 *peer_tx_credits
= ni
->ni_peertxcredits
;
1702 *peer_rtr_credits
= ni
->ni_peerrtrcredits
;
1703 *max_tx_credits
= ni
->ni_maxtxcredits
;
1705 net_config
->ni_status
= ni
->ni_status
->ns_status
;
1708 int num_cpts
= min(ni
->ni_ncpts
, LNET_MAX_SHOW_NUM_CPT
);
1710 for (i
= 0; i
< num_cpts
; i
++)
1711 net_config
->ni_cpts
[i
] = ni
->ni_cpts
[i
];
1713 *cpt_count
= num_cpts
;
1718 lnet_get_net_config(int idx
, __u32
*cpt_count
, __u64
*nid
, int *peer_timeout
,
1719 int *peer_tx_credits
, int *peer_rtr_credits
,
1720 int *max_tx_credits
,
1721 struct lnet_ioctl_net_config
*net_config
)
1724 struct list_head
*tmp
;
1728 cpt
= lnet_net_lock_current();
1730 list_for_each(tmp
, &the_lnet
.ln_nis
) {
1734 ni
= list_entry(tmp
, lnet_ni_t
, ni_list
);
1736 lnet_fill_ni_info(ni
, cpt_count
, nid
, peer_timeout
,
1737 peer_tx_credits
, peer_rtr_credits
,
1738 max_tx_credits
, net_config
);
1744 lnet_net_unlock(cpt
);
1749 lnet_dyn_add_ni(lnet_pid_t requested_pid
, char *nets
,
1750 __s32 peer_timeout
, __s32 peer_cr
, __s32 peer_buf_cr
,
1753 lnet_ping_info_t
*pinfo
;
1754 lnet_handle_md_t md_handle
;
1756 struct list_head net_head
;
1757 lnet_remotenet_t
*rnet
;
1760 INIT_LIST_HEAD(&net_head
);
1762 /* Create a ni structure for the network string */
1763 rc
= lnet_parse_networks(&net_head
, nets
);
1765 return !rc
? -EINVAL
: rc
;
1767 mutex_lock(&the_lnet
.ln_api_mutex
);
1770 rc
= -EINVAL
; /* only add one interface per call */
1774 ni
= list_entry(net_head
.next
, struct lnet_ni
, ni_list
);
1776 lnet_net_lock(LNET_LOCK_EX
);
1777 rnet
= lnet_find_net_locked(LNET_NIDNET(ni
->ni_nid
));
1778 lnet_net_unlock(LNET_LOCK_EX
);
1780 * make sure that the net added doesn't invalidate the current
1781 * configuration LNet is keeping
1784 CERROR("Adding net %s will invalidate routing configuration\n",
1790 rc
= lnet_ping_info_setup(&pinfo
, &md_handle
, 1 + lnet_get_ni_count(),
1795 list_del_init(&ni
->ni_list
);
1797 rc
= lnet_startup_lndni(ni
, peer_timeout
, peer_cr
,
1798 peer_buf_cr
, credits
);
1802 if (ni
->ni_lnd
->lnd_accept
) {
1803 rc
= lnet_acceptor_start();
1805 /* shutdown the ni that we just started */
1806 CERROR("Failed to start up acceptor thread\n");
1807 lnet_shutdown_lndni(ni
);
1812 lnet_ping_target_update(pinfo
, md_handle
);
1813 mutex_unlock(&the_lnet
.ln_api_mutex
);
1818 lnet_ping_md_unlink(pinfo
, &md_handle
);
1819 lnet_ping_info_free(pinfo
);
1821 mutex_unlock(&the_lnet
.ln_api_mutex
);
1822 while (!list_empty(&net_head
)) {
1823 ni
= list_entry(net_head
.next
, struct lnet_ni
, ni_list
);
1824 list_del_init(&ni
->ni_list
);
1831 lnet_dyn_del_ni(__u32 net
)
1834 lnet_ping_info_t
*pinfo
;
1835 lnet_handle_md_t md_handle
;
1838 /* don't allow userspace to shutdown the LOLND */
1839 if (LNET_NETTYP(net
) == LOLND
)
1842 mutex_lock(&the_lnet
.ln_api_mutex
);
1843 /* create and link a new ping info, before removing the old one */
1844 rc
= lnet_ping_info_setup(&pinfo
, &md_handle
,
1845 lnet_get_ni_count() - 1, false);
1849 ni
= lnet_net2ni(net
);
1855 /* decrement the reference counter taken by lnet_net2ni() */
1856 lnet_ni_decref_locked(ni
, 0);
1858 lnet_shutdown_lndni(ni
);
1860 if (!lnet_count_acceptor_nis())
1861 lnet_acceptor_stop();
1863 lnet_ping_target_update(pinfo
, md_handle
);
1866 lnet_ping_md_unlink(pinfo
, &md_handle
);
1867 lnet_ping_info_free(pinfo
);
1869 mutex_unlock(&the_lnet
.ln_api_mutex
);
1875 * LNet ioctl handler.
1879 LNetCtl(unsigned int cmd
, void *arg
)
1881 struct libcfs_ioctl_data
*data
= arg
;
1882 struct lnet_ioctl_config_data
*config
;
1883 lnet_process_id_t id
= {0};
1886 unsigned long secs_passed
;
1888 LASSERT(the_lnet
.ln_init
);
1891 case IOC_LIBCFS_GET_NI
:
1892 rc
= LNetGetId(data
->ioc_count
, &id
);
1893 data
->ioc_nid
= id
.nid
;
1896 case IOC_LIBCFS_FAIL_NID
:
1897 return lnet_fail_nid(data
->ioc_nid
, data
->ioc_count
);
1899 case IOC_LIBCFS_ADD_ROUTE
:
1902 if (config
->cfg_hdr
.ioc_len
< sizeof(*config
))
1905 mutex_lock(&the_lnet
.ln_api_mutex
);
1906 rc
= lnet_add_route(config
->cfg_net
,
1907 config
->cfg_config_u
.cfg_route
.rtr_hop
,
1909 config
->cfg_config_u
.cfg_route
.rtr_priority
);
1910 mutex_unlock(&the_lnet
.ln_api_mutex
);
1911 return rc
? rc
: lnet_check_routes();
1913 case IOC_LIBCFS_DEL_ROUTE
:
1916 if (config
->cfg_hdr
.ioc_len
< sizeof(*config
))
1919 mutex_lock(&the_lnet
.ln_api_mutex
);
1920 rc
= lnet_del_route(config
->cfg_net
, config
->cfg_nid
);
1921 mutex_unlock(&the_lnet
.ln_api_mutex
);
1924 case IOC_LIBCFS_GET_ROUTE
:
1927 if (config
->cfg_hdr
.ioc_len
< sizeof(*config
))
1930 return lnet_get_route(config
->cfg_count
,
1932 &config
->cfg_config_u
.cfg_route
.rtr_hop
,
1934 &config
->cfg_config_u
.cfg_route
.rtr_flags
,
1935 &config
->cfg_config_u
.cfg_route
.rtr_priority
);
1937 case IOC_LIBCFS_GET_NET
: {
1938 struct lnet_ioctl_net_config
*net_config
;
1939 size_t total
= sizeof(*config
) + sizeof(*net_config
);
1943 if (config
->cfg_hdr
.ioc_len
< total
)
1946 net_config
= (struct lnet_ioctl_net_config
*)
1951 return lnet_get_net_config(config
->cfg_count
,
1954 &config
->cfg_config_u
.cfg_net
.net_peer_timeout
,
1955 &config
->cfg_config_u
.cfg_net
.net_peer_tx_credits
,
1956 &config
->cfg_config_u
.cfg_net
.net_peer_rtr_credits
,
1957 &config
->cfg_config_u
.cfg_net
.net_max_tx_credits
,
1961 case IOC_LIBCFS_GET_LNET_STATS
: {
1962 struct lnet_ioctl_lnet_stats
*lnet_stats
= arg
;
1964 if (lnet_stats
->st_hdr
.ioc_len
< sizeof(*lnet_stats
))
1967 lnet_counters_get(&lnet_stats
->st_cntrs
);
1971 case IOC_LIBCFS_CONFIG_RTR
:
1974 if (config
->cfg_hdr
.ioc_len
< sizeof(*config
))
1977 mutex_lock(&the_lnet
.ln_api_mutex
);
1978 if (config
->cfg_config_u
.cfg_buffers
.buf_enable
) {
1979 rc
= lnet_rtrpools_enable();
1980 mutex_unlock(&the_lnet
.ln_api_mutex
);
1983 lnet_rtrpools_disable();
1984 mutex_unlock(&the_lnet
.ln_api_mutex
);
1987 case IOC_LIBCFS_ADD_BUF
:
1990 if (config
->cfg_hdr
.ioc_len
< sizeof(*config
))
1993 mutex_lock(&the_lnet
.ln_api_mutex
);
1994 rc
= lnet_rtrpools_adjust(config
->cfg_config_u
.cfg_buffers
.buf_tiny
,
1995 config
->cfg_config_u
.cfg_buffers
.buf_small
,
1996 config
->cfg_config_u
.cfg_buffers
.buf_large
);
1997 mutex_unlock(&the_lnet
.ln_api_mutex
);
2000 case IOC_LIBCFS_GET_BUF
: {
2001 struct lnet_ioctl_pool_cfg
*pool_cfg
;
2002 size_t total
= sizeof(*config
) + sizeof(*pool_cfg
);
2006 if (config
->cfg_hdr
.ioc_len
< total
)
2009 pool_cfg
= (struct lnet_ioctl_pool_cfg
*)config
->cfg_bulk
;
2010 return lnet_get_rtr_pool_cfg(config
->cfg_count
, pool_cfg
);
2013 case IOC_LIBCFS_GET_PEER_INFO
: {
2014 struct lnet_ioctl_peer
*peer_info
= arg
;
2016 if (peer_info
->pr_hdr
.ioc_len
< sizeof(*peer_info
))
2019 return lnet_get_peer_info(peer_info
->pr_count
,
2021 peer_info
->pr_lnd_u
.pr_peer_credits
.cr_aliveness
,
2022 &peer_info
->pr_lnd_u
.pr_peer_credits
.cr_ncpt
,
2023 &peer_info
->pr_lnd_u
.pr_peer_credits
.cr_refcount
,
2024 &peer_info
->pr_lnd_u
.pr_peer_credits
.cr_ni_peer_tx_credits
,
2025 &peer_info
->pr_lnd_u
.pr_peer_credits
.cr_peer_tx_credits
,
2026 &peer_info
->pr_lnd_u
.pr_peer_credits
.cr_peer_rtr_credits
,
2027 &peer_info
->pr_lnd_u
.pr_peer_credits
.cr_peer_min_rtr_credits
,
2028 &peer_info
->pr_lnd_u
.pr_peer_credits
.cr_peer_tx_qnob
);
2031 case IOC_LIBCFS_NOTIFY_ROUTER
:
2032 secs_passed
= (ktime_get_real_seconds() - data
->ioc_u64
[0]);
2033 return lnet_notify(NULL
, data
->ioc_nid
, data
->ioc_flags
,
2034 jiffies
- secs_passed
* HZ
);
2036 case IOC_LIBCFS_LNET_DIST
:
2037 rc
= LNetDist(data
->ioc_nid
, &data
->ioc_nid
, &data
->ioc_u32
[1]);
2038 if (rc
< 0 && rc
!= -EHOSTUNREACH
)
2041 data
->ioc_u32
[0] = rc
;
2044 case IOC_LIBCFS_TESTPROTOCOMPAT
:
2045 lnet_net_lock(LNET_LOCK_EX
);
2046 the_lnet
.ln_testprotocompat
= data
->ioc_flags
;
2047 lnet_net_unlock(LNET_LOCK_EX
);
2050 case IOC_LIBCFS_PING
:
2051 id
.nid
= data
->ioc_nid
;
2052 id
.pid
= data
->ioc_u32
[0];
2053 rc
= lnet_ping(id
, data
->ioc_u32
[1], /* timeout */
2055 data
->ioc_plen1
/ sizeof(lnet_process_id_t
));
2058 data
->ioc_count
= rc
;
2062 ni
= lnet_net2ni(data
->ioc_net
);
2066 if (!ni
->ni_lnd
->lnd_ctl
)
2069 rc
= ni
->ni_lnd
->lnd_ctl(ni
, cmd
, arg
);
2076 EXPORT_SYMBOL(LNetCtl
);
2078 void LNetDebugPeer(lnet_process_id_t id
)
2080 lnet_debug_peer(id
.nid
);
2082 EXPORT_SYMBOL(LNetDebugPeer
);
2085 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
2086 * all interfaces share a same PID, as requested by LNetNIInit().
2088 * \param index Index of the interface to look up.
2089 * \param id On successful return, this location will hold the
2090 * lnet_process_id_t ID of the interface.
2092 * \retval 0 If an interface exists at \a index.
2093 * \retval -ENOENT If no interface has been found.
2096 LNetGetId(unsigned int index
, lnet_process_id_t
*id
)
2099 struct list_head
*tmp
;
2103 LASSERT(the_lnet
.ln_init
);
2105 /* LNetNI initilization failed? */
2106 if (!the_lnet
.ln_refcount
)
2109 cpt
= lnet_net_lock_current();
2111 list_for_each(tmp
, &the_lnet
.ln_nis
) {
2115 ni
= list_entry(tmp
, lnet_ni_t
, ni_list
);
2117 id
->nid
= ni
->ni_nid
;
2118 id
->pid
= the_lnet
.ln_pid
;
2123 lnet_net_unlock(cpt
);
2126 EXPORT_SYMBOL(LNetGetId
);
2129 * Print a string representation of handle \a h into buffer \a str of
2133 LNetSnprintHandle(char *str
, int len
, lnet_handle_any_t h
)
2135 snprintf(str
, len
, "%#llx", h
.cookie
);
2137 EXPORT_SYMBOL(LNetSnprintHandle
);
2139 static int lnet_ping(lnet_process_id_t id
, int timeout_ms
,
2140 lnet_process_id_t __user
*ids
, int n_ids
)
2142 lnet_handle_eq_t eqh
;
2143 lnet_handle_md_t mdh
;
2145 lnet_md_t md
= { NULL
};
2149 const int a_long_time
= 60000; /* mS */
2151 lnet_ping_info_t
*info
;
2152 lnet_process_id_t tmpid
;
2159 infosz
= offsetof(lnet_ping_info_t
, pi_ni
[n_ids
]);
2162 id
.nid
== LNET_NID_ANY
||
2163 timeout_ms
> 500000 || /* arbitrary limit! */
2164 n_ids
> 20) /* arbitrary limit! */
2167 if (id
.pid
== LNET_PID_ANY
)
2168 id
.pid
= LNET_PID_LUSTRE
;
2170 LIBCFS_ALLOC(info
, infosz
);
2174 /* NB 2 events max (including any unlink event) */
2175 rc
= LNetEQAlloc(2, LNET_EQ_HANDLER_NONE
, &eqh
);
2177 CERROR("Can't allocate EQ: %d\n", rc
);
2181 /* initialize md content */
2184 md
.threshold
= 2; /*GET/REPLY*/
2186 md
.options
= LNET_MD_TRUNCATE
;
2190 rc
= LNetMDBind(md
, LNET_UNLINK
, &mdh
);
2192 CERROR("Can't bind MD: %d\n", rc
);
2196 rc
= LNetGet(LNET_NID_ANY
, mdh
, id
,
2197 LNET_RESERVED_PORTAL
,
2198 LNET_PROTO_PING_MATCHBITS
, 0);
2201 /* Don't CERROR; this could be deliberate! */
2203 rc2
= LNetMDUnlink(mdh
);
2206 /* NB must wait for the UNLINK event below... */
2208 timeout_ms
= a_long_time
;
2212 /* MUST block for unlink to complete */
2214 blocked
= cfs_block_allsigs();
2216 rc2
= LNetEQPoll(&eqh
, 1, timeout_ms
, &event
, &which
);
2219 cfs_restore_sigs(blocked
);
2221 CDEBUG(D_NET
, "poll %d(%d %d)%s\n", rc2
,
2222 (rc2
<= 0) ? -1 : event
.type
,
2223 (rc2
<= 0) ? -1 : event
.status
,
2224 (rc2
> 0 && event
.unlinked
) ? " unlinked" : "");
2226 LASSERT(rc2
!= -EOVERFLOW
); /* can't miss anything */
2228 if (rc2
<= 0 || event
.status
) {
2229 /* timeout or error */
2230 if (!replied
&& !rc
)
2231 rc
= (rc2
< 0) ? rc2
:
2236 /* Ensure completion in finite time... */
2238 /* No assertion (racing with network) */
2240 timeout_ms
= a_long_time
;
2242 /* timed out waiting for unlink */
2243 CWARN("ping %s: late network completion\n",
2246 } else if (event
.type
== LNET_EVENT_REPLY
) {
2251 } while (rc2
<= 0 || !event
.unlinked
);
2255 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2262 LASSERT(nob
>= 0 && nob
<= infosz
);
2264 rc
= -EPROTO
; /* if I can't parse... */
2267 /* can't check magic/version */
2268 CERROR("%s: ping info too short %d\n",
2269 libcfs_id2str(id
), nob
);
2273 if (info
->pi_magic
== __swab32(LNET_PROTO_PING_MAGIC
)) {
2274 lnet_swap_pinginfo(info
);
2275 } else if (info
->pi_magic
!= LNET_PROTO_PING_MAGIC
) {
2276 CERROR("%s: Unexpected magic %08x\n",
2277 libcfs_id2str(id
), info
->pi_magic
);
2281 if (!(info
->pi_features
& LNET_PING_FEAT_NI_STATUS
)) {
2282 CERROR("%s: ping w/o NI status: 0x%x\n",
2283 libcfs_id2str(id
), info
->pi_features
);
2287 if (nob
< offsetof(lnet_ping_info_t
, pi_ni
[0])) {
2288 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id
),
2289 nob
, (int)offsetof(lnet_ping_info_t
, pi_ni
[0]));
2293 if (info
->pi_nnis
< n_ids
)
2294 n_ids
= info
->pi_nnis
;
2296 if (nob
< offsetof(lnet_ping_info_t
, pi_ni
[n_ids
])) {
2297 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id
),
2298 nob
, (int)offsetof(lnet_ping_info_t
, pi_ni
[n_ids
]));
2302 rc
= -EFAULT
; /* If I SEGV... */
2304 memset(&tmpid
, 0, sizeof(tmpid
));
2305 for (i
= 0; i
< n_ids
; i
++) {
2306 tmpid
.pid
= info
->pi_pid
;
2307 tmpid
.nid
= info
->pi_ni
[i
].ns_nid
;
2308 if (copy_to_user(&ids
[i
], &tmpid
, sizeof(tmpid
)))
2314 rc2
= LNetEQFree(eqh
);
2316 CERROR("rc2 %d\n", rc2
);
2320 LIBCFS_FREE(info
, infosz
);