1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
39 #define DEBUG_SUBSYSTEM S_LNET
40 #include <lnet/lib-lnet.h>
43 #define D_LNI D_CONSOLE
45 #define D_LNI D_CONFIG
48 lnet_t the_lnet; /* THE state of the network */
52 static char *ip2nets = "";
53 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
54 "LNET network <- IP table");
56 static char *networks = "";
57 CFS_MODULE_PARM(networks, "s", charp, 0444,
60 static char *routes = "";
61 CFS_MODULE_PARM(routes, "s", charp, 0444,
62 "routes to non-local networks");
71 lnet_get_networks(void)
76 if (*networks != 0 && *ip2nets != 0) {
77 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
78 "'ip2nets' but not both at once\n");
83 rc = lnet_parse_ip2nets(&nets, ip2nets);
84 return (rc == 0) ? nets : NULL;
96 cfs_spin_lock_init (&the_lnet.ln_lock);
97 cfs_waitq_init (&the_lnet.ln_waitq);
98 cfs_mutex_init(&the_lnet.ln_lnd_mutex);
99 cfs_mutex_init(&the_lnet.ln_api_mutex);
103 lnet_fini_locks(void)
110 lnet_get_routes(void)
112 char *str = getenv("LNET_ROUTES");
114 return (str == NULL) ? "" : str;
118 lnet_get_networks (void)
120 static char default_networks[256];
121 char *networks = getenv ("LNET_NETWORKS");
122 char *ip2nets = getenv ("LNET_IP2NETS");
131 if (networks != NULL && ip2nets != NULL) {
132 LCONSOLE_ERROR_MSG(0x103, "Please set EITHER 'LNET_NETWORKS' or"
133 " 'LNET_IP2NETS' but not both at once\n");
137 if (ip2nets != NULL) {
138 rc = lnet_parse_ip2nets(&networks, ip2nets);
139 return (rc == 0) ? networks : NULL;
142 SET_BUT_UNUSED(ip2nets);
145 if (networks != NULL)
148 /* In userland, the default 'networks=' is the list of known net types */
150 len = sizeof(default_networks);
151 str = default_networks;
155 cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
156 lnd_t *lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
158 nob = snprintf(str, len, "%s%s", sep,
159 libcfs_lnd2str(lnd->lnd_type));
162 /* overflowed the string; leave it where it was */
171 return default_networks;
174 # ifndef HAVE_LIBPTHREAD
176 void lnet_init_locks(void)
178 the_lnet.ln_lock = 0;
179 the_lnet.ln_lnd_mutex = 0;
180 the_lnet.ln_api_mutex = 0;
183 void lnet_fini_locks(void)
185 LASSERT (the_lnet.ln_api_mutex == 0);
186 LASSERT (the_lnet.ln_lnd_mutex == 0);
187 LASSERT (the_lnet.ln_lock == 0);
192 void lnet_init_locks(void)
194 pthread_cond_init(&the_lnet.ln_cond, NULL);
195 pthread_mutex_init(&the_lnet.ln_lock, NULL);
196 pthread_mutex_init(&the_lnet.ln_lnd_mutex, NULL);
197 pthread_mutex_init(&the_lnet.ln_api_mutex, NULL);
200 void lnet_fini_locks(void)
202 pthread_mutex_destroy(&the_lnet.ln_api_mutex);
203 pthread_mutex_destroy(&the_lnet.ln_lnd_mutex);
204 pthread_mutex_destroy(&the_lnet.ln_lock);
205 pthread_cond_destroy(&the_lnet.ln_cond);
211 void lnet_assert_wire_constants (void)
213 /* Wire protocol assertions generated by 'wirecheck'
214 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
215 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
216 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
219 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
220 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
221 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
222 CLASSERT (LNET_MSG_ACK == 0);
223 CLASSERT (LNET_MSG_PUT == 1);
224 CLASSERT (LNET_MSG_GET == 2);
225 CLASSERT (LNET_MSG_REPLY == 3);
226 CLASSERT (LNET_MSG_HELLO == 4);
228 /* Checks for struct ptl_handle_wire_t */
229 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
230 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
231 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
232 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
233 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
235 /* Checks for struct lnet_magicversion_t */
236 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
237 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
238 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
239 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
240 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
241 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
242 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
244 /* Checks for struct lnet_hdr_t */
245 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
246 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
247 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
248 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
249 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
250 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
251 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
252 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
253 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
254 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
255 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
256 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
257 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
258 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
259 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
262 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
263 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
264 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
265 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
266 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
267 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
270 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
271 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
272 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
273 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
274 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
275 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
276 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
277 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
278 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
279 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
282 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
283 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
284 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
285 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
286 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
287 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
288 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
289 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
290 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
291 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
294 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
295 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
298 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
299 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
300 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
301 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
305 lnet_find_lnd_by_type (int type)
310 /* holding lnd mutex */
311 cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
312 lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
314 if ((int)lnd->lnd_type == type)
322 lnet_register_lnd (lnd_t *lnd)
324 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
326 LASSERT (the_lnet.ln_init);
327 LASSERT (libcfs_isknown_lnd(lnd->lnd_type));
328 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
330 cfs_list_add_tail (&lnd->lnd_list, &the_lnet.ln_lnds);
331 lnd->lnd_refcount = 0;
333 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
335 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
339 lnet_unregister_lnd (lnd_t *lnd)
341 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
343 LASSERT (the_lnet.ln_init);
344 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
345 LASSERT (lnd->lnd_refcount == 0);
347 cfs_list_del (&lnd->lnd_list);
348 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
350 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
353 #ifndef LNET_USE_LIB_FREELIST
356 lnet_descriptor_setup (void)
362 lnet_descriptor_cleanup (void)
369 lnet_freelist_init (lnet_freelist_t *fl, int n, int size)
375 size += offsetof (lnet_freeobj_t, fo_contents);
377 LIBCFS_ALLOC(space, n * size);
381 CFS_INIT_LIST_HEAD (&fl->fl_list);
384 fl->fl_objsize = size;
388 memset (space, 0, size);
389 cfs_list_add ((cfs_list_t *)space, &fl->fl_list);
397 lnet_freelist_fini (lnet_freelist_t *fl)
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));
416 lnet_descriptor_setup (void)
418 /* NB on failure caller must still call lnet_descriptor_cleanup */
422 memset (&the_lnet.ln_free_mes, 0, sizeof (the_lnet.ln_free_mes));
423 memset (&the_lnet.ln_free_msgs, 0, sizeof (the_lnet.ln_free_msgs));
424 memset (&the_lnet.ln_free_mds, 0, sizeof (the_lnet.ln_free_mds));
425 memset (&the_lnet.ln_free_eqs, 0, sizeof (the_lnet.ln_free_eqs));
427 rc = lnet_freelist_init(&the_lnet.ln_free_mes,
428 MAX_MES, sizeof (lnet_me_t));
432 rc = lnet_freelist_init(&the_lnet.ln_free_msgs,
433 MAX_MSGS, sizeof (lnet_msg_t));
437 rc = lnet_freelist_init(&the_lnet.ln_free_mds,
438 MAX_MDS, sizeof (lnet_libmd_t));
442 rc = lnet_freelist_init(&the_lnet.ln_free_eqs,
443 MAX_EQS, sizeof (lnet_eq_t));
448 lnet_descriptor_cleanup (void)
450 lnet_freelist_fini (&the_lnet.ln_free_mes);
451 lnet_freelist_fini (&the_lnet.ln_free_msgs);
452 lnet_freelist_fini (&the_lnet.ln_free_mds);
453 lnet_freelist_fini (&the_lnet.ln_free_eqs);
459 lnet_create_interface_cookie (void)
461 /* NB the interface cookie in wire handles guards against delayed
462 * replies and ACKs appearing valid after reboot. Initialisation time,
463 * even if it's only implemented to millisecond resolution is probably
464 * easily good enough. */
468 int rc = gettimeofday (&tv, NULL);
471 cfs_gettimeofday(&tv);
475 cookie += tv.tv_usec;
480 lnet_setup_handle_hash (void)
484 /* Arbitrary choice of hash table size */
486 the_lnet.ln_lh_hash_size =
487 (2 * CFS_PAGE_SIZE) / sizeof (cfs_list_t);
489 the_lnet.ln_lh_hash_size = (MAX_MES + MAX_MDS + MAX_EQS)/4;
491 LIBCFS_ALLOC(the_lnet.ln_lh_hash_table,
492 the_lnet.ln_lh_hash_size * sizeof (cfs_list_t));
493 if (the_lnet.ln_lh_hash_table == NULL)
496 for (i = 0; i < the_lnet.ln_lh_hash_size; i++)
497 CFS_INIT_LIST_HEAD (&the_lnet.ln_lh_hash_table[i]);
499 the_lnet.ln_next_object_cookie = LNET_COOKIE_TYPES;
505 lnet_cleanup_handle_hash (void)
507 if (the_lnet.ln_lh_hash_table == NULL)
510 LIBCFS_FREE(the_lnet.ln_lh_hash_table,
511 the_lnet.ln_lh_hash_size * sizeof (cfs_list_t));
515 lnet_lookup_cookie (__u64 cookie, int type)
517 /* ALWAYS called with LNET_LOCK held */
522 if ((cookie & (LNET_COOKIE_TYPES - 1)) != type)
525 hash = ((unsigned int)(cookie >> LNET_COOKIE_TYPE_BITS)) % the_lnet.ln_lh_hash_size;
526 list = &the_lnet.ln_lh_hash_table[hash];
528 cfs_list_for_each (el, list) {
529 lnet_libhandle_t *lh = cfs_list_entry (el, lnet_libhandle_t,
532 if (lh->lh_cookie == cookie)
540 lnet_initialise_handle (lnet_libhandle_t *lh, int type)
542 /* ALWAYS called with LNET_LOCK held */
545 LASSERT (type >= 0 && type < LNET_COOKIE_TYPES);
546 lh->lh_cookie = the_lnet.ln_next_object_cookie | type;
547 the_lnet.ln_next_object_cookie += LNET_COOKIE_TYPES;
549 hash = ((unsigned int)(lh->lh_cookie >> LNET_COOKIE_TYPE_BITS)) % the_lnet.ln_lh_hash_size;
550 cfs_list_add (&lh->lh_hash_chain, &the_lnet.ln_lh_hash_table[hash]);
554 lnet_invalidate_handle (lnet_libhandle_t *lh)
556 /* ALWAYS called with LNET_LOCK held */
557 cfs_list_del (&lh->lh_hash_chain);
561 lnet_portal_mhash_alloc(void)
566 LIBCFS_ALLOC(mhash, sizeof(cfs_list_t) * LNET_PORTAL_HASH_SIZE);
570 for (i = 0; i < LNET_PORTAL_HASH_SIZE; i++)
571 CFS_INIT_LIST_HEAD(&mhash[i]);
577 lnet_portal_mhash_free(cfs_list_t *mhash)
581 for (i = 0; i < LNET_PORTAL_HASH_SIZE; i++) {
582 while (!cfs_list_empty(&mhash[i])) {
583 lnet_me_t *me = cfs_list_entry(mhash[i].next,
585 CERROR ("Active ME %p on exit portal mhash\n", me);
586 cfs_list_del(&me->me_list);
590 LIBCFS_FREE(mhash, sizeof(cfs_list_t) * LNET_PORTAL_HASH_SIZE);
594 lnet_init_finalizers(void)
599 the_lnet.ln_nfinalizers = (int) cfs_num_online_cpus();
601 LIBCFS_ALLOC(the_lnet.ln_finalizers,
602 the_lnet.ln_nfinalizers *
603 sizeof(*the_lnet.ln_finalizers));
604 if (the_lnet.ln_finalizers == NULL) {
605 CERROR("Can't allocate ln_finalizers\n");
609 for (i = 0; i < the_lnet.ln_nfinalizers; i++)
610 the_lnet.ln_finalizers[i] = NULL;
612 the_lnet.ln_finalizing = 0;
615 CFS_INIT_LIST_HEAD(&the_lnet.ln_finalizeq);
620 lnet_fini_finalizers(void)
625 for (i = 0; i < the_lnet.ln_nfinalizers; i++)
626 LASSERT (the_lnet.ln_finalizers[i] == NULL);
628 LIBCFS_FREE(the_lnet.ln_finalizers,
629 the_lnet.ln_nfinalizers *
630 sizeof(*the_lnet.ln_finalizers));
632 LASSERT (!the_lnet.ln_finalizing);
634 LASSERT (cfs_list_empty(&the_lnet.ln_finalizeq));
639 * Reserved API - do not use.
640 * Temporary workaround to allow uOSS and test programs force server
641 * mode in userspace. See comments near ln_server_mode_flag in
642 * lnet/lib-types.h */
646 the_lnet.ln_server_mode_flag = 1;
651 lnet_prepare(lnet_pid_t requested_pid)
653 /* Prepare to bring up the network */
657 LASSERT (the_lnet.ln_refcount == 0);
659 the_lnet.ln_routing = 0;
662 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
663 the_lnet.ln_pid = requested_pid;
665 if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
666 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
668 if (cfs_curproc_uid())/* Only root can run user-space server */
670 the_lnet.ln_pid = requested_pid;
672 } else {/* client case (liblustre) */
674 /* My PID must be unique on this node and flag I'm userspace */
675 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
679 rc = lnet_descriptor_setup();
683 memset(&the_lnet.ln_counters, 0,
684 sizeof(the_lnet.ln_counters));
686 CFS_INIT_LIST_HEAD (&the_lnet.ln_active_msgs);
687 CFS_INIT_LIST_HEAD (&the_lnet.ln_active_mds);
688 CFS_INIT_LIST_HEAD (&the_lnet.ln_active_eqs);
689 CFS_INIT_LIST_HEAD (&the_lnet.ln_test_peers);
690 CFS_INIT_LIST_HEAD (&the_lnet.ln_nis);
691 CFS_INIT_LIST_HEAD (&the_lnet.ln_zombie_nis);
692 CFS_INIT_LIST_HEAD (&the_lnet.ln_remote_nets);
693 CFS_INIT_LIST_HEAD (&the_lnet.ln_routers);
695 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
697 lnet_init_rtrpools();
699 rc = lnet_setup_handle_hash ();
703 rc = lnet_create_peer_table();
707 rc = lnet_init_finalizers();
711 the_lnet.ln_nportals = MAX_PORTALS;
712 LIBCFS_ALLOC(the_lnet.ln_portals,
713 the_lnet.ln_nportals *
714 sizeof(*the_lnet.ln_portals));
715 if (the_lnet.ln_portals == NULL) {
720 for (i = 0; i < the_lnet.ln_nportals; i++) {
721 CFS_INIT_LIST_HEAD(&(the_lnet.ln_portals[i].ptl_mlist));
722 CFS_INIT_LIST_HEAD(&(the_lnet.ln_portals[i].ptl_msgq));
723 the_lnet.ln_portals[i].ptl_options = 0;
729 lnet_fini_finalizers();
731 lnet_destroy_peer_table();
733 lnet_cleanup_handle_hash();
735 lnet_descriptor_cleanup();
740 lnet_unprepare (void)
744 /* NB no LNET_LOCK since this is the last reference. All LND instances
745 * have shut down already, so it is safe to unlink and free all
746 * descriptors, even those that appear committed to a network op (eg MD
747 * with non-zero pending count) */
749 lnet_fail_nid(LNET_NID_ANY, 0);
751 LASSERT (cfs_list_empty(&the_lnet.ln_test_peers));
752 LASSERT (the_lnet.ln_refcount == 0);
753 LASSERT (cfs_list_empty(&the_lnet.ln_nis));
754 LASSERT (cfs_list_empty(&the_lnet.ln_zombie_nis));
755 LASSERT (the_lnet.ln_nzombie_nis == 0);
757 for (idx = 0; idx < the_lnet.ln_nportals; idx++) {
758 lnet_portal_t *ptl = &the_lnet.ln_portals[idx];
760 LASSERT (cfs_list_empty(&ptl->ptl_msgq));
762 while (!cfs_list_empty(&ptl->ptl_mlist)) {
763 lnet_me_t *me = cfs_list_entry(ptl->ptl_mlist.next,
765 CERROR ("Active ME %p on exit\n", me);
766 cfs_list_del (&me->me_list);
770 if (ptl->ptl_mhash != NULL) {
771 LASSERT (lnet_portal_is_unique(ptl));
772 lnet_portal_mhash_free(ptl->ptl_mhash);
776 while (!cfs_list_empty (&the_lnet.ln_active_mds)) {
777 lnet_libmd_t *md = cfs_list_entry (the_lnet.ln_active_mds.next,
778 lnet_libmd_t, md_list);
780 CERROR ("Active MD %p on exit\n", md);
781 cfs_list_del_init (&md->md_list);
785 while (!cfs_list_empty (&the_lnet.ln_active_eqs)) {
786 lnet_eq_t *eq = cfs_list_entry (the_lnet.ln_active_eqs.next,
789 CERROR ("Active EQ %p on exit\n", eq);
790 cfs_list_del (&eq->eq_list);
794 while (!cfs_list_empty (&the_lnet.ln_active_msgs)) {
795 lnet_msg_t *msg = cfs_list_entry (the_lnet.ln_active_msgs.next,
796 lnet_msg_t, msg_activelist);
798 CERROR ("Active msg %p on exit\n", msg);
799 LASSERT (msg->msg_onactivelist);
800 msg->msg_onactivelist = 0;
801 cfs_list_del (&msg->msg_activelist);
805 LIBCFS_FREE(the_lnet.ln_portals,
806 the_lnet.ln_nportals * sizeof(*the_lnet.ln_portals));
808 lnet_free_rtrpools();
809 lnet_fini_finalizers();
810 lnet_destroy_peer_table();
811 lnet_cleanup_handle_hash();
812 lnet_descriptor_cleanup();
818 lnet_net2ni_locked (__u32 net)
823 cfs_list_for_each (tmp, &the_lnet.ln_nis) {
824 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
826 if (LNET_NIDNET(ni->ni_nid) == net) {
827 lnet_ni_addref_locked(ni);
836 lnet_islocalnet (__u32 net)
841 ni = lnet_net2ni_locked(net);
843 lnet_ni_decref_locked(ni);
850 lnet_nid2ni_locked (lnet_nid_t nid)
855 cfs_list_for_each (tmp, &the_lnet.ln_nis) {
856 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
858 if (ni->ni_nid == nid) {
859 lnet_ni_addref_locked(ni);
868 lnet_islocalnid (lnet_nid_t nid)
873 ni = lnet_nid2ni_locked(nid);
875 lnet_ni_decref_locked(ni);
882 lnet_count_acceptor_nis (void)
884 /* Return the # of NIs that need the acceptor. */
886 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
891 cfs_list_for_each (tmp, &the_lnet.ln_nis) {
892 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
894 if (ni->ni_lnd->lnd_accept != NULL)
900 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
905 lnet_shutdown_lndnis (void)
911 /* NB called holding the global mutex */
913 /* All quiet on the API front */
914 LASSERT (!the_lnet.ln_shutdown);
915 LASSERT (the_lnet.ln_refcount == 0);
916 LASSERT (cfs_list_empty(&the_lnet.ln_zombie_nis));
917 LASSERT (the_lnet.ln_nzombie_nis == 0);
918 LASSERT (cfs_list_empty(&the_lnet.ln_remote_nets));
921 the_lnet.ln_shutdown = 1; /* flag shutdown */
923 /* Unlink NIs from the global table */
924 while (!cfs_list_empty(&the_lnet.ln_nis)) {
925 ni = cfs_list_entry(the_lnet.ln_nis.next,
927 cfs_list_del (&ni->ni_list);
929 the_lnet.ln_nzombie_nis++;
930 lnet_ni_decref_locked(ni); /* drop ln_nis' ref */
933 /* Drop the cached eqwait NI. */
934 if (the_lnet.ln_eqwaitni != NULL) {
935 lnet_ni_decref_locked(the_lnet.ln_eqwaitni);
936 the_lnet.ln_eqwaitni = NULL;
939 /* Drop the cached loopback NI. */
940 if (the_lnet.ln_loni != NULL) {
941 lnet_ni_decref_locked(the_lnet.ln_loni);
942 the_lnet.ln_loni = NULL;
947 /* Clear lazy portals and drop delayed messages which hold refs
948 * on their lnet_msg_t::msg_rxpeer */
949 for (i = 0; i < the_lnet.ln_nportals; i++)
950 LNetClearLazyPortal(i);
952 /* Clear the peer table and wait for all peers to go (they hold refs on
954 lnet_clear_peer_table();
957 /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
958 * and shut them down in guaranteed thread context */
960 while (the_lnet.ln_nzombie_nis != 0) {
962 while (cfs_list_empty(&the_lnet.ln_zombie_nis)) {
966 CDEBUG(D_WARNING,"Waiting for %d zombie NIs\n",
967 the_lnet.ln_nzombie_nis);
968 cfs_pause(cfs_time_seconds(1));
972 ni = cfs_list_entry(the_lnet.ln_zombie_nis.next,
974 cfs_list_del(&ni->ni_list);
975 ni->ni_lnd->lnd_refcount--;
979 islo = ni->ni_lnd->lnd_type == LOLND;
981 LASSERT (!cfs_in_interrupt ());
982 (ni->ni_lnd->lnd_shutdown)(ni);
984 /* can't deref lnd anymore now; it might have unregistered
988 CDEBUG(D_LNI, "Removed LNI %s\n",
989 libcfs_nid2str(ni->ni_nid));
991 LIBCFS_FREE(ni, sizeof(*ni));
994 the_lnet.ln_nzombie_nis--;
997 the_lnet.ln_shutdown = 0;
1000 if (the_lnet.ln_network_tokens != NULL) {
1001 LIBCFS_FREE(the_lnet.ln_network_tokens,
1002 the_lnet.ln_network_tokens_nob);
1003 the_lnet.ln_network_tokens = NULL;
1008 lnet_startup_lndnis (void)
1016 char *nets = lnet_get_networks();
1018 CFS_INIT_LIST_HEAD(&nilist);
1023 rc = lnet_parse_networks(&nilist, nets);
1027 while (!cfs_list_empty(&nilist)) {
1028 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1029 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1031 LASSERT (libcfs_isknown_lnd(lnd_type));
1033 if (lnd_type == CIBLND ||
1034 lnd_type == OPENIBLND ||
1035 lnd_type == IIBLND ||
1036 lnd_type == VIBLND) {
1037 CERROR("LND %s obsoleted\n",
1038 libcfs_lnd2str(lnd_type));
1042 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1043 lnd = lnet_find_lnd_by_type(lnd_type);
1047 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1048 rc = cfs_request_module("%s",
1049 libcfs_lnd2modname(lnd_type));
1050 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1052 lnd = lnet_find_lnd_by_type(lnd_type);
1054 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1055 CERROR("Can't load LND %s, module %s, rc=%d\n",
1056 libcfs_lnd2str(lnd_type),
1057 libcfs_lnd2modname(lnd_type), rc);
1058 #ifndef HAVE_MODULE_LOADING_SUPPORT
1059 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1060 "compiled with kernel module "
1061 "loading support.");
1068 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1069 CERROR("LND %s not supported\n",
1070 libcfs_lnd2str(lnd_type));
1075 ni->ni_refcount = 1;
1078 lnd->lnd_refcount++;
1083 rc = (lnd->lnd_startup)(ni);
1085 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1088 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1090 rc, libcfs_lnd2str(lnd->lnd_type));
1092 lnd->lnd_refcount--;
1097 LASSERT (ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1099 cfs_list_del(&ni->ni_list);
1102 cfs_list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1105 if (lnd->lnd_type == LOLND) {
1107 LASSERT (the_lnet.ln_loni == NULL);
1108 the_lnet.ln_loni = ni;
1113 if (lnd->lnd_wait != NULL) {
1114 if (the_lnet.ln_eqwaitni == NULL) {
1116 the_lnet.ln_eqwaitni = ni;
1119 # ifndef HAVE_LIBPTHREAD
1120 LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1121 "single-threaded runtime\n",
1122 libcfs_lnd2str(lnd_type));
1127 if (ni->ni_peertxcredits == 0 ||
1128 ni->ni_maxtxcredits == 0) {
1129 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1130 libcfs_lnd2str(lnd->lnd_type),
1131 ni->ni_peertxcredits == 0 ?
1136 ni->ni_txcredits = ni->ni_mintxcredits = ni->ni_maxtxcredits;
1138 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1139 libcfs_nid2str(ni->ni_nid),
1140 ni->ni_peertxcredits, ni->ni_txcredits,
1141 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1146 if (the_lnet.ln_eqwaitni != NULL && nicount > 1) {
1147 lnd_type = the_lnet.ln_eqwaitni->ni_lnd->lnd_type;
1148 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1150 libcfs_lnd2str(lnd_type));
1157 lnet_shutdown_lndnis();
1159 while (!cfs_list_empty(&nilist)) {
1160 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1161 cfs_list_del(&ni->ni_list);
1162 LIBCFS_FREE(ni, sizeof(*ni));
1169 * Initialize LNet library.
1171 * Only userspace program needs to call this function - it's automatically
1172 * called in the kernel at module loading time. Caller has to call LNetFini()
1173 * after a call to LNetInit(), if and only if the latter returned 0. It must
1174 * be called exactly once.
1176 * \return 0 on success, and -ve on failures.
1181 lnet_assert_wire_constants ();
1182 LASSERT (!the_lnet.ln_init);
1184 memset(&the_lnet, 0, sizeof(the_lnet));
1187 the_lnet.ln_refcount = 0;
1188 the_lnet.ln_init = 1;
1189 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1190 CFS_INIT_LIST_HEAD(&the_lnet.ln_lnds);
1191 CFS_INIT_LIST_HEAD(&the_lnet.ln_zombie_rcd);
1194 /* All LNDs apart from the LOLND are in separate modules. They
1195 * register themselves when their module loads, and unregister
1196 * themselves when their module is unloaded. */
1199 * NB the order here determines default 'networks=' order */
1201 LNET_REGISTER_ULND(the_ptllnd);
1203 # ifdef HAVE_LIBPTHREAD
1204 LNET_REGISTER_ULND(the_tcplnd);
1207 lnet_register_lnd(&the_lolnd);
1212 * Finalize LNet library.
1214 * Only userspace program needs to call this function. It can be called
1217 * \pre LNetInit() called with success.
1218 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1223 LASSERT (the_lnet.ln_init);
1224 LASSERT (the_lnet.ln_refcount == 0);
1226 while (!cfs_list_empty(&the_lnet.ln_lnds))
1227 lnet_unregister_lnd(cfs_list_entry(the_lnet.ln_lnds.next,
1231 the_lnet.ln_init = 0;
1235 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1237 * Userspace program should call this after a successful call to LNetInit().
1238 * Users must call this function at least once before any other functions.
1239 * For each successful call there must be a corresponding call to
1240 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1243 * The PID used by LNet may be different from the one requested.
1246 * \param requested_pid PID requested by the caller.
1248 * \return 0 on success, and non-zero error code on failures.
1251 LNetNIInit(lnet_pid_t requested_pid)
1253 int im_a_router = 0;
1256 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1258 LASSERT (the_lnet.ln_init);
1259 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1261 if (the_lnet.ln_refcount > 0) {
1262 rc = the_lnet.ln_refcount++;
1266 lnet_get_tunables();
1268 if (requested_pid == LNET_PID_ANY) {
1269 /* Don't instantiate LNET just for me */
1274 rc = lnet_prepare(requested_pid);
1278 rc = lnet_startup_lndnis();
1282 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1286 rc = lnet_check_routes();
1290 rc = lnet_alloc_rtrpools(im_a_router);
1294 rc = lnet_acceptor_start();
1298 the_lnet.ln_refcount = 1;
1299 /* Now I may use my own API functions... */
1301 /* NB router checker needs the_lnet.ln_ping_info in
1302 * lnet_router_checker -> lnet_update_ni_status */
1303 rc = lnet_ping_target_init();
1307 rc = lnet_router_checker_start();
1315 lnet_ping_target_fini();
1317 the_lnet.ln_refcount = 0;
1318 lnet_acceptor_stop();
1320 lnet_destroy_routes();
1321 lnet_shutdown_lndnis();
1327 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1332 * Stop LNet interfaces, routing, and forwarding.
1334 * Users must call this function once for each successful call to LNetNIInit().
1335 * Once the LNetNIFini() operation has been started, the results of pending
1336 * API operations are undefined.
1338 * \return always 0 for current implementation.
1343 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1345 LASSERT (the_lnet.ln_init);
1346 LASSERT (the_lnet.ln_refcount > 0);
1348 if (the_lnet.ln_refcount != 1) {
1349 the_lnet.ln_refcount--;
1351 LASSERT (!the_lnet.ln_niinit_self);
1354 lnet_router_checker_stop();
1355 lnet_ping_target_fini();
1357 /* Teardown fns that use my own API functions BEFORE here */
1358 the_lnet.ln_refcount = 0;
1360 lnet_acceptor_stop();
1361 lnet_destroy_routes();
1362 lnet_shutdown_lndnis();
1366 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1371 * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1372 * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1373 * internal ioctl handler.
1375 * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1377 * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1378 * The data will be printed to system console. Don't use it excessively.
1379 * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1381 * \return Always return 0 when called by users directly (i.e., not via ioctl).
1384 LNetCtl(unsigned int cmd, void *arg)
1386 struct libcfs_ioctl_data *data = arg;
1387 lnet_process_id_t id = {0};
1391 LASSERT (the_lnet.ln_init);
1392 LASSERT (the_lnet.ln_refcount > 0);
1395 case IOC_LIBCFS_GET_NI:
1396 rc = LNetGetId(data->ioc_count, &id);
1397 data->ioc_nid = id.nid;
1400 case IOC_LIBCFS_FAIL_NID:
1401 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1403 case IOC_LIBCFS_ADD_ROUTE:
1404 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1406 return (rc != 0) ? rc : lnet_check_routes();
1408 case IOC_LIBCFS_DEL_ROUTE:
1409 return lnet_del_route(data->ioc_net, data->ioc_nid);
1411 case IOC_LIBCFS_GET_ROUTE:
1412 return lnet_get_route(data->ioc_count,
1413 &data->ioc_net, &data->ioc_count,
1414 &data->ioc_nid, &data->ioc_flags);
1415 case IOC_LIBCFS_NOTIFY_ROUTER:
1416 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1417 cfs_time_current() -
1418 cfs_time_seconds(cfs_time_current_sec() -
1419 (time_t)data->ioc_u64[0]));
1421 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1422 /* This can be removed once lustre stops calling it */
1425 case IOC_LIBCFS_LNET_DIST:
1426 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1427 if (rc < 0 && rc != -EHOSTUNREACH)
1430 data->ioc_u32[0] = rc;
1433 case IOC_LIBCFS_TESTPROTOCOMPAT:
1435 the_lnet.ln_testprotocompat = data->ioc_flags;
1439 case IOC_LIBCFS_PING:
1440 id.nid = data->ioc_nid;
1441 id.pid = data->ioc_u32[0];
1442 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1443 (lnet_process_id_t *)data->ioc_pbuf1,
1444 data->ioc_plen1/sizeof(lnet_process_id_t));
1447 data->ioc_count = rc;
1450 case IOC_LIBCFS_DEBUG_PEER: {
1451 /* CAVEAT EMPTOR: this one designed for calling directly; not
1453 id = *((lnet_process_id_t *) arg);
1455 lnet_debug_peer(id.nid);
1457 ni = lnet_net2ni(LNET_NIDNET(id.nid));
1459 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1461 if (ni->ni_lnd->lnd_ctl == NULL) {
1462 CDEBUG(D_WARNING, "No ctl for %s\n",
1465 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1474 ni = lnet_net2ni(data->ioc_net);
1478 if (ni->ni_lnd->lnd_ctl == NULL)
1481 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1490 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
1491 * all interfaces share a same PID, as requested by LNetNIInit().
1493 * \param index Index of the interface to look up.
1494 * \param id On successful return, this location will hold the
1495 * lnet_process_id_t ID of the interface.
1497 * \retval 0 If an interface exists at \a index.
1498 * \retval -ENOENT If no interface has been found.
1501 LNetGetId(unsigned int index, lnet_process_id_t *id)
1507 LASSERT (the_lnet.ln_init);
1508 LASSERT (the_lnet.ln_refcount > 0);
1512 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
1516 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
1518 id->nid = ni->ni_nid;
1519 id->pid = the_lnet.ln_pid;
1530 * Print a string representation of handle \a h into buffer \a str of
1534 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1536 snprintf(str, len, LPX64, h.cookie);
1540 lnet_create_ping_info(void)
1545 unsigned int infosz;
1547 lnet_process_id_t id;
1548 lnet_ping_info_t *pinfo;
1550 for (n = 0; ; n++) {
1551 rc = LNetGetId(n, &id);
1558 infosz = offsetof(lnet_ping_info_t, pi_ni[n]);
1559 LIBCFS_ALLOC(pinfo, infosz);
1560 if (pinfo == NULL) {
1561 CERROR("Can't allocate ping info[%d]\n", n);
1566 pinfo->pi_pid = the_lnet.ln_pid;
1567 pinfo->pi_magic = LNET_PROTO_PING_MAGIC;
1568 pinfo->pi_version = LNET_PROTO_PING_VERSION;
1570 for (i = 0; i < n; i++) {
1571 lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1573 rc = LNetGetId(i, &id);
1576 ns->ns_nid = id.nid;
1577 ns->ns_status = LNET_NI_STATUS_UP;
1581 ni = lnet_nid2ni_locked(id.nid);
1582 LASSERT (ni != NULL);
1583 LASSERT (ni->ni_status == NULL);
1585 lnet_ni_decref_locked(ni);
1590 the_lnet.ln_ping_info = pinfo;
1595 lnet_destroy_ping_info(void)
1601 cfs_list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
1602 ni->ni_status = NULL;
1607 LIBCFS_FREE(the_lnet.ln_ping_info,
1608 offsetof(lnet_ping_info_t,
1609 pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1610 the_lnet.ln_ping_info = NULL;
1615 lnet_ping_target_init(void)
1618 lnet_handle_me_t meh;
1619 lnet_process_id_t id;
1624 rc = lnet_create_ping_info();
1628 /* We can have a tiny EQ since we only need to see the unlink event on
1629 * teardown, which by definition is the last one! */
1630 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1632 CERROR("Can't allocate ping EQ: %d\n", rc);
1636 memset(&id, 0, sizeof(lnet_process_id_t));
1637 id.nid = LNET_NID_ANY;
1638 id.pid = LNET_PID_ANY;
1640 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1641 LNET_PROTO_PING_MATCHBITS, 0,
1642 LNET_UNLINK, LNET_INS_AFTER,
1645 CERROR("Can't create ping ME: %d\n", rc);
1649 /* initialize md content */
1650 infosz = offsetof(lnet_ping_info_t,
1651 pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1652 md.start = the_lnet.ln_ping_info;
1654 md.threshold = LNET_MD_THRESH_INF;
1656 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1657 LNET_MD_MANAGE_REMOTE;
1659 md.eq_handle = the_lnet.ln_ping_target_eq;
1661 rc = LNetMDAttach(meh, md,
1663 &the_lnet.ln_ping_target_md);
1665 CERROR("Can't attach ping MD: %d\n", rc);
1672 rc2 = LNetMEUnlink(meh);
1675 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1678 lnet_destroy_ping_info();
1683 lnet_ping_target_fini(void)
1688 int timeout_ms = 1000;
1689 cfs_sigset_t blocked = cfs_block_allsigs();
1691 LNetMDUnlink(the_lnet.ln_ping_target_md);
1692 /* NB md could be busy; this just starts the unlink */
1695 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1696 timeout_ms, &event, &which);
1698 /* I expect overflow... */
1699 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1702 /* timed out: provide a diagnostic */
1703 CWARN("Still waiting for ping MD to unlink\n");
1708 /* Got a valid event */
1713 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1715 lnet_destroy_ping_info();
1716 cfs_restore_sigs(blocked);
1720 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1722 lnet_handle_eq_t eqh;
1723 lnet_handle_md_t mdh;
1729 const int a_long_time = 60000; /* mS */
1730 int infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1731 lnet_ping_info_t *info;
1732 lnet_process_id_t tmpid;
1737 cfs_sigset_t blocked;
1740 id.nid == LNET_NID_ANY ||
1741 timeout_ms > 500000 || /* arbitrary limit! */
1742 n_ids > 20) /* arbitrary limit! */
1745 if (id.pid == LNET_PID_ANY)
1746 id.pid = LUSTRE_SRV_LNET_PID;
1748 LIBCFS_ALLOC(info, infosz);
1752 /* NB 2 events max (including any unlink event) */
1753 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1755 CERROR("Can't allocate EQ: %d\n", rc);
1759 /* initialize md content */
1762 md.threshold = 2; /*GET/REPLY*/
1764 md.options = LNET_MD_TRUNCATE;
1768 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1770 CERROR("Can't bind MD: %d\n", rc);
1774 rc = LNetGet(LNET_NID_ANY, mdh, id,
1775 LNET_RESERVED_PORTAL,
1776 LNET_PROTO_PING_MATCHBITS, 0);
1779 /* Don't CERROR; this could be deliberate! */
1781 rc2 = LNetMDUnlink(mdh);
1784 /* NB must wait for the UNLINK event below... */
1786 timeout_ms = a_long_time;
1790 /* MUST block for unlink to complete */
1792 blocked = cfs_block_allsigs();
1794 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1797 cfs_restore_sigs(blocked);
1799 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1800 (rc2 <= 0) ? -1 : event.type,
1801 (rc2 <= 0) ? -1 : event.status,
1802 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1804 LASSERT (rc2 != -EOVERFLOW); /* can't miss anything */
1806 if (rc2 <= 0 || event.status != 0) {
1807 /* timeout or error */
1808 if (!replied && rc == 0)
1809 rc = (rc2 < 0) ? rc2 :
1810 (rc2 == 0) ? -ETIMEDOUT :
1814 /* Ensure completion in finite time... */
1816 /* No assertion (racing with network) */
1818 timeout_ms = a_long_time;
1819 } else if (rc2 == 0) {
1820 /* timed out waiting for unlink */
1821 CWARN("ping %s: late network completion\n",
1824 } else if (event.type == LNET_EVENT_REPLY) {
1829 } while (rc2 <= 0 || !event.unlinked);
1833 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
1840 LASSERT (nob >= 0 && nob <= infosz);
1842 rc = -EPROTO; /* if I can't parse... */
1845 /* can't check magic/version */
1846 CERROR("%s: ping info too short %d\n",
1847 libcfs_id2str(id), nob);
1851 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
1852 lnet_swap_pinginfo(info);
1853 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
1854 CERROR("%s: Unexpected magic %08x\n",
1855 libcfs_id2str(id), info->pi_magic);
1859 if (info->pi_version != LNET_PROTO_PING_VERSION) {
1860 CERROR("%s: Unexpected version 0x%x\n",
1861 libcfs_id2str(id), info->pi_version);
1865 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
1866 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
1867 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
1871 if (info->pi_nnis < n_ids)
1872 n_ids = info->pi_nnis;
1874 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
1875 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
1876 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
1880 rc = -EFAULT; /* If I SEGV... */
1882 for (i = 0; i < n_ids; i++) {
1883 tmpid.pid = info->pi_pid;
1884 tmpid.nid = info->pi_ni[i].ns_nid;
1886 if (cfs_copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
1895 rc2 = LNetEQFree(eqh);
1897 CERROR("rc2 %d\n", rc2);
1901 LIBCFS_FREE(info, infosz);