Whamcloud - gitweb
3eb2ee7c665baebba38b906a3a2bf13c3471da58
[fs/lustre-release.git] / lnet / lnet / api-ni.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
39 #ifdef __KERNEL__
40 #include <linux/log2.h>
41 #endif
42
43 #ifdef __KERNEL__
44 #define D_LNI D_CONSOLE
45 #else
46 #define D_LNI D_CONFIG
47 #endif
48
49 lnet_t      the_lnet;                           /* THE state of the network */
50 EXPORT_SYMBOL(the_lnet);
51
52 #ifdef __KERNEL__
53
54 static char *ip2nets = "";
55 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
56                 "LNET network <- IP table");
57
58 static char *networks = "";
59 CFS_MODULE_PARM(networks, "s", charp, 0444,
60                 "local networks");
61
62 static char *routes = "";
63 CFS_MODULE_PARM(routes, "s", charp, 0444,
64                 "routes to non-local networks");
65
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");
69
70 char *
71 lnet_get_routes(void)
72 {
73         return routes;
74 }
75
76 char *
77 lnet_get_networks(void)
78 {
79         char   *nets;
80         int     rc;
81
82         if (*networks != 0 && *ip2nets != 0) {
83                 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
84                                    "'ip2nets' but not both at once\n");
85                 return NULL;
86         }
87
88         if (*ip2nets != 0) {
89                 rc = lnet_parse_ip2nets(&nets, ip2nets);
90                 return (rc == 0) ? nets : NULL;
91         }
92
93         if (*networks != 0)
94                 return networks;
95
96         return "tcp";
97 }
98
99 void
100 lnet_init_locks(void)
101 {
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);
106 }
107
108 void
109 lnet_fini_locks(void)
110 {
111 }
112
113 #else
114
115 char *
116 lnet_get_routes(void)
117 {
118         char *str = getenv("LNET_ROUTES");
119
120         return (str == NULL) ? "" : str;
121 }
122
123 char *
124 lnet_get_networks (void)
125 {
126         static char       default_networks[256];
127         char             *networks = getenv("LNET_NETWORKS");
128         char             *str;
129         char             *sep;
130         int               len;
131         int               nob;
132         struct list_head *tmp;
133
134         if (networks != NULL)
135                 return networks;
136
137         /* In userland, the default 'networks=' is the list of known net types */
138         len = sizeof(default_networks);
139         str = default_networks;
140         *str = 0;
141         sep = "";
142
143         list_for_each(tmp, &the_lnet.ln_lnds) {
144                 lnd_t *lnd = list_entry(tmp, lnd_t, lnd_list);
145
146                 nob = snprintf(str, len, "%s%s", sep,
147                                libcfs_lnd2str(lnd->lnd_type));
148                 if (nob >= len) {
149                         /* overflowed the string; leave it where it was */
150                         *str = 0;
151                         break;
152                 }
153                 len -= nob;
154                 str += nob;
155                 sep = ",";
156         }
157
158         return default_networks;
159 }
160
161 # ifndef HAVE_LIBPTHREAD
162
163 void lnet_init_locks(void)
164 {
165         the_lnet.ln_eq_wait_lock = 0;
166         the_lnet.ln_lnd_mutex = 0;
167         the_lnet.ln_api_mutex = 0;
168 }
169
170 void lnet_fini_locks(void)
171 {
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);
175 }
176
177 # else
178
179 void lnet_init_locks(void)
180 {
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);
185 }
186
187 void lnet_fini_locks(void)
188 {
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);
193 }
194
195 # endif
196 #endif
197
198 static int
199 lnet_create_remote_nets_table(void)
200 {
201         int               i;
202         struct list_head *hash;
203
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));
207         if (hash == NULL) {
208                 CERROR("Failed to create remote nets hash table\n");
209                 return -ENOMEM;
210         }
211
212         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
213                 INIT_LIST_HEAD(&hash[i]);
214         the_lnet.ln_remote_nets_hash = hash;
215         return 0;
216 }
217
218 static void
219 lnet_destroy_remote_nets_table(void)
220 {
221         int i;
222
223         if (the_lnet.ln_remote_nets_hash == NULL)
224                 return;
225
226         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
227                 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
228
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;
233 }
234
235 static void
236 lnet_destroy_locks(void)
237 {
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;
241         }
242
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;
246         }
247
248         lnet_fini_locks();
249 }
250
251 static int
252 lnet_create_locks(void)
253 {
254         lnet_init_locks();
255
256         the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
257         if (the_lnet.ln_res_lock == NULL)
258                 goto failed;
259
260         the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
261         if (the_lnet.ln_net_lock == NULL)
262                 goto failed;
263
264         return 0;
265
266  failed:
267         lnet_destroy_locks();
268         return -ENOMEM;
269 }
270
271 void lnet_assert_wire_constants (void)
272 {
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) */
277
278         /* Constants... */
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);
287
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);
294
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);
303
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);
320
321         /* Ack */
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);
328
329         /* Put */
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);
340
341         /* Get */
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);
352
353         /* Reply */
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);
356
357         /* Hello */
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);
362 }
363
364 lnd_t *
365 lnet_find_lnd_by_type (int type)
366 {
367         lnd_t            *lnd;
368         struct list_head *tmp;
369
370         /* holding lnd mutex */
371         list_for_each(tmp, &the_lnet.ln_lnds) {
372                 lnd = list_entry(tmp, lnd_t, lnd_list);
373
374                 if ((int)lnd->lnd_type == type)
375                         return lnd;
376         }
377         return NULL;
378 }
379
380 void
381 lnet_register_lnd (lnd_t *lnd)
382 {
383         LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
384
385         LASSERT(the_lnet.ln_init);
386         LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
387         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
388
389         list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
390         lnd->lnd_refcount = 0;
391
392         CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
393
394         LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
395 }
396 EXPORT_SYMBOL(lnet_register_lnd);
397
398 void
399 lnet_unregister_lnd (lnd_t *lnd)
400 {
401         LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
402
403         LASSERT(the_lnet.ln_init);
404         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
405         LASSERT(lnd->lnd_refcount == 0);
406
407         list_del(&lnd->lnd_list);
408         CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
409
410         LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
411 }
412 EXPORT_SYMBOL(lnet_unregister_lnd);
413
414 void
415 lnet_counters_get(lnet_counters_t *counters)
416 {
417         lnet_counters_t *ctr;
418         int             i;
419
420         memset(counters, 0, sizeof(*counters));
421
422         lnet_net_lock(LNET_LOCK_EX);
423
424         cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
425                 counters->msgs_max     += ctr->msgs_max;
426                 counters->msgs_alloc   += ctr->msgs_alloc;
427                 counters->errors       += ctr->errors;
428                 counters->send_count   += ctr->send_count;
429                 counters->recv_count   += ctr->recv_count;
430                 counters->route_count  += ctr->route_count;
431                 counters->drop_count   += ctr->drop_count;
432                 counters->send_length  += ctr->send_length;
433                 counters->recv_length  += ctr->recv_length;
434                 counters->route_length += ctr->route_length;
435                 counters->drop_length  += ctr->drop_length;
436
437         }
438         lnet_net_unlock(LNET_LOCK_EX);
439 }
440 EXPORT_SYMBOL(lnet_counters_get);
441
442 void
443 lnet_counters_reset(void)
444 {
445         lnet_counters_t *counters;
446         int             i;
447
448         lnet_net_lock(LNET_LOCK_EX);
449
450         cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
451                 memset(counters, 0, sizeof(lnet_counters_t));
452
453         lnet_net_unlock(LNET_LOCK_EX);
454 }
455 EXPORT_SYMBOL(lnet_counters_reset);
456
457 #ifdef LNET_USE_LIB_FREELIST
458
459 int
460 lnet_freelist_init(lnet_freelist_t *fl, int n, int size)
461 {
462         char *space;
463
464         LASSERT (n > 0);
465
466         size += offsetof (lnet_freeobj_t, fo_contents);
467
468         LIBCFS_ALLOC(space, n * size);
469         if (space == NULL)
470                 return (-ENOMEM);
471
472         INIT_LIST_HEAD(&fl->fl_list);
473         fl->fl_objs = space;
474         fl->fl_nobjs = n;
475         fl->fl_objsize = size;
476
477         do {
478                 list_add((struct list_head *)space, &fl->fl_list);
479                 space += size;
480         } while (--n != 0);
481
482         return 0;
483 }
484
485 void
486 lnet_freelist_fini(lnet_freelist_t *fl)
487 {
488         struct list_head *el;
489         int               count;
490
491         if (fl->fl_nobjs == 0)
492                 return;
493
494         count = 0;
495         for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
496                 count++;
497
498         LASSERT (count == fl->fl_nobjs);
499
500         LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
501         memset (fl, 0, sizeof (*fl));
502 }
503
504 #endif /* LNET_USE_LIB_FREELIST */
505
506 __u64 lnet_create_interface_cookie (void)
507 {
508         /* NB the interface cookie in wire handles guards against delayed
509          * replies and ACKs appearing valid after reboot. Initialisation time,
510          * even if it's only implemented to millisecond resolution is probably
511          * easily good enough. */
512         struct timeval tv;
513         __u64          cookie;
514 #ifndef __KERNEL__
515         int            rc = gettimeofday (&tv, NULL);
516         LASSERT (rc == 0);
517 #else
518         do_gettimeofday(&tv);
519 #endif
520         cookie = tv.tv_sec;
521         cookie *= 1000000;
522         cookie += tv.tv_usec;
523         return cookie;
524 }
525
526 static char *
527 lnet_res_type2str(int type)
528 {
529         switch (type) {
530         default:
531                 LBUG();
532         case LNET_COOKIE_TYPE_MD:
533                 return "MD";
534         case LNET_COOKIE_TYPE_ME:
535                 return "ME";
536         case LNET_COOKIE_TYPE_EQ:
537                 return "EQ";
538         }
539 }
540
541 void
542 lnet_res_container_cleanup(struct lnet_res_container *rec)
543 {
544         int     count = 0;
545
546         if (rec->rec_type == 0) /* not set yet, it's uninitialized */
547                 return;
548
549         while (!list_empty(&rec->rec_active)) {
550                 struct list_head *e = rec->rec_active.next;
551
552                 list_del_init(e);
553                 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
554                         lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
555
556                 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
557                         lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
558
559                 } else { /* NB: Active MEs should be attached on portals */
560                         LBUG();
561                 }
562                 count++;
563         }
564
565         if (count > 0) {
566                 /* Found alive MD/ME/EQ, user really should unlink/free
567                  * all of them before finalize LNet, but if someone didn't,
568                  * we have to recycle garbage for him */
569                 CERROR("%d active elements on exit of %s container\n",
570                        count, lnet_res_type2str(rec->rec_type));
571         }
572
573 #ifdef LNET_USE_LIB_FREELIST
574         lnet_freelist_fini(&rec->rec_freelist);
575 #endif
576         if (rec->rec_lh_hash != NULL) {
577                 LIBCFS_FREE(rec->rec_lh_hash,
578                             LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
579                 rec->rec_lh_hash = NULL;
580         }
581
582         rec->rec_type = 0; /* mark it as finalized */
583 }
584
585 int
586 lnet_res_container_setup(struct lnet_res_container *rec,
587                          int cpt, int type, int objnum, int objsz)
588 {
589         int     rc = 0;
590         int     i;
591
592         LASSERT(rec->rec_type == 0);
593
594         rec->rec_type = type;
595         INIT_LIST_HEAD(&rec->rec_active);
596
597 #ifdef LNET_USE_LIB_FREELIST
598         memset(&rec->rec_freelist, 0, sizeof(rec->rec_freelist));
599         rc = lnet_freelist_init(&rec->rec_freelist, objnum, objsz);
600         if (rc != 0)
601                 goto out;
602 #endif
603         rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
604
605         /* Arbitrary choice of hash table size */
606         LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
607                          LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
608         if (rec->rec_lh_hash == NULL) {
609                 rc = -ENOMEM;
610                 goto out;
611         }
612
613         for (i = 0; i < LNET_LH_HASH_SIZE; i++)
614                 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
615
616         return 0;
617
618 out:
619         CERROR("Failed to setup %s resource container\n",
620                lnet_res_type2str(type));
621         lnet_res_container_cleanup(rec);
622         return rc;
623 }
624
625 static void
626 lnet_res_containers_destroy(struct lnet_res_container **recs)
627 {
628         struct lnet_res_container       *rec;
629         int                             i;
630
631         cfs_percpt_for_each(rec, i, recs)
632                 lnet_res_container_cleanup(rec);
633
634         cfs_percpt_free(recs);
635 }
636
637 static struct lnet_res_container **
638 lnet_res_containers_create(int type, int objnum, int objsz)
639 {
640         struct lnet_res_container       **recs;
641         struct lnet_res_container       *rec;
642         int                             rc;
643         int                             i;
644
645         recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
646         if (recs == NULL) {
647                 CERROR("Failed to allocate %s resource containers\n",
648                        lnet_res_type2str(type));
649                 return NULL;
650         }
651
652         cfs_percpt_for_each(rec, i, recs) {
653                 rc = lnet_res_container_setup(rec, i, type, objnum, objsz);
654                 if (rc != 0) {
655                         lnet_res_containers_destroy(recs);
656                         return NULL;
657                 }
658         }
659
660         return recs;
661 }
662
663 lnet_libhandle_t *
664 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
665 {
666         /* ALWAYS called with lnet_res_lock held */
667         struct list_head        *head;
668         lnet_libhandle_t        *lh;
669         unsigned int            hash;
670
671         if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
672                 return NULL;
673
674         hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
675         head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
676
677         list_for_each_entry(lh, head, lh_hash_chain) {
678                 if (lh->lh_cookie == cookie)
679                         return lh;
680         }
681
682         return NULL;
683 }
684
685 void
686 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
687 {
688         /* ALWAYS called with lnet_res_lock held */
689         unsigned int    ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
690         unsigned int    hash;
691
692         lh->lh_cookie = rec->rec_lh_cookie;
693         rec->rec_lh_cookie += 1 << ibits;
694
695         hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
696
697         list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
698 }
699
700 #ifndef __KERNEL__
701 /**
702  * Reserved API - do not use.
703  * Temporary workaround to allow uOSS and test programs force server
704  * mode in userspace. See comments near ln_server_mode_flag in
705  * lnet/lib-types.h */
706
707 void
708 lnet_server_mode() {
709         the_lnet.ln_server_mode_flag = 1;
710 }
711 #endif
712
713 int lnet_unprepare(void);
714
715 int
716 lnet_prepare(lnet_pid_t requested_pid)
717 {
718         /* Prepare to bring up the network */
719         struct lnet_res_container **recs;
720         int                       rc = 0;
721
722         LASSERT (the_lnet.ln_refcount == 0);
723
724         the_lnet.ln_routing = 0;
725
726 #ifdef __KERNEL__
727         LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
728         the_lnet.ln_pid = requested_pid;
729 #else
730         if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
731                 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
732
733                 if (current_uid() != 0) /* Only root can run user-space server */
734                         return -EPERM;
735                 the_lnet.ln_pid = requested_pid;
736
737         } else {/* client case (liblustre) */
738
739                 /* My PID must be unique on this node and flag I'm userspace */
740                 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
741         }
742 #endif
743
744         INIT_LIST_HEAD(&the_lnet.ln_test_peers);
745         INIT_LIST_HEAD(&the_lnet.ln_nis);
746         INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
747         INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
748         INIT_LIST_HEAD(&the_lnet.ln_routers);
749
750         rc = lnet_create_remote_nets_table();
751         if (rc != 0)
752                 goto failed;
753
754         the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
755
756         the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
757                                                 sizeof(lnet_counters_t));
758         if (the_lnet.ln_counters == NULL) {
759                 CERROR("Failed to allocate counters for LNet\n");
760                 rc = -ENOMEM;
761                 goto failed;
762         }
763
764         rc = lnet_peer_tables_create();
765         if (rc != 0)
766                 goto failed;
767
768         rc = lnet_msg_containers_create();
769         if (rc != 0)
770                 goto failed;
771
772         rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
773                                       LNET_COOKIE_TYPE_EQ, LNET_FL_MAX_EQS,
774                                       sizeof(lnet_eq_t));
775         if (rc != 0)
776                 goto failed;
777
778         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME, LNET_FL_MAX_MES,
779                                           sizeof(lnet_me_t));
780         if (recs == NULL)
781                 goto failed;
782
783         the_lnet.ln_me_containers = recs;
784
785         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD, LNET_FL_MAX_MDS,
786                                           sizeof(lnet_libmd_t));
787         if (recs == NULL)
788                 goto failed;
789
790         the_lnet.ln_md_containers = recs;
791
792         rc = lnet_portals_create();
793         if (rc != 0) {
794                 CERROR("Failed to create portals for LNet: %d\n", rc);
795                 goto failed;
796         }
797
798         return 0;
799
800  failed:
801         lnet_unprepare();
802         return rc;
803 }
804
805 int
806 lnet_unprepare (void)
807 {
808         /* NB no LNET_LOCK since this is the last reference.  All LND instances
809          * have shut down already, so it is safe to unlink and free all
810          * descriptors, even those that appear committed to a network op (eg MD
811          * with non-zero pending count) */
812
813         lnet_fail_nid(LNET_NID_ANY, 0);
814
815         LASSERT(the_lnet.ln_refcount == 0);
816         LASSERT(list_empty(&the_lnet.ln_test_peers));
817         LASSERT(list_empty(&the_lnet.ln_nis));
818         LASSERT(list_empty(&the_lnet.ln_nis_cpt));
819         LASSERT(list_empty(&the_lnet.ln_nis_zombie));
820
821         lnet_portals_destroy();
822
823         if (the_lnet.ln_md_containers != NULL) {
824                 lnet_res_containers_destroy(the_lnet.ln_md_containers);
825                 the_lnet.ln_md_containers = NULL;
826         }
827
828         if (the_lnet.ln_me_containers != NULL) {
829                 lnet_res_containers_destroy(the_lnet.ln_me_containers);
830                 the_lnet.ln_me_containers = NULL;
831         }
832
833         lnet_res_container_cleanup(&the_lnet.ln_eq_container);
834
835         lnet_msg_containers_destroy();
836         lnet_peer_tables_destroy();
837         lnet_rtrpools_free();
838
839         if (the_lnet.ln_counters != NULL) {
840                 cfs_percpt_free(the_lnet.ln_counters);
841                 the_lnet.ln_counters = NULL;
842         }
843         lnet_destroy_remote_nets_table();
844
845         return 0;
846 }
847
848 lnet_ni_t  *
849 lnet_net2ni_locked(__u32 net, int cpt)
850 {
851         struct list_head *tmp;
852         lnet_ni_t        *ni;
853
854         LASSERT(cpt != LNET_LOCK_EX);
855
856         list_for_each(tmp, &the_lnet.ln_nis) {
857                 ni = list_entry(tmp, lnet_ni_t, ni_list);
858
859                 if (LNET_NIDNET(ni->ni_nid) == net) {
860                         lnet_ni_addref_locked(ni, cpt);
861                         return ni;
862                 }
863         }
864
865         return NULL;
866 }
867
868 lnet_ni_t *
869 lnet_net2ni(__u32 net)
870 {
871         lnet_ni_t *ni;
872
873         lnet_net_lock(0);
874         ni = lnet_net2ni_locked(net, 0);
875         lnet_net_unlock(0);
876
877         return ni;
878 }
879 EXPORT_SYMBOL(lnet_net2ni);
880
881 static unsigned int
882 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
883 {
884         __u64           key = nid;
885         unsigned int    val;
886
887         LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
888
889         if (number == 1)
890                 return 0;
891
892         val = hash_long(key, LNET_CPT_BITS);
893         /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
894         if (val < number)
895                 return val;
896
897         return (unsigned int)(key + val + (val >> 1)) % number;
898 }
899
900 int
901 lnet_cpt_of_nid_locked(lnet_nid_t nid)
902 {
903         struct lnet_ni *ni;
904
905         /* must called with hold of lnet_net_lock */
906         if (LNET_CPT_NUMBER == 1)
907                 return 0; /* the only one */
908
909         /* take lnet_net_lock(any) would be OK */
910         if (!list_empty(&the_lnet.ln_nis_cpt)) {
911                 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
912                         if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
913                                 continue;
914
915                         LASSERT(ni->ni_cpts != NULL);
916                         return ni->ni_cpts[lnet_nid_cpt_hash
917                                            (nid, ni->ni_ncpts)];
918                 }
919         }
920
921         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
922 }
923
924 int
925 lnet_cpt_of_nid(lnet_nid_t nid)
926 {
927         int     cpt;
928         int     cpt2;
929
930         if (LNET_CPT_NUMBER == 1)
931                 return 0; /* the only one */
932
933         if (list_empty(&the_lnet.ln_nis_cpt))
934                 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
935
936         cpt = lnet_net_lock_current();
937         cpt2 = lnet_cpt_of_nid_locked(nid);
938         lnet_net_unlock(cpt);
939
940         return cpt2;
941 }
942 EXPORT_SYMBOL(lnet_cpt_of_nid);
943
944 int
945 lnet_islocalnet(__u32 net)
946 {
947         struct lnet_ni  *ni;
948         int             cpt;
949
950         cpt = lnet_net_lock_current();
951
952         ni = lnet_net2ni_locked(net, cpt);
953         if (ni != NULL)
954                 lnet_ni_decref_locked(ni, cpt);
955
956         lnet_net_unlock(cpt);
957
958         return ni != NULL;
959 }
960
961 lnet_ni_t  *
962 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
963 {
964         struct lnet_ni   *ni;
965         struct list_head *tmp;
966
967         LASSERT(cpt != LNET_LOCK_EX);
968
969         list_for_each(tmp, &the_lnet.ln_nis) {
970                 ni = list_entry(tmp, lnet_ni_t, ni_list);
971
972                 if (ni->ni_nid == nid) {
973                         lnet_ni_addref_locked(ni, cpt);
974                         return ni;
975                 }
976         }
977
978         return NULL;
979 }
980
981 int
982 lnet_islocalnid(lnet_nid_t nid)
983 {
984         struct lnet_ni  *ni;
985         int             cpt;
986
987         cpt = lnet_net_lock_current();
988         ni = lnet_nid2ni_locked(nid, cpt);
989         if (ni != NULL)
990                 lnet_ni_decref_locked(ni, cpt);
991         lnet_net_unlock(cpt);
992
993         return ni != NULL;
994 }
995
996 int
997 lnet_count_acceptor_nis (void)
998 {
999         /* Return the # of NIs that need the acceptor. */
1000         int              count = 0;
1001 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
1002         struct list_head *tmp;
1003         struct lnet_ni   *ni;
1004         int              cpt;
1005
1006         cpt = lnet_net_lock_current();
1007         list_for_each(tmp, &the_lnet.ln_nis) {
1008                 ni = list_entry(tmp, lnet_ni_t, ni_list);
1009
1010                 if (ni->ni_lnd->lnd_accept != NULL)
1011                         count++;
1012         }
1013
1014         lnet_net_unlock(cpt);
1015
1016 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
1017         return count;
1018 }
1019
1020 static int
1021 lnet_ni_tq_credits(lnet_ni_t *ni)
1022 {
1023         int     credits;
1024
1025         LASSERT(ni->ni_ncpts >= 1);
1026
1027         if (ni->ni_ncpts == 1)
1028                 return ni->ni_maxtxcredits;
1029
1030         credits = ni->ni_maxtxcredits / ni->ni_ncpts;
1031         credits = max(credits, 8 * ni->ni_peertxcredits);
1032         credits = min(credits, ni->ni_maxtxcredits);
1033
1034         return credits;
1035 }
1036
1037 void
1038 lnet_shutdown_lndnis (void)
1039 {
1040         int                i;
1041         int                islo;
1042         lnet_ni_t         *ni;
1043
1044         /* NB called holding the global mutex */
1045
1046         /* All quiet on the API front */
1047         LASSERT(!the_lnet.ln_shutdown);
1048         LASSERT(the_lnet.ln_refcount == 0);
1049         LASSERT(list_empty(&the_lnet.ln_nis_zombie));
1050
1051         lnet_net_lock(LNET_LOCK_EX);
1052         the_lnet.ln_shutdown = 1;       /* flag shutdown */
1053
1054         /* Unlink NIs from the global table */
1055         while (!list_empty(&the_lnet.ln_nis)) {
1056                 ni = list_entry(the_lnet.ln_nis.next,
1057                                 lnet_ni_t, ni_list);
1058                 /* move it to zombie list and nobody can find it anymore */
1059                 list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1060                 lnet_ni_decref_locked(ni, 0);   /* drop ln_nis' ref */
1061
1062                 if (!list_empty(&ni->ni_cptlist)) {
1063                         list_del_init(&ni->ni_cptlist);
1064                         lnet_ni_decref_locked(ni, 0);
1065                 }
1066         }
1067
1068         /* Drop the cached eqwait NI. */
1069         if (the_lnet.ln_eq_waitni != NULL) {
1070                 lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
1071                 the_lnet.ln_eq_waitni = NULL;
1072         }
1073
1074         /* Drop the cached loopback NI. */
1075         if (the_lnet.ln_loni != NULL) {
1076                 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1077                 the_lnet.ln_loni = NULL;
1078         }
1079
1080         lnet_net_unlock(LNET_LOCK_EX);
1081
1082         /* Clear lazy portals and drop delayed messages which hold refs
1083          * on their lnet_msg_t::msg_rxpeer */
1084         for (i = 0; i < the_lnet.ln_nportals; i++)
1085                 LNetClearLazyPortal(i);
1086
1087         /* Clear the peer table and wait for all peers to go (they hold refs on
1088          * their NIs) */
1089         lnet_peer_tables_cleanup(NULL);
1090
1091         lnet_net_lock(LNET_LOCK_EX);
1092         /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
1093          * and shut them down in guaranteed thread context */
1094         i = 2;
1095         while (!list_empty(&the_lnet.ln_nis_zombie)) {
1096                 int     *ref;
1097                 int     j;
1098
1099                 ni = list_entry(the_lnet.ln_nis_zombie.next,
1100                                 lnet_ni_t, ni_list);
1101                 list_del_init(&ni->ni_list);
1102                 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1103                         if (*ref == 0)
1104                                 continue;
1105                         /* still busy, add it back to zombie list */
1106                         list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1107                         break;
1108                 }
1109
1110                 if (!list_empty(&ni->ni_list)) {
1111                         lnet_net_unlock(LNET_LOCK_EX);
1112                         ++i;
1113                         if ((i & (-i)) == i) {
1114                                 CDEBUG(D_WARNING, "Waiting for zombie LNI %s\n",
1115                                        libcfs_nid2str(ni->ni_nid));
1116                         }
1117                         cfs_pause(cfs_time_seconds(1));
1118                         lnet_net_lock(LNET_LOCK_EX);
1119                         continue;
1120                 }
1121
1122                 ni->ni_lnd->lnd_refcount--;
1123                 lnet_net_unlock(LNET_LOCK_EX);
1124
1125                 islo = ni->ni_lnd->lnd_type == LOLND;
1126
1127                 LASSERT (!in_interrupt ());
1128                 (ni->ni_lnd->lnd_shutdown)(ni);
1129
1130                 /* can't deref lnd anymore now; it might have unregistered
1131                  * itself...  */
1132
1133                 if (!islo)
1134                         CDEBUG(D_LNI, "Removed LNI %s\n",
1135                                libcfs_nid2str(ni->ni_nid));
1136
1137                 lnet_ni_free(ni);
1138                 i = 2;
1139
1140                 lnet_net_lock(LNET_LOCK_EX);
1141         }
1142
1143         the_lnet.ln_shutdown = 0;
1144         lnet_net_unlock(LNET_LOCK_EX);
1145 }
1146
1147 int
1148 lnet_startup_lndnis (void)
1149 {
1150         lnd_t                   *lnd;
1151         struct lnet_ni          *ni;
1152         struct lnet_tx_queue    *tq;
1153         struct list_head        nilist;
1154         int                     i;
1155         int                     rc = 0;
1156         int                     lnd_type;
1157         int                     nicount = 0;
1158         char                    *nets = lnet_get_networks();
1159
1160         INIT_LIST_HEAD(&nilist);
1161
1162         if (nets == NULL)
1163                 goto failed;
1164
1165         rc = lnet_parse_networks(&nilist, nets);
1166         if (rc != 0)
1167                 goto failed;
1168
1169         while (!list_empty(&nilist)) {
1170                 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1171                 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1172
1173                 LASSERT(libcfs_isknown_lnd(lnd_type));
1174
1175                 if (lnd_type == CIBLND    ||
1176                     lnd_type == OPENIBLND ||
1177                     lnd_type == IIBLND    ||
1178                     lnd_type == VIBLND) {
1179                         CERROR("LND %s obsoleted\n",
1180                                libcfs_lnd2str(lnd_type));
1181                         goto failed;
1182                 }
1183
1184                 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1185                 lnd = lnet_find_lnd_by_type(lnd_type);
1186
1187 #ifdef __KERNEL__
1188                 if (lnd == NULL) {
1189                         LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1190                         rc = request_module("%s",
1191                                                 libcfs_lnd2modname(lnd_type));
1192                         LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1193
1194                         lnd = lnet_find_lnd_by_type(lnd_type);
1195                         if (lnd == NULL) {
1196                                 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1197                                 CERROR("Can't load LND %s, module %s, rc=%d\n",
1198                                        libcfs_lnd2str(lnd_type),
1199                                        libcfs_lnd2modname(lnd_type), rc);
1200 #ifndef HAVE_MODULE_LOADING_SUPPORT
1201                                 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1202                                          "compiled with kernel module "
1203                                          "loading support.");
1204 #endif
1205                                 goto failed;
1206                         }
1207                 }
1208 #else
1209                 if (lnd == NULL) {
1210                         LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1211                         CERROR("LND %s not supported\n",
1212                                libcfs_lnd2str(lnd_type));
1213                         goto failed;
1214                 }
1215 #endif
1216
1217                 lnet_net_lock(LNET_LOCK_EX);
1218                 lnd->lnd_refcount++;
1219                 lnet_net_unlock(LNET_LOCK_EX);
1220
1221                 ni->ni_lnd = lnd;
1222
1223                 rc = (lnd->lnd_startup)(ni);
1224
1225                 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1226
1227                 if (rc != 0) {
1228                         LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1229                                            "\n",
1230                                            rc, libcfs_lnd2str(lnd->lnd_type));
1231                         lnet_net_lock(LNET_LOCK_EX);
1232                         lnd->lnd_refcount--;
1233                         lnet_net_unlock(LNET_LOCK_EX);
1234                         goto failed;
1235                 }
1236
1237                 LASSERT (ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1238
1239                 list_del(&ni->ni_list);
1240
1241                 lnet_net_lock(LNET_LOCK_EX);
1242                 /* refcount for ln_nis */
1243                 lnet_ni_addref_locked(ni, 0);
1244                 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1245                 if (ni->ni_cpts != NULL) {
1246                         list_add_tail(&ni->ni_cptlist,
1247                                       &the_lnet.ln_nis_cpt);
1248                         lnet_ni_addref_locked(ni, 0);
1249                 }
1250
1251                 lnet_net_unlock(LNET_LOCK_EX);
1252
1253                 if (lnd->lnd_type == LOLND) {
1254                         lnet_ni_addref(ni);
1255                         LASSERT (the_lnet.ln_loni == NULL);
1256                         the_lnet.ln_loni = ni;
1257                         continue;
1258                 }
1259
1260 #ifndef __KERNEL__
1261                 if (lnd->lnd_wait != NULL) {
1262                         if (the_lnet.ln_eq_waitni == NULL) {
1263                                 lnet_ni_addref(ni);
1264                                 the_lnet.ln_eq_waitni = ni;
1265                         }
1266                 } else {
1267 # ifndef HAVE_LIBPTHREAD
1268                         LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1269                                            "single-threaded runtime\n",
1270                                            libcfs_lnd2str(lnd_type));
1271                         goto failed;
1272 # endif
1273                 }
1274 #endif
1275                 if (ni->ni_peertxcredits == 0 ||
1276                     ni->ni_maxtxcredits == 0) {
1277                         LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1278                                            libcfs_lnd2str(lnd->lnd_type),
1279                                            ni->ni_peertxcredits == 0 ?
1280                                            "" : "per-peer ");
1281                         goto failed;
1282                 }
1283
1284                 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1285                         tq->tq_credits_min =
1286                         tq->tq_credits_max =
1287                         tq->tq_credits = lnet_ni_tq_credits(ni);
1288                 }
1289
1290                 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1291                        libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1292                        lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1293                        ni->ni_peerrtrcredits, ni->ni_peertimeout);
1294
1295                 nicount++;
1296         }
1297
1298         if (the_lnet.ln_eq_waitni != NULL && nicount > 1) {
1299                 lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1300                 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1301                                    "\n",
1302                                    libcfs_lnd2str(lnd_type));
1303                 goto failed;
1304         }
1305
1306         return 0;
1307
1308  failed:
1309         lnet_shutdown_lndnis();
1310
1311         while (!list_empty(&nilist)) {
1312                 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1313                 list_del(&ni->ni_list);
1314                 lnet_ni_free(ni);
1315         }
1316
1317         return -ENETDOWN;
1318 }
1319
1320 /**
1321  * Initialize LNet library.
1322  *
1323  * Only userspace program needs to call this function - it's automatically
1324  * called in the kernel at module loading time. Caller has to call LNetFini()
1325  * after a call to LNetInit(), if and only if the latter returned 0. It must
1326  * be called exactly once.
1327  *
1328  * \return 0 on success, and -ve on failures.
1329  */
1330 int
1331 LNetInit(void)
1332 {
1333         int     rc;
1334
1335         lnet_assert_wire_constants();
1336         LASSERT(!the_lnet.ln_init);
1337
1338         memset(&the_lnet, 0, sizeof(the_lnet));
1339
1340         /* refer to global cfs_cpt_table for now */
1341         the_lnet.ln_cpt_table   = cfs_cpt_table;
1342         the_lnet.ln_cpt_number  = cfs_cpt_number(cfs_cpt_table);
1343
1344         LASSERT(the_lnet.ln_cpt_number > 0);
1345         if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1346                 /* we are under risk of consuming all lh_cookie */
1347                 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1348                        "please change setting of CPT-table and retry\n",
1349                        the_lnet.ln_cpt_number, LNET_CPT_MAX);
1350                 return -1;
1351         }
1352
1353         while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1354                 the_lnet.ln_cpt_bits++;
1355
1356         rc = lnet_create_locks();
1357         if (rc != 0) {
1358                 CERROR("Can't create LNet global locks: %d\n", rc);
1359                 return -1;
1360         }
1361
1362         the_lnet.ln_refcount = 0;
1363         the_lnet.ln_init = 1;
1364         LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1365         INIT_LIST_HEAD(&the_lnet.ln_lnds);
1366         INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1367         INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1368
1369 #ifdef __KERNEL__
1370         /* The hash table size is the number of bits it takes to express the set
1371          * ln_num_routes, minus 1 (better to under estimate than over so we
1372          * don't waste memory). */
1373         if (rnet_htable_size <= 0)
1374                 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1375         else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1376                 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1377         the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1378                                            order_base_2(rnet_htable_size) - 1);
1379
1380         /* All LNDs apart from the LOLND are in separate modules.  They
1381          * register themselves when their module loads, and unregister
1382          * themselves when their module is unloaded. */
1383 #else
1384         the_lnet.ln_remote_nets_hbits = 8;
1385
1386         /* Register LNDs
1387          * NB the order here determines default 'networks=' order */
1388 # ifdef HAVE_LIBPTHREAD
1389         LNET_REGISTER_ULND(the_tcplnd);
1390 # endif
1391 #endif
1392         lnet_register_lnd(&the_lolnd);
1393         return 0;
1394 }
1395 EXPORT_SYMBOL(LNetInit);
1396
1397 /**
1398  * Finalize LNet library.
1399  *
1400  * Only userspace program needs to call this function. It can be called
1401  * at most once.
1402  *
1403  * \pre LNetInit() called with success.
1404  * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1405  */
1406 void
1407 LNetFini(void)
1408 {
1409         LASSERT(the_lnet.ln_init);
1410         LASSERT(the_lnet.ln_refcount == 0);
1411
1412         while (!list_empty(&the_lnet.ln_lnds))
1413                 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1414                                                lnd_t, lnd_list));
1415         lnet_destroy_locks();
1416
1417         the_lnet.ln_init = 0;
1418 }
1419 EXPORT_SYMBOL(LNetFini);
1420
1421 /**
1422  * Set LNet PID and start LNet interfaces, routing, and forwarding.
1423  *
1424  * Userspace program should call this after a successful call to LNetInit().
1425  * Users must call this function at least once before any other functions.
1426  * For each successful call there must be a corresponding call to
1427  * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1428  * ignored.
1429  *
1430  * The PID used by LNet may be different from the one requested.
1431  * See LNetGetId().
1432  *
1433  * \param requested_pid PID requested by the caller.
1434  *
1435  * \return >= 0 on success, and < 0 error code on failures.
1436  */
1437 int
1438 LNetNIInit(lnet_pid_t requested_pid)
1439 {
1440         int         im_a_router = 0;
1441         int         rc;
1442
1443         LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1444
1445         LASSERT (the_lnet.ln_init);
1446         CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1447
1448         if (the_lnet.ln_refcount > 0) {
1449                 rc = the_lnet.ln_refcount++;
1450                 goto out;
1451         }
1452
1453         lnet_get_tunables();
1454
1455         if (requested_pid == LNET_PID_ANY) {
1456                 /* Don't instantiate LNET just for me */
1457                 rc = -ENETDOWN;
1458                 goto failed0;
1459         }
1460
1461         rc = lnet_prepare(requested_pid);
1462         if (rc != 0)
1463                 goto failed0;
1464
1465         rc = lnet_startup_lndnis();
1466         if (rc != 0)
1467                 goto failed1;
1468
1469         rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1470         if (rc != 0)
1471                 goto failed2;
1472
1473         rc = lnet_check_routes();
1474         if (rc != 0)
1475                 goto failed2;
1476
1477         rc = lnet_rtrpools_alloc(im_a_router);
1478         if (rc != 0)
1479                 goto failed2;
1480
1481         rc = lnet_acceptor_start();
1482         if (rc != 0)
1483                 goto failed2;
1484
1485         the_lnet.ln_refcount = 1;
1486         /* Now I may use my own API functions... */
1487
1488         /* NB router checker needs the_lnet.ln_ping_info in
1489          * lnet_router_checker -> lnet_update_ni_status_locked */
1490         rc = lnet_ping_target_init();
1491         if (rc != 0)
1492                 goto failed3;
1493
1494         rc = lnet_router_checker_start();
1495         if (rc != 0)
1496                 goto failed4;
1497
1498         lnet_proc_init();
1499         goto out;
1500
1501  failed4:
1502         lnet_ping_target_fini();
1503  failed3:
1504         the_lnet.ln_refcount = 0;
1505         lnet_acceptor_stop();
1506  failed2:
1507         lnet_destroy_routes();
1508         lnet_shutdown_lndnis();
1509  failed1:
1510         lnet_unprepare();
1511  failed0:
1512         LASSERT (rc < 0);
1513  out:
1514         LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1515         return rc;
1516 }
1517 EXPORT_SYMBOL(LNetNIInit);
1518
1519 /**
1520  * Stop LNet interfaces, routing, and forwarding.
1521  *
1522  * Users must call this function once for each successful call to LNetNIInit().
1523  * Once the LNetNIFini() operation has been started, the results of pending
1524  * API operations are undefined.
1525  *
1526  * \return always 0 for current implementation.
1527  */
1528 int
1529 LNetNIFini()
1530 {
1531         LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1532
1533         LASSERT (the_lnet.ln_init);
1534         LASSERT (the_lnet.ln_refcount > 0);
1535
1536         if (the_lnet.ln_refcount != 1) {
1537                 the_lnet.ln_refcount--;
1538         } else {
1539                 LASSERT (!the_lnet.ln_niinit_self);
1540
1541                 lnet_proc_fini();
1542                 lnet_router_checker_stop();
1543                 lnet_ping_target_fini();
1544
1545                 /* Teardown fns that use my own API functions BEFORE here */
1546                 the_lnet.ln_refcount = 0;
1547
1548                 lnet_acceptor_stop();
1549                 lnet_destroy_routes();
1550                 lnet_shutdown_lndnis();
1551                 lnet_unprepare();
1552         }
1553
1554         LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1555         return 0;
1556 }
1557 EXPORT_SYMBOL(LNetNIFini);
1558
1559 /**
1560  * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1561  * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1562  * internal ioctl handler.
1563  *
1564  * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1565  *
1566  * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1567  * The data will be printed to system console. Don't use it excessively.
1568  * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1569  *
1570  * \return Always return 0 when called by users directly (i.e., not via ioctl).
1571  */
1572 int
1573 LNetCtl(unsigned int cmd, void *arg)
1574 {
1575         struct libcfs_ioctl_data *data = arg;
1576         lnet_process_id_t         id = {0};
1577         lnet_ni_t                *ni;
1578         int                       rc;
1579
1580         LASSERT (the_lnet.ln_init);
1581         LASSERT (the_lnet.ln_refcount > 0);
1582
1583         switch (cmd) {
1584         case IOC_LIBCFS_GET_NI:
1585                 rc = LNetGetId(data->ioc_count, &id);
1586                 data->ioc_nid = id.nid;
1587                 return rc;
1588
1589         case IOC_LIBCFS_FAIL_NID:
1590                 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1591
1592         case IOC_LIBCFS_ADD_ROUTE:
1593                 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1594                                     data->ioc_nid, data->ioc_priority);
1595                 return (rc != 0) ? rc : lnet_check_routes();
1596
1597         case IOC_LIBCFS_DEL_ROUTE:
1598                 return lnet_del_route(data->ioc_net, data->ioc_nid);
1599
1600         case IOC_LIBCFS_GET_ROUTE:
1601                 return lnet_get_route(data->ioc_count,
1602                                       &data->ioc_net, &data->ioc_count,
1603                                       &data->ioc_nid, &data->ioc_flags,
1604                                       &data->ioc_priority);
1605         case IOC_LIBCFS_NOTIFY_ROUTER:
1606                 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1607                                    cfs_time_current() -
1608                                    cfs_time_seconds(cfs_time_current_sec() -
1609                                                     (time_t)data->ioc_u64[0]));
1610
1611         case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1612                 /* This can be removed once lustre stops calling it */
1613                 return 0;
1614
1615         case IOC_LIBCFS_LNET_DIST:
1616                 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1617                 if (rc < 0 && rc != -EHOSTUNREACH)
1618                         return rc;
1619
1620                 data->ioc_u32[0] = rc;
1621                 return 0;
1622
1623         case IOC_LIBCFS_TESTPROTOCOMPAT:
1624                 lnet_net_lock(LNET_LOCK_EX);
1625                 the_lnet.ln_testprotocompat = data->ioc_flags;
1626                 lnet_net_unlock(LNET_LOCK_EX);
1627                 return 0;
1628
1629         case IOC_LIBCFS_PING:
1630                 id.nid = data->ioc_nid;
1631                 id.pid = data->ioc_u32[0];
1632                 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1633                                (lnet_process_id_t *)data->ioc_pbuf1,
1634                                data->ioc_plen1/sizeof(lnet_process_id_t));
1635                 if (rc < 0)
1636                         return rc;
1637                 data->ioc_count = rc;
1638                 return 0;
1639
1640         case IOC_LIBCFS_DEBUG_PEER: {
1641                 /* CAVEAT EMPTOR: this one designed for calling directly; not
1642                  * via an ioctl */
1643                 id = *((lnet_process_id_t *) arg);
1644
1645                 lnet_debug_peer(id.nid);
1646
1647                 ni = lnet_net2ni(LNET_NIDNET(id.nid));
1648                 if (ni == NULL) {
1649                         CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1650                 } else {
1651                         if (ni->ni_lnd->lnd_ctl == NULL) {
1652                                 CDEBUG(D_WARNING, "No ctl for %s\n",
1653                                        libcfs_id2str(id));
1654                         } else {
1655                                 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1656                         }
1657
1658                         lnet_ni_decref(ni);
1659                 }
1660                 return 0;
1661         }
1662
1663         default:
1664                 ni = lnet_net2ni(data->ioc_net);
1665                 if (ni == NULL)
1666                         return -EINVAL;
1667
1668                 if (ni->ni_lnd->lnd_ctl == NULL)
1669                         rc = -EINVAL;
1670                 else
1671                         rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1672
1673                 lnet_ni_decref(ni);
1674                 return rc;
1675         }
1676         /* not reached */
1677 }
1678 EXPORT_SYMBOL(LNetCtl);
1679
1680 /**
1681  * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
1682  * all interfaces share a same PID, as requested by LNetNIInit().
1683  *
1684  * \param index Index of the interface to look up.
1685  * \param id On successful return, this location will hold the
1686  * lnet_process_id_t ID of the interface.
1687  *
1688  * \retval 0 If an interface exists at \a index.
1689  * \retval -ENOENT If no interface has been found.
1690  */
1691 int
1692 LNetGetId(unsigned int index, lnet_process_id_t *id)
1693 {
1694         struct lnet_ni   *ni;
1695         struct list_head *tmp;
1696         int               cpt;
1697         int               rc = -ENOENT;
1698
1699         LASSERT(the_lnet.ln_init);
1700         LASSERT(the_lnet.ln_refcount > 0);
1701
1702         cpt = lnet_net_lock_current();
1703
1704         list_for_each(tmp, &the_lnet.ln_nis) {
1705                 if (index-- != 0)
1706                         continue;
1707
1708                 ni = list_entry(tmp, lnet_ni_t, ni_list);
1709
1710                 id->nid = ni->ni_nid;
1711                 id->pid = the_lnet.ln_pid;
1712                 rc = 0;
1713                 break;
1714         }
1715
1716         lnet_net_unlock(cpt);
1717         return rc;
1718 }
1719 EXPORT_SYMBOL(LNetGetId);
1720
1721 /**
1722  * Print a string representation of handle \a h into buffer \a str of
1723  * \a len bytes.
1724  */
1725 void
1726 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1727 {
1728         snprintf(str, len, LPX64, h.cookie);
1729 }
1730 EXPORT_SYMBOL(LNetSnprintHandle);
1731
1732 static int
1733 lnet_create_ping_info(void)
1734 {
1735         int               i;
1736         int               n;
1737         int               rc;
1738         unsigned int      infosz;
1739         lnet_ni_t        *ni;
1740         lnet_process_id_t id;
1741         lnet_ping_info_t *pinfo;
1742
1743         for (n = 0; ; n++) {
1744                 rc = LNetGetId(n, &id);
1745                 if (rc == -ENOENT)
1746                         break;
1747
1748                 LASSERT (rc == 0);
1749         }
1750
1751         infosz = offsetof(lnet_ping_info_t, pi_ni[n]);
1752         LIBCFS_ALLOC(pinfo, infosz);
1753         if (pinfo == NULL) {
1754                 CERROR("Can't allocate ping info[%d]\n", n);
1755                 return -ENOMEM;
1756         }
1757
1758         pinfo->pi_nnis    = n;
1759         pinfo->pi_pid     = the_lnet.ln_pid;
1760         pinfo->pi_magic   = LNET_PROTO_PING_MAGIC;
1761         pinfo->pi_features = LNET_PING_FEAT_NI_STATUS;
1762
1763         for (i = 0; i < n; i++) {
1764                 lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1765
1766                 rc = LNetGetId(i, &id);
1767                 LASSERT (rc == 0);
1768
1769                 ns->ns_nid    = id.nid;
1770                 ns->ns_status = LNET_NI_STATUS_UP;
1771
1772                 lnet_net_lock(0);
1773
1774                 ni = lnet_nid2ni_locked(id.nid, 0);
1775                 LASSERT(ni != NULL);
1776
1777                 lnet_ni_lock(ni);
1778                 LASSERT(ni->ni_status == NULL);
1779                 ni->ni_status = ns;
1780                 lnet_ni_unlock(ni);
1781
1782                 lnet_ni_decref_locked(ni, 0);
1783                 lnet_net_unlock(0);
1784         }
1785
1786         the_lnet.ln_ping_info = pinfo;
1787         return 0;
1788 }
1789
1790 static void
1791 lnet_destroy_ping_info(void)
1792 {
1793         struct lnet_ni  *ni;
1794
1795         lnet_net_lock(0);
1796
1797         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1798                 lnet_ni_lock(ni);
1799                 ni->ni_status = NULL;
1800                 lnet_ni_unlock(ni);
1801         }
1802
1803         lnet_net_unlock(0);
1804
1805         LIBCFS_FREE(the_lnet.ln_ping_info,
1806                     offsetof(lnet_ping_info_t,
1807                     pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1808         the_lnet.ln_ping_info = NULL;
1809         return;
1810 }
1811
1812 int
1813 lnet_ping_target_init(void)
1814 {
1815         lnet_md_t         md = {0};
1816         lnet_handle_me_t  meh;
1817         lnet_process_id_t id;
1818         int               rc;
1819         int               rc2;
1820         int               infosz;
1821
1822         rc = lnet_create_ping_info();
1823         if (rc != 0)
1824                 return rc;
1825
1826         /* We can have a tiny EQ since we only need to see the unlink event on
1827          * teardown, which by definition is the last one! */
1828         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1829         if (rc != 0) {
1830                 CERROR("Can't allocate ping EQ: %d\n", rc);
1831                 goto failed_0;
1832         }
1833
1834         memset(&id, 0, sizeof(lnet_process_id_t));
1835         id.nid = LNET_NID_ANY;
1836         id.pid = LNET_PID_ANY;
1837
1838         rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1839                           LNET_PROTO_PING_MATCHBITS, 0,
1840                           LNET_UNLINK, LNET_INS_AFTER,
1841                           &meh);
1842         if (rc != 0) {
1843                 CERROR("Can't create ping ME: %d\n", rc);
1844                 goto failed_1;
1845         }
1846
1847         /* initialize md content */
1848         infosz = offsetof(lnet_ping_info_t,
1849                           pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1850         md.start     = the_lnet.ln_ping_info;
1851         md.length    = infosz;
1852         md.threshold = LNET_MD_THRESH_INF;
1853         md.max_size  = 0;
1854         md.options   = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1855                        LNET_MD_MANAGE_REMOTE;
1856         md.user_ptr  = NULL;
1857         md.eq_handle = the_lnet.ln_ping_target_eq;
1858
1859         rc = LNetMDAttach(meh, md,
1860                           LNET_RETAIN,
1861                           &the_lnet.ln_ping_target_md);
1862         if (rc != 0) {
1863                 CERROR("Can't attach ping MD: %d\n", rc);
1864                 goto failed_2;
1865         }
1866
1867         return 0;
1868
1869  failed_2:
1870         rc2 = LNetMEUnlink(meh);
1871         LASSERT (rc2 == 0);
1872  failed_1:
1873         rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1874         LASSERT (rc2 == 0);
1875  failed_0:
1876         lnet_destroy_ping_info();
1877         return rc;
1878 }
1879
1880 void
1881 lnet_ping_target_fini(void)
1882 {
1883         lnet_event_t    event;
1884         int             rc;
1885         int             which;
1886         int             timeout_ms = 1000;
1887         sigset_t    blocked = cfs_block_allsigs();
1888
1889         LNetMDUnlink(the_lnet.ln_ping_target_md);
1890         /* NB md could be busy; this just starts the unlink */
1891
1892         for (;;) {
1893                 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1894                                 timeout_ms, &event, &which);
1895
1896                 /* I expect overflow... */
1897                 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1898
1899                 if (rc == 0) {
1900                         /* timed out: provide a diagnostic */
1901                         CWARN("Still waiting for ping MD to unlink\n");
1902                         timeout_ms *= 2;
1903                         continue;
1904                 }
1905
1906                 /* Got a valid event */
1907                 if (event.unlinked)
1908                         break;
1909         }
1910
1911         rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1912         LASSERT (rc == 0);
1913         lnet_destroy_ping_info();
1914         cfs_restore_sigs(blocked);
1915 }
1916
1917 int
1918 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1919 {
1920         lnet_handle_eq_t     eqh;
1921         lnet_handle_md_t     mdh;
1922         lnet_event_t         event;
1923         lnet_md_t            md = {0};
1924         int                  which;
1925         int                  unlinked = 0;
1926         int                  replied = 0;
1927         const int            a_long_time = 60000; /* mS */
1928         int                  infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1929         lnet_ping_info_t    *info;
1930         lnet_process_id_t    tmpid;
1931         int                  i;
1932         int                  nob;
1933         int                  rc;
1934         int                  rc2;
1935         sigset_t         blocked;
1936
1937         if (n_ids <= 0 ||
1938             id.nid == LNET_NID_ANY ||
1939             timeout_ms > 500000 ||              /* arbitrary limit! */
1940             n_ids > 20)                         /* arbitrary limit! */
1941                 return -EINVAL;
1942
1943         if (id.pid == LNET_PID_ANY)
1944                 id.pid = LUSTRE_SRV_LNET_PID;
1945
1946         LIBCFS_ALLOC(info, infosz);
1947         if (info == NULL)
1948                 return -ENOMEM;
1949
1950         /* NB 2 events max (including any unlink event) */
1951         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1952         if (rc != 0) {
1953                 CERROR("Can't allocate EQ: %d\n", rc);
1954                 goto out_0;
1955         }
1956
1957         /* initialize md content */
1958         md.start     = info;
1959         md.length    = infosz;
1960         md.threshold = 2; /*GET/REPLY*/
1961         md.max_size  = 0;
1962         md.options   = LNET_MD_TRUNCATE;
1963         md.user_ptr  = NULL;
1964         md.eq_handle = eqh;
1965
1966         rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1967         if (rc != 0) {
1968                 CERROR("Can't bind MD: %d\n", rc);
1969                 goto out_1;
1970         }
1971
1972         rc = LNetGet(LNET_NID_ANY, mdh, id,
1973                      LNET_RESERVED_PORTAL,
1974                      LNET_PROTO_PING_MATCHBITS, 0);
1975
1976         if (rc != 0) {
1977                 /* Don't CERROR; this could be deliberate! */
1978
1979                 rc2 = LNetMDUnlink(mdh);
1980                 LASSERT (rc2 == 0);
1981
1982                 /* NB must wait for the UNLINK event below... */
1983                 unlinked = 1;
1984                 timeout_ms = a_long_time;
1985         }
1986
1987         do {
1988                 /* MUST block for unlink to complete */
1989                 if (unlinked)
1990                         blocked = cfs_block_allsigs();
1991
1992                 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1993
1994                 if (unlinked)
1995                         cfs_restore_sigs(blocked);
1996
1997                 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1998                        (rc2 <= 0) ? -1 : event.type,
1999                        (rc2 <= 0) ? -1 : event.status,
2000                        (rc2 > 0 && event.unlinked) ? " unlinked" : "");
2001
2002                 LASSERT (rc2 != -EOVERFLOW);     /* can't miss anything */
2003
2004                 if (rc2 <= 0 || event.status != 0) {
2005                         /* timeout or error */
2006                         if (!replied && rc == 0)
2007                                 rc = (rc2 < 0) ? rc2 :
2008                                      (rc2 == 0) ? -ETIMEDOUT :
2009                                      event.status;
2010
2011                         if (!unlinked) {
2012                                 /* Ensure completion in finite time... */
2013                                 LNetMDUnlink(mdh);
2014                                 /* No assertion (racing with network) */
2015                                 unlinked = 1;
2016                                 timeout_ms = a_long_time;
2017                         } else if (rc2 == 0) {
2018                                 /* timed out waiting for unlink */
2019                                 CWARN("ping %s: late network completion\n",
2020                                       libcfs_id2str(id));
2021                         }
2022                 } else if (event.type == LNET_EVENT_REPLY) {
2023                         replied = 1;
2024                         rc = event.mlength;
2025                 }
2026
2027         } while (rc2 <= 0 || !event.unlinked);
2028
2029         if (!replied) {
2030                 if (rc >= 0)
2031                         CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2032                               libcfs_id2str(id));
2033                 rc = -EIO;
2034                 goto out_1;
2035         }
2036
2037         nob = rc;
2038         LASSERT (nob >= 0 && nob <= infosz);
2039
2040         rc = -EPROTO;                           /* if I can't parse... */
2041
2042         if (nob < 8) {
2043                 /* can't check magic/version */
2044                 CERROR("%s: ping info too short %d\n",
2045                        libcfs_id2str(id), nob);
2046                 goto out_1;
2047         }
2048
2049         if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2050                 lnet_swap_pinginfo(info);
2051         } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2052                 CERROR("%s: Unexpected magic %08x\n", 
2053                        libcfs_id2str(id), info->pi_magic);
2054                 goto out_1;
2055         }
2056
2057         if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
2058                 CERROR("%s: ping w/o NI status: 0x%x\n",
2059                        libcfs_id2str(id), info->pi_features);
2060                 goto out_1;
2061         }
2062
2063         if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2064                 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2065                        nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2066                 goto out_1;
2067         }
2068
2069         if (info->pi_nnis < n_ids)
2070                 n_ids = info->pi_nnis;
2071
2072         if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2073                 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2074                        nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2075                 goto out_1;
2076         }
2077
2078         rc = -EFAULT;                           /* If I SEGV... */
2079
2080         for (i = 0; i < n_ids; i++) {
2081                 tmpid.pid = info->pi_pid;
2082                 tmpid.nid = info->pi_ni[i].ns_nid;
2083                 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2084                         goto out_1;
2085         }
2086         rc = info->pi_nnis;
2087
2088  out_1:
2089         rc2 = LNetEQFree(eqh);
2090         if (rc2 != 0)
2091                 CERROR("rc2 %d\n", rc2);
2092         LASSERT (rc2 == 0);
2093
2094  out_0:
2095         LIBCFS_FREE(info, infosz);
2096         return rc;
2097 }