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, 2014, 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>
39 #include <lnet/lib-dlc.h>
41 #include <linux/log2.h>
45 #define D_LNI D_CONSOLE
47 #define D_LNI D_CONFIG
50 lnet_t the_lnet; /* THE state of the network */
51 EXPORT_SYMBOL(the_lnet);
55 static char *ip2nets = "";
56 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
57 "LNET network <- IP table");
59 static char *networks = "";
60 CFS_MODULE_PARM(networks, "s", charp, 0444,
63 static char *routes = "";
64 CFS_MODULE_PARM(routes, "s", charp, 0444,
65 "routes to non-local networks");
67 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
68 CFS_MODULE_PARM(rnet_htable_size, "i", int, 0444,
69 "size of remote network hash table");
71 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
72 lnet_process_id_t *ids, int n_ids);
81 lnet_get_networks(void)
86 if (*networks != 0 && *ip2nets != 0) {
87 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
88 "'ip2nets' but not both at once\n");
93 rc = lnet_parse_ip2nets(&nets, ip2nets);
94 return (rc == 0) ? nets : NULL;
104 lnet_init_locks(void)
106 spin_lock_init(&the_lnet.ln_eq_wait_lock);
107 init_waitqueue_head(&the_lnet.ln_eq_waitq);
108 mutex_init(&the_lnet.ln_lnd_mutex);
109 mutex_init(&the_lnet.ln_api_mutex);
113 lnet_fini_locks(void)
120 lnet_get_routes(void)
122 char *str = getenv("LNET_ROUTES");
124 return (str == NULL) ? "" : str;
128 lnet_get_networks (void)
130 static char default_networks[256];
131 char *networks = getenv("LNET_NETWORKS");
136 struct list_head *tmp;
138 if (networks != NULL)
141 /* In userland, the default 'networks=' is the list of known net types */
142 len = sizeof(default_networks);
143 str = default_networks;
147 list_for_each(tmp, &the_lnet.ln_lnds) {
148 lnd_t *lnd = list_entry(tmp, lnd_t, lnd_list);
150 nob = snprintf(str, len, "%s%s", sep,
151 libcfs_lnd2str(lnd->lnd_type));
153 /* overflowed the string; leave it where it was */
162 return default_networks;
165 # ifndef HAVE_LIBPTHREAD
167 static void lnet_init_locks(void)
169 the_lnet.ln_eq_wait_lock = 0;
170 the_lnet.ln_lnd_mutex = 0;
171 the_lnet.ln_api_mutex = 0;
174 static void lnet_fini_locks(void)
176 LASSERT(the_lnet.ln_api_mutex == 0);
177 LASSERT(the_lnet.ln_lnd_mutex == 0);
178 LASSERT(the_lnet.ln_eq_wait_lock == 0);
183 static void lnet_init_locks(void)
185 pthread_cond_init(&the_lnet.ln_eq_cond, NULL);
186 pthread_mutex_init(&the_lnet.ln_eq_wait_lock, NULL);
187 pthread_mutex_init(&the_lnet.ln_lnd_mutex, NULL);
188 pthread_mutex_init(&the_lnet.ln_api_mutex, NULL);
191 static void lnet_fini_locks(void)
193 pthread_mutex_destroy(&the_lnet.ln_api_mutex);
194 pthread_mutex_destroy(&the_lnet.ln_lnd_mutex);
195 pthread_mutex_destroy(&the_lnet.ln_eq_wait_lock);
196 pthread_cond_destroy(&the_lnet.ln_eq_cond);
203 lnet_create_remote_nets_table(void)
206 struct list_head *hash;
208 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
209 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
210 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
212 CERROR("Failed to create remote nets hash table\n");
216 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
217 INIT_LIST_HEAD(&hash[i]);
218 the_lnet.ln_remote_nets_hash = hash;
223 lnet_destroy_remote_nets_table(void)
227 if (the_lnet.ln_remote_nets_hash == NULL)
230 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
231 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
233 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
234 LNET_REMOTE_NETS_HASH_SIZE *
235 sizeof(the_lnet.ln_remote_nets_hash[0]));
236 the_lnet.ln_remote_nets_hash = NULL;
240 lnet_destroy_locks(void)
242 if (the_lnet.ln_res_lock != NULL) {
243 cfs_percpt_lock_free(the_lnet.ln_res_lock);
244 the_lnet.ln_res_lock = NULL;
247 if (the_lnet.ln_net_lock != NULL) {
248 cfs_percpt_lock_free(the_lnet.ln_net_lock);
249 the_lnet.ln_net_lock = NULL;
256 lnet_create_locks(void)
260 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
261 if (the_lnet.ln_res_lock == NULL)
264 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
265 if (the_lnet.ln_net_lock == NULL)
271 lnet_destroy_locks();
275 static void lnet_assert_wire_constants(void)
277 /* Wire protocol assertions generated by 'wirecheck'
278 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
279 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
280 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
283 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
284 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
285 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
286 CLASSERT (LNET_MSG_ACK == 0);
287 CLASSERT (LNET_MSG_PUT == 1);
288 CLASSERT (LNET_MSG_GET == 2);
289 CLASSERT (LNET_MSG_REPLY == 3);
290 CLASSERT (LNET_MSG_HELLO == 4);
292 /* Checks for struct ptl_handle_wire_t */
293 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
294 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
295 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
296 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
297 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
299 /* Checks for struct lnet_magicversion_t */
300 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
301 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
302 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
303 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
304 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
305 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
306 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
308 /* Checks for struct lnet_hdr_t */
309 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
310 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
311 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
312 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
313 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
314 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
315 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
316 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
317 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
318 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
319 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
320 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
321 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
322 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
323 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
326 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
327 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
328 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
329 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
330 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
331 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
334 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
335 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
336 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
337 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
338 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
339 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
340 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
341 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
342 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
343 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
346 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
347 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
348 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
349 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
350 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
351 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
352 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
353 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
354 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
355 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
358 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
359 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
362 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
363 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
364 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
365 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
369 lnet_find_lnd_by_type (int type)
372 struct list_head *tmp;
374 /* holding lnd mutex */
375 list_for_each(tmp, &the_lnet.ln_lnds) {
376 lnd = list_entry(tmp, lnd_t, lnd_list);
378 if ((int)lnd->lnd_type == type)
385 lnet_register_lnd (lnd_t *lnd)
387 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
389 LASSERT(the_lnet.ln_init);
390 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
391 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
393 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
394 lnd->lnd_refcount = 0;
396 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
398 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
400 EXPORT_SYMBOL(lnet_register_lnd);
403 lnet_unregister_lnd (lnd_t *lnd)
405 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
407 LASSERT(the_lnet.ln_init);
408 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
409 LASSERT(lnd->lnd_refcount == 0);
411 list_del(&lnd->lnd_list);
412 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
414 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
416 EXPORT_SYMBOL(lnet_unregister_lnd);
419 lnet_counters_get(lnet_counters_t *counters)
421 lnet_counters_t *ctr;
424 memset(counters, 0, sizeof(*counters));
426 lnet_net_lock(LNET_LOCK_EX);
428 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
429 counters->msgs_max += ctr->msgs_max;
430 counters->msgs_alloc += ctr->msgs_alloc;
431 counters->errors += ctr->errors;
432 counters->send_count += ctr->send_count;
433 counters->recv_count += ctr->recv_count;
434 counters->route_count += ctr->route_count;
435 counters->drop_count += ctr->drop_count;
436 counters->send_length += ctr->send_length;
437 counters->recv_length += ctr->recv_length;
438 counters->route_length += ctr->route_length;
439 counters->drop_length += ctr->drop_length;
442 lnet_net_unlock(LNET_LOCK_EX);
444 EXPORT_SYMBOL(lnet_counters_get);
447 lnet_counters_reset(void)
449 lnet_counters_t *counters;
452 lnet_net_lock(LNET_LOCK_EX);
454 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
455 memset(counters, 0, sizeof(lnet_counters_t));
457 lnet_net_unlock(LNET_LOCK_EX);
459 EXPORT_SYMBOL(lnet_counters_reset);
461 #ifdef LNET_USE_LIB_FREELIST
464 lnet_freelist_init(lnet_freelist_t *fl, int n, int size)
470 size += offsetof (lnet_freeobj_t, fo_contents);
472 LIBCFS_ALLOC(space, n * size);
476 INIT_LIST_HEAD(&fl->fl_list);
479 fl->fl_objsize = size;
482 list_add((struct list_head *)space, &fl->fl_list);
490 lnet_freelist_fini(lnet_freelist_t *fl)
492 struct list_head *el;
495 if (fl->fl_nobjs == 0)
499 for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
502 LASSERT (count == fl->fl_nobjs);
504 LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
505 memset (fl, 0, sizeof (*fl));
508 #endif /* LNET_USE_LIB_FREELIST */
510 static __u64 lnet_create_interface_cookie(void)
512 /* NB the interface cookie in wire handles guards against delayed
513 * replies and ACKs appearing valid after reboot. Initialisation time,
514 * even if it's only implemented to millisecond resolution is probably
515 * easily good enough. */
519 int rc = gettimeofday (&tv, NULL);
522 do_gettimeofday(&tv);
526 cookie += tv.tv_usec;
531 lnet_res_type2str(int type)
536 case LNET_COOKIE_TYPE_MD:
538 case LNET_COOKIE_TYPE_ME:
540 case LNET_COOKIE_TYPE_EQ:
546 lnet_res_container_cleanup(struct lnet_res_container *rec)
550 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
553 while (!list_empty(&rec->rec_active)) {
554 struct list_head *e = rec->rec_active.next;
557 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
558 lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
560 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
561 lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
563 } else { /* NB: Active MEs should be attached on portals */
570 /* Found alive MD/ME/EQ, user really should unlink/free
571 * all of them before finalize LNet, but if someone didn't,
572 * we have to recycle garbage for him */
573 CERROR("%d active elements on exit of %s container\n",
574 count, lnet_res_type2str(rec->rec_type));
577 #ifdef LNET_USE_LIB_FREELIST
578 lnet_freelist_fini(&rec->rec_freelist);
580 if (rec->rec_lh_hash != NULL) {
581 LIBCFS_FREE(rec->rec_lh_hash,
582 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
583 rec->rec_lh_hash = NULL;
586 rec->rec_type = 0; /* mark it as finalized */
590 lnet_res_container_setup(struct lnet_res_container *rec,
591 int cpt, int type, int objnum, int objsz)
596 LASSERT(rec->rec_type == 0);
598 rec->rec_type = type;
599 INIT_LIST_HEAD(&rec->rec_active);
601 #ifdef LNET_USE_LIB_FREELIST
602 memset(&rec->rec_freelist, 0, sizeof(rec->rec_freelist));
603 rc = lnet_freelist_init(&rec->rec_freelist, objnum, objsz);
607 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
609 /* Arbitrary choice of hash table size */
610 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
611 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
612 if (rec->rec_lh_hash == NULL) {
617 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
618 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
623 CERROR("Failed to setup %s resource container\n",
624 lnet_res_type2str(type));
625 lnet_res_container_cleanup(rec);
630 lnet_res_containers_destroy(struct lnet_res_container **recs)
632 struct lnet_res_container *rec;
635 cfs_percpt_for_each(rec, i, recs)
636 lnet_res_container_cleanup(rec);
638 cfs_percpt_free(recs);
641 static struct lnet_res_container **
642 lnet_res_containers_create(int type, int objnum, int objsz)
644 struct lnet_res_container **recs;
645 struct lnet_res_container *rec;
649 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
651 CERROR("Failed to allocate %s resource containers\n",
652 lnet_res_type2str(type));
656 cfs_percpt_for_each(rec, i, recs) {
657 rc = lnet_res_container_setup(rec, i, type, objnum, objsz);
659 lnet_res_containers_destroy(recs);
668 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
670 /* ALWAYS called with lnet_res_lock held */
671 struct list_head *head;
672 lnet_libhandle_t *lh;
675 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
678 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
679 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
681 list_for_each_entry(lh, head, lh_hash_chain) {
682 if (lh->lh_cookie == cookie)
690 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
692 /* ALWAYS called with lnet_res_lock held */
693 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
696 lh->lh_cookie = rec->rec_lh_cookie;
697 rec->rec_lh_cookie += 1 << ibits;
699 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
701 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
706 * Reserved API - do not use.
707 * Temporary workaround to allow uOSS and test programs force server
708 * mode in userspace. See comments near ln_server_mode_flag in
709 * lnet/lib-types.h */
713 the_lnet.ln_server_mode_flag = 1;
717 static int lnet_unprepare(void);
720 lnet_prepare(lnet_pid_t requested_pid)
722 /* Prepare to bring up the network */
723 struct lnet_res_container **recs;
726 if (requested_pid == LNET_PID_ANY) {
727 /* Don't instantiate LNET just for me */
731 LASSERT(the_lnet.ln_refcount == 0);
733 the_lnet.ln_routing = 0;
736 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
737 the_lnet.ln_pid = requested_pid;
739 if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
740 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
741 /* Only root can run user-space server */
742 if (current_uid() != 0)
744 the_lnet.ln_pid = requested_pid;
746 } else {/* client case (liblustre) */
747 /* My PID must be unique on this node and flag I'm userspace */
748 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
752 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
753 INIT_LIST_HEAD(&the_lnet.ln_nis);
754 INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
755 INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
756 INIT_LIST_HEAD(&the_lnet.ln_routers);
757 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
758 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
760 rc = lnet_create_remote_nets_table();
764 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
766 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
767 sizeof(lnet_counters_t));
768 if (the_lnet.ln_counters == NULL) {
769 CERROR("Failed to allocate counters for LNet\n");
774 rc = lnet_peer_tables_create();
778 rc = lnet_msg_containers_create();
782 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
783 LNET_COOKIE_TYPE_EQ, LNET_FL_MAX_EQS,
788 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME, LNET_FL_MAX_MES,
795 the_lnet.ln_me_containers = recs;
797 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD, LNET_FL_MAX_MDS,
798 sizeof(lnet_libmd_t));
804 the_lnet.ln_md_containers = recs;
806 rc = lnet_portals_create();
808 CERROR("Failed to create portals for LNet: %d\n", rc);
820 lnet_unprepare (void)
822 /* NB no LNET_LOCK since this is the last reference. All LND instances
823 * have shut down already, so it is safe to unlink and free all
824 * descriptors, even those that appear committed to a network op (eg MD
825 * with non-zero pending count) */
827 lnet_fail_nid(LNET_NID_ANY, 0);
829 LASSERT(the_lnet.ln_refcount == 0);
830 LASSERT(list_empty(&the_lnet.ln_test_peers));
831 LASSERT(list_empty(&the_lnet.ln_nis));
832 LASSERT(list_empty(&the_lnet.ln_nis_cpt));
833 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
835 lnet_portals_destroy();
837 if (the_lnet.ln_md_containers != NULL) {
838 lnet_res_containers_destroy(the_lnet.ln_md_containers);
839 the_lnet.ln_md_containers = NULL;
842 if (the_lnet.ln_me_containers != NULL) {
843 lnet_res_containers_destroy(the_lnet.ln_me_containers);
844 the_lnet.ln_me_containers = NULL;
847 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
849 lnet_msg_containers_destroy();
850 lnet_peer_tables_destroy();
851 lnet_rtrpools_free(0);
853 if (the_lnet.ln_counters != NULL) {
854 cfs_percpt_free(the_lnet.ln_counters);
855 the_lnet.ln_counters = NULL;
857 lnet_destroy_remote_nets_table();
863 lnet_net2ni_locked(__u32 net, int cpt)
865 struct list_head *tmp;
868 LASSERT(cpt != LNET_LOCK_EX);
870 list_for_each(tmp, &the_lnet.ln_nis) {
871 ni = list_entry(tmp, lnet_ni_t, ni_list);
873 if (LNET_NIDNET(ni->ni_nid) == net) {
874 lnet_ni_addref_locked(ni, cpt);
883 lnet_net2ni(__u32 net)
888 ni = lnet_net2ni_locked(net, 0);
893 EXPORT_SYMBOL(lnet_net2ni);
896 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
901 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
906 val = hash_long(key, LNET_CPT_BITS);
907 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
911 return (unsigned int)(key + val + (val >> 1)) % number;
915 lnet_cpt_of_nid_locked(lnet_nid_t nid)
919 /* must called with hold of lnet_net_lock */
920 if (LNET_CPT_NUMBER == 1)
921 return 0; /* the only one */
923 /* take lnet_net_lock(any) would be OK */
924 if (!list_empty(&the_lnet.ln_nis_cpt)) {
925 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
926 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
929 LASSERT(ni->ni_cpts != NULL);
930 return ni->ni_cpts[lnet_nid_cpt_hash
931 (nid, ni->ni_ncpts)];
935 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
939 lnet_cpt_of_nid(lnet_nid_t nid)
944 if (LNET_CPT_NUMBER == 1)
945 return 0; /* the only one */
947 if (list_empty(&the_lnet.ln_nis_cpt))
948 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
950 cpt = lnet_net_lock_current();
951 cpt2 = lnet_cpt_of_nid_locked(nid);
952 lnet_net_unlock(cpt);
956 EXPORT_SYMBOL(lnet_cpt_of_nid);
959 lnet_islocalnet(__u32 net)
964 cpt = lnet_net_lock_current();
966 ni = lnet_net2ni_locked(net, cpt);
968 lnet_ni_decref_locked(ni, cpt);
970 lnet_net_unlock(cpt);
976 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
979 struct list_head *tmp;
981 LASSERT(cpt != LNET_LOCK_EX);
983 list_for_each(tmp, &the_lnet.ln_nis) {
984 ni = list_entry(tmp, lnet_ni_t, ni_list);
986 if (ni->ni_nid == nid) {
987 lnet_ni_addref_locked(ni, cpt);
996 lnet_islocalnid(lnet_nid_t nid)
1001 cpt = lnet_net_lock_current();
1002 ni = lnet_nid2ni_locked(nid, cpt);
1004 lnet_ni_decref_locked(ni, cpt);
1005 lnet_net_unlock(cpt);
1011 lnet_count_acceptor_nis (void)
1013 /* Return the # of NIs that need the acceptor. */
1015 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
1016 struct list_head *tmp;
1020 cpt = lnet_net_lock_current();
1021 list_for_each(tmp, &the_lnet.ln_nis) {
1022 ni = list_entry(tmp, lnet_ni_t, ni_list);
1024 if (ni->ni_lnd->lnd_accept != NULL)
1028 lnet_net_unlock(cpt);
1030 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
1034 static lnet_ping_info_t *
1035 lnet_ping_info_create(int num_ni)
1037 lnet_ping_info_t *ping_info;
1038 unsigned int infosz;
1040 infosz = offsetof(lnet_ping_info_t, pi_ni[num_ni]);
1041 LIBCFS_ALLOC(ping_info, infosz);
1042 if (ping_info == NULL) {
1043 CERROR("Can't allocate ping info[%d]\n", num_ni);
1047 ping_info->pi_nnis = num_ni;
1048 ping_info->pi_pid = the_lnet.ln_pid;
1049 ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
1050 ping_info->pi_features = LNET_PING_FEAT_NI_STATUS;
1056 lnet_get_ni_count(void)
1063 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list)
1072 lnet_ping_info_free(lnet_ping_info_t *pinfo)
1075 offsetof(lnet_ping_info_t,
1076 pi_ni[pinfo->pi_nnis]));
1080 lnet_ping_info_destroy(void)
1084 lnet_net_lock(LNET_LOCK_EX);
1086 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1088 ni->ni_status = NULL;
1092 lnet_ping_info_free(the_lnet.ln_ping_info);
1093 the_lnet.ln_ping_info = NULL;
1095 lnet_net_unlock(LNET_LOCK_EX);
1099 lnet_ping_event_handler(lnet_event_t *event)
1101 lnet_ping_info_t *pinfo = event->md.user_ptr;
1103 if (event->unlinked)
1104 pinfo->pi_features = LNET_PING_FEAT_INVAL;
1108 lnet_ping_info_setup(lnet_ping_info_t **ppinfo, lnet_handle_md_t *md_handle,
1109 int ni_count, bool set_eq)
1111 lnet_handle_me_t me_handle;
1112 lnet_process_id_t id = {LNET_NID_ANY, LNET_PID_ANY};
1113 lnet_md_t md = {NULL};
1117 rc = LNetEQAlloc(0, lnet_ping_event_handler,
1118 &the_lnet.ln_ping_target_eq);
1120 CERROR("Can't allocate ping EQ: %d\n", rc);
1125 *ppinfo = lnet_ping_info_create(ni_count);
1126 if (*ppinfo == NULL) {
1131 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1132 LNET_PROTO_PING_MATCHBITS, 0,
1133 LNET_UNLINK, LNET_INS_AFTER,
1136 CERROR("Can't create ping ME: %d\n", rc);
1140 /* initialize md content */
1142 md.length = offsetof(lnet_ping_info_t,
1143 pi_ni[(*ppinfo)->pi_nnis]);
1144 md.threshold = LNET_MD_THRESH_INF;
1146 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1147 LNET_MD_MANAGE_REMOTE;
1149 md.eq_handle = the_lnet.ln_ping_target_eq;
1150 md.user_ptr = *ppinfo;
1152 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, md_handle);
1154 CERROR("Can't attach ping MD: %d\n", rc);
1161 rc2 = LNetMEUnlink(me_handle);
1164 lnet_ping_info_free(*ppinfo);
1168 LNetEQFree(the_lnet.ln_ping_target_eq);
1173 lnet_ping_md_unlink(lnet_ping_info_t *pinfo, lnet_handle_md_t *md_handle)
1175 sigset_t blocked = cfs_block_allsigs();
1177 LNetMDUnlink(*md_handle);
1178 LNetInvalidateHandle(md_handle);
1180 /* NB md could be busy; this just starts the unlink */
1181 while (pinfo->pi_features != LNET_PING_FEAT_INVAL) {
1182 CDEBUG(D_NET, "Still waiting for ping MD to unlink\n");
1183 cfs_pause(cfs_time_seconds(1));
1186 cfs_restore_sigs(blocked);
1190 lnet_ping_info_install_locked(lnet_ping_info_t *ping_info)
1194 lnet_ni_status_t *ns;
1197 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1198 LASSERT(i < ping_info->pi_nnis);
1200 ns = &ping_info->pi_ni[i];
1202 ns->ns_nid = ni->ni_nid;
1205 ns->ns_status = (ni->ni_status != NULL) ?
1206 ni->ni_status->ns_status : LNET_NI_STATUS_UP;
1215 lnet_ping_target_update(lnet_ping_info_t *pinfo, lnet_handle_md_t md_handle)
1217 lnet_ping_info_t *old_pinfo = NULL;
1218 lnet_handle_md_t old_md;
1220 /* switch the NIs to point to the new ping info created */
1221 lnet_net_lock(LNET_LOCK_EX);
1223 if (!the_lnet.ln_routing)
1224 pinfo->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1225 lnet_ping_info_install_locked(pinfo);
1227 if (the_lnet.ln_ping_info != NULL) {
1228 old_pinfo = the_lnet.ln_ping_info;
1229 old_md = the_lnet.ln_ping_target_md;
1231 the_lnet.ln_ping_target_md = md_handle;
1232 the_lnet.ln_ping_info = pinfo;
1234 lnet_net_unlock(LNET_LOCK_EX);
1236 if (old_pinfo != NULL) {
1237 /* unlink the old ping info */
1238 lnet_ping_md_unlink(old_pinfo, &old_md);
1239 lnet_ping_info_free(old_pinfo);
1244 lnet_ping_target_fini(void)
1248 lnet_ping_md_unlink(the_lnet.ln_ping_info,
1249 &the_lnet.ln_ping_target_md);
1251 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1254 lnet_ping_info_destroy();
1258 lnet_ni_tq_credits(lnet_ni_t *ni)
1262 LASSERT(ni->ni_ncpts >= 1);
1264 if (ni->ni_ncpts == 1)
1265 return ni->ni_maxtxcredits;
1267 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
1268 credits = max(credits, 8 * ni->ni_peertxcredits);
1269 credits = min(credits, ni->ni_maxtxcredits);
1275 lnet_ni_unlink_locked(lnet_ni_t *ni)
1277 if (!list_empty(&ni->ni_cptlist)) {
1278 list_del_init(&ni->ni_cptlist);
1279 lnet_ni_decref_locked(ni, 0);
1282 /* move it to zombie list and nobody can find it anymore */
1283 LASSERT(!list_empty(&ni->ni_list));
1284 list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1285 lnet_ni_decref_locked(ni, 0); /* drop ln_nis' ref */
1289 lnet_clear_zombies_nis_locked(void)
1295 /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
1296 * and shut them down in guaranteed thread context */
1298 while (!list_empty(&the_lnet.ln_nis_zombie)) {
1302 ni = list_entry(the_lnet.ln_nis_zombie.next,
1303 lnet_ni_t, ni_list);
1304 list_del_init(&ni->ni_list);
1305 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1308 /* still busy, add it back to zombie list */
1309 list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1313 if (!list_empty(&ni->ni_list)) {
1314 lnet_net_unlock(LNET_LOCK_EX);
1316 if ((i & (-i)) == i) {
1318 "Waiting for zombie LNI %s\n",
1319 libcfs_nid2str(ni->ni_nid));
1321 cfs_pause(cfs_time_seconds(1));
1322 lnet_net_lock(LNET_LOCK_EX);
1326 ni->ni_lnd->lnd_refcount--;
1327 lnet_net_unlock(LNET_LOCK_EX);
1329 islo = ni->ni_lnd->lnd_type == LOLND;
1331 LASSERT(!in_interrupt());
1332 (ni->ni_lnd->lnd_shutdown)(ni);
1334 /* can't deref lnd anymore now; it might have unregistered
1338 CDEBUG(D_LNI, "Removed LNI %s\n",
1339 libcfs_nid2str(ni->ni_nid));
1343 lnet_net_lock(LNET_LOCK_EX);
1348 lnet_shutdown_lndnis(void)
1353 /* NB called holding the global mutex */
1355 /* All quiet on the API front */
1356 LASSERT(!the_lnet.ln_shutdown);
1357 LASSERT(the_lnet.ln_refcount == 0);
1358 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
1360 lnet_net_lock(LNET_LOCK_EX);
1361 the_lnet.ln_shutdown = 1; /* flag shutdown */
1363 /* Unlink NIs from the global table */
1364 while (!list_empty(&the_lnet.ln_nis)) {
1365 ni = list_entry(the_lnet.ln_nis.next,
1366 lnet_ni_t, ni_list);
1367 lnet_ni_unlink_locked(ni);
1370 /* Drop the cached eqwait NI. */
1371 if (the_lnet.ln_eq_waitni != NULL) {
1372 lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
1373 the_lnet.ln_eq_waitni = NULL;
1376 /* Drop the cached loopback NI. */
1377 if (the_lnet.ln_loni != NULL) {
1378 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1379 the_lnet.ln_loni = NULL;
1382 lnet_net_unlock(LNET_LOCK_EX);
1384 /* Clear lazy portals and drop delayed messages which hold refs
1385 * on their lnet_msg_t::msg_rxpeer */
1386 for (i = 0; i < the_lnet.ln_nportals; i++)
1387 LNetClearLazyPortal(i);
1389 /* Clear the peer table and wait for all peers to go (they hold refs on
1391 lnet_peer_tables_cleanup(NULL);
1393 lnet_net_lock(LNET_LOCK_EX);
1395 lnet_clear_zombies_nis_locked();
1396 the_lnet.ln_shutdown = 0;
1397 lnet_net_unlock(LNET_LOCK_EX);
1400 /* shutdown down the NI and release refcount */
1402 lnet_shutdown_lndni(struct lnet_ni *ni)
1404 lnet_net_lock(LNET_LOCK_EX);
1405 lnet_ni_unlink_locked(ni);
1406 lnet_net_unlock(LNET_LOCK_EX);
1408 /* Do peer table cleanup for this ni */
1409 lnet_peer_tables_cleanup(ni);
1411 lnet_net_lock(LNET_LOCK_EX);
1412 lnet_clear_zombies_nis_locked();
1413 lnet_net_unlock(LNET_LOCK_EX);
1417 lnet_startup_lndni(struct lnet_ni *ni, __s32 peer_timeout,
1418 __s32 peer_cr, __s32 peer_buf_cr, __s32 credits)
1423 struct lnet_tx_queue *tq;
1426 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1428 LASSERT(libcfs_isknown_lnd(lnd_type));
1430 if (lnd_type == CIBLND || lnd_type == OPENIBLND ||
1431 lnd_type == IIBLND || lnd_type == VIBLND) {
1432 CERROR("LND %s obsoleted\n", libcfs_lnd2str(lnd_type));
1436 /* Make sure this new NI is unique. */
1437 lnet_net_lock(LNET_LOCK_EX);
1438 rc = lnet_net_unique(LNET_NIDNET(ni->ni_nid), &the_lnet.ln_nis);
1439 lnet_net_unlock(LNET_LOCK_EX);
1442 if (lnd_type == LOLND) {
1447 CERROR("Net %s is not unique\n",
1448 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
1454 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1455 lnd = lnet_find_lnd_by_type(lnd_type);
1459 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1460 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1461 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1463 lnd = lnet_find_lnd_by_type(lnd_type);
1465 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1466 CERROR("Can't load LND %s, module %s, rc=%d\n",
1467 libcfs_lnd2str(lnd_type),
1468 libcfs_lnd2modname(lnd_type), rc);
1469 #ifndef HAVE_MODULE_LOADING_SUPPORT
1470 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1471 "compiled with kernel module "
1472 "loading support.");
1480 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1481 CERROR("LND %s not supported\n",
1482 libcfs_lnd2str(lnd_type));
1487 lnet_net_lock(LNET_LOCK_EX);
1488 lnd->lnd_refcount++;
1489 lnet_net_unlock(LNET_LOCK_EX);
1493 rc = (lnd->lnd_startup)(ni);
1495 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1498 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1499 rc, libcfs_lnd2str(lnd->lnd_type));
1500 lnet_net_lock(LNET_LOCK_EX);
1501 lnd->lnd_refcount--;
1502 lnet_net_unlock(LNET_LOCK_EX);
1506 /* If given some LND tunable parameters, parse those now to
1507 * override the values in the NI structure. */
1508 if (peer_buf_cr >= 0)
1509 ni->ni_peerrtrcredits = peer_buf_cr;
1510 if (peer_timeout >= 0)
1511 ni->ni_peertimeout = peer_timeout;
1514 * Note: For now, don't allow the user to change
1515 * peertxcredits as this number is used in the
1516 * IB LND to control queue depth.
1517 * if (peer_cr != -1)
1518 * ni->ni_peertxcredits = peer_cr;
1521 ni->ni_maxtxcredits = credits;
1523 LASSERT(ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1525 lnet_net_lock(LNET_LOCK_EX);
1526 /* refcount for ln_nis */
1527 lnet_ni_addref_locked(ni, 0);
1528 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1529 if (ni->ni_cpts != NULL) {
1530 lnet_ni_addref_locked(ni, 0);
1531 list_add_tail(&ni->ni_cptlist, &the_lnet.ln_nis_cpt);
1534 lnet_net_unlock(LNET_LOCK_EX);
1536 if (lnd->lnd_type == LOLND) {
1538 LASSERT(the_lnet.ln_loni == NULL);
1539 the_lnet.ln_loni = ni;
1544 if (lnd->lnd_wait != NULL) {
1545 if (the_lnet.ln_eq_waitni == NULL) {
1547 the_lnet.ln_eq_waitni = ni;
1550 # ifndef HAVE_LIBPTHREAD
1551 LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1552 "single-threaded runtime\n",
1553 libcfs_lnd2str(lnd_type));
1554 /* shutdown the NI since if we get here then it must've already
1557 lnet_shutdown_lndni(ni);
1562 if (ni->ni_peertxcredits == 0 || ni->ni_maxtxcredits == 0) {
1563 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1564 libcfs_lnd2str(lnd->lnd_type),
1565 ni->ni_peertxcredits == 0 ?
1567 /* shutdown the NI since if we get here then it must've already
1570 lnet_shutdown_lndni(ni);
1574 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1575 tq->tq_credits_min =
1576 tq->tq_credits_max =
1577 tq->tq_credits = lnet_ni_tq_credits(ni);
1580 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1581 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1582 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1583 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1592 lnet_startup_lndnis(struct list_head *nilist)
1599 while (!list_empty(nilist)) {
1600 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
1601 list_del(&ni->ni_list);
1602 rc = lnet_startup_lndni(ni, -1, -1, -1, -1);
1610 if (the_lnet.ln_eq_waitni != NULL && ni_count > 1) {
1611 lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1612 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1614 libcfs_lnd2str(lnd_type));
1621 lnet_shutdown_lndnis();
1627 * Initialize LNet library.
1629 * Only userspace program needs to call this function - it's automatically
1630 * called in the kernel at module loading time. Caller has to call LNetFini()
1631 * after a call to LNetInit(), if and only if the latter returned 0. It must
1632 * be called exactly once.
1634 * \return 0 on success, and -ve on failures.
1641 lnet_assert_wire_constants();
1642 LASSERT(!the_lnet.ln_init);
1644 memset(&the_lnet, 0, sizeof(the_lnet));
1646 /* refer to global cfs_cpt_table for now */
1647 the_lnet.ln_cpt_table = cfs_cpt_table;
1648 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1650 LASSERT(the_lnet.ln_cpt_number > 0);
1651 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1652 /* we are under risk of consuming all lh_cookie */
1653 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1654 "please change setting of CPT-table and retry\n",
1655 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1659 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1660 the_lnet.ln_cpt_bits++;
1662 rc = lnet_create_locks();
1664 CERROR("Can't create LNet global locks: %d\n", rc);
1668 the_lnet.ln_refcount = 0;
1669 the_lnet.ln_init = 1;
1670 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1671 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1672 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1673 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1676 /* The hash table size is the number of bits it takes to express the set
1677 * ln_num_routes, minus 1 (better to under estimate than over so we
1678 * don't waste memory). */
1679 if (rnet_htable_size <= 0)
1680 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1681 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1682 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1683 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1684 order_base_2(rnet_htable_size) - 1);
1686 /* All LNDs apart from the LOLND are in separate modules. They
1687 * register themselves when their module loads, and unregister
1688 * themselves when their module is unloaded. */
1690 the_lnet.ln_remote_nets_hbits = 8;
1693 * NB the order here determines default 'networks=' order */
1694 # ifdef HAVE_LIBPTHREAD
1695 LNET_REGISTER_ULND(the_tcplnd);
1698 lnet_register_lnd(&the_lolnd);
1701 EXPORT_SYMBOL(LNetInit);
1704 * Finalize LNet library.
1706 * Only userspace program needs to call this function. It can be called
1709 * \pre LNetInit() called with success.
1710 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1715 LASSERT(the_lnet.ln_init);
1716 LASSERT(the_lnet.ln_refcount == 0);
1718 while (!list_empty(&the_lnet.ln_lnds))
1719 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1721 lnet_destroy_locks();
1723 the_lnet.ln_init = 0;
1725 EXPORT_SYMBOL(LNetFini);
1728 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1730 * Userspace program should call this after a successful call to LNetInit().
1731 * Users must call this function at least once before any other functions.
1732 * For each successful call there must be a corresponding call to
1733 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1736 * The PID used by LNet may be different from the one requested.
1739 * \param requested_pid PID requested by the caller.
1741 * \return >= 0 on success, and < 0 error code on failures.
1744 LNetNIInit(lnet_pid_t requested_pid)
1746 int im_a_router = 0;
1749 lnet_ping_info_t *pinfo;
1750 lnet_handle_md_t md_handle;
1751 struct list_head net_head;
1753 INIT_LIST_HEAD(&net_head);
1755 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1757 LASSERT(the_lnet.ln_init);
1758 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1760 if (the_lnet.ln_refcount > 0) {
1761 rc = the_lnet.ln_refcount++;
1762 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1766 rc = lnet_prepare(requested_pid);
1768 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1772 /* Add in the loopback network */
1773 if (lnet_ni_alloc(LNET_MKNET(LOLND, 0), NULL, &net_head) == NULL) {
1778 /* If LNet is being initialized via DLC it is possible
1779 * that the user requests not to load module parameters (ones which
1780 * are supported by DLC) on initialization. Therefore, make sure not
1781 * to load networks, routes and forwarding from module parameters
1782 * in this case. On cleanup in case of failure only clean up
1783 * routes if it has been loaded */
1784 if (!the_lnet.ln_nis_from_mod_params) {
1785 rc = lnet_parse_networks(&net_head,
1786 lnet_get_networks());
1791 ni_count = lnet_startup_lndnis(&net_head);
1797 if (!the_lnet.ln_nis_from_mod_params) {
1798 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1802 rc = lnet_check_routes();
1806 rc = lnet_rtrpools_alloc(im_a_router);
1811 rc = lnet_acceptor_start();
1814 the_lnet.ln_refcount = 1;
1815 /* Now I may use my own API functions... */
1817 rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
1821 lnet_ping_target_update(pinfo, md_handle);
1823 rc = lnet_router_checker_start();
1830 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1835 lnet_ping_target_fini();
1837 the_lnet.ln_refcount = 0;
1838 lnet_acceptor_stop();
1840 if (!the_lnet.ln_nis_from_mod_params)
1841 lnet_destroy_routes();
1843 lnet_shutdown_lndnis();
1847 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1848 while (!list_empty(&net_head)) {
1850 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1851 list_del_init(&ni->ni_list);
1856 EXPORT_SYMBOL(LNetNIInit);
1859 * Stop LNet interfaces, routing, and forwarding.
1861 * Users must call this function once for each successful call to LNetNIInit().
1862 * Once the LNetNIFini() operation has been started, the results of pending
1863 * API operations are undefined.
1865 * \return always 0 for current implementation.
1870 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1872 LASSERT (the_lnet.ln_init);
1873 LASSERT (the_lnet.ln_refcount > 0);
1875 if (the_lnet.ln_refcount != 1) {
1876 the_lnet.ln_refcount--;
1878 LASSERT(!the_lnet.ln_niinit_self);
1883 lnet_router_checker_stop();
1884 lnet_ping_target_fini();
1886 /* Teardown fns that use my own API functions BEFORE here */
1887 the_lnet.ln_refcount = 0;
1889 lnet_acceptor_stop();
1890 lnet_destroy_routes();
1891 lnet_shutdown_lndnis();
1895 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1898 EXPORT_SYMBOL(LNetNIFini);
1901 * Grabs the ni data from the ni structure and fills the out
1904 * \param[in] ni network interface structure
1905 * \param[out] cpt_count the number of cpts the ni is on
1906 * \param[out] nid Network Interface ID
1907 * \param[out] peer_timeout NI peer timeout
1908 * \param[out] peer_tx_crdits NI peer transmit credits
1909 * \param[out] peer_rtr_credits NI peer router credits
1910 * \param[out] max_tx_credits NI max transmit credit
1911 * \param[out] net_config Network configuration
1914 lnet_fill_ni_info(struct lnet_ni *ni, __u32 *cpt_count, __u64 *nid,
1915 int *peer_timeout, int *peer_tx_credits,
1916 int *peer_rtr_credits, int *max_tx_credits,
1917 struct lnet_ioctl_net_config *net_config)
1924 if (net_config == NULL)
1927 CLASSERT(ARRAY_SIZE(ni->ni_interfaces) ==
1928 ARRAY_SIZE(net_config->ni_interfaces));
1930 if (ni->ni_interfaces[0] != NULL) {
1931 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
1932 if (ni->ni_interfaces[i] != NULL) {
1933 strncpy(net_config->ni_interfaces[i],
1934 ni->ni_interfaces[i],
1935 sizeof(net_config->ni_interfaces[i]));
1941 *peer_timeout = ni->ni_peertimeout;
1942 *peer_tx_credits = ni->ni_peertxcredits;
1943 *peer_rtr_credits = ni->ni_peerrtrcredits;
1944 *max_tx_credits = ni->ni_maxtxcredits;
1946 net_config->ni_status = ni->ni_status->ns_status;
1949 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
1950 i < LNET_MAX_SHOW_NUM_CPT;
1952 net_config->ni_cpts[i] = ni->ni_cpts[i];
1954 *cpt_count = ni->ni_ncpts;
1958 lnet_get_net_config(int idx, __u32 *cpt_count, __u64 *nid, int *peer_timeout,
1959 int *peer_tx_credits, int *peer_rtr_credits,
1960 int *max_tx_credits,
1961 struct lnet_ioctl_net_config *net_config)
1964 struct list_head *tmp;
1968 cpt = lnet_net_lock_current();
1970 list_for_each(tmp, &the_lnet.ln_nis) {
1971 ni = list_entry(tmp, lnet_ni_t, ni_list);
1975 lnet_fill_ni_info(ni, cpt_count, nid, peer_timeout,
1976 peer_tx_credits, peer_rtr_credits,
1977 max_tx_credits, net_config);
1983 lnet_net_unlock(cpt);
1988 lnet_dyn_add_ni(lnet_pid_t requested_pid, char *nets,
1989 __s32 peer_timeout, __s32 peer_cr, __s32 peer_buf_cr,
1992 lnet_ping_info_t *pinfo;
1993 lnet_handle_md_t md_handle;
1995 struct list_head net_head;
1997 lnet_remotenet_t *rnet;
1999 INIT_LIST_HEAD(&net_head);
2001 /* Create a ni structure for the network string */
2002 rc = lnet_parse_networks(&net_head, nets);
2004 return rc == 0 ? -EINVAL : rc;
2006 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2009 rc = -EINVAL; /* only add one interface per call */
2013 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
2015 lnet_net_lock(LNET_LOCK_EX);
2016 rnet = lnet_find_net_locked(LNET_NIDNET(ni->ni_nid));
2017 lnet_net_unlock(LNET_LOCK_EX);
2018 /* make sure that the net added doesn't invalidate the current
2019 * configuration LNet is keeping */
2021 CERROR("Adding net %s will invalidate routing configuration\n",
2027 rc = lnet_ping_info_setup(&pinfo, &md_handle, 1 + lnet_get_ni_count(),
2032 list_del_init(&ni->ni_list);
2034 rc = lnet_startup_lndni(ni, peer_timeout, peer_cr,
2035 peer_buf_cr, credits);
2039 if (ni->ni_lnd->lnd_accept != NULL) {
2040 rc = lnet_acceptor_start();
2042 /* shutdown the ni that we just started */
2043 CERROR("Failed to start up acceptor thread\n");
2044 lnet_shutdown_lndni(ni);
2049 lnet_ping_target_update(pinfo, md_handle);
2050 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2055 lnet_ping_md_unlink(pinfo, &md_handle);
2056 lnet_ping_info_free(pinfo);
2058 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2059 while (!list_empty(&net_head)) {
2060 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
2061 list_del_init(&ni->ni_list);
2068 lnet_dyn_del_ni(__u32 net)
2071 lnet_ping_info_t *pinfo;
2072 lnet_handle_md_t md_handle;
2075 /* don't allow userspace to shutdown the LOLND */
2076 if (LNET_NETTYP(net) == LOLND)
2079 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2080 /* create and link a new ping info, before removing the old one */
2081 rc = lnet_ping_info_setup(&pinfo, &md_handle,
2082 lnet_get_ni_count() - 1, false);
2086 ni = lnet_net2ni(net);
2092 /* decrement the reference counter taken by lnet_net2ni() */
2093 lnet_ni_decref_locked(ni, 0);
2095 lnet_shutdown_lndni(ni);
2097 if (lnet_count_acceptor_nis() == 0)
2098 lnet_acceptor_stop();
2100 lnet_ping_target_update(pinfo, md_handle);
2103 lnet_ping_md_unlink(pinfo, &md_handle);
2104 lnet_ping_info_free(pinfo);
2106 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2112 * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
2113 * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
2114 * internal ioctl handler.
2116 * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
2118 * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
2119 * The data will be printed to system console. Don't use it excessively.
2120 * \param arg A pointer to lnet_process_id_t, process ID of the peer.
2122 * \return Always return 0 when called by users directly (i.e., not via ioctl).
2125 LNetCtl(unsigned int cmd, void *arg)
2127 struct libcfs_ioctl_data *data = arg;
2128 struct lnet_ioctl_config_data *config;
2129 lnet_process_id_t id = {0};
2133 CLASSERT(LIBCFS_IOC_DATA_MAX >= sizeof(struct lnet_ioctl_net_config) +
2134 sizeof(struct lnet_ioctl_config_data));
2135 LASSERT(the_lnet.ln_init);
2138 case IOC_LIBCFS_GET_NI:
2139 rc = LNetGetId(data->ioc_count, &id);
2140 data->ioc_nid = id.nid;
2143 case IOC_LIBCFS_FAIL_NID:
2144 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
2146 case IOC_LIBCFS_ADD_ROUTE:
2149 if (config->cfg_hdr.ioc_len < sizeof(*config))
2152 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2153 rc = lnet_add_route(config->cfg_net,
2154 config->cfg_config_u.cfg_route.rtr_hop,
2156 config->cfg_config_u.cfg_route.
2159 rc = lnet_check_routes();
2161 lnet_del_route(config->cfg_net,
2164 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2167 case IOC_LIBCFS_DEL_ROUTE:
2170 if (config->cfg_hdr.ioc_len < sizeof(*config))
2173 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2174 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
2175 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2178 case IOC_LIBCFS_GET_ROUTE:
2181 if (config->cfg_hdr.ioc_len < sizeof(*config))
2184 return lnet_get_route(config->cfg_count,
2186 &config->cfg_config_u.cfg_route.rtr_hop,
2188 &config->cfg_config_u.cfg_route.rtr_flags,
2189 &config->cfg_config_u.cfg_route.
2192 case IOC_LIBCFS_GET_NET: {
2193 struct lnet_ioctl_net_config *net_config;
2194 size_t total = sizeof(*config) + sizeof(*net_config);
2198 if (config->cfg_hdr.ioc_len < total)
2201 net_config = (struct lnet_ioctl_net_config *)
2203 if (config == NULL || net_config == NULL)
2206 return lnet_get_net_config(config->cfg_count,
2209 &config->cfg_config_u.
2210 cfg_net.net_peer_timeout,
2211 &config->cfg_config_u.cfg_net.
2212 net_peer_tx_credits,
2213 &config->cfg_config_u.cfg_net.
2214 net_peer_rtr_credits,
2215 &config->cfg_config_u.cfg_net.
2220 case IOC_LIBCFS_GET_LNET_STATS:
2222 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
2224 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
2227 lnet_counters_get(&lnet_stats->st_cntrs);
2231 #if defined(__KERNEL__) && defined(LNET_ROUTER)
2232 case IOC_LIBCFS_CONFIG_RTR:
2235 if (config->cfg_hdr.ioc_len < sizeof(*config))
2238 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2239 if (config->cfg_config_u.cfg_buffers.buf_enable) {
2240 rc = lnet_rtrpools_enable();
2241 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2244 lnet_rtrpools_disable();
2245 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2248 case IOC_LIBCFS_ADD_BUF:
2251 if (config->cfg_hdr.ioc_len < sizeof(*config))
2254 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2255 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
2257 config->cfg_config_u.cfg_buffers.
2259 config->cfg_config_u.cfg_buffers.
2261 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2265 case IOC_LIBCFS_GET_BUF: {
2266 struct lnet_ioctl_pool_cfg *pool_cfg;
2267 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2271 if (config->cfg_hdr.ioc_len < total)
2274 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2275 return lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2278 case IOC_LIBCFS_GET_PEER_INFO: {
2279 struct lnet_ioctl_peer *peer_info = arg;
2281 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2284 return lnet_get_peer_info(
2285 peer_info->pr_count,
2287 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2288 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2289 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2290 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2291 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2292 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2293 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_rtr_credits,
2294 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2297 case IOC_LIBCFS_NOTIFY_ROUTER:
2298 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2299 cfs_time_current() -
2300 cfs_time_seconds(cfs_time_current_sec() -
2301 (time_t)data->ioc_u64[0]));
2303 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
2304 /* This can be removed once lustre stops calling it */
2307 case IOC_LIBCFS_LNET_DIST:
2308 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2309 if (rc < 0 && rc != -EHOSTUNREACH)
2312 data->ioc_u32[0] = rc;
2315 case IOC_LIBCFS_TESTPROTOCOMPAT:
2316 lnet_net_lock(LNET_LOCK_EX);
2317 the_lnet.ln_testprotocompat = data->ioc_flags;
2318 lnet_net_unlock(LNET_LOCK_EX);
2321 case IOC_LIBCFS_LNET_FAULT:
2322 return lnet_fault_ctl(data->ioc_flags, data);
2324 case IOC_LIBCFS_PING:
2325 id.nid = data->ioc_nid;
2326 id.pid = data->ioc_u32[0];
2327 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
2328 (lnet_process_id_t __user *)data->ioc_pbuf1,
2329 data->ioc_plen1/sizeof(lnet_process_id_t));
2332 data->ioc_count = rc;
2335 case IOC_LIBCFS_DEBUG_PEER: {
2336 /* CAVEAT EMPTOR: this one designed for calling directly; not
2338 id = *((lnet_process_id_t *) arg);
2340 lnet_debug_peer(id.nid);
2342 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2344 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
2346 if (ni->ni_lnd->lnd_ctl == NULL) {
2347 CDEBUG(D_WARNING, "No ctl for %s\n",
2350 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2359 ni = lnet_net2ni(data->ioc_net);
2363 if (ni->ni_lnd->lnd_ctl == NULL)
2366 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2373 EXPORT_SYMBOL(LNetCtl);
2376 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
2377 * all interfaces share a same PID, as requested by LNetNIInit().
2379 * \param index Index of the interface to look up.
2380 * \param id On successful return, this location will hold the
2381 * lnet_process_id_t ID of the interface.
2383 * \retval 0 If an interface exists at \a index.
2384 * \retval -ENOENT If no interface has been found.
2387 LNetGetId(unsigned int index, lnet_process_id_t *id)
2390 struct list_head *tmp;
2394 LASSERT(the_lnet.ln_init);
2395 LASSERT(the_lnet.ln_refcount > 0);
2397 cpt = lnet_net_lock_current();
2399 list_for_each(tmp, &the_lnet.ln_nis) {
2403 ni = list_entry(tmp, lnet_ni_t, ni_list);
2405 id->nid = ni->ni_nid;
2406 id->pid = the_lnet.ln_pid;
2411 lnet_net_unlock(cpt);
2414 EXPORT_SYMBOL(LNetGetId);
2417 * Print a string representation of handle \a h into buffer \a str of
2421 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
2423 snprintf(str, len, LPX64, h.cookie);
2425 EXPORT_SYMBOL(LNetSnprintHandle);
2428 lnet_ping(lnet_process_id_t id, int timeout_ms, lnet_process_id_t __user *ids,
2431 lnet_handle_eq_t eqh;
2432 lnet_handle_md_t mdh;
2434 lnet_md_t md = { NULL };
2438 const int a_long_time = 60000; /* mS */
2440 lnet_ping_info_t *info;
2441 lnet_process_id_t tmpid;
2448 infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
2451 id.nid == LNET_NID_ANY ||
2452 timeout_ms > 500000 || /* arbitrary limit! */
2453 n_ids > 20) /* arbitrary limit! */
2456 if (id.pid == LNET_PID_ANY)
2457 id.pid = LNET_PID_LUSTRE;
2459 LIBCFS_ALLOC(info, infosz);
2463 /* NB 2 events max (including any unlink event) */
2464 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
2466 CERROR("Can't allocate EQ: %d\n", rc);
2470 /* initialize md content */
2473 md.threshold = 2; /*GET/REPLY*/
2475 md.options = LNET_MD_TRUNCATE;
2479 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
2481 CERROR("Can't bind MD: %d\n", rc);
2485 rc = LNetGet(LNET_NID_ANY, mdh, id,
2486 LNET_RESERVED_PORTAL,
2487 LNET_PROTO_PING_MATCHBITS, 0);
2490 /* Don't CERROR; this could be deliberate! */
2492 rc2 = LNetMDUnlink(mdh);
2495 /* NB must wait for the UNLINK event below... */
2497 timeout_ms = a_long_time;
2501 /* MUST block for unlink to complete */
2503 blocked = cfs_block_allsigs();
2505 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
2508 cfs_restore_sigs(blocked);
2510 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
2511 (rc2 <= 0) ? -1 : event.type,
2512 (rc2 <= 0) ? -1 : event.status,
2513 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
2515 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
2517 if (rc2 <= 0 || event.status != 0) {
2518 /* timeout or error */
2519 if (!replied && rc == 0)
2520 rc = (rc2 < 0) ? rc2 :
2521 (rc2 == 0) ? -ETIMEDOUT :
2525 /* Ensure completion in finite time... */
2527 /* No assertion (racing with network) */
2529 timeout_ms = a_long_time;
2530 } else if (rc2 == 0) {
2531 /* timed out waiting for unlink */
2532 CWARN("ping %s: late network completion\n",
2535 } else if (event.type == LNET_EVENT_REPLY) {
2540 } while (rc2 <= 0 || !event.unlinked);
2544 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2551 LASSERT(nob >= 0 && nob <= infosz);
2553 rc = -EPROTO; /* if I can't parse... */
2556 /* can't check magic/version */
2557 CERROR("%s: ping info too short %d\n",
2558 libcfs_id2str(id), nob);
2562 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2563 lnet_swap_pinginfo(info);
2564 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2565 CERROR("%s: Unexpected magic %08x\n",
2566 libcfs_id2str(id), info->pi_magic);
2570 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
2571 CERROR("%s: ping w/o NI status: 0x%x\n",
2572 libcfs_id2str(id), info->pi_features);
2576 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2577 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2578 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2582 if (info->pi_nnis < n_ids)
2583 n_ids = info->pi_nnis;
2585 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2586 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2587 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2591 rc = -EFAULT; /* If I SEGV... */
2593 for (i = 0; i < n_ids; i++) {
2594 tmpid.pid = info->pi_pid;
2595 tmpid.nid = info->pi_ni[i].ns_nid;
2596 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2602 rc2 = LNetEQFree(eqh);
2604 CERROR("rc2 %d\n", rc2);
2608 LIBCFS_FREE(info, infosz);