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, Whamcloud, Inc.
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 <lnet/lib-lnet.h>
41 #define D_LNI D_CONSOLE
43 #define D_LNI D_CONFIG
46 lnet_t the_lnet; /* THE state of the network */
50 static char *ip2nets = "";
51 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
52 "LNET network <- IP table");
54 static char *networks = "";
55 CFS_MODULE_PARM(networks, "s", charp, 0444,
58 static char *routes = "";
59 CFS_MODULE_PARM(routes, "s", charp, 0444,
60 "routes to non-local networks");
69 lnet_get_networks(void)
74 if (*networks != 0 && *ip2nets != 0) {
75 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
76 "'ip2nets' but not both at once\n");
81 rc = lnet_parse_ip2nets(&nets, ip2nets);
82 return (rc == 0) ? nets : NULL;
94 cfs_spin_lock_init (&the_lnet.ln_lock);
95 cfs_waitq_init (&the_lnet.ln_waitq);
96 cfs_mutex_init(&the_lnet.ln_lnd_mutex);
97 cfs_mutex_init(&the_lnet.ln_api_mutex);
101 lnet_fini_locks(void)
108 lnet_get_routes(void)
110 char *str = getenv("LNET_ROUTES");
112 return (str == NULL) ? "" : str;
116 lnet_get_networks (void)
118 static char default_networks[256];
119 char *networks = getenv ("LNET_NETWORKS");
120 char *ip2nets = getenv ("LNET_IP2NETS");
129 if (networks != NULL && ip2nets != NULL) {
130 LCONSOLE_ERROR_MSG(0x103, "Please set EITHER 'LNET_NETWORKS' or"
131 " 'LNET_IP2NETS' but not both at once\n");
135 if (ip2nets != NULL) {
136 rc = lnet_parse_ip2nets(&networks, ip2nets);
137 return (rc == 0) ? networks : NULL;
140 SET_BUT_UNUSED(ip2nets);
143 if (networks != NULL)
146 /* In userland, the default 'networks=' is the list of known net types */
148 len = sizeof(default_networks);
149 str = default_networks;
153 cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
154 lnd_t *lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
156 nob = snprintf(str, len, "%s%s", sep,
157 libcfs_lnd2str(lnd->lnd_type));
160 /* overflowed the string; leave it where it was */
169 return default_networks;
172 # ifndef HAVE_LIBPTHREAD
174 void lnet_init_locks(void)
176 the_lnet.ln_lock = 0;
177 the_lnet.ln_lnd_mutex = 0;
178 the_lnet.ln_api_mutex = 0;
181 void lnet_fini_locks(void)
183 LASSERT (the_lnet.ln_api_mutex == 0);
184 LASSERT (the_lnet.ln_lnd_mutex == 0);
185 LASSERT (the_lnet.ln_lock == 0);
190 void lnet_init_locks(void)
192 pthread_cond_init(&the_lnet.ln_cond, NULL);
193 pthread_mutex_init(&the_lnet.ln_lock, NULL);
194 pthread_mutex_init(&the_lnet.ln_lnd_mutex, NULL);
195 pthread_mutex_init(&the_lnet.ln_api_mutex, NULL);
198 void lnet_fini_locks(void)
200 pthread_mutex_destroy(&the_lnet.ln_api_mutex);
201 pthread_mutex_destroy(&the_lnet.ln_lnd_mutex);
202 pthread_mutex_destroy(&the_lnet.ln_lock);
203 pthread_cond_destroy(&the_lnet.ln_cond);
209 void lnet_assert_wire_constants (void)
211 /* Wire protocol assertions generated by 'wirecheck'
212 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
213 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
214 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
217 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
218 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
219 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
220 CLASSERT (LNET_MSG_ACK == 0);
221 CLASSERT (LNET_MSG_PUT == 1);
222 CLASSERT (LNET_MSG_GET == 2);
223 CLASSERT (LNET_MSG_REPLY == 3);
224 CLASSERT (LNET_MSG_HELLO == 4);
226 /* Checks for struct ptl_handle_wire_t */
227 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
228 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
229 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
230 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
231 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
233 /* Checks for struct lnet_magicversion_t */
234 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
235 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
236 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
237 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
238 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
239 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
240 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
242 /* Checks for struct lnet_hdr_t */
243 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
244 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
245 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
246 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
247 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
248 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
249 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
250 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
251 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
252 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
253 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
254 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
255 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
256 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
257 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
260 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
261 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
262 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
263 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
264 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
265 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
268 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
269 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
270 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
271 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
272 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
273 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
274 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
275 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
276 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
277 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
280 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
281 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
282 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
283 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
284 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
285 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
286 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
287 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
288 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
289 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
292 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
293 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
296 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
297 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
298 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
299 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
303 lnet_find_lnd_by_type (int type)
308 /* holding lnd mutex */
309 cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
310 lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
312 if ((int)lnd->lnd_type == type)
320 lnet_register_lnd (lnd_t *lnd)
322 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
324 LASSERT (the_lnet.ln_init);
325 LASSERT (libcfs_isknown_lnd(lnd->lnd_type));
326 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
328 cfs_list_add_tail (&lnd->lnd_list, &the_lnet.ln_lnds);
329 lnd->lnd_refcount = 0;
331 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
333 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
337 lnet_unregister_lnd (lnd_t *lnd)
339 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
341 LASSERT (the_lnet.ln_init);
342 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
343 LASSERT (lnd->lnd_refcount == 0);
345 cfs_list_del (&lnd->lnd_list);
346 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
348 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
351 #ifndef LNET_USE_LIB_FREELIST
354 lnet_descriptor_setup (void)
360 lnet_descriptor_cleanup (void)
367 lnet_freelist_init (lnet_freelist_t *fl, int n, int size)
373 size += offsetof (lnet_freeobj_t, fo_contents);
375 LIBCFS_ALLOC(space, n * size);
379 CFS_INIT_LIST_HEAD (&fl->fl_list);
382 fl->fl_objsize = size;
386 memset (space, 0, size);
387 cfs_list_add ((cfs_list_t *)space, &fl->fl_list);
395 lnet_freelist_fini (lnet_freelist_t *fl)
400 if (fl->fl_nobjs == 0)
404 for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
407 LASSERT (count == fl->fl_nobjs);
409 LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
410 memset (fl, 0, sizeof (*fl));
414 lnet_descriptor_setup (void)
416 /* NB on failure caller must still call lnet_descriptor_cleanup */
420 memset (&the_lnet.ln_free_msgs, 0, sizeof (the_lnet.ln_free_msgs));
421 rc = lnet_freelist_init(&the_lnet.ln_free_msgs,
422 LNET_FL_MAX_MSGS, sizeof(lnet_msg_t));
429 lnet_descriptor_cleanup (void)
431 lnet_freelist_fini (&the_lnet.ln_free_msgs);
434 #endif /* LNET_USE_LIB_FREELIST */
437 lnet_create_interface_cookie (void)
439 /* NB the interface cookie in wire handles guards against delayed
440 * replies and ACKs appearing valid after reboot. Initialisation time,
441 * even if it's only implemented to millisecond resolution is probably
442 * easily good enough. */
446 int rc = gettimeofday (&tv, NULL);
449 cfs_gettimeofday(&tv);
453 cookie += tv.tv_usec;
458 lnet_res_type2str(int type)
463 case LNET_COOKIE_TYPE_MD:
465 case LNET_COOKIE_TYPE_ME:
467 case LNET_COOKIE_TYPE_EQ:
473 lnet_res_container_cleanup(struct lnet_res_container *rec)
477 if (rec->rec_type == 0) /* not set yet, it's a uninitialized */
480 while (!cfs_list_empty(&rec->rec_active)) {
481 cfs_list_t *e = rec->rec_active.next;
483 cfs_list_del_init(e);
484 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
485 lnet_eq_free(cfs_list_entry(e, lnet_eq_t, eq_list));
487 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
488 lnet_md_free(cfs_list_entry(e, lnet_libmd_t, md_list));
490 } else { /* NB: Active MEs should be attached on portals */
497 /* Found alive MD/ME/EQ, user really should unlink/free
498 * all of them before finalize LNet, but if someone didn't,
499 * we have to recycle garbage for him */
500 CERROR("%d active elements on exit of %s container\n",
501 count, lnet_res_type2str(rec->rec_type));
504 #ifdef LNET_USE_LIB_FREELIST
505 lnet_freelist_fini(&rec->rec_freelist);
507 if (rec->rec_lh_hash != NULL) {
508 LIBCFS_FREE(rec->rec_lh_hash,
509 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
510 rec->rec_lh_hash = NULL;
513 rec->rec_type = 0; /* mark it as finalized */
517 lnet_res_container_setup(struct lnet_res_container *rec,
518 int type, int objnum, int objsz)
523 LASSERT(rec->rec_type == 0);
525 rec->rec_type = type;
526 CFS_INIT_LIST_HEAD(&rec->rec_active);
528 #ifdef LNET_USE_LIB_FREELIST
529 memset(&rec->rec_freelist, 0, sizeof(rec->rec_freelist));
530 rc = lnet_freelist_init(&rec->rec_freelist, objnum, objsz);
534 rec->rec_lh_cookie = type;
536 /* Arbitrary choice of hash table size */
537 LIBCFS_ALLOC(rec->rec_lh_hash,
538 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
539 if (rec->rec_lh_hash == NULL) {
544 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
545 CFS_INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
550 CERROR("Failed to setup %s resource container\n",
551 lnet_res_type2str(type));
552 lnet_res_container_cleanup(rec);
557 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
559 /* ALWAYS called with lnet_res_lock held */
561 lnet_libhandle_t *lh;
564 if ((cookie & (LNET_COOKIE_TYPES - 1)) != rec->rec_type)
567 hash = cookie >> LNET_COOKIE_TYPE_BITS;
568 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
570 cfs_list_for_each_entry(lh, head, lh_hash_chain) {
571 if (lh->lh_cookie == cookie)
579 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
581 /* ALWAYS called with lnet_res_lock held */
582 unsigned int ibits = LNET_COOKIE_TYPE_BITS;
585 lh->lh_cookie = rec->rec_lh_cookie;
586 rec->rec_lh_cookie += 1 << ibits;
588 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
590 cfs_list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
594 lnet_portal_mhash_alloc(void)
599 LIBCFS_ALLOC(mhash, sizeof(cfs_list_t) * LNET_PORTAL_HASH_SIZE);
603 for (i = 0; i < LNET_PORTAL_HASH_SIZE; i++)
604 CFS_INIT_LIST_HEAD(&mhash[i]);
610 lnet_portal_mhash_free(cfs_list_t *mhash)
614 for (i = 0; i < LNET_PORTAL_HASH_SIZE; i++) {
615 while (!cfs_list_empty(&mhash[i])) {
616 lnet_me_t *me = cfs_list_entry(mhash[i].next,
618 CERROR ("Active ME %p on exit portal mhash\n", me);
619 cfs_list_del(&me->me_list);
623 LIBCFS_FREE(mhash, sizeof(cfs_list_t) * LNET_PORTAL_HASH_SIZE);
627 lnet_init_finalizers(void)
632 the_lnet.ln_nfinalizers = (int) cfs_num_online_cpus();
634 LIBCFS_ALLOC(the_lnet.ln_finalizers,
635 the_lnet.ln_nfinalizers *
636 sizeof(*the_lnet.ln_finalizers));
637 if (the_lnet.ln_finalizers == NULL) {
638 CERROR("Can't allocate ln_finalizers\n");
642 for (i = 0; i < the_lnet.ln_nfinalizers; i++)
643 the_lnet.ln_finalizers[i] = NULL;
645 the_lnet.ln_finalizing = 0;
648 CFS_INIT_LIST_HEAD(&the_lnet.ln_finalizeq);
653 lnet_fini_finalizers(void)
658 for (i = 0; i < the_lnet.ln_nfinalizers; i++)
659 LASSERT (the_lnet.ln_finalizers[i] == NULL);
661 LIBCFS_FREE(the_lnet.ln_finalizers,
662 the_lnet.ln_nfinalizers *
663 sizeof(*the_lnet.ln_finalizers));
665 LASSERT (!the_lnet.ln_finalizing);
667 LASSERT (cfs_list_empty(&the_lnet.ln_finalizeq));
672 * Reserved API - do not use.
673 * Temporary workaround to allow uOSS and test programs force server
674 * mode in userspace. See comments near ln_server_mode_flag in
675 * lnet/lib-types.h */
679 the_lnet.ln_server_mode_flag = 1;
684 lnet_prepare(lnet_pid_t requested_pid)
686 /* Prepare to bring up the network */
690 LASSERT (the_lnet.ln_refcount == 0);
692 the_lnet.ln_routing = 0;
695 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
696 the_lnet.ln_pid = requested_pid;
698 if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
699 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
701 if (cfs_curproc_uid())/* Only root can run user-space server */
703 the_lnet.ln_pid = requested_pid;
705 } else {/* client case (liblustre) */
707 /* My PID must be unique on this node and flag I'm userspace */
708 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
712 rc = lnet_descriptor_setup();
716 memset(&the_lnet.ln_counters, 0,
717 sizeof(the_lnet.ln_counters));
719 CFS_INIT_LIST_HEAD (&the_lnet.ln_active_msgs);
720 CFS_INIT_LIST_HEAD (&the_lnet.ln_test_peers);
721 CFS_INIT_LIST_HEAD (&the_lnet.ln_nis);
722 CFS_INIT_LIST_HEAD (&the_lnet.ln_zombie_nis);
723 CFS_INIT_LIST_HEAD (&the_lnet.ln_remote_nets);
724 CFS_INIT_LIST_HEAD (&the_lnet.ln_routers);
726 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
728 lnet_init_rtrpools();
730 rc = lnet_create_peer_table();
734 rc = lnet_init_finalizers();
738 rc = lnet_res_container_setup(&the_lnet.ln_eq_container,
739 LNET_COOKIE_TYPE_EQ, LNET_FL_MAX_EQS,
742 CERROR("Failed to create EQ container for LNet: %d\n", rc);
746 /* NB: we will have instance of ME container per CPT soon */
747 rc = lnet_res_container_setup(&the_lnet.ln_me_container,
748 LNET_COOKIE_TYPE_ME, LNET_FL_MAX_MES,
751 CERROR("Failed to create ME container for LNet: %d\n", rc);
755 /* NB: we will have instance of MD container per CPT soon */
756 rc = lnet_res_container_setup(&the_lnet.ln_md_container,
757 LNET_COOKIE_TYPE_MD, LNET_FL_MAX_MDS,
758 sizeof(lnet_libmd_t));
760 CERROR("Failed to create MD container for LNet: %d\n", rc);
764 the_lnet.ln_nportals = MAX_PORTALS;
765 LIBCFS_ALLOC(the_lnet.ln_portals,
766 the_lnet.ln_nportals *
767 sizeof(*the_lnet.ln_portals));
768 if (the_lnet.ln_portals == NULL) {
773 for (i = 0; i < the_lnet.ln_nportals; i++) {
774 CFS_INIT_LIST_HEAD(&(the_lnet.ln_portals[i].ptl_mlist));
775 CFS_INIT_LIST_HEAD(&(the_lnet.ln_portals[i].ptl_msgq));
776 the_lnet.ln_portals[i].ptl_options = 0;
782 /* NB: lnet_res_container_cleanup is safe to call for
783 * uninitialized container */
784 lnet_res_container_cleanup(&the_lnet.ln_md_container);
785 lnet_res_container_cleanup(&the_lnet.ln_me_container);
786 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
788 lnet_fini_finalizers();
790 lnet_destroy_peer_table();
792 lnet_descriptor_cleanup();
797 lnet_unprepare (void)
801 /* NB no LNET_LOCK since this is the last reference. All LND instances
802 * have shut down already, so it is safe to unlink and free all
803 * descriptors, even those that appear committed to a network op (eg MD
804 * with non-zero pending count) */
806 lnet_fail_nid(LNET_NID_ANY, 0);
808 LASSERT (cfs_list_empty(&the_lnet.ln_test_peers));
809 LASSERT (the_lnet.ln_refcount == 0);
810 LASSERT (cfs_list_empty(&the_lnet.ln_nis));
811 LASSERT (cfs_list_empty(&the_lnet.ln_zombie_nis));
812 LASSERT (the_lnet.ln_nzombie_nis == 0);
814 for (idx = 0; idx < the_lnet.ln_nportals; idx++) {
815 lnet_portal_t *ptl = &the_lnet.ln_portals[idx];
817 LASSERT (cfs_list_empty(&ptl->ptl_msgq));
819 while (!cfs_list_empty(&ptl->ptl_mlist)) {
820 lnet_me_t *me = cfs_list_entry(ptl->ptl_mlist.next,
822 CERROR ("Active ME %p on exit\n", me);
823 cfs_list_del (&me->me_list);
827 if (ptl->ptl_mhash != NULL) {
828 LASSERT (lnet_portal_is_unique(ptl));
829 lnet_portal_mhash_free(ptl->ptl_mhash);
833 lnet_res_container_cleanup(&the_lnet.ln_md_container);
834 lnet_res_container_cleanup(&the_lnet.ln_me_container);
835 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
837 while (!cfs_list_empty (&the_lnet.ln_active_msgs)) {
838 lnet_msg_t *msg = cfs_list_entry (the_lnet.ln_active_msgs.next,
839 lnet_msg_t, msg_activelist);
841 CERROR ("Active msg %p on exit\n", msg);
842 LASSERT (msg->msg_onactivelist);
843 msg->msg_onactivelist = 0;
844 cfs_list_del (&msg->msg_activelist);
848 LIBCFS_FREE(the_lnet.ln_portals,
849 the_lnet.ln_nportals * sizeof(*the_lnet.ln_portals));
851 lnet_free_rtrpools();
852 lnet_fini_finalizers();
853 lnet_destroy_peer_table();
854 lnet_descriptor_cleanup();
860 lnet_net2ni_locked (__u32 net)
865 cfs_list_for_each (tmp, &the_lnet.ln_nis) {
866 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
868 if (LNET_NIDNET(ni->ni_nid) == net) {
869 lnet_ni_addref_locked(ni);
878 lnet_islocalnet (__u32 net)
883 ni = lnet_net2ni_locked(net);
885 lnet_ni_decref_locked(ni);
892 lnet_nid2ni_locked (lnet_nid_t nid)
897 cfs_list_for_each (tmp, &the_lnet.ln_nis) {
898 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
900 if (ni->ni_nid == nid) {
901 lnet_ni_addref_locked(ni);
910 lnet_islocalnid (lnet_nid_t nid)
915 ni = lnet_nid2ni_locked(nid);
917 lnet_ni_decref_locked(ni);
924 lnet_count_acceptor_nis (void)
926 /* Return the # of NIs that need the acceptor. */
928 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
933 cfs_list_for_each (tmp, &the_lnet.ln_nis) {
934 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
936 if (ni->ni_lnd->lnd_accept != NULL)
942 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
947 lnet_shutdown_lndnis (void)
953 /* NB called holding the global mutex */
955 /* All quiet on the API front */
956 LASSERT (!the_lnet.ln_shutdown);
957 LASSERT (the_lnet.ln_refcount == 0);
958 LASSERT (cfs_list_empty(&the_lnet.ln_zombie_nis));
959 LASSERT (the_lnet.ln_nzombie_nis == 0);
960 LASSERT (cfs_list_empty(&the_lnet.ln_remote_nets));
963 the_lnet.ln_shutdown = 1; /* flag shutdown */
965 /* Unlink NIs from the global table */
966 while (!cfs_list_empty(&the_lnet.ln_nis)) {
967 ni = cfs_list_entry(the_lnet.ln_nis.next,
969 cfs_list_del (&ni->ni_list);
971 the_lnet.ln_nzombie_nis++;
972 lnet_ni_decref_locked(ni); /* drop ln_nis' ref */
975 /* Drop the cached eqwait NI. */
976 if (the_lnet.ln_eqwaitni != NULL) {
977 lnet_ni_decref_locked(the_lnet.ln_eqwaitni);
978 the_lnet.ln_eqwaitni = NULL;
981 /* Drop the cached loopback NI. */
982 if (the_lnet.ln_loni != NULL) {
983 lnet_ni_decref_locked(the_lnet.ln_loni);
984 the_lnet.ln_loni = NULL;
989 /* Clear lazy portals and drop delayed messages which hold refs
990 * on their lnet_msg_t::msg_rxpeer */
991 for (i = 0; i < the_lnet.ln_nportals; i++)
992 LNetClearLazyPortal(i);
994 /* Clear the peer table and wait for all peers to go (they hold refs on
996 lnet_clear_peer_table();
999 /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
1000 * and shut them down in guaranteed thread context */
1002 while (the_lnet.ln_nzombie_nis != 0) {
1004 while (cfs_list_empty(&the_lnet.ln_zombie_nis)) {
1007 if ((i & (-i)) == i)
1008 CDEBUG(D_WARNING,"Waiting for %d zombie NIs\n",
1009 the_lnet.ln_nzombie_nis);
1010 cfs_pause(cfs_time_seconds(1));
1014 ni = cfs_list_entry(the_lnet.ln_zombie_nis.next,
1015 lnet_ni_t, ni_list);
1016 cfs_list_del(&ni->ni_list);
1017 ni->ni_lnd->lnd_refcount--;
1021 islo = ni->ni_lnd->lnd_type == LOLND;
1023 LASSERT (!cfs_in_interrupt ());
1024 (ni->ni_lnd->lnd_shutdown)(ni);
1026 /* can't deref lnd anymore now; it might have unregistered
1030 CDEBUG(D_LNI, "Removed LNI %s\n",
1031 libcfs_nid2str(ni->ni_nid));
1033 LIBCFS_FREE(ni, sizeof(*ni));
1036 the_lnet.ln_nzombie_nis--;
1039 the_lnet.ln_shutdown = 0;
1042 if (the_lnet.ln_network_tokens != NULL) {
1043 LIBCFS_FREE(the_lnet.ln_network_tokens,
1044 the_lnet.ln_network_tokens_nob);
1045 the_lnet.ln_network_tokens = NULL;
1050 lnet_startup_lndnis (void)
1058 char *nets = lnet_get_networks();
1060 CFS_INIT_LIST_HEAD(&nilist);
1065 rc = lnet_parse_networks(&nilist, nets);
1069 while (!cfs_list_empty(&nilist)) {
1070 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1071 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1073 LASSERT (libcfs_isknown_lnd(lnd_type));
1075 if (lnd_type == CIBLND ||
1076 lnd_type == OPENIBLND ||
1077 lnd_type == IIBLND ||
1078 lnd_type == VIBLND) {
1079 CERROR("LND %s obsoleted\n",
1080 libcfs_lnd2str(lnd_type));
1084 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1085 lnd = lnet_find_lnd_by_type(lnd_type);
1089 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1090 rc = cfs_request_module("%s",
1091 libcfs_lnd2modname(lnd_type));
1092 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1094 lnd = lnet_find_lnd_by_type(lnd_type);
1096 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1097 CERROR("Can't load LND %s, module %s, rc=%d\n",
1098 libcfs_lnd2str(lnd_type),
1099 libcfs_lnd2modname(lnd_type), rc);
1100 #ifndef HAVE_MODULE_LOADING_SUPPORT
1101 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1102 "compiled with kernel module "
1103 "loading support.");
1110 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1111 CERROR("LND %s not supported\n",
1112 libcfs_lnd2str(lnd_type));
1117 ni->ni_refcount = 1;
1120 lnd->lnd_refcount++;
1125 rc = (lnd->lnd_startup)(ni);
1127 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1130 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1132 rc, libcfs_lnd2str(lnd->lnd_type));
1134 lnd->lnd_refcount--;
1139 LASSERT (ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1141 cfs_list_del(&ni->ni_list);
1144 cfs_list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1147 if (lnd->lnd_type == LOLND) {
1149 LASSERT (the_lnet.ln_loni == NULL);
1150 the_lnet.ln_loni = ni;
1155 if (lnd->lnd_wait != NULL) {
1156 if (the_lnet.ln_eqwaitni == NULL) {
1158 the_lnet.ln_eqwaitni = ni;
1161 # ifndef HAVE_LIBPTHREAD
1162 LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1163 "single-threaded runtime\n",
1164 libcfs_lnd2str(lnd_type));
1169 if (ni->ni_peertxcredits == 0 ||
1170 ni->ni_maxtxcredits == 0) {
1171 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1172 libcfs_lnd2str(lnd->lnd_type),
1173 ni->ni_peertxcredits == 0 ?
1178 ni->ni_txcredits = ni->ni_mintxcredits = ni->ni_maxtxcredits;
1180 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1181 libcfs_nid2str(ni->ni_nid),
1182 ni->ni_peertxcredits, ni->ni_txcredits,
1183 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1188 if (the_lnet.ln_eqwaitni != NULL && nicount > 1) {
1189 lnd_type = the_lnet.ln_eqwaitni->ni_lnd->lnd_type;
1190 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1192 libcfs_lnd2str(lnd_type));
1199 lnet_shutdown_lndnis();
1201 while (!cfs_list_empty(&nilist)) {
1202 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1203 cfs_list_del(&ni->ni_list);
1204 LIBCFS_FREE(ni, sizeof(*ni));
1211 * Initialize LNet library.
1213 * Only userspace program needs to call this function - it's automatically
1214 * called in the kernel at module loading time. Caller has to call LNetFini()
1215 * after a call to LNetInit(), if and only if the latter returned 0. It must
1216 * be called exactly once.
1218 * \return 0 on success, and -ve on failures.
1223 lnet_assert_wire_constants ();
1224 LASSERT (!the_lnet.ln_init);
1226 memset(&the_lnet, 0, sizeof(the_lnet));
1229 the_lnet.ln_refcount = 0;
1230 the_lnet.ln_init = 1;
1231 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1232 CFS_INIT_LIST_HEAD(&the_lnet.ln_lnds);
1233 CFS_INIT_LIST_HEAD(&the_lnet.ln_zombie_rcd);
1236 /* All LNDs apart from the LOLND are in separate modules. They
1237 * register themselves when their module loads, and unregister
1238 * themselves when their module is unloaded. */
1241 * NB the order here determines default 'networks=' order */
1243 LNET_REGISTER_ULND(the_ptllnd);
1245 # ifdef HAVE_LIBPTHREAD
1246 LNET_REGISTER_ULND(the_tcplnd);
1249 lnet_register_lnd(&the_lolnd);
1254 * Finalize LNet library.
1256 * Only userspace program needs to call this function. It can be called
1259 * \pre LNetInit() called with success.
1260 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1265 LASSERT (the_lnet.ln_init);
1266 LASSERT (the_lnet.ln_refcount == 0);
1268 while (!cfs_list_empty(&the_lnet.ln_lnds))
1269 lnet_unregister_lnd(cfs_list_entry(the_lnet.ln_lnds.next,
1273 the_lnet.ln_init = 0;
1277 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1279 * Userspace program should call this after a successful call to LNetInit().
1280 * Users must call this function at least once before any other functions.
1281 * For each successful call there must be a corresponding call to
1282 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1285 * The PID used by LNet may be different from the one requested.
1288 * \param requested_pid PID requested by the caller.
1290 * \return >= 0 on success, and < 0 error code on failures.
1293 LNetNIInit(lnet_pid_t requested_pid)
1295 int im_a_router = 0;
1298 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1300 LASSERT (the_lnet.ln_init);
1301 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1303 if (the_lnet.ln_refcount > 0) {
1304 rc = the_lnet.ln_refcount++;
1308 lnet_get_tunables();
1310 if (requested_pid == LNET_PID_ANY) {
1311 /* Don't instantiate LNET just for me */
1316 rc = lnet_prepare(requested_pid);
1320 rc = lnet_startup_lndnis();
1324 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1328 rc = lnet_check_routes();
1332 rc = lnet_alloc_rtrpools(im_a_router);
1336 rc = lnet_acceptor_start();
1340 the_lnet.ln_refcount = 1;
1341 /* Now I may use my own API functions... */
1343 /* NB router checker needs the_lnet.ln_ping_info in
1344 * lnet_router_checker -> lnet_update_ni_status */
1345 rc = lnet_ping_target_init();
1349 rc = lnet_router_checker_start();
1357 lnet_ping_target_fini();
1359 the_lnet.ln_refcount = 0;
1360 lnet_acceptor_stop();
1362 lnet_destroy_routes();
1363 lnet_shutdown_lndnis();
1369 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1374 * Stop LNet interfaces, routing, and forwarding.
1376 * Users must call this function once for each successful call to LNetNIInit().
1377 * Once the LNetNIFini() operation has been started, the results of pending
1378 * API operations are undefined.
1380 * \return always 0 for current implementation.
1385 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1387 LASSERT (the_lnet.ln_init);
1388 LASSERT (the_lnet.ln_refcount > 0);
1390 if (the_lnet.ln_refcount != 1) {
1391 the_lnet.ln_refcount--;
1393 LASSERT (!the_lnet.ln_niinit_self);
1396 lnet_router_checker_stop();
1397 lnet_ping_target_fini();
1399 /* Teardown fns that use my own API functions BEFORE here */
1400 the_lnet.ln_refcount = 0;
1402 lnet_acceptor_stop();
1403 lnet_destroy_routes();
1404 lnet_shutdown_lndnis();
1408 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1413 * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1414 * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1415 * internal ioctl handler.
1417 * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1419 * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1420 * The data will be printed to system console. Don't use it excessively.
1421 * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1423 * \return Always return 0 when called by users directly (i.e., not via ioctl).
1426 LNetCtl(unsigned int cmd, void *arg)
1428 struct libcfs_ioctl_data *data = arg;
1429 lnet_process_id_t id = {0};
1433 LASSERT (the_lnet.ln_init);
1434 LASSERT (the_lnet.ln_refcount > 0);
1437 case IOC_LIBCFS_GET_NI:
1438 rc = LNetGetId(data->ioc_count, &id);
1439 data->ioc_nid = id.nid;
1442 case IOC_LIBCFS_FAIL_NID:
1443 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1445 case IOC_LIBCFS_ADD_ROUTE:
1446 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1448 return (rc != 0) ? rc : lnet_check_routes();
1450 case IOC_LIBCFS_DEL_ROUTE:
1451 return lnet_del_route(data->ioc_net, data->ioc_nid);
1453 case IOC_LIBCFS_GET_ROUTE:
1454 return lnet_get_route(data->ioc_count,
1455 &data->ioc_net, &data->ioc_count,
1456 &data->ioc_nid, &data->ioc_flags);
1457 case IOC_LIBCFS_NOTIFY_ROUTER:
1458 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1459 cfs_time_current() -
1460 cfs_time_seconds(cfs_time_current_sec() -
1461 (time_t)data->ioc_u64[0]));
1463 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1464 /* This can be removed once lustre stops calling it */
1467 case IOC_LIBCFS_LNET_DIST:
1468 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1469 if (rc < 0 && rc != -EHOSTUNREACH)
1472 data->ioc_u32[0] = rc;
1475 case IOC_LIBCFS_TESTPROTOCOMPAT:
1477 the_lnet.ln_testprotocompat = data->ioc_flags;
1481 case IOC_LIBCFS_PING:
1482 id.nid = data->ioc_nid;
1483 id.pid = data->ioc_u32[0];
1484 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1485 (lnet_process_id_t *)data->ioc_pbuf1,
1486 data->ioc_plen1/sizeof(lnet_process_id_t));
1489 data->ioc_count = rc;
1492 case IOC_LIBCFS_DEBUG_PEER: {
1493 /* CAVEAT EMPTOR: this one designed for calling directly; not
1495 id = *((lnet_process_id_t *) arg);
1497 lnet_debug_peer(id.nid);
1499 ni = lnet_net2ni(LNET_NIDNET(id.nid));
1501 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1503 if (ni->ni_lnd->lnd_ctl == NULL) {
1504 CDEBUG(D_WARNING, "No ctl for %s\n",
1507 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1516 ni = lnet_net2ni(data->ioc_net);
1520 if (ni->ni_lnd->lnd_ctl == NULL)
1523 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1532 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
1533 * all interfaces share a same PID, as requested by LNetNIInit().
1535 * \param index Index of the interface to look up.
1536 * \param id On successful return, this location will hold the
1537 * lnet_process_id_t ID of the interface.
1539 * \retval 0 If an interface exists at \a index.
1540 * \retval -ENOENT If no interface has been found.
1543 LNetGetId(unsigned int index, lnet_process_id_t *id)
1549 LASSERT (the_lnet.ln_init);
1550 LASSERT (the_lnet.ln_refcount > 0);
1554 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
1558 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
1560 id->nid = ni->ni_nid;
1561 id->pid = the_lnet.ln_pid;
1572 * Print a string representation of handle \a h into buffer \a str of
1576 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1578 snprintf(str, len, LPX64, h.cookie);
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_version = LNET_PROTO_PING_VERSION;
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);
1624 LASSERT (ni != NULL);
1625 LASSERT (ni->ni_status == NULL);
1627 lnet_ni_decref_locked(ni);
1632 the_lnet.ln_ping_info = pinfo;
1637 lnet_destroy_ping_info(void)
1643 cfs_list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
1644 ni->ni_status = NULL;
1649 LIBCFS_FREE(the_lnet.ln_ping_info,
1650 offsetof(lnet_ping_info_t,
1651 pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1652 the_lnet.ln_ping_info = NULL;
1657 lnet_ping_target_init(void)
1660 lnet_handle_me_t meh;
1661 lnet_process_id_t id;
1666 rc = lnet_create_ping_info();
1670 /* We can have a tiny EQ since we only need to see the unlink event on
1671 * teardown, which by definition is the last one! */
1672 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1674 CERROR("Can't allocate ping EQ: %d\n", rc);
1678 memset(&id, 0, sizeof(lnet_process_id_t));
1679 id.nid = LNET_NID_ANY;
1680 id.pid = LNET_PID_ANY;
1682 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1683 LNET_PROTO_PING_MATCHBITS, 0,
1684 LNET_UNLINK, LNET_INS_AFTER,
1687 CERROR("Can't create ping ME: %d\n", rc);
1691 /* initialize md content */
1692 infosz = offsetof(lnet_ping_info_t,
1693 pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1694 md.start = the_lnet.ln_ping_info;
1696 md.threshold = LNET_MD_THRESH_INF;
1698 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1699 LNET_MD_MANAGE_REMOTE;
1701 md.eq_handle = the_lnet.ln_ping_target_eq;
1703 rc = LNetMDAttach(meh, md,
1705 &the_lnet.ln_ping_target_md);
1707 CERROR("Can't attach ping MD: %d\n", rc);
1714 rc2 = LNetMEUnlink(meh);
1717 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1720 lnet_destroy_ping_info();
1725 lnet_ping_target_fini(void)
1730 int timeout_ms = 1000;
1731 cfs_sigset_t blocked = cfs_block_allsigs();
1733 LNetMDUnlink(the_lnet.ln_ping_target_md);
1734 /* NB md could be busy; this just starts the unlink */
1737 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1738 timeout_ms, &event, &which);
1740 /* I expect overflow... */
1741 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1744 /* timed out: provide a diagnostic */
1745 CWARN("Still waiting for ping MD to unlink\n");
1750 /* Got a valid event */
1755 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1757 lnet_destroy_ping_info();
1758 cfs_restore_sigs(blocked);
1762 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1764 lnet_handle_eq_t eqh;
1765 lnet_handle_md_t mdh;
1771 const int a_long_time = 60000; /* mS */
1772 int infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1773 lnet_ping_info_t *info;
1774 lnet_process_id_t tmpid;
1779 cfs_sigset_t blocked;
1782 id.nid == LNET_NID_ANY ||
1783 timeout_ms > 500000 || /* arbitrary limit! */
1784 n_ids > 20) /* arbitrary limit! */
1787 if (id.pid == LNET_PID_ANY)
1788 id.pid = LUSTRE_SRV_LNET_PID;
1790 LIBCFS_ALLOC(info, infosz);
1794 /* NB 2 events max (including any unlink event) */
1795 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1797 CERROR("Can't allocate EQ: %d\n", rc);
1801 /* initialize md content */
1804 md.threshold = 2; /*GET/REPLY*/
1806 md.options = LNET_MD_TRUNCATE;
1810 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1812 CERROR("Can't bind MD: %d\n", rc);
1816 rc = LNetGet(LNET_NID_ANY, mdh, id,
1817 LNET_RESERVED_PORTAL,
1818 LNET_PROTO_PING_MATCHBITS, 0);
1821 /* Don't CERROR; this could be deliberate! */
1823 rc2 = LNetMDUnlink(mdh);
1826 /* NB must wait for the UNLINK event below... */
1828 timeout_ms = a_long_time;
1832 /* MUST block for unlink to complete */
1834 blocked = cfs_block_allsigs();
1836 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1839 cfs_restore_sigs(blocked);
1841 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1842 (rc2 <= 0) ? -1 : event.type,
1843 (rc2 <= 0) ? -1 : event.status,
1844 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1846 LASSERT (rc2 != -EOVERFLOW); /* can't miss anything */
1848 if (rc2 <= 0 || event.status != 0) {
1849 /* timeout or error */
1850 if (!replied && rc == 0)
1851 rc = (rc2 < 0) ? rc2 :
1852 (rc2 == 0) ? -ETIMEDOUT :
1856 /* Ensure completion in finite time... */
1858 /* No assertion (racing with network) */
1860 timeout_ms = a_long_time;
1861 } else if (rc2 == 0) {
1862 /* timed out waiting for unlink */
1863 CWARN("ping %s: late network completion\n",
1866 } else if (event.type == LNET_EVENT_REPLY) {
1871 } while (rc2 <= 0 || !event.unlinked);
1875 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
1882 LASSERT (nob >= 0 && nob <= infosz);
1884 rc = -EPROTO; /* if I can't parse... */
1887 /* can't check magic/version */
1888 CERROR("%s: ping info too short %d\n",
1889 libcfs_id2str(id), nob);
1893 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
1894 lnet_swap_pinginfo(info);
1895 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
1896 CERROR("%s: Unexpected magic %08x\n",
1897 libcfs_id2str(id), info->pi_magic);
1901 if (info->pi_version != LNET_PROTO_PING_VERSION) {
1902 CERROR("%s: Unexpected version 0x%x\n",
1903 libcfs_id2str(id), info->pi_version);
1907 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
1908 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
1909 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
1913 if (info->pi_nnis < n_ids)
1914 n_ids = info->pi_nnis;
1916 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
1917 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
1918 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
1922 rc = -EFAULT; /* If I SEGV... */
1924 for (i = 0; i < n_ids; i++) {
1925 tmpid.pid = info->pi_pid;
1926 tmpid.nid = info->pi_ni[i].ns_nid;
1928 if (cfs_copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
1937 rc2 = LNetEQFree(eqh);
1939 CERROR("rc2 %d\n", rc2);
1943 LIBCFS_FREE(info, infosz);