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