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, 2012, 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 "../../include/linux/lnet/lib-lnet.h"
39 #include <linux/log2.h>
41 #define D_LNI D_CONSOLE
43 lnet_t the_lnet; /* THE state of the network */
44 EXPORT_SYMBOL(the_lnet);
47 static char *ip2nets = "";
48 module_param(ip2nets, charp, 0444);
49 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
51 static char *networks = "";
52 module_param(networks, charp, 0444);
53 MODULE_PARM_DESC(networks, "local networks");
55 static char *routes = "";
56 module_param(routes, charp, 0444);
57 MODULE_PARM_DESC(routes, "routes to non-local networks");
59 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
60 module_param(rnet_htable_size, int, 0444);
61 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
70 lnet_get_networks(void)
75 if (*networks != 0 && *ip2nets != 0) {
76 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
77 "'ip2nets' but not both at once\n");
82 rc = lnet_parse_ip2nets(&nets, ip2nets);
83 return (rc == 0) ? nets : NULL;
95 spin_lock_init(&the_lnet.ln_eq_wait_lock);
96 init_waitqueue_head(&the_lnet.ln_eq_waitq);
97 mutex_init(&the_lnet.ln_lnd_mutex);
98 mutex_init(&the_lnet.ln_api_mutex);
102 lnet_fini_locks(void)
108 lnet_create_remote_nets_table(void)
111 struct list_head *hash;
113 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
114 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
115 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
117 CERROR("Failed to create remote nets hash table\n");
121 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
122 INIT_LIST_HEAD(&hash[i]);
123 the_lnet.ln_remote_nets_hash = hash;
128 lnet_destroy_remote_nets_table(void)
132 if (the_lnet.ln_remote_nets_hash == NULL)
135 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
136 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
138 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
139 LNET_REMOTE_NETS_HASH_SIZE *
140 sizeof(the_lnet.ln_remote_nets_hash[0]));
141 the_lnet.ln_remote_nets_hash = NULL;
145 lnet_destroy_locks(void)
147 if (the_lnet.ln_res_lock != NULL) {
148 cfs_percpt_lock_free(the_lnet.ln_res_lock);
149 the_lnet.ln_res_lock = NULL;
152 if (the_lnet.ln_net_lock != NULL) {
153 cfs_percpt_lock_free(the_lnet.ln_net_lock);
154 the_lnet.ln_net_lock = NULL;
161 lnet_create_locks(void)
165 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
166 if (the_lnet.ln_res_lock == NULL)
169 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
170 if (the_lnet.ln_net_lock == NULL)
176 lnet_destroy_locks();
180 static void lnet_assert_wire_constants (void)
182 /* Wire protocol assertions generated by 'wirecheck'
183 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
184 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
185 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
188 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
189 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
190 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
191 CLASSERT (LNET_MSG_ACK == 0);
192 CLASSERT (LNET_MSG_PUT == 1);
193 CLASSERT (LNET_MSG_GET == 2);
194 CLASSERT (LNET_MSG_REPLY == 3);
195 CLASSERT (LNET_MSG_HELLO == 4);
197 /* Checks for struct ptl_handle_wire_t */
198 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
199 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
200 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
201 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
202 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
204 /* Checks for struct lnet_magicversion_t */
205 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
206 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
207 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
208 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
209 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
210 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
211 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
213 /* Checks for struct lnet_hdr_t */
214 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
215 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
216 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
217 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
218 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
219 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
220 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
221 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
222 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
223 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
224 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
225 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
226 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
227 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
228 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
231 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
232 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
233 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
234 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
235 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
236 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
239 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
240 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
241 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
242 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
243 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
244 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
245 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
246 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
247 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
248 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
251 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
252 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
253 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
254 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
255 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
256 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
257 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
258 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
259 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
260 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
263 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
264 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
267 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
268 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
269 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
270 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
274 lnet_find_lnd_by_type (int type)
277 struct list_head *tmp;
279 /* holding lnd mutex */
280 list_for_each (tmp, &the_lnet.ln_lnds) {
281 lnd = list_entry(tmp, lnd_t, lnd_list);
283 if ((int)lnd->lnd_type == type)
291 lnet_register_lnd (lnd_t *lnd)
293 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
295 LASSERT (the_lnet.ln_init);
296 LASSERT (libcfs_isknown_lnd(lnd->lnd_type));
297 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
299 list_add_tail (&lnd->lnd_list, &the_lnet.ln_lnds);
300 lnd->lnd_refcount = 0;
302 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
304 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
306 EXPORT_SYMBOL(lnet_register_lnd);
309 lnet_unregister_lnd (lnd_t *lnd)
311 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
313 LASSERT (the_lnet.ln_init);
314 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
315 LASSERT (lnd->lnd_refcount == 0);
317 list_del (&lnd->lnd_list);
318 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
320 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
322 EXPORT_SYMBOL(lnet_unregister_lnd);
325 lnet_counters_get(lnet_counters_t *counters)
327 lnet_counters_t *ctr;
330 memset(counters, 0, sizeof(*counters));
332 lnet_net_lock(LNET_LOCK_EX);
334 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
335 counters->msgs_max += ctr->msgs_max;
336 counters->msgs_alloc += ctr->msgs_alloc;
337 counters->errors += ctr->errors;
338 counters->send_count += ctr->send_count;
339 counters->recv_count += ctr->recv_count;
340 counters->route_count += ctr->route_count;
341 counters->drop_count += ctr->drop_count;
342 counters->send_length += ctr->send_length;
343 counters->recv_length += ctr->recv_length;
344 counters->route_length += ctr->route_length;
345 counters->drop_length += ctr->drop_length;
348 lnet_net_unlock(LNET_LOCK_EX);
350 EXPORT_SYMBOL(lnet_counters_get);
353 lnet_counters_reset(void)
355 lnet_counters_t *counters;
358 lnet_net_lock(LNET_LOCK_EX);
360 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
361 memset(counters, 0, sizeof(lnet_counters_t));
363 lnet_net_unlock(LNET_LOCK_EX);
365 EXPORT_SYMBOL(lnet_counters_reset);
367 #ifdef LNET_USE_LIB_FREELIST
370 lnet_freelist_init (lnet_freelist_t *fl, int n, int size)
376 size += offsetof (lnet_freeobj_t, fo_contents);
378 LIBCFS_ALLOC(space, n * size);
382 INIT_LIST_HEAD (&fl->fl_list);
385 fl->fl_objsize = size;
388 memset (space, 0, size);
389 list_add ((struct list_head *)space, &fl->fl_list);
397 lnet_freelist_fini (lnet_freelist_t *fl)
399 struct list_head *el;
402 if (fl->fl_nobjs == 0)
406 for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
409 LASSERT (count == fl->fl_nobjs);
411 LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
412 memset (fl, 0, sizeof (*fl));
415 #endif /* LNET_USE_LIB_FREELIST */
418 lnet_create_interface_cookie (void)
420 /* NB the interface cookie in wire handles guards against delayed
421 * replies and ACKs appearing valid after reboot. Initialisation time,
422 * even if it's only implemented to millisecond resolution is probably
423 * easily good enough. */
426 do_gettimeofday(&tv);
429 cookie += tv.tv_usec;
434 lnet_res_type2str(int type)
439 case LNET_COOKIE_TYPE_MD:
441 case LNET_COOKIE_TYPE_ME:
443 case LNET_COOKIE_TYPE_EQ:
449 lnet_res_container_cleanup(struct lnet_res_container *rec)
453 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
456 while (!list_empty(&rec->rec_active)) {
457 struct list_head *e = rec->rec_active.next;
460 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
461 lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
463 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
464 lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
466 } else { /* NB: Active MEs should be attached on portals */
473 /* Found alive MD/ME/EQ, user really should unlink/free
474 * all of them before finalize LNet, but if someone didn't,
475 * we have to recycle garbage for him */
476 CERROR("%d active elements on exit of %s container\n",
477 count, lnet_res_type2str(rec->rec_type));
480 #ifdef LNET_USE_LIB_FREELIST
481 lnet_freelist_fini(&rec->rec_freelist);
483 if (rec->rec_lh_hash != NULL) {
484 LIBCFS_FREE(rec->rec_lh_hash,
485 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
486 rec->rec_lh_hash = NULL;
489 rec->rec_type = 0; /* mark it as finalized */
493 lnet_res_container_setup(struct lnet_res_container *rec,
494 int cpt, int type, int objnum, int objsz)
499 LASSERT(rec->rec_type == 0);
501 rec->rec_type = type;
502 INIT_LIST_HEAD(&rec->rec_active);
504 #ifdef LNET_USE_LIB_FREELIST
505 memset(&rec->rec_freelist, 0, sizeof(rec->rec_freelist));
506 rc = lnet_freelist_init(&rec->rec_freelist, objnum, objsz);
510 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
512 /* Arbitrary choice of hash table size */
513 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
514 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
515 if (rec->rec_lh_hash == NULL) {
520 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
521 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
526 CERROR("Failed to setup %s resource container\n",
527 lnet_res_type2str(type));
528 lnet_res_container_cleanup(rec);
533 lnet_res_containers_destroy(struct lnet_res_container **recs)
535 struct lnet_res_container *rec;
538 cfs_percpt_for_each(rec, i, recs)
539 lnet_res_container_cleanup(rec);
541 cfs_percpt_free(recs);
544 static struct lnet_res_container **
545 lnet_res_containers_create(int type, int objnum, int objsz)
547 struct lnet_res_container **recs;
548 struct lnet_res_container *rec;
552 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
554 CERROR("Failed to allocate %s resource containers\n",
555 lnet_res_type2str(type));
559 cfs_percpt_for_each(rec, i, recs) {
560 rc = lnet_res_container_setup(rec, i, type, objnum, objsz);
562 lnet_res_containers_destroy(recs);
571 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
573 /* ALWAYS called with lnet_res_lock held */
574 struct list_head *head;
575 lnet_libhandle_t *lh;
578 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
581 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
582 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
584 list_for_each_entry(lh, head, lh_hash_chain) {
585 if (lh->lh_cookie == cookie)
593 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
595 /* ALWAYS called with lnet_res_lock held */
596 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
599 lh->lh_cookie = rec->rec_lh_cookie;
600 rec->rec_lh_cookie += 1 << ibits;
602 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
604 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
608 int lnet_unprepare(void);
611 lnet_prepare(lnet_pid_t requested_pid)
613 /* Prepare to bring up the network */
614 struct lnet_res_container **recs;
617 LASSERT (the_lnet.ln_refcount == 0);
619 the_lnet.ln_routing = 0;
621 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
622 the_lnet.ln_pid = requested_pid;
624 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
625 INIT_LIST_HEAD(&the_lnet.ln_nis);
626 INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
627 INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
628 INIT_LIST_HEAD(&the_lnet.ln_routers);
630 rc = lnet_create_remote_nets_table();
634 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
636 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
637 sizeof(lnet_counters_t));
638 if (the_lnet.ln_counters == NULL) {
639 CERROR("Failed to allocate counters for LNet\n");
644 rc = lnet_peer_tables_create();
648 rc = lnet_msg_containers_create();
652 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
653 LNET_COOKIE_TYPE_EQ, LNET_FL_MAX_EQS,
658 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME, LNET_FL_MAX_MES,
663 the_lnet.ln_me_containers = recs;
665 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD, LNET_FL_MAX_MDS,
666 sizeof(lnet_libmd_t));
670 the_lnet.ln_md_containers = recs;
672 rc = lnet_portals_create();
674 CERROR("Failed to create portals for LNet: %d\n", rc);
686 lnet_unprepare (void)
688 /* NB no LNET_LOCK since this is the last reference. All LND instances
689 * have shut down already, so it is safe to unlink and free all
690 * descriptors, even those that appear committed to a network op (eg MD
691 * with non-zero pending count) */
693 lnet_fail_nid(LNET_NID_ANY, 0);
695 LASSERT(the_lnet.ln_refcount == 0);
696 LASSERT(list_empty(&the_lnet.ln_test_peers));
697 LASSERT(list_empty(&the_lnet.ln_nis));
698 LASSERT(list_empty(&the_lnet.ln_nis_cpt));
699 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
701 lnet_portals_destroy();
703 if (the_lnet.ln_md_containers != NULL) {
704 lnet_res_containers_destroy(the_lnet.ln_md_containers);
705 the_lnet.ln_md_containers = NULL;
708 if (the_lnet.ln_me_containers != NULL) {
709 lnet_res_containers_destroy(the_lnet.ln_me_containers);
710 the_lnet.ln_me_containers = NULL;
713 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
715 lnet_msg_containers_destroy();
716 lnet_peer_tables_destroy();
717 lnet_rtrpools_free();
719 if (the_lnet.ln_counters != NULL) {
720 cfs_percpt_free(the_lnet.ln_counters);
721 the_lnet.ln_counters = NULL;
723 lnet_destroy_remote_nets_table();
729 lnet_net2ni_locked(__u32 net, int cpt)
731 struct list_head *tmp;
734 LASSERT(cpt != LNET_LOCK_EX);
736 list_for_each(tmp, &the_lnet.ln_nis) {
737 ni = list_entry(tmp, lnet_ni_t, ni_list);
739 if (LNET_NIDNET(ni->ni_nid) == net) {
740 lnet_ni_addref_locked(ni, cpt);
749 lnet_net2ni(__u32 net)
754 ni = lnet_net2ni_locked(net, 0);
759 EXPORT_SYMBOL(lnet_net2ni);
762 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
767 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
772 val = hash_long(key, LNET_CPT_BITS);
773 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
777 return (unsigned int)(key + val + (val >> 1)) % number;
781 lnet_cpt_of_nid_locked(lnet_nid_t nid)
785 /* must called with hold of lnet_net_lock */
786 if (LNET_CPT_NUMBER == 1)
787 return 0; /* the only one */
789 /* take lnet_net_lock(any) would be OK */
790 if (!list_empty(&the_lnet.ln_nis_cpt)) {
791 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
792 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
795 LASSERT(ni->ni_cpts != NULL);
796 return ni->ni_cpts[lnet_nid_cpt_hash
797 (nid, ni->ni_ncpts)];
801 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
805 lnet_cpt_of_nid(lnet_nid_t nid)
810 if (LNET_CPT_NUMBER == 1)
811 return 0; /* the only one */
813 if (list_empty(&the_lnet.ln_nis_cpt))
814 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
816 cpt = lnet_net_lock_current();
817 cpt2 = lnet_cpt_of_nid_locked(nid);
818 lnet_net_unlock(cpt);
822 EXPORT_SYMBOL(lnet_cpt_of_nid);
825 lnet_islocalnet(__u32 net)
830 cpt = lnet_net_lock_current();
832 ni = lnet_net2ni_locked(net, cpt);
834 lnet_ni_decref_locked(ni, cpt);
836 lnet_net_unlock(cpt);
842 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
845 struct list_head *tmp;
847 LASSERT(cpt != LNET_LOCK_EX);
849 list_for_each(tmp, &the_lnet.ln_nis) {
850 ni = list_entry(tmp, lnet_ni_t, ni_list);
852 if (ni->ni_nid == nid) {
853 lnet_ni_addref_locked(ni, cpt);
862 lnet_islocalnid(lnet_nid_t nid)
867 cpt = lnet_net_lock_current();
868 ni = lnet_nid2ni_locked(nid, cpt);
870 lnet_ni_decref_locked(ni, cpt);
871 lnet_net_unlock(cpt);
877 lnet_count_acceptor_nis (void)
879 /* Return the # of NIs that need the acceptor. */
881 struct list_head *tmp;
885 cpt = lnet_net_lock_current();
886 list_for_each(tmp, &the_lnet.ln_nis) {
887 ni = list_entry(tmp, lnet_ni_t, ni_list);
889 if (ni->ni_lnd->lnd_accept != NULL)
893 lnet_net_unlock(cpt);
899 lnet_ni_tq_credits(lnet_ni_t *ni)
903 LASSERT(ni->ni_ncpts >= 1);
905 if (ni->ni_ncpts == 1)
906 return ni->ni_maxtxcredits;
908 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
909 credits = max(credits, 8 * ni->ni_peertxcredits);
910 credits = min(credits, ni->ni_maxtxcredits);
916 lnet_shutdown_lndnis (void)
922 /* NB called holding the global mutex */
924 /* All quiet on the API front */
925 LASSERT(!the_lnet.ln_shutdown);
926 LASSERT(the_lnet.ln_refcount == 0);
927 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
929 lnet_net_lock(LNET_LOCK_EX);
930 the_lnet.ln_shutdown = 1; /* flag shutdown */
932 /* Unlink NIs from the global table */
933 while (!list_empty(&the_lnet.ln_nis)) {
934 ni = list_entry(the_lnet.ln_nis.next,
936 /* move it to zombie list and nobody can find it anymore */
937 list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
938 lnet_ni_decref_locked(ni, 0); /* drop ln_nis' ref */
940 if (!list_empty(&ni->ni_cptlist)) {
941 list_del_init(&ni->ni_cptlist);
942 lnet_ni_decref_locked(ni, 0);
946 /* Drop the cached eqwait NI. */
947 if (the_lnet.ln_eq_waitni != NULL) {
948 lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
949 the_lnet.ln_eq_waitni = NULL;
952 /* Drop the cached loopback NI. */
953 if (the_lnet.ln_loni != NULL) {
954 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
955 the_lnet.ln_loni = NULL;
958 lnet_net_unlock(LNET_LOCK_EX);
960 /* Clear lazy portals and drop delayed messages which hold refs
961 * on their lnet_msg_t::msg_rxpeer */
962 for (i = 0; i < the_lnet.ln_nportals; i++)
963 LNetClearLazyPortal(i);
965 /* Clear the peer table and wait for all peers to go (they hold refs on
967 lnet_peer_tables_cleanup();
969 lnet_net_lock(LNET_LOCK_EX);
970 /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
971 * and shut them down in guaranteed thread context */
973 while (!list_empty(&the_lnet.ln_nis_zombie)) {
977 ni = list_entry(the_lnet.ln_nis_zombie.next,
979 list_del_init(&ni->ni_list);
980 cfs_percpt_for_each(ref, j, ni->ni_refs) {
983 /* still busy, add it back to zombie list */
984 list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
988 if (!list_empty(&ni->ni_list)) {
989 lnet_net_unlock(LNET_LOCK_EX);
991 if ((i & (-i)) == i) {
992 CDEBUG(D_WARNING, "Waiting for zombie LNI %s\n",
993 libcfs_nid2str(ni->ni_nid));
995 set_current_state(TASK_UNINTERRUPTIBLE);
996 schedule_timeout(cfs_time_seconds(1));
997 lnet_net_lock(LNET_LOCK_EX);
1001 ni->ni_lnd->lnd_refcount--;
1002 lnet_net_unlock(LNET_LOCK_EX);
1004 islo = ni->ni_lnd->lnd_type == LOLND;
1006 LASSERT (!in_interrupt ());
1007 (ni->ni_lnd->lnd_shutdown)(ni);
1009 /* can't deref lnd anymore now; it might have unregistered
1013 CDEBUG(D_LNI, "Removed LNI %s\n",
1014 libcfs_nid2str(ni->ni_nid));
1019 lnet_net_lock(LNET_LOCK_EX);
1022 the_lnet.ln_shutdown = 0;
1023 lnet_net_unlock(LNET_LOCK_EX);
1025 if (the_lnet.ln_network_tokens != NULL) {
1026 LIBCFS_FREE(the_lnet.ln_network_tokens,
1027 the_lnet.ln_network_tokens_nob);
1028 the_lnet.ln_network_tokens = NULL;
1033 lnet_startup_lndnis (void)
1037 struct lnet_tx_queue *tq;
1038 struct list_head nilist;
1043 char *nets = lnet_get_networks();
1045 INIT_LIST_HEAD(&nilist);
1050 rc = lnet_parse_networks(&nilist, nets);
1054 while (!list_empty(&nilist)) {
1055 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1056 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1058 LASSERT (libcfs_isknown_lnd(lnd_type));
1060 if (lnd_type == CIBLND ||
1061 lnd_type == OPENIBLND ||
1062 lnd_type == IIBLND ||
1063 lnd_type == VIBLND) {
1064 CERROR("LND %s obsoleted\n",
1065 libcfs_lnd2str(lnd_type));
1069 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1070 lnd = lnet_find_lnd_by_type(lnd_type);
1073 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1074 rc = request_module("%s",
1075 libcfs_lnd2modname(lnd_type));
1076 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1078 lnd = lnet_find_lnd_by_type(lnd_type);
1080 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1081 CERROR("Can't load LND %s, module %s, rc=%d\n",
1082 libcfs_lnd2str(lnd_type),
1083 libcfs_lnd2modname(lnd_type), rc);
1088 lnet_net_lock(LNET_LOCK_EX);
1089 lnd->lnd_refcount++;
1090 lnet_net_unlock(LNET_LOCK_EX);
1094 rc = (lnd->lnd_startup)(ni);
1096 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1099 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1101 rc, libcfs_lnd2str(lnd->lnd_type));
1102 lnet_net_lock(LNET_LOCK_EX);
1103 lnd->lnd_refcount--;
1104 lnet_net_unlock(LNET_LOCK_EX);
1108 LASSERT (ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1110 list_del(&ni->ni_list);
1112 lnet_net_lock(LNET_LOCK_EX);
1113 /* refcount for ln_nis */
1114 lnet_ni_addref_locked(ni, 0);
1115 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1116 if (ni->ni_cpts != NULL) {
1117 list_add_tail(&ni->ni_cptlist,
1118 &the_lnet.ln_nis_cpt);
1119 lnet_ni_addref_locked(ni, 0);
1122 lnet_net_unlock(LNET_LOCK_EX);
1124 if (lnd->lnd_type == LOLND) {
1126 LASSERT (the_lnet.ln_loni == NULL);
1127 the_lnet.ln_loni = ni;
1131 if (ni->ni_peertxcredits == 0 ||
1132 ni->ni_maxtxcredits == 0) {
1133 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1134 libcfs_lnd2str(lnd->lnd_type),
1135 ni->ni_peertxcredits == 0 ?
1140 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1141 tq->tq_credits_min =
1142 tq->tq_credits_max =
1143 tq->tq_credits = lnet_ni_tq_credits(ni);
1146 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1147 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1148 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1149 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1154 if (the_lnet.ln_eq_waitni != NULL && nicount > 1) {
1155 lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1156 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1158 libcfs_lnd2str(lnd_type));
1165 lnet_shutdown_lndnis();
1167 while (!list_empty(&nilist)) {
1168 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1169 list_del(&ni->ni_list);
1177 * Initialize LNet library.
1179 * Only userspace program needs to call this function - it's automatically
1180 * called in the kernel at module loading time. Caller has to call LNetFini()
1181 * after a call to LNetInit(), if and only if the latter returned 0. It must
1182 * be called exactly once.
1184 * \return 0 on success, and -ve on failures.
1191 lnet_assert_wire_constants();
1192 LASSERT(!the_lnet.ln_init);
1194 memset(&the_lnet, 0, sizeof(the_lnet));
1196 /* refer to global cfs_cpt_table for now */
1197 the_lnet.ln_cpt_table = cfs_cpt_table;
1198 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1200 LASSERT(the_lnet.ln_cpt_number > 0);
1201 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1202 /* we are under risk of consuming all lh_cookie */
1203 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1204 "please change setting of CPT-table and retry\n",
1205 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1209 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1210 the_lnet.ln_cpt_bits++;
1212 rc = lnet_create_locks();
1214 CERROR("Can't create LNet global locks: %d\n", rc);
1218 the_lnet.ln_refcount = 0;
1219 the_lnet.ln_init = 1;
1220 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1221 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1222 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1223 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1225 /* The hash table size is the number of bits it takes to express the set
1226 * ln_num_routes, minus 1 (better to under estimate than over so we
1227 * don't waste memory). */
1228 if (rnet_htable_size <= 0)
1229 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1230 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1231 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1232 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1233 order_base_2(rnet_htable_size) - 1);
1235 /* All LNDs apart from the LOLND are in separate modules. They
1236 * register themselves when their module loads, and unregister
1237 * themselves when their module is unloaded. */
1238 lnet_register_lnd(&the_lolnd);
1241 EXPORT_SYMBOL(LNetInit);
1244 * Finalize LNet library.
1246 * Only userspace program needs to call this function. It can be called
1249 * \pre LNetInit() called with success.
1250 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1255 LASSERT(the_lnet.ln_init);
1256 LASSERT(the_lnet.ln_refcount == 0);
1258 while (!list_empty(&the_lnet.ln_lnds))
1259 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1261 lnet_destroy_locks();
1263 the_lnet.ln_init = 0;
1265 EXPORT_SYMBOL(LNetFini);
1268 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1270 * Userspace program should call this after a successful call to LNetInit().
1271 * Users must call this function at least once before any other functions.
1272 * For each successful call there must be a corresponding call to
1273 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1276 * The PID used by LNet may be different from the one requested.
1279 * \param requested_pid PID requested by the caller.
1281 * \return >= 0 on success, and < 0 error code on failures.
1284 LNetNIInit(lnet_pid_t requested_pid)
1286 int im_a_router = 0;
1289 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1291 LASSERT (the_lnet.ln_init);
1292 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1294 if (the_lnet.ln_refcount > 0) {
1295 rc = the_lnet.ln_refcount++;
1299 lnet_get_tunables();
1301 if (requested_pid == LNET_PID_ANY) {
1302 /* Don't instantiate LNET just for me */
1307 rc = lnet_prepare(requested_pid);
1311 rc = lnet_startup_lndnis();
1315 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1319 rc = lnet_check_routes();
1323 rc = lnet_rtrpools_alloc(im_a_router);
1327 rc = lnet_acceptor_start();
1331 the_lnet.ln_refcount = 1;
1332 /* Now I may use my own API functions... */
1334 /* NB router checker needs the_lnet.ln_ping_info in
1335 * lnet_router_checker -> lnet_update_ni_status_locked */
1336 rc = lnet_ping_target_init();
1340 rc = lnet_router_checker_start();
1348 lnet_ping_target_fini();
1350 the_lnet.ln_refcount = 0;
1351 lnet_acceptor_stop();
1353 lnet_destroy_routes();
1354 lnet_shutdown_lndnis();
1360 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1363 EXPORT_SYMBOL(LNetNIInit);
1366 * Stop LNet interfaces, routing, and forwarding.
1368 * Users must call this function once for each successful call to LNetNIInit().
1369 * Once the LNetNIFini() operation has been started, the results of pending
1370 * API operations are undefined.
1372 * \return always 0 for current implementation.
1377 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1379 LASSERT (the_lnet.ln_init);
1380 LASSERT (the_lnet.ln_refcount > 0);
1382 if (the_lnet.ln_refcount != 1) {
1383 the_lnet.ln_refcount--;
1385 LASSERT (!the_lnet.ln_niinit_self);
1388 lnet_router_checker_stop();
1389 lnet_ping_target_fini();
1391 /* Teardown fns that use my own API functions BEFORE here */
1392 the_lnet.ln_refcount = 0;
1394 lnet_acceptor_stop();
1395 lnet_destroy_routes();
1396 lnet_shutdown_lndnis();
1400 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1403 EXPORT_SYMBOL(LNetNIFini);
1406 * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1407 * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1408 * internal ioctl handler.
1410 * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1412 * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1413 * The data will be printed to system console. Don't use it excessively.
1414 * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1416 * \return Always return 0 when called by users directly (i.e., not via ioctl).
1419 LNetCtl(unsigned int cmd, void *arg)
1421 struct libcfs_ioctl_data *data = arg;
1422 lnet_process_id_t id = {0};
1426 LASSERT (the_lnet.ln_init);
1427 LASSERT (the_lnet.ln_refcount > 0);
1430 case IOC_LIBCFS_GET_NI:
1431 rc = LNetGetId(data->ioc_count, &id);
1432 data->ioc_nid = id.nid;
1435 case IOC_LIBCFS_FAIL_NID:
1436 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1438 case IOC_LIBCFS_ADD_ROUTE:
1439 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1440 data->ioc_nid, data->ioc_priority);
1441 return (rc != 0) ? rc : lnet_check_routes();
1443 case IOC_LIBCFS_DEL_ROUTE:
1444 return lnet_del_route(data->ioc_net, data->ioc_nid);
1446 case IOC_LIBCFS_GET_ROUTE:
1447 return lnet_get_route(data->ioc_count,
1448 &data->ioc_net, &data->ioc_count,
1449 &data->ioc_nid, &data->ioc_flags,
1450 &data->ioc_priority);
1451 case IOC_LIBCFS_NOTIFY_ROUTER:
1452 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1453 cfs_time_current() -
1454 cfs_time_seconds(get_seconds() -
1455 (time_t)data->ioc_u64[0]));
1457 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1458 /* This can be removed once lustre stops calling it */
1461 case IOC_LIBCFS_LNET_DIST:
1462 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1463 if (rc < 0 && rc != -EHOSTUNREACH)
1466 data->ioc_u32[0] = rc;
1469 case IOC_LIBCFS_TESTPROTOCOMPAT:
1470 lnet_net_lock(LNET_LOCK_EX);
1471 the_lnet.ln_testprotocompat = data->ioc_flags;
1472 lnet_net_unlock(LNET_LOCK_EX);
1475 case IOC_LIBCFS_PING:
1476 id.nid = data->ioc_nid;
1477 id.pid = data->ioc_u32[0];
1478 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1479 (lnet_process_id_t *)data->ioc_pbuf1,
1480 data->ioc_plen1/sizeof(lnet_process_id_t));
1483 data->ioc_count = rc;
1486 case IOC_LIBCFS_DEBUG_PEER: {
1487 /* CAVEAT EMPTOR: this one designed for calling directly; not
1489 id = *((lnet_process_id_t *) arg);
1491 lnet_debug_peer(id.nid);
1493 ni = lnet_net2ni(LNET_NIDNET(id.nid));
1495 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1497 if (ni->ni_lnd->lnd_ctl == NULL) {
1498 CDEBUG(D_WARNING, "No ctl for %s\n",
1501 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1510 ni = lnet_net2ni(data->ioc_net);
1514 if (ni->ni_lnd->lnd_ctl == NULL)
1517 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1524 EXPORT_SYMBOL(LNetCtl);
1527 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
1528 * all interfaces share a same PID, as requested by LNetNIInit().
1530 * \param index Index of the interface to look up.
1531 * \param id On successful return, this location will hold the
1532 * lnet_process_id_t ID of the interface.
1534 * \retval 0 If an interface exists at \a index.
1535 * \retval -ENOENT If no interface has been found.
1538 LNetGetId(unsigned int index, lnet_process_id_t *id)
1541 struct list_head *tmp;
1545 LASSERT(the_lnet.ln_init);
1547 /* LNetNI initilization failed? */
1548 if (the_lnet.ln_refcount == 0)
1551 cpt = lnet_net_lock_current();
1553 list_for_each(tmp, &the_lnet.ln_nis) {
1557 ni = list_entry(tmp, lnet_ni_t, ni_list);
1559 id->nid = ni->ni_nid;
1560 id->pid = the_lnet.ln_pid;
1565 lnet_net_unlock(cpt);
1568 EXPORT_SYMBOL(LNetGetId);
1571 * Print a string representation of handle \a h into buffer \a str of
1575 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1577 snprintf(str, len, "%#llx", h.cookie);
1579 EXPORT_SYMBOL(LNetSnprintHandle);
1582 lnet_create_ping_info(void)
1587 unsigned int infosz;
1589 lnet_process_id_t id;
1590 lnet_ping_info_t *pinfo;
1592 for (n = 0; ; n++) {
1593 rc = LNetGetId(n, &id);
1600 infosz = offsetof(lnet_ping_info_t, pi_ni[n]);
1601 LIBCFS_ALLOC(pinfo, infosz);
1602 if (pinfo == NULL) {
1603 CERROR("Can't allocate ping info[%d]\n", n);
1608 pinfo->pi_pid = the_lnet.ln_pid;
1609 pinfo->pi_magic = LNET_PROTO_PING_MAGIC;
1610 pinfo->pi_features = LNET_PING_FEAT_NI_STATUS;
1612 for (i = 0; i < n; i++) {
1613 lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1615 rc = LNetGetId(i, &id);
1618 ns->ns_nid = id.nid;
1619 ns->ns_status = LNET_NI_STATUS_UP;
1623 ni = lnet_nid2ni_locked(id.nid, 0);
1624 LASSERT(ni != NULL);
1627 LASSERT(ni->ni_status == NULL);
1631 lnet_ni_decref_locked(ni, 0);
1635 the_lnet.ln_ping_info = pinfo;
1640 lnet_destroy_ping_info(void)
1646 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1648 ni->ni_status = NULL;
1654 LIBCFS_FREE(the_lnet.ln_ping_info,
1655 offsetof(lnet_ping_info_t,
1656 pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1657 the_lnet.ln_ping_info = NULL;
1662 lnet_ping_target_init(void)
1664 lnet_md_t md = { NULL };
1665 lnet_handle_me_t meh;
1666 lnet_process_id_t id;
1671 rc = lnet_create_ping_info();
1675 /* We can have a tiny EQ since we only need to see the unlink event on
1676 * teardown, which by definition is the last one! */
1677 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1679 CERROR("Can't allocate ping EQ: %d\n", rc);
1683 memset(&id, 0, sizeof(lnet_process_id_t));
1684 id.nid = LNET_NID_ANY;
1685 id.pid = LNET_PID_ANY;
1687 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1688 LNET_PROTO_PING_MATCHBITS, 0,
1689 LNET_UNLINK, LNET_INS_AFTER,
1692 CERROR("Can't create ping ME: %d\n", rc);
1696 /* initialize md content */
1697 infosz = offsetof(lnet_ping_info_t,
1698 pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1699 md.start = the_lnet.ln_ping_info;
1701 md.threshold = LNET_MD_THRESH_INF;
1703 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1704 LNET_MD_MANAGE_REMOTE;
1706 md.eq_handle = the_lnet.ln_ping_target_eq;
1708 rc = LNetMDAttach(meh, md,
1710 &the_lnet.ln_ping_target_md);
1712 CERROR("Can't attach ping MD: %d\n", rc);
1719 rc2 = LNetMEUnlink(meh);
1722 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1725 lnet_destroy_ping_info();
1730 lnet_ping_target_fini(void)
1735 int timeout_ms = 1000;
1736 sigset_t blocked = cfs_block_allsigs();
1738 LNetMDUnlink(the_lnet.ln_ping_target_md);
1739 /* NB md could be busy; this just starts the unlink */
1742 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1743 timeout_ms, &event, &which);
1745 /* I expect overflow... */
1746 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1749 /* timed out: provide a diagnostic */
1750 CWARN("Still waiting for ping MD to unlink\n");
1755 /* Got a valid event */
1760 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1762 lnet_destroy_ping_info();
1763 cfs_restore_sigs(blocked);
1767 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1769 lnet_handle_eq_t eqh;
1770 lnet_handle_md_t mdh;
1772 lnet_md_t md = { NULL };
1776 const int a_long_time = 60000; /* mS */
1777 int infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1778 lnet_ping_info_t *info;
1779 lnet_process_id_t tmpid;
1787 id.nid == LNET_NID_ANY ||
1788 timeout_ms > 500000 || /* arbitrary limit! */
1789 n_ids > 20) /* arbitrary limit! */
1792 if (id.pid == LNET_PID_ANY)
1793 id.pid = LUSTRE_SRV_LNET_PID;
1795 LIBCFS_ALLOC(info, infosz);
1799 /* NB 2 events max (including any unlink event) */
1800 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1802 CERROR("Can't allocate EQ: %d\n", rc);
1806 /* initialize md content */
1809 md.threshold = 2; /*GET/REPLY*/
1811 md.options = LNET_MD_TRUNCATE;
1815 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1817 CERROR("Can't bind MD: %d\n", rc);
1821 rc = LNetGet(LNET_NID_ANY, mdh, id,
1822 LNET_RESERVED_PORTAL,
1823 LNET_PROTO_PING_MATCHBITS, 0);
1826 /* Don't CERROR; this could be deliberate! */
1828 rc2 = LNetMDUnlink(mdh);
1831 /* NB must wait for the UNLINK event below... */
1833 timeout_ms = a_long_time;
1837 /* MUST block for unlink to complete */
1839 blocked = cfs_block_allsigs();
1841 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1844 cfs_restore_sigs(blocked);
1846 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1847 (rc2 <= 0) ? -1 : event.type,
1848 (rc2 <= 0) ? -1 : event.status,
1849 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1851 LASSERT (rc2 != -EOVERFLOW); /* can't miss anything */
1853 if (rc2 <= 0 || event.status != 0) {
1854 /* timeout or error */
1855 if (!replied && rc == 0)
1856 rc = (rc2 < 0) ? rc2 :
1857 (rc2 == 0) ? -ETIMEDOUT :
1861 /* Ensure completion in finite time... */
1863 /* No assertion (racing with network) */
1865 timeout_ms = a_long_time;
1866 } else if (rc2 == 0) {
1867 /* timed out waiting for unlink */
1868 CWARN("ping %s: late network completion\n",
1871 } else if (event.type == LNET_EVENT_REPLY) {
1876 } while (rc2 <= 0 || !event.unlinked);
1880 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
1887 LASSERT (nob >= 0 && nob <= infosz);
1889 rc = -EPROTO; /* if I can't parse... */
1892 /* can't check magic/version */
1893 CERROR("%s: ping info too short %d\n",
1894 libcfs_id2str(id), nob);
1898 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
1899 lnet_swap_pinginfo(info);
1900 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
1901 CERROR("%s: Unexpected magic %08x\n",
1902 libcfs_id2str(id), info->pi_magic);
1906 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
1907 CERROR("%s: ping w/o NI status: 0x%x\n",
1908 libcfs_id2str(id), info->pi_features);
1912 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
1913 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
1914 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
1918 if (info->pi_nnis < n_ids)
1919 n_ids = info->pi_nnis;
1921 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
1922 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
1923 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
1927 rc = -EFAULT; /* If I SEGV... */
1929 memset(&tmpid, 0, sizeof(tmpid));
1930 for (i = 0; i < n_ids; i++) {
1931 tmpid.pid = info->pi_pid;
1932 tmpid.nid = info->pi_ni[i].ns_nid;
1933 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
1939 rc2 = LNetEQFree(eqh);
1941 CERROR("rc2 %d\n", rc2);
1945 LIBCFS_FREE(info, infosz);