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