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, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
40 #include <linux/log2.h>
44 #define D_LNI D_CONSOLE
46 #define D_LNI D_CONFIG
49 lnet_t the_lnet; /* THE state of the network */
50 EXPORT_SYMBOL(the_lnet);
54 static char *ip2nets = "";
55 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
56 "LNET network <- IP table");
58 static char *networks = "";
59 CFS_MODULE_PARM(networks, "s", charp, 0444,
62 static char *routes = "";
63 CFS_MODULE_PARM(routes, "s", charp, 0444,
64 "routes to non-local networks");
66 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
67 CFS_MODULE_PARM(rnet_htable_size, "i", int, 0444,
68 "size of remote network hash table");
77 lnet_get_networks(void)
82 if (*networks != 0 && *ip2nets != 0) {
83 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
84 "'ip2nets' but not both at once\n");
89 rc = lnet_parse_ip2nets(&nets, ip2nets);
90 return (rc == 0) ? nets : NULL;
100 lnet_init_locks(void)
102 spin_lock_init(&the_lnet.ln_eq_wait_lock);
103 init_waitqueue_head(&the_lnet.ln_eq_waitq);
104 mutex_init(&the_lnet.ln_lnd_mutex);
105 mutex_init(&the_lnet.ln_api_mutex);
109 lnet_fini_locks(void)
116 lnet_get_routes(void)
118 char *str = getenv("LNET_ROUTES");
120 return (str == NULL) ? "" : str;
124 lnet_get_networks (void)
126 static char default_networks[256];
127 char *networks = getenv ("LNET_NETWORKS");
134 if (networks != NULL)
137 /* In userland, the default 'networks=' is the list of known net types */
138 len = sizeof(default_networks);
139 str = default_networks;
143 cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
144 lnd_t *lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
146 nob = snprintf(str, len, "%s%s", sep,
147 libcfs_lnd2str(lnd->lnd_type));
149 /* overflowed the string; leave it where it was */
158 return default_networks;
161 # ifndef HAVE_LIBPTHREAD
163 void lnet_init_locks(void)
165 the_lnet.ln_eq_wait_lock = 0;
166 the_lnet.ln_lnd_mutex = 0;
167 the_lnet.ln_api_mutex = 0;
170 void lnet_fini_locks(void)
172 LASSERT(the_lnet.ln_api_mutex == 0);
173 LASSERT(the_lnet.ln_lnd_mutex == 0);
174 LASSERT(the_lnet.ln_eq_wait_lock == 0);
179 void lnet_init_locks(void)
181 pthread_cond_init(&the_lnet.ln_eq_cond, NULL);
182 pthread_mutex_init(&the_lnet.ln_eq_wait_lock, NULL);
183 pthread_mutex_init(&the_lnet.ln_lnd_mutex, NULL);
184 pthread_mutex_init(&the_lnet.ln_api_mutex, NULL);
187 void lnet_fini_locks(void)
189 pthread_mutex_destroy(&the_lnet.ln_api_mutex);
190 pthread_mutex_destroy(&the_lnet.ln_lnd_mutex);
191 pthread_mutex_destroy(&the_lnet.ln_eq_wait_lock);
192 pthread_cond_destroy(&the_lnet.ln_eq_cond);
199 lnet_create_remote_nets_table(void)
204 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
205 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
206 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
208 CERROR("Failed to create remote nets hash table\n");
212 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
213 CFS_INIT_LIST_HEAD(&hash[i]);
214 the_lnet.ln_remote_nets_hash = hash;
219 lnet_destroy_remote_nets_table(void)
223 if (the_lnet.ln_remote_nets_hash == NULL)
226 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
227 LASSERT(cfs_list_empty(&the_lnet.ln_remote_nets_hash[i]));
229 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
230 LNET_REMOTE_NETS_HASH_SIZE *
231 sizeof(the_lnet.ln_remote_nets_hash[0]));
232 the_lnet.ln_remote_nets_hash = NULL;
236 lnet_destroy_locks(void)
238 if (the_lnet.ln_res_lock != NULL) {
239 cfs_percpt_lock_free(the_lnet.ln_res_lock);
240 the_lnet.ln_res_lock = NULL;
243 if (the_lnet.ln_net_lock != NULL) {
244 cfs_percpt_lock_free(the_lnet.ln_net_lock);
245 the_lnet.ln_net_lock = NULL;
252 lnet_create_locks(void)
256 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
257 if (the_lnet.ln_res_lock == NULL)
260 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
261 if (the_lnet.ln_net_lock == NULL)
267 lnet_destroy_locks();
271 void lnet_assert_wire_constants (void)
273 /* Wire protocol assertions generated by 'wirecheck'
274 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
275 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
276 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
279 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
280 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
281 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
282 CLASSERT (LNET_MSG_ACK == 0);
283 CLASSERT (LNET_MSG_PUT == 1);
284 CLASSERT (LNET_MSG_GET == 2);
285 CLASSERT (LNET_MSG_REPLY == 3);
286 CLASSERT (LNET_MSG_HELLO == 4);
288 /* Checks for struct ptl_handle_wire_t */
289 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
290 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
291 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
292 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
293 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
295 /* Checks for struct lnet_magicversion_t */
296 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
297 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
298 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
299 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
300 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
301 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
302 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
304 /* Checks for struct lnet_hdr_t */
305 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
306 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
307 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
308 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
309 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
310 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
311 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
312 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
313 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
314 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
315 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
316 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
317 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
318 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
319 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
322 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
323 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
324 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
325 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
326 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
327 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
330 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
331 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
332 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
333 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
334 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
335 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
336 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
337 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
338 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
339 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
342 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
343 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
344 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
345 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
346 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
347 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
348 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
349 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
350 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
351 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
354 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
355 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
358 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
359 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
360 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
361 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
365 lnet_find_lnd_by_type (int type)
370 /* holding lnd mutex */
371 cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
372 lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
374 if ((int)lnd->lnd_type == type)
382 lnet_register_lnd (lnd_t *lnd)
384 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
386 LASSERT (the_lnet.ln_init);
387 LASSERT (libcfs_isknown_lnd(lnd->lnd_type));
388 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
390 cfs_list_add_tail (&lnd->lnd_list, &the_lnet.ln_lnds);
391 lnd->lnd_refcount = 0;
393 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
395 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
397 EXPORT_SYMBOL(lnet_register_lnd);
400 lnet_unregister_lnd (lnd_t *lnd)
402 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
404 LASSERT (the_lnet.ln_init);
405 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
406 LASSERT (lnd->lnd_refcount == 0);
408 cfs_list_del (&lnd->lnd_list);
409 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
411 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
413 EXPORT_SYMBOL(lnet_unregister_lnd);
416 lnet_counters_get(lnet_counters_t *counters)
418 lnet_counters_t *ctr;
421 memset(counters, 0, sizeof(*counters));
423 lnet_net_lock(LNET_LOCK_EX);
425 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
426 counters->msgs_max += ctr->msgs_max;
427 counters->msgs_alloc += ctr->msgs_alloc;
428 counters->errors += ctr->errors;
429 counters->send_count += ctr->send_count;
430 counters->recv_count += ctr->recv_count;
431 counters->route_count += ctr->route_count;
432 counters->drop_count += ctr->drop_count;
433 counters->send_length += ctr->send_length;
434 counters->recv_length += ctr->recv_length;
435 counters->route_length += ctr->route_length;
436 counters->drop_length += ctr->drop_length;
439 lnet_net_unlock(LNET_LOCK_EX);
441 EXPORT_SYMBOL(lnet_counters_get);
444 lnet_counters_reset(void)
446 lnet_counters_t *counters;
449 lnet_net_lock(LNET_LOCK_EX);
451 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
452 memset(counters, 0, sizeof(lnet_counters_t));
454 lnet_net_unlock(LNET_LOCK_EX);
456 EXPORT_SYMBOL(lnet_counters_reset);
458 #ifdef LNET_USE_LIB_FREELIST
461 lnet_freelist_init (lnet_freelist_t *fl, int n, int size)
467 size += offsetof (lnet_freeobj_t, fo_contents);
469 LIBCFS_ALLOC(space, n * size);
473 CFS_INIT_LIST_HEAD (&fl->fl_list);
476 fl->fl_objsize = size;
480 memset (space, 0, size);
481 cfs_list_add ((cfs_list_t *)space, &fl->fl_list);
489 lnet_freelist_fini (lnet_freelist_t *fl)
494 if (fl->fl_nobjs == 0)
498 for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
501 LASSERT (count == fl->fl_nobjs);
503 LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
504 memset (fl, 0, sizeof (*fl));
507 #endif /* LNET_USE_LIB_FREELIST */
509 __u64 lnet_create_interface_cookie (void)
511 /* NB the interface cookie in wire handles guards against delayed
512 * replies and ACKs appearing valid after reboot. Initialisation time,
513 * even if it's only implemented to millisecond resolution is probably
514 * easily good enough. */
518 int rc = gettimeofday (&tv, NULL);
521 do_gettimeofday(&tv);
525 cookie += tv.tv_usec;
530 lnet_res_type2str(int type)
535 case LNET_COOKIE_TYPE_MD:
537 case LNET_COOKIE_TYPE_ME:
539 case LNET_COOKIE_TYPE_EQ:
545 lnet_res_container_cleanup(struct lnet_res_container *rec)
549 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
552 while (!cfs_list_empty(&rec->rec_active)) {
553 cfs_list_t *e = rec->rec_active.next;
555 cfs_list_del_init(e);
556 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
557 lnet_eq_free(cfs_list_entry(e, lnet_eq_t, eq_list));
559 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
560 lnet_md_free(cfs_list_entry(e, lnet_libmd_t, md_list));
562 } else { /* NB: Active MEs should be attached on portals */
569 /* Found alive MD/ME/EQ, user really should unlink/free
570 * all of them before finalize LNet, but if someone didn't,
571 * we have to recycle garbage for him */
572 CERROR("%d active elements on exit of %s container\n",
573 count, lnet_res_type2str(rec->rec_type));
576 #ifdef LNET_USE_LIB_FREELIST
577 lnet_freelist_fini(&rec->rec_freelist);
579 if (rec->rec_lh_hash != NULL) {
580 LIBCFS_FREE(rec->rec_lh_hash,
581 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
582 rec->rec_lh_hash = NULL;
585 rec->rec_type = 0; /* mark it as finalized */
589 lnet_res_container_setup(struct lnet_res_container *rec,
590 int cpt, int type, int objnum, int objsz)
595 LASSERT(rec->rec_type == 0);
597 rec->rec_type = type;
598 CFS_INIT_LIST_HEAD(&rec->rec_active);
600 #ifdef LNET_USE_LIB_FREELIST
601 memset(&rec->rec_freelist, 0, sizeof(rec->rec_freelist));
602 rc = lnet_freelist_init(&rec->rec_freelist, objnum, objsz);
606 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
608 /* Arbitrary choice of hash table size */
609 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
610 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
611 if (rec->rec_lh_hash == NULL) {
616 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
617 CFS_INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
622 CERROR("Failed to setup %s resource container\n",
623 lnet_res_type2str(type));
624 lnet_res_container_cleanup(rec);
629 lnet_res_containers_destroy(struct lnet_res_container **recs)
631 struct lnet_res_container *rec;
634 cfs_percpt_for_each(rec, i, recs)
635 lnet_res_container_cleanup(rec);
637 cfs_percpt_free(recs);
640 static struct lnet_res_container **
641 lnet_res_containers_create(int type, int objnum, int objsz)
643 struct lnet_res_container **recs;
644 struct lnet_res_container *rec;
648 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
650 CERROR("Failed to allocate %s resource containers\n",
651 lnet_res_type2str(type));
655 cfs_percpt_for_each(rec, i, recs) {
656 rc = lnet_res_container_setup(rec, i, type, objnum, objsz);
658 lnet_res_containers_destroy(recs);
667 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
669 /* ALWAYS called with lnet_res_lock held */
671 lnet_libhandle_t *lh;
674 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
677 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
678 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
680 cfs_list_for_each_entry(lh, head, lh_hash_chain) {
681 if (lh->lh_cookie == cookie)
689 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
691 /* ALWAYS called with lnet_res_lock held */
692 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
695 lh->lh_cookie = rec->rec_lh_cookie;
696 rec->rec_lh_cookie += 1 << ibits;
698 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
700 cfs_list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
705 * Reserved API - do not use.
706 * Temporary workaround to allow uOSS and test programs force server
707 * mode in userspace. See comments near ln_server_mode_flag in
708 * lnet/lib-types.h */
712 the_lnet.ln_server_mode_flag = 1;
716 int lnet_unprepare(void);
719 lnet_prepare(lnet_pid_t requested_pid)
721 /* Prepare to bring up the network */
722 struct lnet_res_container **recs;
725 LASSERT (the_lnet.ln_refcount == 0);
727 the_lnet.ln_routing = 0;
730 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
731 the_lnet.ln_pid = requested_pid;
733 if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
734 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
736 if (current_uid() != 0) /* Only root can run user-space server */
738 the_lnet.ln_pid = requested_pid;
740 } else {/* client case (liblustre) */
742 /* My PID must be unique on this node and flag I'm userspace */
743 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
747 CFS_INIT_LIST_HEAD(&the_lnet.ln_test_peers);
748 CFS_INIT_LIST_HEAD(&the_lnet.ln_nis);
749 CFS_INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
750 CFS_INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
751 CFS_INIT_LIST_HEAD(&the_lnet.ln_routers);
753 rc = lnet_create_remote_nets_table();
757 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
759 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
760 sizeof(lnet_counters_t));
761 if (the_lnet.ln_counters == NULL) {
762 CERROR("Failed to allocate counters for LNet\n");
767 rc = lnet_peer_tables_create();
771 rc = lnet_msg_containers_create();
775 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
776 LNET_COOKIE_TYPE_EQ, LNET_FL_MAX_EQS,
781 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME, LNET_FL_MAX_MES,
786 the_lnet.ln_me_containers = recs;
788 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD, LNET_FL_MAX_MDS,
789 sizeof(lnet_libmd_t));
793 the_lnet.ln_md_containers = recs;
795 rc = lnet_portals_create();
797 CERROR("Failed to create portals for LNet: %d\n", rc);
809 lnet_unprepare (void)
811 /* NB no LNET_LOCK since this is the last reference. All LND instances
812 * have shut down already, so it is safe to unlink and free all
813 * descriptors, even those that appear committed to a network op (eg MD
814 * with non-zero pending count) */
816 lnet_fail_nid(LNET_NID_ANY, 0);
818 LASSERT(the_lnet.ln_refcount == 0);
819 LASSERT(cfs_list_empty(&the_lnet.ln_test_peers));
820 LASSERT(cfs_list_empty(&the_lnet.ln_nis));
821 LASSERT(cfs_list_empty(&the_lnet.ln_nis_cpt));
822 LASSERT(cfs_list_empty(&the_lnet.ln_nis_zombie));
824 lnet_portals_destroy();
826 if (the_lnet.ln_md_containers != NULL) {
827 lnet_res_containers_destroy(the_lnet.ln_md_containers);
828 the_lnet.ln_md_containers = NULL;
831 if (the_lnet.ln_me_containers != NULL) {
832 lnet_res_containers_destroy(the_lnet.ln_me_containers);
833 the_lnet.ln_me_containers = NULL;
836 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
838 lnet_msg_containers_destroy();
839 lnet_peer_tables_destroy();
840 lnet_rtrpools_free();
842 if (the_lnet.ln_counters != NULL) {
843 cfs_percpt_free(the_lnet.ln_counters);
844 the_lnet.ln_counters = NULL;
846 lnet_destroy_remote_nets_table();
852 lnet_net2ni_locked(__u32 net, int cpt)
857 LASSERT(cpt != LNET_LOCK_EX);
859 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
860 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
862 if (LNET_NIDNET(ni->ni_nid) == net) {
863 lnet_ni_addref_locked(ni, cpt);
872 lnet_net2ni(__u32 net)
877 ni = lnet_net2ni_locked(net, 0);
882 EXPORT_SYMBOL(lnet_net2ni);
885 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
890 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
895 val = hash_long(key, LNET_CPT_BITS);
896 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
900 return (unsigned int)(key + val + (val >> 1)) % number;
904 lnet_cpt_of_nid_locked(lnet_nid_t nid)
908 /* must called with hold of lnet_net_lock */
909 if (LNET_CPT_NUMBER == 1)
910 return 0; /* the only one */
912 /* take lnet_net_lock(any) would be OK */
913 if (!cfs_list_empty(&the_lnet.ln_nis_cpt)) {
914 cfs_list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
915 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
918 LASSERT(ni->ni_cpts != NULL);
919 return ni->ni_cpts[lnet_nid_cpt_hash
920 (nid, ni->ni_ncpts)];
924 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
928 lnet_cpt_of_nid(lnet_nid_t nid)
933 if (LNET_CPT_NUMBER == 1)
934 return 0; /* the only one */
936 if (cfs_list_empty(&the_lnet.ln_nis_cpt))
937 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
939 cpt = lnet_net_lock_current();
940 cpt2 = lnet_cpt_of_nid_locked(nid);
941 lnet_net_unlock(cpt);
945 EXPORT_SYMBOL(lnet_cpt_of_nid);
948 lnet_islocalnet(__u32 net)
953 cpt = lnet_net_lock_current();
955 ni = lnet_net2ni_locked(net, cpt);
957 lnet_ni_decref_locked(ni, cpt);
959 lnet_net_unlock(cpt);
965 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
970 LASSERT(cpt != LNET_LOCK_EX);
972 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
973 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
975 if (ni->ni_nid == nid) {
976 lnet_ni_addref_locked(ni, cpt);
985 lnet_islocalnid(lnet_nid_t nid)
990 cpt = lnet_net_lock_current();
991 ni = lnet_nid2ni_locked(nid, cpt);
993 lnet_ni_decref_locked(ni, cpt);
994 lnet_net_unlock(cpt);
1000 lnet_count_acceptor_nis (void)
1002 /* Return the # of NIs that need the acceptor. */
1004 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
1009 cpt = lnet_net_lock_current();
1010 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
1011 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
1013 if (ni->ni_lnd->lnd_accept != NULL)
1017 lnet_net_unlock(cpt);
1019 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
1024 lnet_ni_tq_credits(lnet_ni_t *ni)
1028 LASSERT(ni->ni_ncpts >= 1);
1030 if (ni->ni_ncpts == 1)
1031 return ni->ni_maxtxcredits;
1033 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
1034 credits = max(credits, 8 * ni->ni_peertxcredits);
1035 credits = min(credits, ni->ni_maxtxcredits);
1041 lnet_shutdown_lndnis (void)
1047 /* NB called holding the global mutex */
1049 /* All quiet on the API front */
1050 LASSERT(!the_lnet.ln_shutdown);
1051 LASSERT(the_lnet.ln_refcount == 0);
1052 LASSERT(cfs_list_empty(&the_lnet.ln_nis_zombie));
1054 lnet_net_lock(LNET_LOCK_EX);
1055 the_lnet.ln_shutdown = 1; /* flag shutdown */
1057 /* Unlink NIs from the global table */
1058 while (!cfs_list_empty(&the_lnet.ln_nis)) {
1059 ni = cfs_list_entry(the_lnet.ln_nis.next,
1060 lnet_ni_t, ni_list);
1061 /* move it to zombie list and nobody can find it anymore */
1062 cfs_list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1063 lnet_ni_decref_locked(ni, 0); /* drop ln_nis' ref */
1065 if (!cfs_list_empty(&ni->ni_cptlist)) {
1066 cfs_list_del_init(&ni->ni_cptlist);
1067 lnet_ni_decref_locked(ni, 0);
1071 /* Drop the cached eqwait NI. */
1072 if (the_lnet.ln_eq_waitni != NULL) {
1073 lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
1074 the_lnet.ln_eq_waitni = NULL;
1077 /* Drop the cached loopback NI. */
1078 if (the_lnet.ln_loni != NULL) {
1079 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1080 the_lnet.ln_loni = NULL;
1083 lnet_net_unlock(LNET_LOCK_EX);
1085 /* Clear lazy portals and drop delayed messages which hold refs
1086 * on their lnet_msg_t::msg_rxpeer */
1087 for (i = 0; i < the_lnet.ln_nportals; i++)
1088 LNetClearLazyPortal(i);
1090 /* Clear the peer table and wait for all peers to go (they hold refs on
1092 lnet_peer_tables_cleanup();
1094 lnet_net_lock(LNET_LOCK_EX);
1095 /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
1096 * and shut them down in guaranteed thread context */
1098 while (!list_empty(&the_lnet.ln_nis_zombie)) {
1102 ni = list_entry(the_lnet.ln_nis_zombie.next,
1103 lnet_ni_t, ni_list);
1104 list_del_init(&ni->ni_list);
1105 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1108 /* still busy, add it back to zombie list */
1109 list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1113 if (!list_empty(&ni->ni_list)) {
1114 lnet_net_unlock(LNET_LOCK_EX);
1116 if ((i & (-i)) == i) {
1117 CDEBUG(D_WARNING, "Waiting for zombie LNI %s\n",
1118 libcfs_nid2str(ni->ni_nid));
1120 cfs_pause(cfs_time_seconds(1));
1121 lnet_net_lock(LNET_LOCK_EX);
1125 ni->ni_lnd->lnd_refcount--;
1126 lnet_net_unlock(LNET_LOCK_EX);
1128 islo = ni->ni_lnd->lnd_type == LOLND;
1130 LASSERT (!in_interrupt ());
1131 (ni->ni_lnd->lnd_shutdown)(ni);
1133 /* can't deref lnd anymore now; it might have unregistered
1137 CDEBUG(D_LNI, "Removed LNI %s\n",
1138 libcfs_nid2str(ni->ni_nid));
1143 lnet_net_lock(LNET_LOCK_EX);
1146 the_lnet.ln_shutdown = 0;
1147 lnet_net_unlock(LNET_LOCK_EX);
1149 if (the_lnet.ln_network_tokens != NULL) {
1150 LIBCFS_FREE(the_lnet.ln_network_tokens,
1151 the_lnet.ln_network_tokens_nob);
1152 the_lnet.ln_network_tokens = NULL;
1157 lnet_startup_lndnis (void)
1161 struct lnet_tx_queue *tq;
1167 char *nets = lnet_get_networks();
1169 CFS_INIT_LIST_HEAD(&nilist);
1174 rc = lnet_parse_networks(&nilist, nets);
1178 while (!cfs_list_empty(&nilist)) {
1179 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1180 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1182 LASSERT (libcfs_isknown_lnd(lnd_type));
1184 if (lnd_type == CIBLND ||
1185 lnd_type == OPENIBLND ||
1186 lnd_type == IIBLND ||
1187 lnd_type == VIBLND) {
1188 CERROR("LND %s obsoleted\n",
1189 libcfs_lnd2str(lnd_type));
1193 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1194 lnd = lnet_find_lnd_by_type(lnd_type);
1198 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1199 rc = request_module("%s",
1200 libcfs_lnd2modname(lnd_type));
1201 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1203 lnd = lnet_find_lnd_by_type(lnd_type);
1205 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1206 CERROR("Can't load LND %s, module %s, rc=%d\n",
1207 libcfs_lnd2str(lnd_type),
1208 libcfs_lnd2modname(lnd_type), rc);
1209 #ifndef HAVE_MODULE_LOADING_SUPPORT
1210 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1211 "compiled with kernel module "
1212 "loading support.");
1219 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1220 CERROR("LND %s not supported\n",
1221 libcfs_lnd2str(lnd_type));
1226 lnet_net_lock(LNET_LOCK_EX);
1227 lnd->lnd_refcount++;
1228 lnet_net_unlock(LNET_LOCK_EX);
1232 rc = (lnd->lnd_startup)(ni);
1234 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1237 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1239 rc, libcfs_lnd2str(lnd->lnd_type));
1240 lnet_net_lock(LNET_LOCK_EX);
1241 lnd->lnd_refcount--;
1242 lnet_net_unlock(LNET_LOCK_EX);
1246 LASSERT (ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1248 cfs_list_del(&ni->ni_list);
1250 lnet_net_lock(LNET_LOCK_EX);
1251 /* refcount for ln_nis */
1252 lnet_ni_addref_locked(ni, 0);
1253 cfs_list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1254 if (ni->ni_cpts != NULL) {
1255 cfs_list_add_tail(&ni->ni_cptlist,
1256 &the_lnet.ln_nis_cpt);
1257 lnet_ni_addref_locked(ni, 0);
1260 lnet_net_unlock(LNET_LOCK_EX);
1262 if (lnd->lnd_type == LOLND) {
1264 LASSERT (the_lnet.ln_loni == NULL);
1265 the_lnet.ln_loni = ni;
1270 if (lnd->lnd_wait != NULL) {
1271 if (the_lnet.ln_eq_waitni == NULL) {
1273 the_lnet.ln_eq_waitni = ni;
1276 # ifndef HAVE_LIBPTHREAD
1277 LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1278 "single-threaded runtime\n",
1279 libcfs_lnd2str(lnd_type));
1284 if (ni->ni_peertxcredits == 0 ||
1285 ni->ni_maxtxcredits == 0) {
1286 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1287 libcfs_lnd2str(lnd->lnd_type),
1288 ni->ni_peertxcredits == 0 ?
1293 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1294 tq->tq_credits_min =
1295 tq->tq_credits_max =
1296 tq->tq_credits = lnet_ni_tq_credits(ni);
1299 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1300 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1301 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1302 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1307 if (the_lnet.ln_eq_waitni != NULL && nicount > 1) {
1308 lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1309 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1311 libcfs_lnd2str(lnd_type));
1318 lnet_shutdown_lndnis();
1320 while (!cfs_list_empty(&nilist)) {
1321 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1322 cfs_list_del(&ni->ni_list);
1330 * Initialize LNet library.
1332 * Only userspace program needs to call this function - it's automatically
1333 * called in the kernel at module loading time. Caller has to call LNetFini()
1334 * after a call to LNetInit(), if and only if the latter returned 0. It must
1335 * be called exactly once.
1337 * \return 0 on success, and -ve on failures.
1344 lnet_assert_wire_constants();
1345 LASSERT(!the_lnet.ln_init);
1347 memset(&the_lnet, 0, sizeof(the_lnet));
1349 /* refer to global cfs_cpt_table for now */
1350 the_lnet.ln_cpt_table = cfs_cpt_table;
1351 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1353 LASSERT(the_lnet.ln_cpt_number > 0);
1354 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1355 /* we are under risk of consuming all lh_cookie */
1356 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1357 "please change setting of CPT-table and retry\n",
1358 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1362 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1363 the_lnet.ln_cpt_bits++;
1365 rc = lnet_create_locks();
1367 CERROR("Can't create LNet global locks: %d\n", rc);
1371 the_lnet.ln_refcount = 0;
1372 the_lnet.ln_init = 1;
1373 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1374 CFS_INIT_LIST_HEAD(&the_lnet.ln_lnds);
1375 CFS_INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1376 CFS_INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1379 /* The hash table size is the number of bits it takes to express the set
1380 * ln_num_routes, minus 1 (better to under estimate than over so we
1381 * don't waste memory). */
1382 if (rnet_htable_size <= 0)
1383 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1384 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1385 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1386 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1387 order_base_2(rnet_htable_size) - 1);
1389 /* All LNDs apart from the LOLND are in separate modules. They
1390 * register themselves when their module loads, and unregister
1391 * themselves when their module is unloaded. */
1393 the_lnet.ln_remote_nets_hbits = 8;
1396 * NB the order here determines default 'networks=' order */
1397 # ifdef HAVE_LIBPTHREAD
1398 LNET_REGISTER_ULND(the_tcplnd);
1401 lnet_register_lnd(&the_lolnd);
1404 EXPORT_SYMBOL(LNetInit);
1407 * Finalize LNet library.
1409 * Only userspace program needs to call this function. It can be called
1412 * \pre LNetInit() called with success.
1413 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1418 LASSERT(the_lnet.ln_init);
1419 LASSERT(the_lnet.ln_refcount == 0);
1421 while (!cfs_list_empty(&the_lnet.ln_lnds))
1422 lnet_unregister_lnd(cfs_list_entry(the_lnet.ln_lnds.next,
1424 lnet_destroy_locks();
1426 the_lnet.ln_init = 0;
1428 EXPORT_SYMBOL(LNetFini);
1431 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1433 * Userspace program should call this after a successful call to LNetInit().
1434 * Users must call this function at least once before any other functions.
1435 * For each successful call there must be a corresponding call to
1436 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1439 * The PID used by LNet may be different from the one requested.
1442 * \param requested_pid PID requested by the caller.
1444 * \return >= 0 on success, and < 0 error code on failures.
1447 LNetNIInit(lnet_pid_t requested_pid)
1449 int im_a_router = 0;
1452 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1454 LASSERT (the_lnet.ln_init);
1455 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1457 if (the_lnet.ln_refcount > 0) {
1458 rc = the_lnet.ln_refcount++;
1462 lnet_get_tunables();
1464 if (requested_pid == LNET_PID_ANY) {
1465 /* Don't instantiate LNET just for me */
1470 rc = lnet_prepare(requested_pid);
1474 rc = lnet_startup_lndnis();
1478 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1482 rc = lnet_check_routes();
1486 rc = lnet_rtrpools_alloc(im_a_router);
1490 rc = lnet_acceptor_start();
1494 the_lnet.ln_refcount = 1;
1495 /* Now I may use my own API functions... */
1497 /* NB router checker needs the_lnet.ln_ping_info in
1498 * lnet_router_checker -> lnet_update_ni_status_locked */
1499 rc = lnet_ping_target_init();
1503 rc = lnet_router_checker_start();
1511 lnet_ping_target_fini();
1513 the_lnet.ln_refcount = 0;
1514 lnet_acceptor_stop();
1516 lnet_destroy_routes();
1517 lnet_shutdown_lndnis();
1523 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1526 EXPORT_SYMBOL(LNetNIInit);
1529 * Stop LNet interfaces, routing, and forwarding.
1531 * Users must call this function once for each successful call to LNetNIInit().
1532 * Once the LNetNIFini() operation has been started, the results of pending
1533 * API operations are undefined.
1535 * \return always 0 for current implementation.
1540 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1542 LASSERT (the_lnet.ln_init);
1543 LASSERT (the_lnet.ln_refcount > 0);
1545 if (the_lnet.ln_refcount != 1) {
1546 the_lnet.ln_refcount--;
1548 LASSERT (!the_lnet.ln_niinit_self);
1551 lnet_router_checker_stop();
1552 lnet_ping_target_fini();
1554 /* Teardown fns that use my own API functions BEFORE here */
1555 the_lnet.ln_refcount = 0;
1557 lnet_acceptor_stop();
1558 lnet_destroy_routes();
1559 lnet_shutdown_lndnis();
1563 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1566 EXPORT_SYMBOL(LNetNIFini);
1569 * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1570 * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1571 * internal ioctl handler.
1573 * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1575 * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1576 * The data will be printed to system console. Don't use it excessively.
1577 * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1579 * \return Always return 0 when called by users directly (i.e., not via ioctl).
1582 LNetCtl(unsigned int cmd, void *arg)
1584 struct libcfs_ioctl_data *data = arg;
1585 lnet_process_id_t id = {0};
1589 LASSERT (the_lnet.ln_init);
1590 LASSERT (the_lnet.ln_refcount > 0);
1593 case IOC_LIBCFS_GET_NI:
1594 rc = LNetGetId(data->ioc_count, &id);
1595 data->ioc_nid = id.nid;
1598 case IOC_LIBCFS_FAIL_NID:
1599 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1601 case IOC_LIBCFS_ADD_ROUTE:
1602 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1603 data->ioc_nid, data->ioc_priority);
1604 return (rc != 0) ? rc : lnet_check_routes();
1606 case IOC_LIBCFS_DEL_ROUTE:
1607 return lnet_del_route(data->ioc_net, data->ioc_nid);
1609 case IOC_LIBCFS_GET_ROUTE:
1610 return lnet_get_route(data->ioc_count,
1611 &data->ioc_net, &data->ioc_count,
1612 &data->ioc_nid, &data->ioc_flags,
1613 &data->ioc_priority);
1614 case IOC_LIBCFS_NOTIFY_ROUTER:
1615 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1616 cfs_time_current() -
1617 cfs_time_seconds(cfs_time_current_sec() -
1618 (time_t)data->ioc_u64[0]));
1620 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1621 /* This can be removed once lustre stops calling it */
1624 case IOC_LIBCFS_LNET_DIST:
1625 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1626 if (rc < 0 && rc != -EHOSTUNREACH)
1629 data->ioc_u32[0] = rc;
1632 case IOC_LIBCFS_TESTPROTOCOMPAT:
1633 lnet_net_lock(LNET_LOCK_EX);
1634 the_lnet.ln_testprotocompat = data->ioc_flags;
1635 lnet_net_unlock(LNET_LOCK_EX);
1638 case IOC_LIBCFS_PING:
1639 id.nid = data->ioc_nid;
1640 id.pid = data->ioc_u32[0];
1641 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1642 (lnet_process_id_t *)data->ioc_pbuf1,
1643 data->ioc_plen1/sizeof(lnet_process_id_t));
1646 data->ioc_count = rc;
1649 case IOC_LIBCFS_DEBUG_PEER: {
1650 /* CAVEAT EMPTOR: this one designed for calling directly; not
1652 id = *((lnet_process_id_t *) arg);
1654 lnet_debug_peer(id.nid);
1656 ni = lnet_net2ni(LNET_NIDNET(id.nid));
1658 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1660 if (ni->ni_lnd->lnd_ctl == NULL) {
1661 CDEBUG(D_WARNING, "No ctl for %s\n",
1664 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1673 ni = lnet_net2ni(data->ioc_net);
1677 if (ni->ni_lnd->lnd_ctl == NULL)
1680 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1687 EXPORT_SYMBOL(LNetCtl);
1690 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
1691 * all interfaces share a same PID, as requested by LNetNIInit().
1693 * \param index Index of the interface to look up.
1694 * \param id On successful return, this location will hold the
1695 * lnet_process_id_t ID of the interface.
1697 * \retval 0 If an interface exists at \a index.
1698 * \retval -ENOENT If no interface has been found.
1701 LNetGetId(unsigned int index, lnet_process_id_t *id)
1708 LASSERT(the_lnet.ln_init);
1709 LASSERT(the_lnet.ln_refcount > 0);
1711 cpt = lnet_net_lock_current();
1713 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
1717 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
1719 id->nid = ni->ni_nid;
1720 id->pid = the_lnet.ln_pid;
1725 lnet_net_unlock(cpt);
1728 EXPORT_SYMBOL(LNetGetId);
1731 * Print a string representation of handle \a h into buffer \a str of
1735 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1737 snprintf(str, len, LPX64, h.cookie);
1739 EXPORT_SYMBOL(LNetSnprintHandle);
1742 lnet_create_ping_info(void)
1747 unsigned int infosz;
1749 lnet_process_id_t id;
1750 lnet_ping_info_t *pinfo;
1752 for (n = 0; ; n++) {
1753 rc = LNetGetId(n, &id);
1760 infosz = offsetof(lnet_ping_info_t, pi_ni[n]);
1761 LIBCFS_ALLOC(pinfo, infosz);
1762 if (pinfo == NULL) {
1763 CERROR("Can't allocate ping info[%d]\n", n);
1768 pinfo->pi_pid = the_lnet.ln_pid;
1769 pinfo->pi_magic = LNET_PROTO_PING_MAGIC;
1770 pinfo->pi_features = LNET_PING_FEAT_NI_STATUS;
1772 for (i = 0; i < n; i++) {
1773 lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1775 rc = LNetGetId(i, &id);
1778 ns->ns_nid = id.nid;
1779 ns->ns_status = LNET_NI_STATUS_UP;
1783 ni = lnet_nid2ni_locked(id.nid, 0);
1784 LASSERT(ni != NULL);
1787 LASSERT(ni->ni_status == NULL);
1791 lnet_ni_decref_locked(ni, 0);
1795 the_lnet.ln_ping_info = pinfo;
1800 lnet_destroy_ping_info(void)
1806 cfs_list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1808 ni->ni_status = NULL;
1814 LIBCFS_FREE(the_lnet.ln_ping_info,
1815 offsetof(lnet_ping_info_t,
1816 pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1817 the_lnet.ln_ping_info = NULL;
1822 lnet_ping_target_init(void)
1825 lnet_handle_me_t meh;
1826 lnet_process_id_t id;
1831 rc = lnet_create_ping_info();
1835 /* We can have a tiny EQ since we only need to see the unlink event on
1836 * teardown, which by definition is the last one! */
1837 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1839 CERROR("Can't allocate ping EQ: %d\n", rc);
1843 memset(&id, 0, sizeof(lnet_process_id_t));
1844 id.nid = LNET_NID_ANY;
1845 id.pid = LNET_PID_ANY;
1847 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1848 LNET_PROTO_PING_MATCHBITS, 0,
1849 LNET_UNLINK, LNET_INS_AFTER,
1852 CERROR("Can't create ping ME: %d\n", rc);
1856 /* initialize md content */
1857 infosz = offsetof(lnet_ping_info_t,
1858 pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1859 md.start = the_lnet.ln_ping_info;
1861 md.threshold = LNET_MD_THRESH_INF;
1863 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1864 LNET_MD_MANAGE_REMOTE;
1866 md.eq_handle = the_lnet.ln_ping_target_eq;
1868 rc = LNetMDAttach(meh, md,
1870 &the_lnet.ln_ping_target_md);
1872 CERROR("Can't attach ping MD: %d\n", rc);
1879 rc2 = LNetMEUnlink(meh);
1882 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1885 lnet_destroy_ping_info();
1890 lnet_ping_target_fini(void)
1895 int timeout_ms = 1000;
1896 sigset_t blocked = cfs_block_allsigs();
1898 LNetMDUnlink(the_lnet.ln_ping_target_md);
1899 /* NB md could be busy; this just starts the unlink */
1902 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1903 timeout_ms, &event, &which);
1905 /* I expect overflow... */
1906 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1909 /* timed out: provide a diagnostic */
1910 CWARN("Still waiting for ping MD to unlink\n");
1915 /* Got a valid event */
1920 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1922 lnet_destroy_ping_info();
1923 cfs_restore_sigs(blocked);
1927 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1929 lnet_handle_eq_t eqh;
1930 lnet_handle_md_t mdh;
1936 const int a_long_time = 60000; /* mS */
1937 int infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1938 lnet_ping_info_t *info;
1939 lnet_process_id_t tmpid;
1947 id.nid == LNET_NID_ANY ||
1948 timeout_ms > 500000 || /* arbitrary limit! */
1949 n_ids > 20) /* arbitrary limit! */
1952 if (id.pid == LNET_PID_ANY)
1953 id.pid = LUSTRE_SRV_LNET_PID;
1955 LIBCFS_ALLOC(info, infosz);
1959 /* NB 2 events max (including any unlink event) */
1960 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1962 CERROR("Can't allocate EQ: %d\n", rc);
1966 /* initialize md content */
1969 md.threshold = 2; /*GET/REPLY*/
1971 md.options = LNET_MD_TRUNCATE;
1975 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1977 CERROR("Can't bind MD: %d\n", rc);
1981 rc = LNetGet(LNET_NID_ANY, mdh, id,
1982 LNET_RESERVED_PORTAL,
1983 LNET_PROTO_PING_MATCHBITS, 0);
1986 /* Don't CERROR; this could be deliberate! */
1988 rc2 = LNetMDUnlink(mdh);
1991 /* NB must wait for the UNLINK event below... */
1993 timeout_ms = a_long_time;
1997 /* MUST block for unlink to complete */
1999 blocked = cfs_block_allsigs();
2001 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
2004 cfs_restore_sigs(blocked);
2006 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
2007 (rc2 <= 0) ? -1 : event.type,
2008 (rc2 <= 0) ? -1 : event.status,
2009 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
2011 LASSERT (rc2 != -EOVERFLOW); /* can't miss anything */
2013 if (rc2 <= 0 || event.status != 0) {
2014 /* timeout or error */
2015 if (!replied && rc == 0)
2016 rc = (rc2 < 0) ? rc2 :
2017 (rc2 == 0) ? -ETIMEDOUT :
2021 /* Ensure completion in finite time... */
2023 /* No assertion (racing with network) */
2025 timeout_ms = a_long_time;
2026 } else if (rc2 == 0) {
2027 /* timed out waiting for unlink */
2028 CWARN("ping %s: late network completion\n",
2031 } else if (event.type == LNET_EVENT_REPLY) {
2036 } while (rc2 <= 0 || !event.unlinked);
2040 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2047 LASSERT (nob >= 0 && nob <= infosz);
2049 rc = -EPROTO; /* if I can't parse... */
2052 /* can't check magic/version */
2053 CERROR("%s: ping info too short %d\n",
2054 libcfs_id2str(id), nob);
2058 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2059 lnet_swap_pinginfo(info);
2060 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2061 CERROR("%s: Unexpected magic %08x\n",
2062 libcfs_id2str(id), info->pi_magic);
2066 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
2067 CERROR("%s: ping w/o NI status: 0x%x\n",
2068 libcfs_id2str(id), info->pi_features);
2072 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2073 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2074 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2078 if (info->pi_nnis < n_ids)
2079 n_ids = info->pi_nnis;
2081 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2082 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2083 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2087 rc = -EFAULT; /* If I SEGV... */
2089 for (i = 0; i < n_ids; i++) {
2090 tmpid.pid = info->pi_pid;
2091 tmpid.nid = info->pi_ni[i].ns_nid;
2092 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2098 rc2 = LNetEQFree(eqh);
2100 CERROR("rc2 %d\n", rc2);
2104 LIBCFS_FREE(info, infosz);