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 */
47 EXPORT_SYMBOL(the_lnet);
51 static char *ip2nets = "";
52 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
53 "LNET network <- IP table");
55 static char *networks = "";
56 CFS_MODULE_PARM(networks, "s", charp, 0444,
59 static char *routes = "";
60 CFS_MODULE_PARM(routes, "s", charp, 0444,
61 "routes to non-local networks");
70 lnet_get_networks(void)
75 if (*networks != 0 && *ip2nets != 0) {
76 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
77 "'ip2nets' but not both at once\n");
82 rc = lnet_parse_ip2nets(&nets, ip2nets);
83 return (rc == 0) ? nets : NULL;
95 spin_lock_init(&the_lnet.ln_eq_wait_lock);
96 cfs_waitq_init(&the_lnet.ln_eq_waitq);
97 mutex_init(&the_lnet.ln_lnd_mutex);
98 mutex_init(&the_lnet.ln_api_mutex);
102 lnet_fini_locks(void)
109 lnet_get_routes(void)
111 char *str = getenv("LNET_ROUTES");
113 return (str == NULL) ? "" : str;
117 lnet_get_networks (void)
119 static char default_networks[256];
120 char *networks = getenv ("LNET_NETWORKS");
121 char *ip2nets = getenv ("LNET_IP2NETS");
130 if (networks != NULL && ip2nets != NULL) {
131 LCONSOLE_ERROR_MSG(0x103, "Please set EITHER 'LNET_NETWORKS' or"
132 " 'LNET_IP2NETS' but not both at once\n");
136 if (ip2nets != NULL) {
137 rc = lnet_parse_ip2nets(&networks, ip2nets);
138 return (rc == 0) ? networks : NULL;
141 SET_BUT_UNUSED(ip2nets);
144 if (networks != NULL)
147 /* In userland, the default 'networks=' is the list of known net types */
149 len = sizeof(default_networks);
150 str = default_networks;
154 cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
155 lnd_t *lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
157 nob = snprintf(str, len, "%s%s", sep,
158 libcfs_lnd2str(lnd->lnd_type));
161 /* overflowed the string; leave it where it was */
170 return default_networks;
173 # ifndef HAVE_LIBPTHREAD
175 void lnet_init_locks(void)
177 the_lnet.ln_eq_wait_lock = 0;
178 the_lnet.ln_lnd_mutex = 0;
179 the_lnet.ln_api_mutex = 0;
182 void lnet_fini_locks(void)
184 LASSERT(the_lnet.ln_api_mutex == 0);
185 LASSERT(the_lnet.ln_lnd_mutex == 0);
186 LASSERT(the_lnet.ln_eq_wait_lock == 0);
191 void lnet_init_locks(void)
193 pthread_cond_init(&the_lnet.ln_eq_cond, NULL);
194 pthread_mutex_init(&the_lnet.ln_eq_wait_lock, NULL);
195 pthread_mutex_init(&the_lnet.ln_lnd_mutex, NULL);
196 pthread_mutex_init(&the_lnet.ln_api_mutex, NULL);
199 void lnet_fini_locks(void)
201 pthread_mutex_destroy(&the_lnet.ln_api_mutex);
202 pthread_mutex_destroy(&the_lnet.ln_lnd_mutex);
203 pthread_mutex_destroy(&the_lnet.ln_eq_wait_lock);
204 pthread_cond_destroy(&the_lnet.ln_eq_cond);
211 lnet_create_locks(void)
215 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
216 if (the_lnet.ln_res_lock == NULL)
219 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
220 if (the_lnet.ln_net_lock == NULL)
231 lnet_destroy_locks(void)
233 if (the_lnet.ln_res_lock != NULL) {
234 cfs_percpt_lock_free(the_lnet.ln_res_lock);
235 the_lnet.ln_res_lock = NULL;
238 if (the_lnet.ln_net_lock != NULL) {
239 cfs_percpt_lock_free(the_lnet.ln_net_lock);
240 the_lnet.ln_net_lock = NULL;
246 void lnet_assert_wire_constants (void)
248 /* Wire protocol assertions generated by 'wirecheck'
249 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
250 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
251 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
254 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
255 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
256 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
257 CLASSERT (LNET_MSG_ACK == 0);
258 CLASSERT (LNET_MSG_PUT == 1);
259 CLASSERT (LNET_MSG_GET == 2);
260 CLASSERT (LNET_MSG_REPLY == 3);
261 CLASSERT (LNET_MSG_HELLO == 4);
263 /* Checks for struct ptl_handle_wire_t */
264 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
265 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
266 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
267 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
268 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
270 /* Checks for struct lnet_magicversion_t */
271 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
272 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
273 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
274 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
275 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
276 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
277 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
279 /* Checks for struct lnet_hdr_t */
280 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
281 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
282 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
283 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
284 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
285 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
286 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
287 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
288 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
289 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
290 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
291 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
292 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
293 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
294 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
297 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
298 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
299 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
300 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
301 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
302 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
305 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
306 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
307 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
308 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
309 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
310 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
311 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
312 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
313 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
314 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
317 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
318 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
319 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
320 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
321 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
322 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
323 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
324 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
325 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
326 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
329 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
330 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
333 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
334 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
335 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
336 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
340 lnet_find_lnd_by_type (int type)
345 /* holding lnd mutex */
346 cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
347 lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
349 if ((int)lnd->lnd_type == type)
357 lnet_register_lnd (lnd_t *lnd)
359 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
361 LASSERT (the_lnet.ln_init);
362 LASSERT (libcfs_isknown_lnd(lnd->lnd_type));
363 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
365 cfs_list_add_tail (&lnd->lnd_list, &the_lnet.ln_lnds);
366 lnd->lnd_refcount = 0;
368 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
370 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
372 EXPORT_SYMBOL(lnet_register_lnd);
375 lnet_unregister_lnd (lnd_t *lnd)
377 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
379 LASSERT (the_lnet.ln_init);
380 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
381 LASSERT (lnd->lnd_refcount == 0);
383 cfs_list_del (&lnd->lnd_list);
384 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
386 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
388 EXPORT_SYMBOL(lnet_unregister_lnd);
391 lnet_counters_get(lnet_counters_t *counters)
393 lnet_counters_t *ctr;
396 memset(counters, 0, sizeof(*counters));
398 lnet_net_lock(LNET_LOCK_EX);
400 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
401 counters->msgs_max += ctr->msgs_max;
402 counters->msgs_alloc += ctr->msgs_alloc;
403 counters->errors += ctr->errors;
404 counters->send_count += ctr->send_count;
405 counters->recv_count += ctr->recv_count;
406 counters->route_count += ctr->route_count;
407 counters->drop_length += ctr->drop_length;
408 counters->send_length += ctr->send_length;
409 counters->recv_length += ctr->recv_length;
410 counters->route_length += ctr->route_length;
411 counters->drop_length += ctr->drop_length;
414 lnet_net_unlock(LNET_LOCK_EX);
416 EXPORT_SYMBOL(lnet_counters_get);
419 lnet_counters_reset(void)
421 lnet_counters_t *counters;
424 lnet_net_lock(LNET_LOCK_EX);
426 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
427 memset(counters, 0, sizeof(lnet_counters_t));
429 lnet_net_unlock(LNET_LOCK_EX);
431 EXPORT_SYMBOL(lnet_counters_reset);
433 #ifdef LNET_USE_LIB_FREELIST
436 lnet_freelist_init (lnet_freelist_t *fl, int n, int size)
442 size += offsetof (lnet_freeobj_t, fo_contents);
444 LIBCFS_ALLOC(space, n * size);
448 CFS_INIT_LIST_HEAD (&fl->fl_list);
451 fl->fl_objsize = size;
455 memset (space, 0, size);
456 cfs_list_add ((cfs_list_t *)space, &fl->fl_list);
464 lnet_freelist_fini (lnet_freelist_t *fl)
469 if (fl->fl_nobjs == 0)
473 for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
476 LASSERT (count == fl->fl_nobjs);
478 LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
479 memset (fl, 0, sizeof (*fl));
482 #endif /* LNET_USE_LIB_FREELIST */
485 lnet_create_interface_cookie (void)
487 /* NB the interface cookie in wire handles guards against delayed
488 * replies and ACKs appearing valid after reboot. Initialisation time,
489 * even if it's only implemented to millisecond resolution is probably
490 * easily good enough. */
494 int rc = gettimeofday (&tv, NULL);
497 cfs_gettimeofday(&tv);
501 cookie += tv.tv_usec;
506 lnet_res_type2str(int type)
511 case LNET_COOKIE_TYPE_MD:
513 case LNET_COOKIE_TYPE_ME:
515 case LNET_COOKIE_TYPE_EQ:
521 lnet_res_container_cleanup(struct lnet_res_container *rec)
525 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
528 while (!cfs_list_empty(&rec->rec_active)) {
529 cfs_list_t *e = rec->rec_active.next;
531 cfs_list_del_init(e);
532 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
533 lnet_eq_free(cfs_list_entry(e, lnet_eq_t, eq_list));
535 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
536 lnet_md_free(cfs_list_entry(e, lnet_libmd_t, md_list));
538 } else { /* NB: Active MEs should be attached on portals */
545 /* Found alive MD/ME/EQ, user really should unlink/free
546 * all of them before finalize LNet, but if someone didn't,
547 * we have to recycle garbage for him */
548 CERROR("%d active elements on exit of %s container\n",
549 count, lnet_res_type2str(rec->rec_type));
552 #ifdef LNET_USE_LIB_FREELIST
553 lnet_freelist_fini(&rec->rec_freelist);
555 if (rec->rec_lh_hash != NULL) {
556 LIBCFS_FREE(rec->rec_lh_hash,
557 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
558 rec->rec_lh_hash = NULL;
561 rec->rec_type = 0; /* mark it as finalized */
565 lnet_res_container_setup(struct lnet_res_container *rec,
566 int cpt, int type, int objnum, int objsz)
571 LASSERT(rec->rec_type == 0);
573 rec->rec_type = type;
574 CFS_INIT_LIST_HEAD(&rec->rec_active);
576 #ifdef LNET_USE_LIB_FREELIST
577 memset(&rec->rec_freelist, 0, sizeof(rec->rec_freelist));
578 rc = lnet_freelist_init(&rec->rec_freelist, objnum, objsz);
582 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
584 /* Arbitrary choice of hash table size */
585 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
586 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
587 if (rec->rec_lh_hash == NULL) {
592 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
593 CFS_INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
598 CERROR("Failed to setup %s resource container\n",
599 lnet_res_type2str(type));
600 lnet_res_container_cleanup(rec);
605 lnet_res_containers_destroy(struct lnet_res_container **recs)
607 struct lnet_res_container *rec;
610 cfs_percpt_for_each(rec, i, recs)
611 lnet_res_container_cleanup(rec);
613 cfs_percpt_free(recs);
616 static struct lnet_res_container **
617 lnet_res_containers_create(int type, int objnum, int objsz)
619 struct lnet_res_container **recs;
620 struct lnet_res_container *rec;
624 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
626 CERROR("Failed to allocate %s resource containers\n",
627 lnet_res_type2str(type));
631 cfs_percpt_for_each(rec, i, recs) {
632 rc = lnet_res_container_setup(rec, i, type, objnum, objsz);
634 lnet_res_containers_destroy(recs);
643 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
645 /* ALWAYS called with lnet_res_lock held */
647 lnet_libhandle_t *lh;
650 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
653 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
654 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
656 cfs_list_for_each_entry(lh, head, lh_hash_chain) {
657 if (lh->lh_cookie == cookie)
665 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
667 /* ALWAYS called with lnet_res_lock held */
668 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
671 lh->lh_cookie = rec->rec_lh_cookie;
672 rec->rec_lh_cookie += 1 << ibits;
674 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
676 cfs_list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
681 * Reserved API - do not use.
682 * Temporary workaround to allow uOSS and test programs force server
683 * mode in userspace. See comments near ln_server_mode_flag in
684 * lnet/lib-types.h */
688 the_lnet.ln_server_mode_flag = 1;
692 int lnet_unprepare(void);
695 lnet_prepare(lnet_pid_t requested_pid)
697 /* Prepare to bring up the network */
698 struct lnet_res_container **recs;
701 LASSERT (the_lnet.ln_refcount == 0);
703 the_lnet.ln_routing = 0;
706 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
707 the_lnet.ln_pid = requested_pid;
709 if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
710 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
712 if (cfs_curproc_uid())/* Only root can run user-space server */
714 the_lnet.ln_pid = requested_pid;
716 } else {/* client case (liblustre) */
718 /* My PID must be unique on this node and flag I'm userspace */
719 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
723 CFS_INIT_LIST_HEAD(&the_lnet.ln_test_peers);
724 CFS_INIT_LIST_HEAD(&the_lnet.ln_nis);
725 CFS_INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
726 CFS_INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
727 CFS_INIT_LIST_HEAD(&the_lnet.ln_remote_nets);
728 CFS_INIT_LIST_HEAD(&the_lnet.ln_routers);
730 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
732 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
733 sizeof(lnet_counters_t));
734 if (the_lnet.ln_counters == NULL) {
735 CERROR("Failed to allocate counters for LNet\n");
740 rc = lnet_peer_tables_create();
744 rc = lnet_msg_containers_create();
748 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
749 LNET_COOKIE_TYPE_EQ, LNET_FL_MAX_EQS,
754 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME, LNET_FL_MAX_MES,
759 the_lnet.ln_me_containers = recs;
761 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD, LNET_FL_MAX_MDS,
762 sizeof(lnet_libmd_t));
766 the_lnet.ln_md_containers = recs;
768 rc = lnet_portals_create();
770 CERROR("Failed to create portals for LNet: %d\n", rc);
782 lnet_unprepare (void)
784 /* NB no LNET_LOCK since this is the last reference. All LND instances
785 * have shut down already, so it is safe to unlink and free all
786 * descriptors, even those that appear committed to a network op (eg MD
787 * with non-zero pending count) */
789 lnet_fail_nid(LNET_NID_ANY, 0);
791 LASSERT(the_lnet.ln_refcount == 0);
792 LASSERT(cfs_list_empty(&the_lnet.ln_test_peers));
793 LASSERT(cfs_list_empty(&the_lnet.ln_nis));
794 LASSERT(cfs_list_empty(&the_lnet.ln_nis_cpt));
795 LASSERT(cfs_list_empty(&the_lnet.ln_nis_zombie));
797 lnet_portals_destroy();
799 if (the_lnet.ln_md_containers != NULL) {
800 lnet_res_containers_destroy(the_lnet.ln_md_containers);
801 the_lnet.ln_md_containers = NULL;
804 if (the_lnet.ln_me_containers != NULL) {
805 lnet_res_containers_destroy(the_lnet.ln_me_containers);
806 the_lnet.ln_me_containers = NULL;
809 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
811 lnet_msg_containers_destroy();
812 lnet_peer_tables_destroy();
813 lnet_rtrpools_free();
815 if (the_lnet.ln_counters != NULL) {
816 cfs_percpt_free(the_lnet.ln_counters);
817 the_lnet.ln_counters = NULL;
824 lnet_net2ni_locked(__u32 net, int cpt)
829 LASSERT(cpt != LNET_LOCK_EX);
831 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
832 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
834 if (LNET_NIDNET(ni->ni_nid) == net) {
835 lnet_ni_addref_locked(ni, cpt);
844 lnet_net2ni(__u32 net)
849 ni = lnet_net2ni_locked(net, 0);
854 EXPORT_SYMBOL(lnet_net2ni);
857 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
862 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
867 val = cfs_hash_long(key, LNET_CPT_BITS);
868 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
872 return (unsigned int)(key + val + (val >> 1)) % number;
876 lnet_cpt_of_nid_locked(lnet_nid_t nid)
880 /* must called with hold of lnet_net_lock */
881 if (LNET_CPT_NUMBER == 1)
882 return 0; /* the only one */
884 /* take lnet_net_lock(any) would be OK */
885 if (!cfs_list_empty(&the_lnet.ln_nis_cpt)) {
886 cfs_list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
887 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
890 LASSERT(ni->ni_cpts != NULL);
891 return ni->ni_cpts[lnet_nid_cpt_hash
892 (nid, ni->ni_ncpts)];
896 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
900 lnet_cpt_of_nid(lnet_nid_t nid)
905 if (LNET_CPT_NUMBER == 1)
906 return 0; /* the only one */
908 if (cfs_list_empty(&the_lnet.ln_nis_cpt))
909 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
911 cpt = lnet_net_lock_current();
912 cpt2 = lnet_cpt_of_nid_locked(nid);
913 lnet_net_unlock(cpt);
917 EXPORT_SYMBOL(lnet_cpt_of_nid);
920 lnet_islocalnet(__u32 net)
925 cpt = lnet_net_lock_current();
927 ni = lnet_net2ni_locked(net, cpt);
929 lnet_ni_decref_locked(ni, cpt);
931 lnet_net_unlock(cpt);
937 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
942 LASSERT(cpt != LNET_LOCK_EX);
944 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
945 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
947 if (ni->ni_nid == nid) {
948 lnet_ni_addref_locked(ni, cpt);
957 lnet_islocalnid(lnet_nid_t nid)
962 cpt = lnet_net_lock_current();
963 ni = lnet_nid2ni_locked(nid, cpt);
965 lnet_ni_decref_locked(ni, cpt);
966 lnet_net_unlock(cpt);
972 lnet_count_acceptor_nis (void)
974 /* Return the # of NIs that need the acceptor. */
976 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
981 cpt = lnet_net_lock_current();
982 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
983 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
985 if (ni->ni_lnd->lnd_accept != NULL)
989 lnet_net_unlock(cpt);
991 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
996 lnet_ni_tq_credits(lnet_ni_t *ni)
1000 LASSERT(ni->ni_ncpts >= 1);
1002 if (ni->ni_ncpts == 1)
1003 return ni->ni_maxtxcredits;
1005 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
1006 credits = max(credits, 8 * ni->ni_peertxcredits);
1007 credits = min(credits, ni->ni_maxtxcredits);
1013 lnet_shutdown_lndnis (void)
1019 /* NB called holding the global mutex */
1021 /* All quiet on the API front */
1022 LASSERT(!the_lnet.ln_shutdown);
1023 LASSERT(the_lnet.ln_refcount == 0);
1024 LASSERT(cfs_list_empty(&the_lnet.ln_nis_zombie));
1025 LASSERT(cfs_list_empty(&the_lnet.ln_remote_nets));
1027 lnet_net_lock(LNET_LOCK_EX);
1028 the_lnet.ln_shutdown = 1; /* flag shutdown */
1030 /* Unlink NIs from the global table */
1031 while (!cfs_list_empty(&the_lnet.ln_nis)) {
1032 ni = cfs_list_entry(the_lnet.ln_nis.next,
1033 lnet_ni_t, ni_list);
1034 /* move it to zombie list and nobody can find it anymore */
1035 cfs_list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1036 lnet_ni_decref_locked(ni, 0); /* drop ln_nis' ref */
1038 if (!cfs_list_empty(&ni->ni_cptlist)) {
1039 cfs_list_del_init(&ni->ni_cptlist);
1040 lnet_ni_decref_locked(ni, 0);
1044 /* Drop the cached eqwait NI. */
1045 if (the_lnet.ln_eq_waitni != NULL) {
1046 lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
1047 the_lnet.ln_eq_waitni = NULL;
1050 /* Drop the cached loopback NI. */
1051 if (the_lnet.ln_loni != NULL) {
1052 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1053 the_lnet.ln_loni = NULL;
1056 lnet_net_unlock(LNET_LOCK_EX);
1058 /* Clear lazy portals and drop delayed messages which hold refs
1059 * on their lnet_msg_t::msg_rxpeer */
1060 for (i = 0; i < the_lnet.ln_nportals; i++)
1061 LNetClearLazyPortal(i);
1063 /* Clear the peer table and wait for all peers to go (they hold refs on
1065 lnet_peer_tables_cleanup();
1067 lnet_net_lock(LNET_LOCK_EX);
1068 /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
1069 * and shut them down in guaranteed thread context */
1071 while (!cfs_list_empty(&the_lnet.ln_nis_zombie)) {
1075 ni = cfs_list_entry(the_lnet.ln_nis_zombie.next,
1076 lnet_ni_t, ni_list);
1077 cfs_list_del_init(&ni->ni_list);
1078 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1081 /* still busy, add it back to zombie list */
1082 cfs_list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1086 while (!cfs_list_empty(&ni->ni_list)) {
1087 lnet_net_unlock(LNET_LOCK_EX);
1089 if ((i & (-i)) == i) {
1091 "Waiting for zombie LNI %s\n",
1092 libcfs_nid2str(ni->ni_nid));
1094 cfs_pause(cfs_time_seconds(1));
1095 lnet_net_lock(LNET_LOCK_EX);
1099 ni->ni_lnd->lnd_refcount--;
1100 lnet_net_unlock(LNET_LOCK_EX);
1102 islo = ni->ni_lnd->lnd_type == LOLND;
1104 LASSERT (!cfs_in_interrupt ());
1105 (ni->ni_lnd->lnd_shutdown)(ni);
1107 /* can't deref lnd anymore now; it might have unregistered
1111 CDEBUG(D_LNI, "Removed LNI %s\n",
1112 libcfs_nid2str(ni->ni_nid));
1115 lnet_net_lock(LNET_LOCK_EX);
1118 the_lnet.ln_shutdown = 0;
1119 lnet_net_unlock(LNET_LOCK_EX);
1121 if (the_lnet.ln_network_tokens != NULL) {
1122 LIBCFS_FREE(the_lnet.ln_network_tokens,
1123 the_lnet.ln_network_tokens_nob);
1124 the_lnet.ln_network_tokens = NULL;
1129 lnet_startup_lndnis (void)
1133 struct lnet_tx_queue *tq;
1139 char *nets = lnet_get_networks();
1141 CFS_INIT_LIST_HEAD(&nilist);
1146 rc = lnet_parse_networks(&nilist, nets);
1150 while (!cfs_list_empty(&nilist)) {
1151 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1152 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1154 LASSERT (libcfs_isknown_lnd(lnd_type));
1156 if (lnd_type == CIBLND ||
1157 lnd_type == OPENIBLND ||
1158 lnd_type == IIBLND ||
1159 lnd_type == VIBLND) {
1160 CERROR("LND %s obsoleted\n",
1161 libcfs_lnd2str(lnd_type));
1165 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1166 lnd = lnet_find_lnd_by_type(lnd_type);
1170 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1171 rc = cfs_request_module("%s",
1172 libcfs_lnd2modname(lnd_type));
1173 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1175 lnd = lnet_find_lnd_by_type(lnd_type);
1177 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1178 CERROR("Can't load LND %s, module %s, rc=%d\n",
1179 libcfs_lnd2str(lnd_type),
1180 libcfs_lnd2modname(lnd_type), rc);
1181 #ifndef HAVE_MODULE_LOADING_SUPPORT
1182 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1183 "compiled with kernel module "
1184 "loading support.");
1191 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1192 CERROR("LND %s not supported\n",
1193 libcfs_lnd2str(lnd_type));
1198 lnet_net_lock(LNET_LOCK_EX);
1199 lnd->lnd_refcount++;
1200 lnet_net_unlock(LNET_LOCK_EX);
1204 rc = (lnd->lnd_startup)(ni);
1206 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1209 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1211 rc, libcfs_lnd2str(lnd->lnd_type));
1212 lnet_net_lock(LNET_LOCK_EX);
1213 lnd->lnd_refcount--;
1214 lnet_net_unlock(LNET_LOCK_EX);
1218 LASSERT (ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1220 cfs_list_del(&ni->ni_list);
1222 lnet_net_lock(LNET_LOCK_EX);
1223 /* refcount for ln_nis */
1224 lnet_ni_addref_locked(ni, 0);
1225 cfs_list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1226 if (ni->ni_cpts != NULL) {
1227 cfs_list_add_tail(&ni->ni_cptlist,
1228 &the_lnet.ln_nis_cpt);
1229 lnet_ni_addref_locked(ni, 0);
1232 lnet_net_unlock(LNET_LOCK_EX);
1234 if (lnd->lnd_type == LOLND) {
1236 LASSERT (the_lnet.ln_loni == NULL);
1237 the_lnet.ln_loni = ni;
1242 if (lnd->lnd_wait != NULL) {
1243 if (the_lnet.ln_eq_waitni == NULL) {
1245 the_lnet.ln_eq_waitni = ni;
1248 # ifndef HAVE_LIBPTHREAD
1249 LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1250 "single-threaded runtime\n",
1251 libcfs_lnd2str(lnd_type));
1256 if (ni->ni_peertxcredits == 0 ||
1257 ni->ni_maxtxcredits == 0) {
1258 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1259 libcfs_lnd2str(lnd->lnd_type),
1260 ni->ni_peertxcredits == 0 ?
1265 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1266 tq->tq_credits_min =
1267 tq->tq_credits_max =
1268 tq->tq_credits = lnet_ni_tq_credits(ni);
1271 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1272 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1273 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1274 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1279 if (the_lnet.ln_eq_waitni != NULL && nicount > 1) {
1280 lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1281 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1283 libcfs_lnd2str(lnd_type));
1290 lnet_shutdown_lndnis();
1292 while (!cfs_list_empty(&nilist)) {
1293 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1294 cfs_list_del(&ni->ni_list);
1302 * Initialize LNet library.
1304 * Only userspace program needs to call this function - it's automatically
1305 * called in the kernel at module loading time. Caller has to call LNetFini()
1306 * after a call to LNetInit(), if and only if the latter returned 0. It must
1307 * be called exactly once.
1309 * \return 0 on success, and -ve on failures.
1316 lnet_assert_wire_constants();
1317 LASSERT(!the_lnet.ln_init);
1319 memset(&the_lnet, 0, sizeof(the_lnet));
1321 /* refer to global cfs_cpt_table for now */
1322 the_lnet.ln_cpt_table = cfs_cpt_table;
1323 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1325 LASSERT(the_lnet.ln_cpt_number > 0);
1326 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1327 /* we are under risk of consuming all lh_cookie */
1328 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1329 "please change setting of CPT-table and retry\n",
1330 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1334 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1335 the_lnet.ln_cpt_bits++;
1337 rc = lnet_create_locks();
1339 CERROR("Can't create LNet global locks: %d\n", rc);
1343 the_lnet.ln_refcount = 0;
1344 the_lnet.ln_init = 1;
1345 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1346 CFS_INIT_LIST_HEAD(&the_lnet.ln_lnds);
1347 CFS_INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1348 CFS_INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1351 /* All LNDs apart from the LOLND are in separate modules. They
1352 * register themselves when their module loads, and unregister
1353 * themselves when their module is unloaded. */
1356 * NB the order here determines default 'networks=' order */
1357 # ifdef HAVE_LIBPTHREAD
1358 LNET_REGISTER_ULND(the_tcplnd);
1361 lnet_register_lnd(&the_lolnd);
1364 EXPORT_SYMBOL(LNetInit);
1367 * Finalize LNet library.
1369 * Only userspace program needs to call this function. It can be called
1372 * \pre LNetInit() called with success.
1373 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1378 LASSERT(the_lnet.ln_init);
1379 LASSERT(the_lnet.ln_refcount == 0);
1381 while (!cfs_list_empty(&the_lnet.ln_lnds))
1382 lnet_unregister_lnd(cfs_list_entry(the_lnet.ln_lnds.next,
1384 lnet_destroy_locks();
1386 the_lnet.ln_init = 0;
1388 EXPORT_SYMBOL(LNetFini);
1391 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1393 * Userspace program should call this after a successful call to LNetInit().
1394 * Users must call this function at least once before any other functions.
1395 * For each successful call there must be a corresponding call to
1396 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1399 * The PID used by LNet may be different from the one requested.
1402 * \param requested_pid PID requested by the caller.
1404 * \return >= 0 on success, and < 0 error code on failures.
1407 LNetNIInit(lnet_pid_t requested_pid)
1409 int im_a_router = 0;
1412 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1414 LASSERT (the_lnet.ln_init);
1415 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1417 if (the_lnet.ln_refcount > 0) {
1418 rc = the_lnet.ln_refcount++;
1422 lnet_get_tunables();
1424 if (requested_pid == LNET_PID_ANY) {
1425 /* Don't instantiate LNET just for me */
1430 rc = lnet_prepare(requested_pid);
1434 rc = lnet_startup_lndnis();
1438 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1442 rc = lnet_check_routes();
1446 rc = lnet_rtrpools_alloc(im_a_router);
1450 rc = lnet_acceptor_start();
1454 the_lnet.ln_refcount = 1;
1455 /* Now I may use my own API functions... */
1457 /* NB router checker needs the_lnet.ln_ping_info in
1458 * lnet_router_checker -> lnet_update_ni_status_locked */
1459 rc = lnet_ping_target_init();
1463 rc = lnet_router_checker_start();
1471 lnet_ping_target_fini();
1473 the_lnet.ln_refcount = 0;
1474 lnet_acceptor_stop();
1476 lnet_destroy_routes();
1477 lnet_shutdown_lndnis();
1483 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1486 EXPORT_SYMBOL(LNetNIInit);
1489 * Stop LNet interfaces, routing, and forwarding.
1491 * Users must call this function once for each successful call to LNetNIInit().
1492 * Once the LNetNIFini() operation has been started, the results of pending
1493 * API operations are undefined.
1495 * \return always 0 for current implementation.
1500 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1502 LASSERT (the_lnet.ln_init);
1503 LASSERT (the_lnet.ln_refcount > 0);
1505 if (the_lnet.ln_refcount != 1) {
1506 the_lnet.ln_refcount--;
1508 LASSERT (!the_lnet.ln_niinit_self);
1511 lnet_router_checker_stop();
1512 lnet_ping_target_fini();
1514 /* Teardown fns that use my own API functions BEFORE here */
1515 the_lnet.ln_refcount = 0;
1517 lnet_acceptor_stop();
1518 lnet_destroy_routes();
1519 lnet_shutdown_lndnis();
1523 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1526 EXPORT_SYMBOL(LNetNIFini);
1529 * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1530 * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1531 * internal ioctl handler.
1533 * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1535 * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1536 * The data will be printed to system console. Don't use it excessively.
1537 * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1539 * \return Always return 0 when called by users directly (i.e., not via ioctl).
1542 LNetCtl(unsigned int cmd, void *arg)
1544 struct libcfs_ioctl_data *data = arg;
1545 lnet_process_id_t id = {0};
1549 LASSERT (the_lnet.ln_init);
1550 LASSERT (the_lnet.ln_refcount > 0);
1553 case IOC_LIBCFS_GET_NI:
1554 rc = LNetGetId(data->ioc_count, &id);
1555 data->ioc_nid = id.nid;
1558 case IOC_LIBCFS_FAIL_NID:
1559 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1561 case IOC_LIBCFS_ADD_ROUTE:
1562 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1564 return (rc != 0) ? rc : lnet_check_routes();
1566 case IOC_LIBCFS_DEL_ROUTE:
1567 return lnet_del_route(data->ioc_net, data->ioc_nid);
1569 case IOC_LIBCFS_GET_ROUTE:
1570 return lnet_get_route(data->ioc_count,
1571 &data->ioc_net, &data->ioc_count,
1572 &data->ioc_nid, &data->ioc_flags);
1573 case IOC_LIBCFS_NOTIFY_ROUTER:
1574 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1575 cfs_time_current() -
1576 cfs_time_seconds(cfs_time_current_sec() -
1577 (time_t)data->ioc_u64[0]));
1579 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1580 /* This can be removed once lustre stops calling it */
1583 case IOC_LIBCFS_LNET_DIST:
1584 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1585 if (rc < 0 && rc != -EHOSTUNREACH)
1588 data->ioc_u32[0] = rc;
1591 case IOC_LIBCFS_TESTPROTOCOMPAT:
1592 lnet_net_lock(LNET_LOCK_EX);
1593 the_lnet.ln_testprotocompat = data->ioc_flags;
1594 lnet_net_unlock(LNET_LOCK_EX);
1597 case IOC_LIBCFS_PING:
1598 id.nid = data->ioc_nid;
1599 id.pid = data->ioc_u32[0];
1600 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1601 (lnet_process_id_t *)data->ioc_pbuf1,
1602 data->ioc_plen1/sizeof(lnet_process_id_t));
1605 data->ioc_count = rc;
1608 case IOC_LIBCFS_DEBUG_PEER: {
1609 /* CAVEAT EMPTOR: this one designed for calling directly; not
1611 id = *((lnet_process_id_t *) arg);
1613 lnet_debug_peer(id.nid);
1615 ni = lnet_net2ni(LNET_NIDNET(id.nid));
1617 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1619 if (ni->ni_lnd->lnd_ctl == NULL) {
1620 CDEBUG(D_WARNING, "No ctl for %s\n",
1623 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1632 ni = lnet_net2ni(data->ioc_net);
1636 if (ni->ni_lnd->lnd_ctl == NULL)
1639 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1646 EXPORT_SYMBOL(LNetCtl);
1649 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
1650 * all interfaces share a same PID, as requested by LNetNIInit().
1652 * \param index Index of the interface to look up.
1653 * \param id On successful return, this location will hold the
1654 * lnet_process_id_t ID of the interface.
1656 * \retval 0 If an interface exists at \a index.
1657 * \retval -ENOENT If no interface has been found.
1660 LNetGetId(unsigned int index, lnet_process_id_t *id)
1667 LASSERT(the_lnet.ln_init);
1668 LASSERT(the_lnet.ln_refcount > 0);
1670 cpt = lnet_net_lock_current();
1672 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
1676 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
1678 id->nid = ni->ni_nid;
1679 id->pid = the_lnet.ln_pid;
1684 lnet_net_unlock(cpt);
1687 EXPORT_SYMBOL(LNetGetId);
1690 * Print a string representation of handle \a h into buffer \a str of
1694 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1696 snprintf(str, len, LPX64, h.cookie);
1698 EXPORT_SYMBOL(LNetSnprintHandle);
1701 lnet_create_ping_info(void)
1706 unsigned int infosz;
1708 lnet_process_id_t id;
1709 lnet_ping_info_t *pinfo;
1711 for (n = 0; ; n++) {
1712 rc = LNetGetId(n, &id);
1719 infosz = offsetof(lnet_ping_info_t, pi_ni[n]);
1720 LIBCFS_ALLOC(pinfo, infosz);
1721 if (pinfo == NULL) {
1722 CERROR("Can't allocate ping info[%d]\n", n);
1727 pinfo->pi_pid = the_lnet.ln_pid;
1728 pinfo->pi_magic = LNET_PROTO_PING_MAGIC;
1729 pinfo->pi_features = LNET_PING_FEAT_NI_STATUS;
1731 for (i = 0; i < n; i++) {
1732 lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1734 rc = LNetGetId(i, &id);
1737 ns->ns_nid = id.nid;
1738 ns->ns_status = LNET_NI_STATUS_UP;
1742 ni = lnet_nid2ni_locked(id.nid, 0);
1743 LASSERT(ni != NULL);
1746 LASSERT(ni->ni_status == NULL);
1750 lnet_ni_decref_locked(ni, 0);
1754 the_lnet.ln_ping_info = pinfo;
1759 lnet_destroy_ping_info(void)
1765 cfs_list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1767 ni->ni_status = NULL;
1773 LIBCFS_FREE(the_lnet.ln_ping_info,
1774 offsetof(lnet_ping_info_t,
1775 pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1776 the_lnet.ln_ping_info = NULL;
1781 lnet_ping_target_init(void)
1784 lnet_handle_me_t meh;
1785 lnet_process_id_t id;
1790 rc = lnet_create_ping_info();
1794 /* We can have a tiny EQ since we only need to see the unlink event on
1795 * teardown, which by definition is the last one! */
1796 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1798 CERROR("Can't allocate ping EQ: %d\n", rc);
1802 memset(&id, 0, sizeof(lnet_process_id_t));
1803 id.nid = LNET_NID_ANY;
1804 id.pid = LNET_PID_ANY;
1806 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1807 LNET_PROTO_PING_MATCHBITS, 0,
1808 LNET_UNLINK, LNET_INS_AFTER,
1811 CERROR("Can't create ping ME: %d\n", rc);
1815 /* initialize md content */
1816 infosz = offsetof(lnet_ping_info_t,
1817 pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1818 md.start = the_lnet.ln_ping_info;
1820 md.threshold = LNET_MD_THRESH_INF;
1822 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1823 LNET_MD_MANAGE_REMOTE;
1825 md.eq_handle = the_lnet.ln_ping_target_eq;
1827 rc = LNetMDAttach(meh, md,
1829 &the_lnet.ln_ping_target_md);
1831 CERROR("Can't attach ping MD: %d\n", rc);
1838 rc2 = LNetMEUnlink(meh);
1841 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1844 lnet_destroy_ping_info();
1849 lnet_ping_target_fini(void)
1854 int timeout_ms = 1000;
1855 cfs_sigset_t blocked = cfs_block_allsigs();
1857 LNetMDUnlink(the_lnet.ln_ping_target_md);
1858 /* NB md could be busy; this just starts the unlink */
1861 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1862 timeout_ms, &event, &which);
1864 /* I expect overflow... */
1865 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1868 /* timed out: provide a diagnostic */
1869 CWARN("Still waiting for ping MD to unlink\n");
1874 /* Got a valid event */
1879 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1881 lnet_destroy_ping_info();
1882 cfs_restore_sigs(blocked);
1886 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1888 lnet_handle_eq_t eqh;
1889 lnet_handle_md_t mdh;
1895 const int a_long_time = 60000; /* mS */
1896 int infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1897 lnet_ping_info_t *info;
1898 lnet_process_id_t tmpid;
1903 cfs_sigset_t blocked;
1906 id.nid == LNET_NID_ANY ||
1907 timeout_ms > 500000 || /* arbitrary limit! */
1908 n_ids > 20) /* arbitrary limit! */
1911 if (id.pid == LNET_PID_ANY)
1912 id.pid = LUSTRE_SRV_LNET_PID;
1914 LIBCFS_ALLOC(info, infosz);
1918 /* NB 2 events max (including any unlink event) */
1919 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1921 CERROR("Can't allocate EQ: %d\n", rc);
1925 /* initialize md content */
1928 md.threshold = 2; /*GET/REPLY*/
1930 md.options = LNET_MD_TRUNCATE;
1934 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1936 CERROR("Can't bind MD: %d\n", rc);
1940 rc = LNetGet(LNET_NID_ANY, mdh, id,
1941 LNET_RESERVED_PORTAL,
1942 LNET_PROTO_PING_MATCHBITS, 0);
1945 /* Don't CERROR; this could be deliberate! */
1947 rc2 = LNetMDUnlink(mdh);
1950 /* NB must wait for the UNLINK event below... */
1952 timeout_ms = a_long_time;
1956 /* MUST block for unlink to complete */
1958 blocked = cfs_block_allsigs();
1960 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1963 cfs_restore_sigs(blocked);
1965 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1966 (rc2 <= 0) ? -1 : event.type,
1967 (rc2 <= 0) ? -1 : event.status,
1968 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1970 LASSERT (rc2 != -EOVERFLOW); /* can't miss anything */
1972 if (rc2 <= 0 || event.status != 0) {
1973 /* timeout or error */
1974 if (!replied && rc == 0)
1975 rc = (rc2 < 0) ? rc2 :
1976 (rc2 == 0) ? -ETIMEDOUT :
1980 /* Ensure completion in finite time... */
1982 /* No assertion (racing with network) */
1984 timeout_ms = a_long_time;
1985 } else if (rc2 == 0) {
1986 /* timed out waiting for unlink */
1987 CWARN("ping %s: late network completion\n",
1990 } else if (event.type == LNET_EVENT_REPLY) {
1995 } while (rc2 <= 0 || !event.unlinked);
1999 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2006 LASSERT (nob >= 0 && nob <= infosz);
2008 rc = -EPROTO; /* if I can't parse... */
2011 /* can't check magic/version */
2012 CERROR("%s: ping info too short %d\n",
2013 libcfs_id2str(id), nob);
2017 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2018 lnet_swap_pinginfo(info);
2019 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2020 CERROR("%s: Unexpected magic %08x\n",
2021 libcfs_id2str(id), info->pi_magic);
2025 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
2026 CERROR("%s: ping w/o NI status: 0x%x\n",
2027 libcfs_id2str(id), info->pi_features);
2031 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2032 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2033 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2037 if (info->pi_nnis < n_ids)
2038 n_ids = info->pi_nnis;
2040 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2041 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2042 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2046 rc = -EFAULT; /* If I SEGV... */
2048 for (i = 0; i < n_ids; i++) {
2049 tmpid.pid = info->pi_pid;
2050 tmpid.nid = info->pi_ni[i].ns_nid;
2052 if (cfs_copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2061 rc2 = LNetEQFree(eqh);
2063 CERROR("rc2 %d\n", rc2);
2067 LIBCFS_FREE(info, infosz);