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_eq_wait_lock);
95 cfs_waitq_init(&the_lnet.ln_eq_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_eq_wait_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_eq_wait_lock == 0);
190 void lnet_init_locks(void)
192 pthread_cond_init(&the_lnet.ln_eq_cond, NULL);
193 pthread_mutex_init(&the_lnet.ln_eq_wait_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_eq_wait_lock);
203 pthread_cond_destroy(&the_lnet.ln_eq_cond);
210 lnet_create_locks(void)
214 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
215 if (the_lnet.ln_res_lock == NULL)
218 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
219 if (the_lnet.ln_net_lock == NULL)
230 lnet_destroy_locks(void)
232 if (the_lnet.ln_res_lock != NULL) {
233 cfs_percpt_lock_free(the_lnet.ln_res_lock);
234 the_lnet.ln_res_lock = NULL;
237 if (the_lnet.ln_net_lock != NULL) {
238 cfs_percpt_lock_free(the_lnet.ln_net_lock);
239 the_lnet.ln_net_lock = NULL;
245 void lnet_assert_wire_constants (void)
247 /* Wire protocol assertions generated by 'wirecheck'
248 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
249 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
250 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
253 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
254 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
255 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
256 CLASSERT (LNET_MSG_ACK == 0);
257 CLASSERT (LNET_MSG_PUT == 1);
258 CLASSERT (LNET_MSG_GET == 2);
259 CLASSERT (LNET_MSG_REPLY == 3);
260 CLASSERT (LNET_MSG_HELLO == 4);
262 /* Checks for struct ptl_handle_wire_t */
263 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
264 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
265 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
266 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
267 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
269 /* Checks for struct lnet_magicversion_t */
270 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
271 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
272 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
273 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
274 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
275 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
276 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
278 /* Checks for struct lnet_hdr_t */
279 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
280 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
281 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
282 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
283 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
284 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
285 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
286 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
287 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
288 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
289 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
290 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
291 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
292 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
293 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
296 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
297 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
298 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
299 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
300 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
301 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
304 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
305 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
306 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
307 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
308 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
309 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
310 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
311 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
312 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
313 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
316 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
317 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
318 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
319 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
320 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
321 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
322 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
323 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
324 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
325 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
328 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
329 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
332 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
333 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
334 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
335 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
339 lnet_find_lnd_by_type (int type)
344 /* holding lnd mutex */
345 cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
346 lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
348 if ((int)lnd->lnd_type == type)
356 lnet_register_lnd (lnd_t *lnd)
358 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
360 LASSERT (the_lnet.ln_init);
361 LASSERT (libcfs_isknown_lnd(lnd->lnd_type));
362 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
364 cfs_list_add_tail (&lnd->lnd_list, &the_lnet.ln_lnds);
365 lnd->lnd_refcount = 0;
367 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
369 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
373 lnet_unregister_lnd (lnd_t *lnd)
375 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
377 LASSERT (the_lnet.ln_init);
378 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
379 LASSERT (lnd->lnd_refcount == 0);
381 cfs_list_del (&lnd->lnd_list);
382 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
384 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
388 lnet_counters_get(lnet_counters_t *counters)
390 lnet_counters_t *ctr;
393 memset(counters, 0, sizeof(*counters));
395 lnet_net_lock(LNET_LOCK_EX);
397 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
398 counters->msgs_max += ctr->msgs_max;
399 counters->msgs_alloc += ctr->msgs_alloc;
400 counters->errors += ctr->errors;
401 counters->send_count += ctr->send_count;
402 counters->recv_count += ctr->recv_count;
403 counters->route_count += ctr->route_count;
404 counters->drop_length += ctr->drop_length;
405 counters->send_length += ctr->send_length;
406 counters->recv_length += ctr->recv_length;
407 counters->route_length += ctr->route_length;
408 counters->drop_length += ctr->drop_length;
411 lnet_net_unlock(LNET_LOCK_EX);
413 EXPORT_SYMBOL(lnet_counters_get);
416 lnet_counters_reset(void)
418 lnet_counters_t *counters;
421 lnet_net_lock(LNET_LOCK_EX);
423 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
424 memset(counters, 0, sizeof(lnet_counters_t));
426 lnet_net_unlock(LNET_LOCK_EX);
428 EXPORT_SYMBOL(lnet_counters_reset);
430 #ifdef LNET_USE_LIB_FREELIST
433 lnet_freelist_init (lnet_freelist_t *fl, int n, int size)
439 size += offsetof (lnet_freeobj_t, fo_contents);
441 LIBCFS_ALLOC(space, n * size);
445 CFS_INIT_LIST_HEAD (&fl->fl_list);
448 fl->fl_objsize = size;
452 memset (space, 0, size);
453 cfs_list_add ((cfs_list_t *)space, &fl->fl_list);
461 lnet_freelist_fini (lnet_freelist_t *fl)
466 if (fl->fl_nobjs == 0)
470 for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
473 LASSERT (count == fl->fl_nobjs);
475 LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
476 memset (fl, 0, sizeof (*fl));
479 #endif /* LNET_USE_LIB_FREELIST */
482 lnet_create_interface_cookie (void)
484 /* NB the interface cookie in wire handles guards against delayed
485 * replies and ACKs appearing valid after reboot. Initialisation time,
486 * even if it's only implemented to millisecond resolution is probably
487 * easily good enough. */
491 int rc = gettimeofday (&tv, NULL);
494 cfs_gettimeofday(&tv);
498 cookie += tv.tv_usec;
503 lnet_res_type2str(int type)
508 case LNET_COOKIE_TYPE_MD:
510 case LNET_COOKIE_TYPE_ME:
512 case LNET_COOKIE_TYPE_EQ:
518 lnet_res_container_cleanup(struct lnet_res_container *rec)
522 if (rec->rec_type == 0) /* not set yet, it's a uninitialized */
525 while (!cfs_list_empty(&rec->rec_active)) {
526 cfs_list_t *e = rec->rec_active.next;
528 cfs_list_del_init(e);
529 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
530 lnet_eq_free(cfs_list_entry(e, lnet_eq_t, eq_list));
532 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
533 lnet_md_free(cfs_list_entry(e, lnet_libmd_t, md_list));
535 } else { /* NB: Active MEs should be attached on portals */
542 /* Found alive MD/ME/EQ, user really should unlink/free
543 * all of them before finalize LNet, but if someone didn't,
544 * we have to recycle garbage for him */
545 CERROR("%d active elements on exit of %s container\n",
546 count, lnet_res_type2str(rec->rec_type));
549 #ifdef LNET_USE_LIB_FREELIST
550 lnet_freelist_fini(&rec->rec_freelist);
552 if (rec->rec_lh_hash != NULL) {
553 LIBCFS_FREE(rec->rec_lh_hash,
554 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
555 rec->rec_lh_hash = NULL;
558 rec->rec_type = 0; /* mark it as finalized */
562 lnet_res_container_setup(struct lnet_res_container *rec,
563 int cpt, int type, int objnum, int objsz)
568 LASSERT(rec->rec_type == 0);
570 rec->rec_type = type;
571 CFS_INIT_LIST_HEAD(&rec->rec_active);
573 #ifdef LNET_USE_LIB_FREELIST
574 memset(&rec->rec_freelist, 0, sizeof(rec->rec_freelist));
575 rc = lnet_freelist_init(&rec->rec_freelist, objnum, objsz);
579 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
581 /* Arbitrary choice of hash table size */
582 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
583 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
584 if (rec->rec_lh_hash == NULL) {
589 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
590 CFS_INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
595 CERROR("Failed to setup %s resource container\n",
596 lnet_res_type2str(type));
597 lnet_res_container_cleanup(rec);
602 lnet_res_containers_destroy(struct lnet_res_container **recs)
604 struct lnet_res_container *rec;
607 cfs_percpt_for_each(rec, i, recs)
608 lnet_res_container_cleanup(rec);
610 cfs_percpt_free(recs);
613 static struct lnet_res_container **
614 lnet_res_containers_create(int type, int objnum, int objsz)
616 struct lnet_res_container **recs;
617 struct lnet_res_container *rec;
621 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
623 CERROR("Failed to allocate %s resource containers\n",
624 lnet_res_type2str(type));
628 cfs_percpt_for_each(rec, i, recs) {
629 rc = lnet_res_container_setup(rec, i, type, objnum, objsz);
631 lnet_res_containers_destroy(recs);
640 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
642 /* ALWAYS called with lnet_res_lock held */
644 lnet_libhandle_t *lh;
647 if ((cookie & (LNET_COOKIE_TYPES - 1)) != rec->rec_type)
650 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
651 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
653 cfs_list_for_each_entry(lh, head, lh_hash_chain) {
654 if (lh->lh_cookie == cookie)
662 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
664 /* ALWAYS called with lnet_res_lock held */
665 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
668 lh->lh_cookie = rec->rec_lh_cookie;
669 rec->rec_lh_cookie += 1 << ibits;
671 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
673 cfs_list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
678 * Reserved API - do not use.
679 * Temporary workaround to allow uOSS and test programs force server
680 * mode in userspace. See comments near ln_server_mode_flag in
681 * lnet/lib-types.h */
685 the_lnet.ln_server_mode_flag = 1;
689 int lnet_unprepare(void);
692 lnet_prepare(lnet_pid_t requested_pid)
694 /* Prepare to bring up the network */
695 struct lnet_res_container **recs;
698 LASSERT (the_lnet.ln_refcount == 0);
700 the_lnet.ln_routing = 0;
703 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
704 the_lnet.ln_pid = requested_pid;
706 if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
707 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
709 if (cfs_curproc_uid())/* Only root can run user-space server */
711 the_lnet.ln_pid = requested_pid;
713 } else {/* client case (liblustre) */
715 /* My PID must be unique on this node and flag I'm userspace */
716 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
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_nis_cpt);
723 CFS_INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
724 CFS_INIT_LIST_HEAD(&the_lnet.ln_remote_nets);
725 CFS_INIT_LIST_HEAD(&the_lnet.ln_routers);
727 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
729 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
730 sizeof(lnet_counters_t));
731 if (the_lnet.ln_counters == NULL) {
732 CERROR("Failed to allocate counters for LNet\n");
737 rc = lnet_peer_tables_create();
741 /* NB: we will have instance of message container per CPT soon */
742 rc = lnet_msg_containers_create();
746 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
747 LNET_COOKIE_TYPE_EQ, LNET_FL_MAX_EQS,
752 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME, LNET_FL_MAX_MES,
757 the_lnet.ln_me_containers = recs;
759 /* NB: we will have instance of MD container per CPT soon */
760 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD, LNET_FL_MAX_MDS,
761 sizeof(lnet_libmd_t));
765 the_lnet.ln_md_containers = recs;
767 rc = lnet_portals_create();
769 CERROR("Failed to create portals for LNet: %d\n", rc);
781 lnet_unprepare (void)
783 /* NB no LNET_LOCK since this is the last reference. All LND instances
784 * have shut down already, so it is safe to unlink and free all
785 * descriptors, even those that appear committed to a network op (eg MD
786 * with non-zero pending count) */
788 lnet_fail_nid(LNET_NID_ANY, 0);
790 LASSERT(the_lnet.ln_refcount == 0);
791 LASSERT(cfs_list_empty(&the_lnet.ln_test_peers));
792 LASSERT(cfs_list_empty(&the_lnet.ln_nis));
793 LASSERT(cfs_list_empty(&the_lnet.ln_nis_cpt));
794 LASSERT(cfs_list_empty(&the_lnet.ln_nis_zombie));
796 lnet_portals_destroy();
798 if (the_lnet.ln_md_containers != NULL) {
799 lnet_res_containers_destroy(the_lnet.ln_md_containers);
800 the_lnet.ln_md_containers = NULL;
803 if (the_lnet.ln_me_containers != NULL) {
804 lnet_res_containers_destroy(the_lnet.ln_me_containers);
805 the_lnet.ln_me_containers = NULL;
808 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
810 lnet_msg_containers_destroy();
811 lnet_peer_tables_destroy();
812 lnet_rtrpools_free();
814 if (the_lnet.ln_counters != NULL) {
815 cfs_percpt_free(the_lnet.ln_counters);
816 the_lnet.ln_counters = NULL;
823 lnet_net2ni_locked(__u32 net, int cpt)
828 LASSERT(cpt != LNET_LOCK_EX);
830 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
831 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
833 if (LNET_NIDNET(ni->ni_nid) == net) {
834 lnet_ni_addref_locked(ni, cpt);
843 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
848 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
853 val = cfs_hash_long(key, LNET_CPT_BITS);
854 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
858 return (unsigned int)((key + val + (val >> 1)) % number);
862 lnet_cpt_of_nid_locked(lnet_nid_t nid)
866 /* must called with hold of lnet_net_lock */
867 if (LNET_CPT_NUMBER == 1)
868 return 0; /* the only one */
870 /* take lnet_net_lock(any) would be OK */
871 if (!cfs_list_empty(&the_lnet.ln_nis_cpt)) {
872 cfs_list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
873 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
876 LASSERT(ni->ni_cpts != NULL);
877 return ni->ni_cpts[lnet_nid_cpt_hash
878 (nid, ni->ni_ncpts)];
882 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
886 lnet_cpt_of_nid(lnet_nid_t nid)
891 if (LNET_CPT_NUMBER == 1)
892 return 0; /* the only one */
894 if (cfs_list_empty(&the_lnet.ln_nis_cpt))
895 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
897 cpt = lnet_net_lock_current();
898 cpt2 = lnet_cpt_of_nid_locked(nid);
899 lnet_net_unlock(cpt);
903 EXPORT_SYMBOL(lnet_cpt_of_nid);
906 lnet_islocalnet(__u32 net)
911 cpt = lnet_net_lock_current();
913 ni = lnet_net2ni_locked(net, cpt);
915 lnet_ni_decref_locked(ni, cpt);
917 lnet_net_unlock(cpt);
923 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
928 LASSERT(cpt != LNET_LOCK_EX);
930 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
931 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
933 if (ni->ni_nid == nid) {
934 lnet_ni_addref_locked(ni, cpt);
943 lnet_islocalnid(lnet_nid_t nid)
948 cpt = lnet_net_lock_current();
949 ni = lnet_nid2ni_locked(nid, cpt);
951 lnet_ni_decref_locked(ni, cpt);
952 lnet_net_unlock(cpt);
958 lnet_count_acceptor_nis (void)
960 /* Return the # of NIs that need the acceptor. */
962 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
967 cpt = lnet_net_lock_current();
968 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
969 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
971 if (ni->ni_lnd->lnd_accept != NULL)
975 lnet_net_unlock(cpt);
977 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
982 lnet_ni_tq_credits(lnet_ni_t *ni)
986 LASSERT(ni->ni_ncpts >= 1);
988 if (ni->ni_ncpts == 1)
989 return ni->ni_maxtxcredits;
991 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
992 credits = max(credits, 8 * ni->ni_peertxcredits);
993 credits = min(credits, ni->ni_maxtxcredits);
999 lnet_shutdown_lndnis (void)
1005 /* NB called holding the global mutex */
1007 /* All quiet on the API front */
1008 LASSERT(!the_lnet.ln_shutdown);
1009 LASSERT(the_lnet.ln_refcount == 0);
1010 LASSERT(cfs_list_empty(&the_lnet.ln_nis_zombie));
1011 LASSERT(cfs_list_empty(&the_lnet.ln_remote_nets));
1013 lnet_net_lock(LNET_LOCK_EX);
1014 the_lnet.ln_shutdown = 1; /* flag shutdown */
1016 /* Unlink NIs from the global table */
1017 while (!cfs_list_empty(&the_lnet.ln_nis)) {
1018 ni = cfs_list_entry(the_lnet.ln_nis.next,
1019 lnet_ni_t, ni_list);
1020 /* move it to zombie list and nobody can find it anymore */
1021 cfs_list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1022 lnet_ni_decref_locked(ni, 0); /* drop ln_nis' ref */
1024 if (!cfs_list_empty(&ni->ni_cptlist)) {
1025 cfs_list_del_init(&ni->ni_cptlist);
1026 lnet_ni_decref_locked(ni, 0);
1030 /* Drop the cached eqwait NI. */
1031 if (the_lnet.ln_eq_waitni != NULL) {
1032 lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
1033 the_lnet.ln_eq_waitni = NULL;
1036 /* Drop the cached loopback NI. */
1037 if (the_lnet.ln_loni != NULL) {
1038 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1039 the_lnet.ln_loni = NULL;
1042 lnet_net_unlock(LNET_LOCK_EX);
1044 /* Clear lazy portals and drop delayed messages which hold refs
1045 * on their lnet_msg_t::msg_rxpeer */
1046 for (i = 0; i < the_lnet.ln_nportals; i++)
1047 LNetClearLazyPortal(i);
1049 /* Clear the peer table and wait for all peers to go (they hold refs on
1051 lnet_peer_tables_cleanup();
1053 lnet_net_lock(LNET_LOCK_EX);
1054 /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
1055 * and shut them down in guaranteed thread context */
1057 while (!cfs_list_empty(&the_lnet.ln_nis_zombie)) {
1061 ni = cfs_list_entry(the_lnet.ln_nis_zombie.next,
1062 lnet_ni_t, ni_list);
1063 cfs_list_del_init(&ni->ni_list);
1064 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1067 /* still busy, add it back to zombie list */
1068 cfs_list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1072 while (!cfs_list_empty(&ni->ni_list)) {
1073 lnet_net_unlock(LNET_LOCK_EX);
1075 if ((i & (-i)) == i) {
1077 "Waiting for zombie LNI %s\n",
1078 libcfs_nid2str(ni->ni_nid));
1080 cfs_pause(cfs_time_seconds(1));
1081 lnet_net_lock(LNET_LOCK_EX);
1085 ni->ni_lnd->lnd_refcount--;
1086 lnet_net_unlock(LNET_LOCK_EX);
1088 islo = ni->ni_lnd->lnd_type == LOLND;
1090 LASSERT (!cfs_in_interrupt ());
1091 (ni->ni_lnd->lnd_shutdown)(ni);
1093 /* can't deref lnd anymore now; it might have unregistered
1097 CDEBUG(D_LNI, "Removed LNI %s\n",
1098 libcfs_nid2str(ni->ni_nid));
1101 lnet_net_lock(LNET_LOCK_EX);
1104 the_lnet.ln_shutdown = 0;
1105 lnet_net_unlock(LNET_LOCK_EX);
1107 if (the_lnet.ln_network_tokens != NULL) {
1108 LIBCFS_FREE(the_lnet.ln_network_tokens,
1109 the_lnet.ln_network_tokens_nob);
1110 the_lnet.ln_network_tokens = NULL;
1115 lnet_startup_lndnis (void)
1119 struct lnet_tx_queue *tq;
1125 char *nets = lnet_get_networks();
1127 CFS_INIT_LIST_HEAD(&nilist);
1132 rc = lnet_parse_networks(&nilist, nets);
1136 while (!cfs_list_empty(&nilist)) {
1137 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1138 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1140 LASSERT (libcfs_isknown_lnd(lnd_type));
1142 if (lnd_type == CIBLND ||
1143 lnd_type == OPENIBLND ||
1144 lnd_type == IIBLND ||
1145 lnd_type == VIBLND) {
1146 CERROR("LND %s obsoleted\n",
1147 libcfs_lnd2str(lnd_type));
1151 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1152 lnd = lnet_find_lnd_by_type(lnd_type);
1156 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1157 rc = cfs_request_module("%s",
1158 libcfs_lnd2modname(lnd_type));
1159 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1161 lnd = lnet_find_lnd_by_type(lnd_type);
1163 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1164 CERROR("Can't load LND %s, module %s, rc=%d\n",
1165 libcfs_lnd2str(lnd_type),
1166 libcfs_lnd2modname(lnd_type), rc);
1167 #ifndef HAVE_MODULE_LOADING_SUPPORT
1168 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1169 "compiled with kernel module "
1170 "loading support.");
1177 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1178 CERROR("LND %s not supported\n",
1179 libcfs_lnd2str(lnd_type));
1184 lnet_net_lock(LNET_LOCK_EX);
1185 lnd->lnd_refcount++;
1186 lnet_net_unlock(LNET_LOCK_EX);
1190 rc = (lnd->lnd_startup)(ni);
1192 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1195 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1197 rc, libcfs_lnd2str(lnd->lnd_type));
1198 lnet_net_lock(LNET_LOCK_EX);
1199 lnd->lnd_refcount--;
1200 lnet_net_unlock(LNET_LOCK_EX);
1204 LASSERT (ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1206 cfs_list_del(&ni->ni_list);
1208 lnet_net_lock(LNET_LOCK_EX);
1209 /* refcount for ln_nis */
1210 lnet_ni_addref_locked(ni, 0);
1211 cfs_list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1212 if (ni->ni_cpts != NULL) {
1213 cfs_list_add_tail(&ni->ni_cptlist,
1214 &the_lnet.ln_nis_cpt);
1215 lnet_ni_addref_locked(ni, 0);
1218 lnet_net_unlock(LNET_LOCK_EX);
1220 if (lnd->lnd_type == LOLND) {
1222 LASSERT (the_lnet.ln_loni == NULL);
1223 the_lnet.ln_loni = ni;
1228 if (lnd->lnd_wait != NULL) {
1229 if (the_lnet.ln_eq_waitni == NULL) {
1231 the_lnet.ln_eq_waitni = ni;
1234 # ifndef HAVE_LIBPTHREAD
1235 LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1236 "single-threaded runtime\n",
1237 libcfs_lnd2str(lnd_type));
1242 if (ni->ni_peertxcredits == 0 ||
1243 ni->ni_maxtxcredits == 0) {
1244 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1245 libcfs_lnd2str(lnd->lnd_type),
1246 ni->ni_peertxcredits == 0 ?
1251 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1252 tq->tq_credits_min =
1253 tq->tq_credits_max =
1254 tq->tq_credits = lnet_ni_tq_credits(ni);
1257 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1258 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1259 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1260 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1265 if (the_lnet.ln_eq_waitni != NULL && nicount > 1) {
1266 lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1267 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1269 libcfs_lnd2str(lnd_type));
1276 lnet_shutdown_lndnis();
1278 while (!cfs_list_empty(&nilist)) {
1279 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1280 cfs_list_del(&ni->ni_list);
1288 * Initialize LNet library.
1290 * Only userspace program needs to call this function - it's automatically
1291 * called in the kernel at module loading time. Caller has to call LNetFini()
1292 * after a call to LNetInit(), if and only if the latter returned 0. It must
1293 * be called exactly once.
1295 * \return 0 on success, and -ve on failures.
1302 lnet_assert_wire_constants ();
1303 LASSERT (!the_lnet.ln_init);
1305 memset(&the_lnet, 0, sizeof(the_lnet));
1307 /* refer to global cfs_cpt_table for now */
1308 the_lnet.ln_cpt_table = cfs_cpt_table;
1309 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1311 LASSERT(the_lnet.ln_cpt_number > 0);
1312 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1313 /* we are under risk of consuming all lh_cookie */
1314 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1315 "please change setting of CPT-table and retry\n",
1316 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1320 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1321 the_lnet.ln_cpt_bits++;
1323 rc = lnet_create_locks();
1325 CERROR("Can't create LNet global locks: %d\n", rc);
1329 the_lnet.ln_refcount = 0;
1330 the_lnet.ln_init = 1;
1331 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1332 CFS_INIT_LIST_HEAD(&the_lnet.ln_lnds);
1333 CFS_INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1334 CFS_INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1337 /* All LNDs apart from the LOLND are in separate modules. They
1338 * register themselves when their module loads, and unregister
1339 * themselves when their module is unloaded. */
1342 * NB the order here determines default 'networks=' order */
1344 LNET_REGISTER_ULND(the_ptllnd);
1346 # ifdef HAVE_LIBPTHREAD
1347 LNET_REGISTER_ULND(the_tcplnd);
1350 lnet_register_lnd(&the_lolnd);
1355 * Finalize LNet library.
1357 * Only userspace program needs to call this function. It can be called
1360 * \pre LNetInit() called with success.
1361 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1366 LASSERT(the_lnet.ln_init);
1367 LASSERT(the_lnet.ln_refcount == 0);
1369 while (!cfs_list_empty(&the_lnet.ln_lnds))
1370 lnet_unregister_lnd(cfs_list_entry(the_lnet.ln_lnds.next,
1372 lnet_destroy_locks();
1374 the_lnet.ln_init = 0;
1378 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1380 * Userspace program should call this after a successful call to LNetInit().
1381 * Users must call this function at least once before any other functions.
1382 * For each successful call there must be a corresponding call to
1383 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1386 * The PID used by LNet may be different from the one requested.
1389 * \param requested_pid PID requested by the caller.
1391 * \return >= 0 on success, and < 0 error code on failures.
1394 LNetNIInit(lnet_pid_t requested_pid)
1396 int im_a_router = 0;
1399 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1401 LASSERT (the_lnet.ln_init);
1402 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1404 if (the_lnet.ln_refcount > 0) {
1405 rc = the_lnet.ln_refcount++;
1409 lnet_get_tunables();
1411 if (requested_pid == LNET_PID_ANY) {
1412 /* Don't instantiate LNET just for me */
1417 rc = lnet_prepare(requested_pid);
1421 rc = lnet_startup_lndnis();
1425 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1429 rc = lnet_check_routes();
1433 rc = lnet_rtrpools_alloc(im_a_router);
1437 rc = lnet_acceptor_start();
1441 the_lnet.ln_refcount = 1;
1442 /* Now I may use my own API functions... */
1444 /* NB router checker needs the_lnet.ln_ping_info in
1445 * lnet_router_checker -> lnet_update_ni_status_locked */
1446 rc = lnet_ping_target_init();
1450 rc = lnet_router_checker_start();
1458 lnet_ping_target_fini();
1460 the_lnet.ln_refcount = 0;
1461 lnet_acceptor_stop();
1463 lnet_destroy_routes();
1464 lnet_shutdown_lndnis();
1470 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1475 * Stop LNet interfaces, routing, and forwarding.
1477 * Users must call this function once for each successful call to LNetNIInit().
1478 * Once the LNetNIFini() operation has been started, the results of pending
1479 * API operations are undefined.
1481 * \return always 0 for current implementation.
1486 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1488 LASSERT (the_lnet.ln_init);
1489 LASSERT (the_lnet.ln_refcount > 0);
1491 if (the_lnet.ln_refcount != 1) {
1492 the_lnet.ln_refcount--;
1494 LASSERT (!the_lnet.ln_niinit_self);
1497 lnet_router_checker_stop();
1498 lnet_ping_target_fini();
1500 /* Teardown fns that use my own API functions BEFORE here */
1501 the_lnet.ln_refcount = 0;
1503 lnet_acceptor_stop();
1504 lnet_destroy_routes();
1505 lnet_shutdown_lndnis();
1509 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1514 * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1515 * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1516 * internal ioctl handler.
1518 * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1520 * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1521 * The data will be printed to system console. Don't use it excessively.
1522 * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1524 * \return Always return 0 when called by users directly (i.e., not via ioctl).
1527 LNetCtl(unsigned int cmd, void *arg)
1529 struct libcfs_ioctl_data *data = arg;
1530 lnet_process_id_t id = {0};
1534 LASSERT (the_lnet.ln_init);
1535 LASSERT (the_lnet.ln_refcount > 0);
1538 case IOC_LIBCFS_GET_NI:
1539 rc = LNetGetId(data->ioc_count, &id);
1540 data->ioc_nid = id.nid;
1543 case IOC_LIBCFS_FAIL_NID:
1544 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1546 case IOC_LIBCFS_ADD_ROUTE:
1547 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1549 return (rc != 0) ? rc : lnet_check_routes();
1551 case IOC_LIBCFS_DEL_ROUTE:
1552 return lnet_del_route(data->ioc_net, data->ioc_nid);
1554 case IOC_LIBCFS_GET_ROUTE:
1555 return lnet_get_route(data->ioc_count,
1556 &data->ioc_net, &data->ioc_count,
1557 &data->ioc_nid, &data->ioc_flags);
1558 case IOC_LIBCFS_NOTIFY_ROUTER:
1559 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1560 cfs_time_current() -
1561 cfs_time_seconds(cfs_time_current_sec() -
1562 (time_t)data->ioc_u64[0]));
1564 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1565 /* This can be removed once lustre stops calling it */
1568 case IOC_LIBCFS_LNET_DIST:
1569 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1570 if (rc < 0 && rc != -EHOSTUNREACH)
1573 data->ioc_u32[0] = rc;
1576 case IOC_LIBCFS_TESTPROTOCOMPAT:
1577 lnet_net_lock(LNET_LOCK_EX);
1578 the_lnet.ln_testprotocompat = data->ioc_flags;
1579 lnet_net_unlock(LNET_LOCK_EX);
1582 case IOC_LIBCFS_PING:
1583 id.nid = data->ioc_nid;
1584 id.pid = data->ioc_u32[0];
1585 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1586 (lnet_process_id_t *)data->ioc_pbuf1,
1587 data->ioc_plen1/sizeof(lnet_process_id_t));
1590 data->ioc_count = rc;
1593 case IOC_LIBCFS_DEBUG_PEER: {
1594 /* CAVEAT EMPTOR: this one designed for calling directly; not
1596 id = *((lnet_process_id_t *) arg);
1598 lnet_debug_peer(id.nid);
1600 ni = lnet_net2ni(LNET_NIDNET(id.nid));
1602 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1604 if (ni->ni_lnd->lnd_ctl == NULL) {
1605 CDEBUG(D_WARNING, "No ctl for %s\n",
1608 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1617 ni = lnet_net2ni(data->ioc_net);
1621 if (ni->ni_lnd->lnd_ctl == NULL)
1624 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1633 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
1634 * all interfaces share a same PID, as requested by LNetNIInit().
1636 * \param index Index of the interface to look up.
1637 * \param id On successful return, this location will hold the
1638 * lnet_process_id_t ID of the interface.
1640 * \retval 0 If an interface exists at \a index.
1641 * \retval -ENOENT If no interface has been found.
1644 LNetGetId(unsigned int index, lnet_process_id_t *id)
1651 LASSERT(the_lnet.ln_init);
1652 LASSERT(the_lnet.ln_refcount > 0);
1654 cpt = lnet_net_lock_current();
1656 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
1660 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
1662 id->nid = ni->ni_nid;
1663 id->pid = the_lnet.ln_pid;
1668 lnet_net_unlock(cpt);
1673 * Print a string representation of handle \a h into buffer \a str of
1677 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1679 snprintf(str, len, LPX64, h.cookie);
1683 lnet_create_ping_info(void)
1688 unsigned int infosz;
1690 lnet_process_id_t id;
1691 lnet_ping_info_t *pinfo;
1693 for (n = 0; ; n++) {
1694 rc = LNetGetId(n, &id);
1701 infosz = offsetof(lnet_ping_info_t, pi_ni[n]);
1702 LIBCFS_ALLOC(pinfo, infosz);
1703 if (pinfo == NULL) {
1704 CERROR("Can't allocate ping info[%d]\n", n);
1709 pinfo->pi_pid = the_lnet.ln_pid;
1710 pinfo->pi_magic = LNET_PROTO_PING_MAGIC;
1711 pinfo->pi_version = LNET_PROTO_PING_VERSION;
1713 for (i = 0; i < n; i++) {
1714 lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1716 rc = LNetGetId(i, &id);
1719 ns->ns_nid = id.nid;
1720 ns->ns_status = LNET_NI_STATUS_UP;
1724 ni = lnet_nid2ni_locked(id.nid, 0);
1725 LASSERT(ni != NULL);
1728 LASSERT(ni->ni_status == NULL);
1732 lnet_ni_decref_locked(ni, 0);
1736 the_lnet.ln_ping_info = pinfo;
1741 lnet_destroy_ping_info(void)
1747 cfs_list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1749 ni->ni_status = NULL;
1755 LIBCFS_FREE(the_lnet.ln_ping_info,
1756 offsetof(lnet_ping_info_t,
1757 pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1758 the_lnet.ln_ping_info = NULL;
1763 lnet_ping_target_init(void)
1766 lnet_handle_me_t meh;
1767 lnet_process_id_t id;
1772 rc = lnet_create_ping_info();
1776 /* We can have a tiny EQ since we only need to see the unlink event on
1777 * teardown, which by definition is the last one! */
1778 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1780 CERROR("Can't allocate ping EQ: %d\n", rc);
1784 memset(&id, 0, sizeof(lnet_process_id_t));
1785 id.nid = LNET_NID_ANY;
1786 id.pid = LNET_PID_ANY;
1788 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1789 LNET_PROTO_PING_MATCHBITS, 0,
1790 LNET_UNLINK, LNET_INS_AFTER,
1793 CERROR("Can't create ping ME: %d\n", rc);
1797 /* initialize md content */
1798 infosz = offsetof(lnet_ping_info_t,
1799 pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1800 md.start = the_lnet.ln_ping_info;
1802 md.threshold = LNET_MD_THRESH_INF;
1804 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1805 LNET_MD_MANAGE_REMOTE;
1807 md.eq_handle = the_lnet.ln_ping_target_eq;
1809 rc = LNetMDAttach(meh, md,
1811 &the_lnet.ln_ping_target_md);
1813 CERROR("Can't attach ping MD: %d\n", rc);
1820 rc2 = LNetMEUnlink(meh);
1823 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1826 lnet_destroy_ping_info();
1831 lnet_ping_target_fini(void)
1836 int timeout_ms = 1000;
1837 cfs_sigset_t blocked = cfs_block_allsigs();
1839 LNetMDUnlink(the_lnet.ln_ping_target_md);
1840 /* NB md could be busy; this just starts the unlink */
1843 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1844 timeout_ms, &event, &which);
1846 /* I expect overflow... */
1847 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1850 /* timed out: provide a diagnostic */
1851 CWARN("Still waiting for ping MD to unlink\n");
1856 /* Got a valid event */
1861 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1863 lnet_destroy_ping_info();
1864 cfs_restore_sigs(blocked);
1868 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1870 lnet_handle_eq_t eqh;
1871 lnet_handle_md_t mdh;
1877 const int a_long_time = 60000; /* mS */
1878 int infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1879 lnet_ping_info_t *info;
1880 lnet_process_id_t tmpid;
1885 cfs_sigset_t blocked;
1888 id.nid == LNET_NID_ANY ||
1889 timeout_ms > 500000 || /* arbitrary limit! */
1890 n_ids > 20) /* arbitrary limit! */
1893 if (id.pid == LNET_PID_ANY)
1894 id.pid = LUSTRE_SRV_LNET_PID;
1896 LIBCFS_ALLOC(info, infosz);
1900 /* NB 2 events max (including any unlink event) */
1901 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1903 CERROR("Can't allocate EQ: %d\n", rc);
1907 /* initialize md content */
1910 md.threshold = 2; /*GET/REPLY*/
1912 md.options = LNET_MD_TRUNCATE;
1916 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1918 CERROR("Can't bind MD: %d\n", rc);
1922 rc = LNetGet(LNET_NID_ANY, mdh, id,
1923 LNET_RESERVED_PORTAL,
1924 LNET_PROTO_PING_MATCHBITS, 0);
1927 /* Don't CERROR; this could be deliberate! */
1929 rc2 = LNetMDUnlink(mdh);
1932 /* NB must wait for the UNLINK event below... */
1934 timeout_ms = a_long_time;
1938 /* MUST block for unlink to complete */
1940 blocked = cfs_block_allsigs();
1942 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1945 cfs_restore_sigs(blocked);
1947 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1948 (rc2 <= 0) ? -1 : event.type,
1949 (rc2 <= 0) ? -1 : event.status,
1950 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1952 LASSERT (rc2 != -EOVERFLOW); /* can't miss anything */
1954 if (rc2 <= 0 || event.status != 0) {
1955 /* timeout or error */
1956 if (!replied && rc == 0)
1957 rc = (rc2 < 0) ? rc2 :
1958 (rc2 == 0) ? -ETIMEDOUT :
1962 /* Ensure completion in finite time... */
1964 /* No assertion (racing with network) */
1966 timeout_ms = a_long_time;
1967 } else if (rc2 == 0) {
1968 /* timed out waiting for unlink */
1969 CWARN("ping %s: late network completion\n",
1972 } else if (event.type == LNET_EVENT_REPLY) {
1977 } while (rc2 <= 0 || !event.unlinked);
1981 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
1988 LASSERT (nob >= 0 && nob <= infosz);
1990 rc = -EPROTO; /* if I can't parse... */
1993 /* can't check magic/version */
1994 CERROR("%s: ping info too short %d\n",
1995 libcfs_id2str(id), nob);
1999 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2000 lnet_swap_pinginfo(info);
2001 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2002 CERROR("%s: Unexpected magic %08x\n",
2003 libcfs_id2str(id), info->pi_magic);
2007 if (info->pi_version != LNET_PROTO_PING_VERSION) {
2008 CERROR("%s: Unexpected version 0x%x\n",
2009 libcfs_id2str(id), info->pi_version);
2013 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2014 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2015 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2019 if (info->pi_nnis < n_ids)
2020 n_ids = info->pi_nnis;
2022 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2023 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2024 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2028 rc = -EFAULT; /* If I SEGV... */
2030 for (i = 0; i < n_ids; i++) {
2031 tmpid.pid = info->pi_pid;
2032 tmpid.nid = info->pi_ni[i].ns_nid;
2034 if (cfs_copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2043 rc2 = LNetEQFree(eqh);
2045 CERROR("rc2 %d\n", rc2);
2049 LIBCFS_FREE(info, infosz);