Whamcloud - gitweb
2c2f38cfde3ee15553d673c551caa63c9589a73b
[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, 2014, 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 #include <lnet/lib-dlc.h>
40 #ifdef __KERNEL__
41 #include <linux/log2.h>
42 #endif
43
44 #ifdef __KERNEL__
45 #define D_LNI D_CONSOLE
46 #else
47 #define D_LNI D_CONFIG
48 #endif
49
50 lnet_t      the_lnet;                           /* THE state of the network */
51 EXPORT_SYMBOL(the_lnet);
52
53 #ifdef __KERNEL__
54
55 static char *ip2nets = "";
56 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
57                 "LNET network <- IP table");
58
59 static char *networks = "";
60 CFS_MODULE_PARM(networks, "s", charp, 0444,
61                 "local networks");
62
63 static char *routes = "";
64 CFS_MODULE_PARM(routes, "s", charp, 0444,
65                 "routes to non-local networks");
66
67 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
68 CFS_MODULE_PARM(rnet_htable_size, "i", int, 0444,
69                 "size of remote network hash table");
70
71 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
72                      lnet_process_id_t *ids, int n_ids);
73
74 static char *
75 lnet_get_routes(void)
76 {
77         return routes;
78 }
79
80 static char *
81 lnet_get_networks(void)
82 {
83         char   *nets;
84         int     rc;
85
86         if (*networks != 0 && *ip2nets != 0) {
87                 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
88                                    "'ip2nets' but not both at once\n");
89                 return NULL;
90         }
91
92         if (*ip2nets != 0) {
93                 rc = lnet_parse_ip2nets(&nets, ip2nets);
94                 return (rc == 0) ? nets : NULL;
95         }
96
97         if (*networks != 0)
98                 return networks;
99
100         return "tcp";
101 }
102
103 static void
104 lnet_init_locks(void)
105 {
106         spin_lock_init(&the_lnet.ln_eq_wait_lock);
107         init_waitqueue_head(&the_lnet.ln_eq_waitq);
108         init_waitqueue_head(&the_lnet.ln_rc_waitq);
109         mutex_init(&the_lnet.ln_lnd_mutex);
110         mutex_init(&the_lnet.ln_api_mutex);
111 }
112
113 static void
114 lnet_fini_locks(void)
115 {
116 }
117
118 #else
119
120 static char *
121 lnet_get_routes(void)
122 {
123         char *str = getenv("LNET_ROUTES");
124
125         return (str == NULL) ? "" : str;
126 }
127
128 static char *
129 lnet_get_networks (void)
130 {
131         static char       default_networks[256];
132         char             *networks = getenv("LNET_NETWORKS");
133         char             *str;
134         char             *sep;
135         int               len;
136         int               nob;
137         struct list_head *tmp;
138
139         if (networks != NULL)
140                 return networks;
141
142         /* In userland, the default 'networks=' is the list of known net types */
143         len = sizeof(default_networks);
144         str = default_networks;
145         *str = 0;
146         sep = "";
147
148         list_for_each(tmp, &the_lnet.ln_lnds) {
149                 lnd_t *lnd = list_entry(tmp, lnd_t, lnd_list);
150
151                 nob = snprintf(str, len, "%s%s", sep,
152                                libcfs_lnd2str(lnd->lnd_type));
153                 if (nob >= len) {
154                         /* overflowed the string; leave it where it was */
155                         *str = 0;
156                         break;
157                 }
158                 len -= nob;
159                 str += nob;
160                 sep = ",";
161         }
162
163         return default_networks;
164 }
165
166 # ifndef HAVE_LIBPTHREAD
167
168 static void lnet_init_locks(void)
169 {
170         the_lnet.ln_eq_wait_lock = 0;
171         the_lnet.ln_lnd_mutex = 0;
172         the_lnet.ln_api_mutex = 0;
173 }
174
175 static void lnet_fini_locks(void)
176 {
177         LASSERT(the_lnet.ln_api_mutex == 0);
178         LASSERT(the_lnet.ln_lnd_mutex == 0);
179         LASSERT(the_lnet.ln_eq_wait_lock == 0);
180 }
181
182 # else
183
184 static void lnet_init_locks(void)
185 {
186         pthread_cond_init(&the_lnet.ln_eq_cond, NULL);
187         pthread_mutex_init(&the_lnet.ln_eq_wait_lock, NULL);
188         pthread_mutex_init(&the_lnet.ln_lnd_mutex, NULL);
189         pthread_mutex_init(&the_lnet.ln_api_mutex, NULL);
190 }
191
192 static void lnet_fini_locks(void)
193 {
194         pthread_mutex_destroy(&the_lnet.ln_api_mutex);
195         pthread_mutex_destroy(&the_lnet.ln_lnd_mutex);
196         pthread_mutex_destroy(&the_lnet.ln_eq_wait_lock);
197         pthread_cond_destroy(&the_lnet.ln_eq_cond);
198 }
199
200 # endif
201 #endif
202
203 static int
204 lnet_create_remote_nets_table(void)
205 {
206         int               i;
207         struct list_head *hash;
208
209         LASSERT(the_lnet.ln_remote_nets_hash == NULL);
210         LASSERT(the_lnet.ln_remote_nets_hbits > 0);
211         LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
212         if (hash == NULL) {
213                 CERROR("Failed to create remote nets hash table\n");
214                 return -ENOMEM;
215         }
216
217         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
218                 INIT_LIST_HEAD(&hash[i]);
219         the_lnet.ln_remote_nets_hash = hash;
220         return 0;
221 }
222
223 static void
224 lnet_destroy_remote_nets_table(void)
225 {
226         int i;
227
228         if (the_lnet.ln_remote_nets_hash == NULL)
229                 return;
230
231         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
232                 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
233
234         LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
235                     LNET_REMOTE_NETS_HASH_SIZE *
236                     sizeof(the_lnet.ln_remote_nets_hash[0]));
237         the_lnet.ln_remote_nets_hash = NULL;
238 }
239
240 static void
241 lnet_destroy_locks(void)
242 {
243         if (the_lnet.ln_res_lock != NULL) {
244                 cfs_percpt_lock_free(the_lnet.ln_res_lock);
245                 the_lnet.ln_res_lock = NULL;
246         }
247
248         if (the_lnet.ln_net_lock != NULL) {
249                 cfs_percpt_lock_free(the_lnet.ln_net_lock);
250                 the_lnet.ln_net_lock = NULL;
251         }
252
253         lnet_fini_locks();
254 }
255
256 static int
257 lnet_create_locks(void)
258 {
259         lnet_init_locks();
260
261         the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
262         if (the_lnet.ln_res_lock == NULL)
263                 goto failed;
264
265         the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
266         if (the_lnet.ln_net_lock == NULL)
267                 goto failed;
268
269         return 0;
270
271  failed:
272         lnet_destroy_locks();
273         return -ENOMEM;
274 }
275
276 static void lnet_assert_wire_constants(void)
277 {
278         /* Wire protocol assertions generated by 'wirecheck'
279          * running on Linux robert.bartonsoftware.com 2.6.8-1.521
280          * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
281          * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
282
283         /* Constants... */
284         CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
285         CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
286         CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
287         CLASSERT (LNET_MSG_ACK == 0);
288         CLASSERT (LNET_MSG_PUT == 1);
289         CLASSERT (LNET_MSG_GET == 2);
290         CLASSERT (LNET_MSG_REPLY == 3);
291         CLASSERT (LNET_MSG_HELLO == 4);
292
293         /* Checks for struct ptl_handle_wire_t */
294         CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
295         CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
296         CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
297         CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
298         CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
299
300         /* Checks for struct lnet_magicversion_t */
301         CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
302         CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
303         CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
304         CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
305         CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
306         CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
307         CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
308
309         /* Checks for struct lnet_hdr_t */
310         CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
311         CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
312         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
313         CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
314         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
315         CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
316         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
317         CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
318         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
319         CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
320         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
321         CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
322         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
323         CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
324         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
325
326         /* Ack */
327         CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
328         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
329         CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
330         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
331         CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
332         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
333
334         /* Put */
335         CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
336         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
337         CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
338         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
339         CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
340         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
341         CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
342         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
343         CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
344         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
345
346         /* Get */
347         CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
348         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
349         CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
350         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
351         CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
352         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
353         CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
354         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
355         CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
356         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
357
358         /* Reply */
359         CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
360         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
361
362         /* Hello */
363         CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
364         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
365         CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
366         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
367 }
368
369 static lnd_t *
370 lnet_find_lnd_by_type (int type)
371 {
372         lnd_t            *lnd;
373         struct list_head *tmp;
374
375         /* holding lnd mutex */
376         list_for_each(tmp, &the_lnet.ln_lnds) {
377                 lnd = list_entry(tmp, lnd_t, lnd_list);
378
379                 if ((int)lnd->lnd_type == type)
380                         return lnd;
381         }
382         return NULL;
383 }
384
385 void
386 lnet_register_lnd (lnd_t *lnd)
387 {
388         LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
389
390         LASSERT(the_lnet.ln_init);
391         LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
392         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
393
394         list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
395         lnd->lnd_refcount = 0;
396
397         CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
398
399         LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
400 }
401 EXPORT_SYMBOL(lnet_register_lnd);
402
403 void
404 lnet_unregister_lnd (lnd_t *lnd)
405 {
406         LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
407
408         LASSERT(the_lnet.ln_init);
409         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
410         LASSERT(lnd->lnd_refcount == 0);
411
412         list_del(&lnd->lnd_list);
413         CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
414
415         LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
416 }
417 EXPORT_SYMBOL(lnet_unregister_lnd);
418
419 void
420 lnet_counters_get(lnet_counters_t *counters)
421 {
422         lnet_counters_t *ctr;
423         int             i;
424
425         memset(counters, 0, sizeof(*counters));
426
427         lnet_net_lock(LNET_LOCK_EX);
428
429         cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
430                 counters->msgs_max     += ctr->msgs_max;
431                 counters->msgs_alloc   += ctr->msgs_alloc;
432                 counters->errors       += ctr->errors;
433                 counters->send_count   += ctr->send_count;
434                 counters->recv_count   += ctr->recv_count;
435                 counters->route_count  += ctr->route_count;
436                 counters->drop_count   += ctr->drop_count;
437                 counters->send_length  += ctr->send_length;
438                 counters->recv_length  += ctr->recv_length;
439                 counters->route_length += ctr->route_length;
440                 counters->drop_length  += ctr->drop_length;
441
442         }
443         lnet_net_unlock(LNET_LOCK_EX);
444 }
445 EXPORT_SYMBOL(lnet_counters_get);
446
447 void
448 lnet_counters_reset(void)
449 {
450         lnet_counters_t *counters;
451         int             i;
452
453         lnet_net_lock(LNET_LOCK_EX);
454
455         cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
456                 memset(counters, 0, sizeof(lnet_counters_t));
457
458         lnet_net_unlock(LNET_LOCK_EX);
459 }
460 EXPORT_SYMBOL(lnet_counters_reset);
461
462 #ifdef LNET_USE_LIB_FREELIST
463
464 int
465 lnet_freelist_init(lnet_freelist_t *fl, int n, int size)
466 {
467         char *space;
468
469         LASSERT (n > 0);
470
471         size += offsetof (lnet_freeobj_t, fo_contents);
472
473         LIBCFS_ALLOC(space, n * size);
474         if (space == NULL)
475                 return (-ENOMEM);
476
477         INIT_LIST_HEAD(&fl->fl_list);
478         fl->fl_objs = space;
479         fl->fl_nobjs = n;
480         fl->fl_objsize = size;
481
482         do {
483                 list_add((struct list_head *)space, &fl->fl_list);
484                 space += size;
485         } while (--n != 0);
486
487         return 0;
488 }
489
490 void
491 lnet_freelist_fini(lnet_freelist_t *fl)
492 {
493         struct list_head *el;
494         int               count;
495
496         if (fl->fl_nobjs == 0)
497                 return;
498
499         count = 0;
500         for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
501                 count++;
502
503         LASSERT (count == fl->fl_nobjs);
504
505         LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
506         memset (fl, 0, sizeof (*fl));
507 }
508
509 #endif /* LNET_USE_LIB_FREELIST */
510
511 static __u64 lnet_create_interface_cookie(void)
512 {
513         /* NB the interface cookie in wire handles guards against delayed
514          * replies and ACKs appearing valid after reboot. Initialisation time,
515          * even if it's only implemented to millisecond resolution is probably
516          * easily good enough. */
517         struct timeval tv;
518         __u64          cookie;
519 #ifndef __KERNEL__
520         int            rc = gettimeofday (&tv, NULL);
521         LASSERT (rc == 0);
522 #else
523         do_gettimeofday(&tv);
524 #endif
525         cookie = tv.tv_sec;
526         cookie *= 1000000;
527         cookie += tv.tv_usec;
528         return cookie;
529 }
530
531 static char *
532 lnet_res_type2str(int type)
533 {
534         switch (type) {
535         default:
536                 LBUG();
537         case LNET_COOKIE_TYPE_MD:
538                 return "MD";
539         case LNET_COOKIE_TYPE_ME:
540                 return "ME";
541         case LNET_COOKIE_TYPE_EQ:
542                 return "EQ";
543         }
544 }
545
546 static void
547 lnet_res_container_cleanup(struct lnet_res_container *rec)
548 {
549         int     count = 0;
550
551         if (rec->rec_type == 0) /* not set yet, it's uninitialized */
552                 return;
553
554         while (!list_empty(&rec->rec_active)) {
555                 struct list_head *e = rec->rec_active.next;
556
557                 list_del_init(e);
558                 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
559                         lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
560
561                 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
562                         lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
563
564                 } else { /* NB: Active MEs should be attached on portals */
565                         LBUG();
566                 }
567                 count++;
568         }
569
570         if (count > 0) {
571                 /* Found alive MD/ME/EQ, user really should unlink/free
572                  * all of them before finalize LNet, but if someone didn't,
573                  * we have to recycle garbage for him */
574                 CERROR("%d active elements on exit of %s container\n",
575                        count, lnet_res_type2str(rec->rec_type));
576         }
577
578 #ifdef LNET_USE_LIB_FREELIST
579         lnet_freelist_fini(&rec->rec_freelist);
580 #endif
581         if (rec->rec_lh_hash != NULL) {
582                 LIBCFS_FREE(rec->rec_lh_hash,
583                             LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
584                 rec->rec_lh_hash = NULL;
585         }
586
587         rec->rec_type = 0; /* mark it as finalized */
588 }
589
590 static int
591 lnet_res_container_setup(struct lnet_res_container *rec,
592                          int cpt, int type, int objnum, int objsz)
593 {
594         int     rc = 0;
595         int     i;
596
597         LASSERT(rec->rec_type == 0);
598
599         rec->rec_type = type;
600         INIT_LIST_HEAD(&rec->rec_active);
601
602 #ifdef LNET_USE_LIB_FREELIST
603         memset(&rec->rec_freelist, 0, sizeof(rec->rec_freelist));
604         rc = lnet_freelist_init(&rec->rec_freelist, objnum, objsz);
605         if (rc != 0)
606                 goto out;
607 #endif
608         rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
609
610         /* Arbitrary choice of hash table size */
611         LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
612                          LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
613         if (rec->rec_lh_hash == NULL) {
614                 rc = -ENOMEM;
615                 goto out;
616         }
617
618         for (i = 0; i < LNET_LH_HASH_SIZE; i++)
619                 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
620
621         return 0;
622
623 out:
624         CERROR("Failed to setup %s resource container\n",
625                lnet_res_type2str(type));
626         lnet_res_container_cleanup(rec);
627         return rc;
628 }
629
630 static void
631 lnet_res_containers_destroy(struct lnet_res_container **recs)
632 {
633         struct lnet_res_container       *rec;
634         int                             i;
635
636         cfs_percpt_for_each(rec, i, recs)
637                 lnet_res_container_cleanup(rec);
638
639         cfs_percpt_free(recs);
640 }
641
642 static struct lnet_res_container **
643 lnet_res_containers_create(int type, int objnum, int objsz)
644 {
645         struct lnet_res_container       **recs;
646         struct lnet_res_container       *rec;
647         int                             rc;
648         int                             i;
649
650         recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
651         if (recs == NULL) {
652                 CERROR("Failed to allocate %s resource containers\n",
653                        lnet_res_type2str(type));
654                 return NULL;
655         }
656
657         cfs_percpt_for_each(rec, i, recs) {
658                 rc = lnet_res_container_setup(rec, i, type, objnum, objsz);
659                 if (rc != 0) {
660                         lnet_res_containers_destroy(recs);
661                         return NULL;
662                 }
663         }
664
665         return recs;
666 }
667
668 lnet_libhandle_t *
669 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
670 {
671         /* ALWAYS called with lnet_res_lock held */
672         struct list_head        *head;
673         lnet_libhandle_t        *lh;
674         unsigned int            hash;
675
676         if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
677                 return NULL;
678
679         hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
680         head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
681
682         list_for_each_entry(lh, head, lh_hash_chain) {
683                 if (lh->lh_cookie == cookie)
684                         return lh;
685         }
686
687         return NULL;
688 }
689
690 void
691 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
692 {
693         /* ALWAYS called with lnet_res_lock held */
694         unsigned int    ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
695         unsigned int    hash;
696
697         lh->lh_cookie = rec->rec_lh_cookie;
698         rec->rec_lh_cookie += 1 << ibits;
699
700         hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
701
702         list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
703 }
704
705 #ifndef __KERNEL__
706 /**
707  * Reserved API - do not use.
708  * Temporary workaround to allow uOSS and test programs force server
709  * mode in userspace. See comments near ln_server_mode_flag in
710  * lnet/lib-types.h */
711
712 void
713 lnet_server_mode() {
714         the_lnet.ln_server_mode_flag = 1;
715 }
716 #endif
717
718 static int lnet_unprepare(void);
719
720 static int
721 lnet_prepare(lnet_pid_t requested_pid)
722 {
723         /* Prepare to bring up the network */
724         struct lnet_res_container **recs;
725         int                       rc = 0;
726
727         if (requested_pid == LNET_PID_ANY) {
728                 /* Don't instantiate LNET just for me */
729                 return -ENETDOWN;
730         }
731
732         LASSERT(the_lnet.ln_refcount == 0);
733
734         the_lnet.ln_routing = 0;
735
736 #ifdef __KERNEL__
737         LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
738         the_lnet.ln_pid = requested_pid;
739 #else
740         if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
741                 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
742                 /* Only root can run user-space server */
743                 if (current_uid() != 0)
744                         return -EPERM;
745                 the_lnet.ln_pid = requested_pid;
746
747         } else {/* client case (liblustre) */
748                 /* My PID must be unique on this node and flag I'm userspace */
749                 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
750         }
751 #endif
752
753         INIT_LIST_HEAD(&the_lnet.ln_test_peers);
754         INIT_LIST_HEAD(&the_lnet.ln_nis);
755         INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
756         INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
757         INIT_LIST_HEAD(&the_lnet.ln_routers);
758         INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
759         INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
760
761         rc = lnet_create_remote_nets_table();
762         if (rc != 0)
763                 goto failed;
764
765         the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
766
767         the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
768                                                 sizeof(lnet_counters_t));
769         if (the_lnet.ln_counters == NULL) {
770                 CERROR("Failed to allocate counters for LNet\n");
771                 rc = -ENOMEM;
772                 goto failed;
773         }
774
775         rc = lnet_peer_tables_create();
776         if (rc != 0)
777                 goto failed;
778
779         rc = lnet_msg_containers_create();
780         if (rc != 0)
781                 goto failed;
782
783         rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
784                                       LNET_COOKIE_TYPE_EQ, LNET_FL_MAX_EQS,
785                                       sizeof(lnet_eq_t));
786         if (rc != 0)
787                 goto failed;
788
789         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME, LNET_FL_MAX_MES,
790                                           sizeof(lnet_me_t));
791         if (recs == NULL) {
792                 rc = -ENOMEM;
793                 goto failed;
794         }
795
796         the_lnet.ln_me_containers = recs;
797
798         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD, LNET_FL_MAX_MDS,
799                                           sizeof(lnet_libmd_t));
800         if (recs == NULL) {
801                 rc = -ENOMEM;
802                 goto failed;
803         }
804
805         the_lnet.ln_md_containers = recs;
806
807         rc = lnet_portals_create();
808         if (rc != 0) {
809                 CERROR("Failed to create portals for LNet: %d\n", rc);
810                 goto failed;
811         }
812
813         return 0;
814
815  failed:
816         lnet_unprepare();
817         return rc;
818 }
819
820 static int
821 lnet_unprepare (void)
822 {
823         /* NB no LNET_LOCK since this is the last reference.  All LND instances
824          * have shut down already, so it is safe to unlink and free all
825          * descriptors, even those that appear committed to a network op (eg MD
826          * with non-zero pending count) */
827
828         lnet_fail_nid(LNET_NID_ANY, 0);
829
830         LASSERT(the_lnet.ln_refcount == 0);
831         LASSERT(list_empty(&the_lnet.ln_test_peers));
832         LASSERT(list_empty(&the_lnet.ln_nis));
833         LASSERT(list_empty(&the_lnet.ln_nis_cpt));
834         LASSERT(list_empty(&the_lnet.ln_nis_zombie));
835
836         lnet_portals_destroy();
837
838         if (the_lnet.ln_md_containers != NULL) {
839                 lnet_res_containers_destroy(the_lnet.ln_md_containers);
840                 the_lnet.ln_md_containers = NULL;
841         }
842
843         if (the_lnet.ln_me_containers != NULL) {
844                 lnet_res_containers_destroy(the_lnet.ln_me_containers);
845                 the_lnet.ln_me_containers = NULL;
846         }
847
848         lnet_res_container_cleanup(&the_lnet.ln_eq_container);
849
850         lnet_msg_containers_destroy();
851         lnet_peer_tables_destroy();
852         lnet_rtrpools_free(0);
853
854         if (the_lnet.ln_counters != NULL) {
855                 cfs_percpt_free(the_lnet.ln_counters);
856                 the_lnet.ln_counters = NULL;
857         }
858         lnet_destroy_remote_nets_table();
859
860         return 0;
861 }
862
863 lnet_ni_t  *
864 lnet_net2ni_locked(__u32 net, int cpt)
865 {
866         struct list_head *tmp;
867         lnet_ni_t        *ni;
868
869         LASSERT(cpt != LNET_LOCK_EX);
870
871         list_for_each(tmp, &the_lnet.ln_nis) {
872                 ni = list_entry(tmp, lnet_ni_t, ni_list);
873
874                 if (LNET_NIDNET(ni->ni_nid) == net) {
875                         lnet_ni_addref_locked(ni, cpt);
876                         return ni;
877                 }
878         }
879
880         return NULL;
881 }
882
883 lnet_ni_t *
884 lnet_net2ni(__u32 net)
885 {
886         lnet_ni_t *ni;
887
888         lnet_net_lock(0);
889         ni = lnet_net2ni_locked(net, 0);
890         lnet_net_unlock(0);
891
892         return ni;
893 }
894 EXPORT_SYMBOL(lnet_net2ni);
895
896 static unsigned int
897 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
898 {
899         __u64           key = nid;
900         unsigned int    val;
901
902         LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
903
904         if (number == 1)
905                 return 0;
906
907         val = hash_long(key, LNET_CPT_BITS);
908         /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
909         if (val < number)
910                 return val;
911
912         return (unsigned int)(key + val + (val >> 1)) % number;
913 }
914
915 int
916 lnet_cpt_of_nid_locked(lnet_nid_t nid)
917 {
918         struct lnet_ni *ni;
919
920         /* must called with hold of lnet_net_lock */
921         if (LNET_CPT_NUMBER == 1)
922                 return 0; /* the only one */
923
924         /* take lnet_net_lock(any) would be OK */
925         if (!list_empty(&the_lnet.ln_nis_cpt)) {
926                 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
927                         if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
928                                 continue;
929
930                         LASSERT(ni->ni_cpts != NULL);
931                         return ni->ni_cpts[lnet_nid_cpt_hash
932                                            (nid, ni->ni_ncpts)];
933                 }
934         }
935
936         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
937 }
938
939 int
940 lnet_cpt_of_nid(lnet_nid_t nid)
941 {
942         int     cpt;
943         int     cpt2;
944
945         if (LNET_CPT_NUMBER == 1)
946                 return 0; /* the only one */
947
948         if (list_empty(&the_lnet.ln_nis_cpt))
949                 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
950
951         cpt = lnet_net_lock_current();
952         cpt2 = lnet_cpt_of_nid_locked(nid);
953         lnet_net_unlock(cpt);
954
955         return cpt2;
956 }
957 EXPORT_SYMBOL(lnet_cpt_of_nid);
958
959 int
960 lnet_islocalnet(__u32 net)
961 {
962         struct lnet_ni  *ni;
963         int             cpt;
964
965         cpt = lnet_net_lock_current();
966
967         ni = lnet_net2ni_locked(net, cpt);
968         if (ni != NULL)
969                 lnet_ni_decref_locked(ni, cpt);
970
971         lnet_net_unlock(cpt);
972
973         return ni != NULL;
974 }
975
976 lnet_ni_t  *
977 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
978 {
979         struct lnet_ni   *ni;
980         struct list_head *tmp;
981
982         LASSERT(cpt != LNET_LOCK_EX);
983
984         list_for_each(tmp, &the_lnet.ln_nis) {
985                 ni = list_entry(tmp, lnet_ni_t, ni_list);
986
987                 if (ni->ni_nid == nid) {
988                         lnet_ni_addref_locked(ni, cpt);
989                         return ni;
990                 }
991         }
992
993         return NULL;
994 }
995
996 int
997 lnet_islocalnid(lnet_nid_t nid)
998 {
999         struct lnet_ni  *ni;
1000         int             cpt;
1001
1002         cpt = lnet_net_lock_current();
1003         ni = lnet_nid2ni_locked(nid, cpt);
1004         if (ni != NULL)
1005                 lnet_ni_decref_locked(ni, cpt);
1006         lnet_net_unlock(cpt);
1007
1008         return ni != NULL;
1009 }
1010
1011 int
1012 lnet_count_acceptor_nis (void)
1013 {
1014         /* Return the # of NIs that need the acceptor. */
1015         int              count = 0;
1016 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
1017         struct list_head *tmp;
1018         struct lnet_ni   *ni;
1019         int              cpt;
1020
1021         cpt = lnet_net_lock_current();
1022         list_for_each(tmp, &the_lnet.ln_nis) {
1023                 ni = list_entry(tmp, lnet_ni_t, ni_list);
1024
1025                 if (ni->ni_lnd->lnd_accept != NULL)
1026                         count++;
1027         }
1028
1029         lnet_net_unlock(cpt);
1030
1031 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
1032         return count;
1033 }
1034
1035 static lnet_ping_info_t *
1036 lnet_ping_info_create(int num_ni)
1037 {
1038         lnet_ping_info_t *ping_info;
1039         unsigned int     infosz;
1040
1041         infosz = offsetof(lnet_ping_info_t, pi_ni[num_ni]);
1042         LIBCFS_ALLOC(ping_info, infosz);
1043         if (ping_info == NULL) {
1044                 CERROR("Can't allocate ping info[%d]\n", num_ni);
1045                 return NULL;
1046         }
1047
1048         ping_info->pi_nnis = num_ni;
1049         ping_info->pi_pid = the_lnet.ln_pid;
1050         ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
1051         ping_info->pi_features = LNET_PING_FEAT_NI_STATUS;
1052
1053         return ping_info;
1054 }
1055
1056 static inline int
1057 lnet_get_ni_count(void)
1058 {
1059         struct lnet_ni *ni;
1060         int            count = 0;
1061
1062         lnet_net_lock(0);
1063
1064         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list)
1065                 count++;
1066
1067         lnet_net_unlock(0);
1068
1069         return count;
1070 }
1071
1072 static inline void
1073 lnet_ping_info_free(lnet_ping_info_t *pinfo)
1074 {
1075         LIBCFS_FREE(pinfo,
1076                     offsetof(lnet_ping_info_t,
1077                              pi_ni[pinfo->pi_nnis]));
1078 }
1079
1080 static void
1081 lnet_ping_info_destroy(void)
1082 {
1083         struct lnet_ni  *ni;
1084
1085         lnet_net_lock(LNET_LOCK_EX);
1086
1087         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1088                 lnet_ni_lock(ni);
1089                 ni->ni_status = NULL;
1090                 lnet_ni_unlock(ni);
1091         }
1092
1093         lnet_ping_info_free(the_lnet.ln_ping_info);
1094         the_lnet.ln_ping_info = NULL;
1095
1096         lnet_net_unlock(LNET_LOCK_EX);
1097 }
1098
1099 static void
1100 lnet_ping_event_handler(lnet_event_t *event)
1101 {
1102         lnet_ping_info_t *pinfo = event->md.user_ptr;
1103
1104         if (event->unlinked)
1105                 pinfo->pi_features = LNET_PING_FEAT_INVAL;
1106 }
1107
1108 static int
1109 lnet_ping_info_setup(lnet_ping_info_t **ppinfo, lnet_handle_md_t *md_handle,
1110                      int ni_count, bool set_eq)
1111 {
1112         lnet_handle_me_t  me_handle;
1113         lnet_process_id_t id = {LNET_NID_ANY, LNET_PID_ANY};
1114         lnet_md_t         md = {NULL};
1115         int               rc, rc2;
1116
1117         if (set_eq) {
1118                 rc = LNetEQAlloc(0, lnet_ping_event_handler,
1119                                  &the_lnet.ln_ping_target_eq);
1120                 if (rc != 0) {
1121                         CERROR("Can't allocate ping EQ: %d\n", rc);
1122                         return rc;
1123                 }
1124         }
1125
1126         *ppinfo = lnet_ping_info_create(ni_count);
1127         if (*ppinfo == NULL) {
1128                 rc = -ENOMEM;
1129                 goto failed_0;
1130         }
1131
1132         rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1133                           LNET_PROTO_PING_MATCHBITS, 0,
1134                           LNET_UNLINK, LNET_INS_AFTER,
1135                           &me_handle);
1136         if (rc != 0) {
1137                 CERROR("Can't create ping ME: %d\n", rc);
1138                 goto failed_1;
1139         }
1140
1141         /* initialize md content */
1142         md.start     = *ppinfo;
1143         md.length    = offsetof(lnet_ping_info_t,
1144                                 pi_ni[(*ppinfo)->pi_nnis]);
1145         md.threshold = LNET_MD_THRESH_INF;
1146         md.max_size  = 0;
1147         md.options   = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1148                        LNET_MD_MANAGE_REMOTE;
1149         md.user_ptr  = NULL;
1150         md.eq_handle = the_lnet.ln_ping_target_eq;
1151         md.user_ptr = *ppinfo;
1152
1153         rc = LNetMDAttach(me_handle, md, LNET_RETAIN, md_handle);
1154         if (rc != 0) {
1155                 CERROR("Can't attach ping MD: %d\n", rc);
1156                 goto failed_2;
1157         }
1158
1159         return 0;
1160
1161 failed_2:
1162         rc2 = LNetMEUnlink(me_handle);
1163         LASSERT(rc2 == 0);
1164 failed_1:
1165         lnet_ping_info_free(*ppinfo);
1166         *ppinfo = NULL;
1167 failed_0:
1168         if (set_eq)
1169                 LNetEQFree(the_lnet.ln_ping_target_eq);
1170         return rc;
1171 }
1172
1173 static void
1174 lnet_ping_md_unlink(lnet_ping_info_t *pinfo, lnet_handle_md_t *md_handle)
1175 {
1176         sigset_t        blocked = cfs_block_allsigs();
1177
1178         LNetMDUnlink(*md_handle);
1179         LNetInvalidateHandle(md_handle);
1180
1181         /* NB md could be busy; this just starts the unlink */
1182         while (pinfo->pi_features != LNET_PING_FEAT_INVAL) {
1183                 CDEBUG(D_NET, "Still waiting for ping MD to unlink\n");
1184                 cfs_pause(cfs_time_seconds(1));
1185         }
1186
1187         cfs_restore_sigs(blocked);
1188 }
1189
1190 static void
1191 lnet_ping_info_install_locked(lnet_ping_info_t *ping_info)
1192 {
1193         int                     i;
1194         lnet_ni_t               *ni;
1195         lnet_ni_status_t        *ns;
1196
1197         i = 0;
1198         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1199                 LASSERT(i < ping_info->pi_nnis);
1200
1201                 ns = &ping_info->pi_ni[i];
1202
1203                 ns->ns_nid = ni->ni_nid;
1204
1205                 lnet_ni_lock(ni);
1206                 ns->ns_status = (ni->ni_status != NULL) ?
1207                                 ni->ni_status->ns_status : LNET_NI_STATUS_UP;
1208                 ni->ni_status = ns;
1209                 lnet_ni_unlock(ni);
1210
1211                 i++;
1212         }
1213 }
1214
1215 static void
1216 lnet_ping_target_update(lnet_ping_info_t *pinfo, lnet_handle_md_t md_handle)
1217 {
1218         lnet_ping_info_t *old_pinfo = NULL;
1219         lnet_handle_md_t old_md;
1220
1221         /* switch the NIs to point to the new ping info created */
1222         lnet_net_lock(LNET_LOCK_EX);
1223
1224         if (!the_lnet.ln_routing)
1225                 pinfo->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1226         lnet_ping_info_install_locked(pinfo);
1227
1228         if (the_lnet.ln_ping_info != NULL) {
1229                 old_pinfo = the_lnet.ln_ping_info;
1230                 old_md = the_lnet.ln_ping_target_md;
1231         }
1232         the_lnet.ln_ping_target_md = md_handle;
1233         the_lnet.ln_ping_info = pinfo;
1234
1235         lnet_net_unlock(LNET_LOCK_EX);
1236
1237         if (old_pinfo != NULL) {
1238                 /* unlink the old ping info */
1239                 lnet_ping_md_unlink(old_pinfo, &old_md);
1240                 lnet_ping_info_free(old_pinfo);
1241         }
1242 }
1243
1244 static void
1245 lnet_ping_target_fini(void)
1246 {
1247         int             rc;
1248
1249         lnet_ping_md_unlink(the_lnet.ln_ping_info,
1250                             &the_lnet.ln_ping_target_md);
1251
1252         rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1253         LASSERT(rc == 0);
1254
1255         lnet_ping_info_destroy();
1256 }
1257
1258 static int
1259 lnet_ni_tq_credits(lnet_ni_t *ni)
1260 {
1261         int     credits;
1262
1263         LASSERT(ni->ni_ncpts >= 1);
1264
1265         if (ni->ni_ncpts == 1)
1266                 return ni->ni_maxtxcredits;
1267
1268         credits = ni->ni_maxtxcredits / ni->ni_ncpts;
1269         credits = max(credits, 8 * ni->ni_peertxcredits);
1270         credits = min(credits, ni->ni_maxtxcredits);
1271
1272         return credits;
1273 }
1274
1275 static void
1276 lnet_ni_unlink_locked(lnet_ni_t *ni)
1277 {
1278         if (!list_empty(&ni->ni_cptlist)) {
1279                 list_del_init(&ni->ni_cptlist);
1280                 lnet_ni_decref_locked(ni, 0);
1281         }
1282
1283         /* move it to zombie list and nobody can find it anymore */
1284         LASSERT(!list_empty(&ni->ni_list));
1285         list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1286         lnet_ni_decref_locked(ni, 0);   /* drop ln_nis' ref */
1287 }
1288
1289 static void
1290 lnet_clear_zombies_nis_locked(void)
1291 {
1292         int             i;
1293         int             islo;
1294         lnet_ni_t       *ni;
1295
1296         /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
1297          * and shut them down in guaranteed thread context */
1298         i = 2;
1299         while (!list_empty(&the_lnet.ln_nis_zombie)) {
1300                 int     *ref;
1301                 int     j;
1302
1303                 ni = list_entry(the_lnet.ln_nis_zombie.next,
1304                                 lnet_ni_t, ni_list);
1305                 list_del_init(&ni->ni_list);
1306                 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1307                         if (*ref == 0)
1308                                 continue;
1309                         /* still busy, add it back to zombie list */
1310                         list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1311                         break;
1312                 }
1313
1314                 if (!list_empty(&ni->ni_list)) {
1315                         lnet_net_unlock(LNET_LOCK_EX);
1316                         ++i;
1317                         if ((i & (-i)) == i) {
1318                                 CDEBUG(D_WARNING,
1319                                        "Waiting for zombie LNI %s\n",
1320                                        libcfs_nid2str(ni->ni_nid));
1321                         }
1322                         cfs_pause(cfs_time_seconds(1));
1323                         lnet_net_lock(LNET_LOCK_EX);
1324                         continue;
1325                 }
1326
1327                 ni->ni_lnd->lnd_refcount--;
1328                 lnet_net_unlock(LNET_LOCK_EX);
1329
1330                 islo = ni->ni_lnd->lnd_type == LOLND;
1331
1332                 LASSERT(!in_interrupt());
1333                 (ni->ni_lnd->lnd_shutdown)(ni);
1334
1335                 /* can't deref lnd anymore now; it might have unregistered
1336                  * itself...  */
1337
1338                 if (!islo)
1339                         CDEBUG(D_LNI, "Removed LNI %s\n",
1340                               libcfs_nid2str(ni->ni_nid));
1341
1342                 lnet_ni_free(ni);
1343                 i = 2;
1344                 lnet_net_lock(LNET_LOCK_EX);
1345         }
1346 }
1347
1348 static void
1349 lnet_shutdown_lndnis(void)
1350 {
1351         int             i;
1352         lnet_ni_t       *ni;
1353
1354         /* NB called holding the global mutex */
1355
1356         /* All quiet on the API front */
1357         LASSERT(!the_lnet.ln_shutdown);
1358         LASSERT(the_lnet.ln_refcount == 0);
1359         LASSERT(list_empty(&the_lnet.ln_nis_zombie));
1360
1361         lnet_net_lock(LNET_LOCK_EX);
1362         the_lnet.ln_shutdown = 1;       /* flag shutdown */
1363
1364         /* Unlink NIs from the global table */
1365         while (!list_empty(&the_lnet.ln_nis)) {
1366                 ni = list_entry(the_lnet.ln_nis.next,
1367                                 lnet_ni_t, ni_list);
1368                 lnet_ni_unlink_locked(ni);
1369         }
1370
1371         /* Drop the cached eqwait NI. */
1372         if (the_lnet.ln_eq_waitni != NULL) {
1373                 lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
1374                 the_lnet.ln_eq_waitni = NULL;
1375         }
1376
1377         /* Drop the cached loopback NI. */
1378         if (the_lnet.ln_loni != NULL) {
1379                 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1380                 the_lnet.ln_loni = NULL;
1381         }
1382
1383         lnet_net_unlock(LNET_LOCK_EX);
1384
1385         /* Clear lazy portals and drop delayed messages which hold refs
1386          * on their lnet_msg_t::msg_rxpeer */
1387         for (i = 0; i < the_lnet.ln_nportals; i++)
1388                 LNetClearLazyPortal(i);
1389
1390         /* Clear the peer table and wait for all peers to go (they hold refs on
1391          * their NIs) */
1392         lnet_peer_tables_cleanup(NULL);
1393
1394         lnet_net_lock(LNET_LOCK_EX);
1395
1396         lnet_clear_zombies_nis_locked();
1397         the_lnet.ln_shutdown = 0;
1398         lnet_net_unlock(LNET_LOCK_EX);
1399 }
1400
1401 /* shutdown down the NI and release refcount */
1402 static void
1403 lnet_shutdown_lndni(struct lnet_ni *ni)
1404 {
1405         lnet_net_lock(LNET_LOCK_EX);
1406         lnet_ni_unlink_locked(ni);
1407         lnet_net_unlock(LNET_LOCK_EX);
1408
1409         /* Do peer table cleanup for this ni */
1410         lnet_peer_tables_cleanup(ni);
1411
1412         lnet_net_lock(LNET_LOCK_EX);
1413         lnet_clear_zombies_nis_locked();
1414         lnet_net_unlock(LNET_LOCK_EX);
1415 }
1416
1417 static int
1418 lnet_startup_lndni(struct lnet_ni *ni, __s32 peer_timeout,
1419                    __s32 peer_cr, __s32 peer_buf_cr, __s32 credits)
1420 {
1421         int                     rc = -EINVAL;
1422         int                     lnd_type;
1423         lnd_t                   *lnd;
1424         struct lnet_tx_queue    *tq;
1425         int                     i;
1426
1427         lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1428
1429         LASSERT(libcfs_isknown_lnd(lnd_type));
1430
1431         if (lnd_type == CIBLND || lnd_type == OPENIBLND ||
1432             lnd_type == IIBLND || lnd_type == VIBLND) {
1433                 CERROR("LND %s obsoleted\n", libcfs_lnd2str(lnd_type));
1434                 goto failed0;
1435         }
1436
1437         /* Make sure this new NI is unique. */
1438         lnet_net_lock(LNET_LOCK_EX);
1439         rc = lnet_net_unique(LNET_NIDNET(ni->ni_nid), &the_lnet.ln_nis);
1440         lnet_net_unlock(LNET_LOCK_EX);
1441
1442         if (!rc) {
1443                 if (lnd_type == LOLND) {
1444                         lnet_ni_free(ni);
1445                         return 0;
1446                 }
1447
1448                 CERROR("Net %s is not unique\n",
1449                        libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
1450
1451                 rc = -EEXIST;
1452                 goto failed0;
1453         }
1454
1455         LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1456         lnd = lnet_find_lnd_by_type(lnd_type);
1457
1458 #ifdef __KERNEL__
1459         if (lnd == NULL) {
1460                 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1461                 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1462                 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1463
1464                 lnd = lnet_find_lnd_by_type(lnd_type);
1465                 if (lnd == NULL) {
1466                         LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1467                         CERROR("Can't load LND %s, module %s, rc=%d\n",
1468                                libcfs_lnd2str(lnd_type),
1469                                libcfs_lnd2modname(lnd_type), rc);
1470 #ifndef HAVE_MODULE_LOADING_SUPPORT
1471                         LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1472                                            "compiled with kernel module "
1473                                            "loading support.");
1474 #endif
1475                         rc = -EINVAL;
1476                         goto failed0;
1477                 }
1478         }
1479 #else
1480         if (lnd == NULL) {
1481                 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1482                 CERROR("LND %s not supported\n",
1483                         libcfs_lnd2str(lnd_type));
1484                 goto failed0;
1485         }
1486 #endif
1487
1488         lnet_net_lock(LNET_LOCK_EX);
1489         lnd->lnd_refcount++;
1490         lnet_net_unlock(LNET_LOCK_EX);
1491
1492         ni->ni_lnd = lnd;
1493
1494         rc = (lnd->lnd_startup)(ni);
1495
1496         LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1497
1498         if (rc != 0) {
1499                 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1500                                    rc, libcfs_lnd2str(lnd->lnd_type));
1501                 lnet_net_lock(LNET_LOCK_EX);
1502                 lnd->lnd_refcount--;
1503                 lnet_net_unlock(LNET_LOCK_EX);
1504                 goto failed0;
1505         }
1506
1507         /* If given some LND tunable parameters, parse those now to
1508          * override the values in the NI structure. */
1509         if (peer_buf_cr >= 0)
1510                 ni->ni_peerrtrcredits = peer_buf_cr;
1511         if (peer_timeout >= 0)
1512                 ni->ni_peertimeout = peer_timeout;
1513         /*
1514          * TODO
1515          * Note: For now, don't allow the user to change
1516          * peertxcredits as this number is used in the
1517          * IB LND to control queue depth.
1518          * if (peer_cr != -1)
1519          *      ni->ni_peertxcredits = peer_cr;
1520          */
1521         if (credits >= 0)
1522                 ni->ni_maxtxcredits = credits;
1523
1524         LASSERT(ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1525
1526         lnet_net_lock(LNET_LOCK_EX);
1527         /* refcount for ln_nis */
1528         lnet_ni_addref_locked(ni, 0);
1529         list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1530         if (ni->ni_cpts != NULL) {
1531                 lnet_ni_addref_locked(ni, 0);
1532                 list_add_tail(&ni->ni_cptlist, &the_lnet.ln_nis_cpt);
1533         }
1534
1535         lnet_net_unlock(LNET_LOCK_EX);
1536
1537         if (lnd->lnd_type == LOLND) {
1538                 lnet_ni_addref(ni);
1539                 LASSERT(the_lnet.ln_loni == NULL);
1540                 the_lnet.ln_loni = ni;
1541                 return 0;
1542         }
1543
1544 #ifndef __KERNEL__
1545         if (lnd->lnd_wait != NULL) {
1546                 if (the_lnet.ln_eq_waitni == NULL) {
1547                         lnet_ni_addref(ni);
1548                         the_lnet.ln_eq_waitni = ni;
1549                 }
1550         } else {
1551 # ifndef HAVE_LIBPTHREAD
1552                 LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1553                                         "single-threaded runtime\n",
1554                                         libcfs_lnd2str(lnd_type));
1555                 /* shutdown the NI since if we get here then it must've already
1556                  * been started
1557                  */
1558                 lnet_shutdown_lndni(ni);
1559                 return -EINVAL;
1560 # endif
1561         }
1562 #endif
1563         if (ni->ni_peertxcredits == 0 || ni->ni_maxtxcredits == 0) {
1564                 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1565                                    libcfs_lnd2str(lnd->lnd_type),
1566                                    ni->ni_peertxcredits == 0 ?
1567                                         "" : "per-peer ");
1568                 /* shutdown the NI since if we get here then it must've already
1569                  * been started
1570                  */
1571                 lnet_shutdown_lndni(ni);
1572                 return -EINVAL;
1573         }
1574
1575         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1576                 tq->tq_credits_min =
1577                 tq->tq_credits_max =
1578                 tq->tq_credits = lnet_ni_tq_credits(ni);
1579         }
1580
1581         CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1582                 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1583                 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1584                 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1585
1586         return 0;
1587 failed0:
1588         lnet_ni_free(ni);
1589         return rc;
1590 }
1591
1592 static int
1593 lnet_startup_lndnis(struct list_head *nilist)
1594 {
1595         struct lnet_ni          *ni;
1596         int                     rc;
1597         int                     lnd_type;
1598         int                     ni_count = 0;
1599
1600         while (!list_empty(nilist)) {
1601                 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
1602                 list_del(&ni->ni_list);
1603                 rc = lnet_startup_lndni(ni, -1, -1, -1, -1);
1604
1605                 if (rc < 0)
1606                         goto failed;
1607
1608                 ni_count++;
1609         }
1610
1611         if (the_lnet.ln_eq_waitni != NULL && ni_count > 1) {
1612                 lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1613                 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1614                                    "\n",
1615                                    libcfs_lnd2str(lnd_type));
1616                 rc = -EINVAL;
1617                 goto failed;
1618         }
1619
1620         return ni_count;
1621 failed:
1622         lnet_shutdown_lndnis();
1623
1624         return rc;
1625 }
1626
1627 /**
1628  * Initialize LNet library.
1629  *
1630  * Only userspace program needs to call this function - it's automatically
1631  * called in the kernel at module loading time. Caller has to call LNetFini()
1632  * after a call to LNetInit(), if and only if the latter returned 0. It must
1633  * be called exactly once.
1634  *
1635  * \return 0 on success, and -ve on failures.
1636  */
1637 int
1638 LNetInit(void)
1639 {
1640         int     rc;
1641
1642         lnet_assert_wire_constants();
1643         LASSERT(!the_lnet.ln_init);
1644
1645         memset(&the_lnet, 0, sizeof(the_lnet));
1646
1647         /* refer to global cfs_cpt_table for now */
1648         the_lnet.ln_cpt_table   = cfs_cpt_table;
1649         the_lnet.ln_cpt_number  = cfs_cpt_number(cfs_cpt_table);
1650
1651         LASSERT(the_lnet.ln_cpt_number > 0);
1652         if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1653                 /* we are under risk of consuming all lh_cookie */
1654                 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1655                        "please change setting of CPT-table and retry\n",
1656                        the_lnet.ln_cpt_number, LNET_CPT_MAX);
1657                 return -1;
1658         }
1659
1660         while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1661                 the_lnet.ln_cpt_bits++;
1662
1663         rc = lnet_create_locks();
1664         if (rc != 0) {
1665                 CERROR("Can't create LNet global locks: %d\n", rc);
1666                 return -1;
1667         }
1668
1669         the_lnet.ln_refcount = 0;
1670         the_lnet.ln_init = 1;
1671         LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1672         INIT_LIST_HEAD(&the_lnet.ln_lnds);
1673         INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1674         INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1675
1676 #ifdef __KERNEL__
1677         /* The hash table size is the number of bits it takes to express the set
1678          * ln_num_routes, minus 1 (better to under estimate than over so we
1679          * don't waste memory). */
1680         if (rnet_htable_size <= 0)
1681                 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1682         else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1683                 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1684         the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1685                                            order_base_2(rnet_htable_size) - 1);
1686
1687         /* All LNDs apart from the LOLND are in separate modules.  They
1688          * register themselves when their module loads, and unregister
1689          * themselves when their module is unloaded. */
1690 #else
1691         the_lnet.ln_remote_nets_hbits = 8;
1692
1693         /* Register LNDs
1694          * NB the order here determines default 'networks=' order */
1695 # ifdef HAVE_LIBPTHREAD
1696         LNET_REGISTER_ULND(the_tcplnd);
1697 # endif
1698 #endif
1699         lnet_register_lnd(&the_lolnd);
1700         return 0;
1701 }
1702 EXPORT_SYMBOL(LNetInit);
1703
1704 /**
1705  * Finalize LNet library.
1706  *
1707  * Only userspace program needs to call this function. It can be called
1708  * at most once.
1709  *
1710  * \pre LNetInit() called with success.
1711  * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1712  */
1713 void
1714 LNetFini(void)
1715 {
1716         LASSERT(the_lnet.ln_init);
1717         LASSERT(the_lnet.ln_refcount == 0);
1718
1719         while (!list_empty(&the_lnet.ln_lnds))
1720                 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1721                                                lnd_t, lnd_list));
1722         lnet_destroy_locks();
1723
1724         the_lnet.ln_init = 0;
1725 }
1726 EXPORT_SYMBOL(LNetFini);
1727
1728 /**
1729  * Set LNet PID and start LNet interfaces, routing, and forwarding.
1730  *
1731  * Userspace program should call this after a successful call to LNetInit().
1732  * Users must call this function at least once before any other functions.
1733  * For each successful call there must be a corresponding call to
1734  * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1735  * ignored.
1736  *
1737  * The PID used by LNet may be different from the one requested.
1738  * See LNetGetId().
1739  *
1740  * \param requested_pid PID requested by the caller.
1741  *
1742  * \return >= 0 on success, and < 0 error code on failures.
1743  */
1744 int
1745 LNetNIInit(lnet_pid_t requested_pid)
1746 {
1747         int                     im_a_router = 0;
1748         int                     rc;
1749         int                     ni_count;
1750         lnet_ping_info_t        *pinfo;
1751         lnet_handle_md_t        md_handle;
1752         struct list_head        net_head;
1753
1754         INIT_LIST_HEAD(&net_head);
1755
1756         LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1757
1758         LASSERT(the_lnet.ln_init);
1759         CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1760
1761         if (the_lnet.ln_refcount > 0) {
1762                 rc = the_lnet.ln_refcount++;
1763                 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1764                 return rc;
1765         }
1766
1767         rc = lnet_prepare(requested_pid);
1768         if (rc != 0) {
1769                 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1770                 return rc;
1771         }
1772
1773         /* Add in the loopback network */
1774         if (lnet_ni_alloc(LNET_MKNET(LOLND, 0), NULL, &net_head) == NULL) {
1775                 rc = -ENOMEM;
1776                 goto failed0;
1777         }
1778
1779         /* If LNet is being initialized via DLC it is possible
1780          * that the user requests not to load module parameters (ones which
1781          * are supported by DLC) on initialization.  Therefore, make sure not
1782          * to load networks, routes and forwarding from module parameters
1783          * in this case.  On cleanup in case of failure only clean up
1784          * routes if it has been loaded */
1785         if (!the_lnet.ln_nis_from_mod_params) {
1786                 rc = lnet_parse_networks(&net_head,
1787                                          lnet_get_networks());
1788                 if (rc < 0)
1789                         goto failed0;
1790         }
1791
1792         ni_count = lnet_startup_lndnis(&net_head);
1793         if (ni_count < 0) {
1794                 rc = ni_count;
1795                 goto failed0;
1796         }
1797
1798         if (!the_lnet.ln_nis_from_mod_params) {
1799                 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1800                 if (rc != 0)
1801                         goto failed1;
1802
1803                 rc = lnet_check_routes();
1804                 if (rc != 0)
1805                         goto failed2;
1806
1807                 rc = lnet_rtrpools_alloc(im_a_router);
1808                 if (rc != 0)
1809                         goto failed2;
1810         }
1811
1812         rc = lnet_acceptor_start();
1813         if (rc != 0)
1814                 goto failed2;
1815         the_lnet.ln_refcount = 1;
1816         /* Now I may use my own API functions... */
1817
1818         rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
1819         if (rc != 0)
1820                 goto failed3;
1821
1822         lnet_ping_target_update(pinfo, md_handle);
1823
1824         rc = lnet_router_checker_start();
1825         if (rc != 0)
1826                 goto failed4;
1827
1828         lnet_fault_init();
1829         lnet_proc_init();
1830
1831         LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1832
1833         return 0;
1834
1835 failed4:
1836         lnet_ping_target_fini();
1837 failed3:
1838         the_lnet.ln_refcount = 0;
1839         lnet_acceptor_stop();
1840 failed2:
1841         if (!the_lnet.ln_nis_from_mod_params)
1842                 lnet_destroy_routes();
1843 failed1:
1844         lnet_shutdown_lndnis();
1845 failed0:
1846         lnet_unprepare();
1847         LASSERT(rc < 0);
1848         LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1849         while (!list_empty(&net_head)) {
1850                 struct lnet_ni *ni;
1851                 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1852                 list_del_init(&ni->ni_list);
1853                 lnet_ni_free(ni);
1854         }
1855         return rc;
1856 }
1857 EXPORT_SYMBOL(LNetNIInit);
1858
1859 /**
1860  * Stop LNet interfaces, routing, and forwarding.
1861  *
1862  * Users must call this function once for each successful call to LNetNIInit().
1863  * Once the LNetNIFini() operation has been started, the results of pending
1864  * API operations are undefined.
1865  *
1866  * \return always 0 for current implementation.
1867  */
1868 int
1869 LNetNIFini()
1870 {
1871         LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1872
1873         LASSERT (the_lnet.ln_init);
1874         LASSERT (the_lnet.ln_refcount > 0);
1875
1876         if (the_lnet.ln_refcount != 1) {
1877                 the_lnet.ln_refcount--;
1878         } else {
1879                 LASSERT(!the_lnet.ln_niinit_self);
1880
1881                 lnet_fault_fini();
1882
1883                 lnet_proc_fini();
1884                 lnet_router_checker_stop();
1885                 lnet_ping_target_fini();
1886
1887                 /* Teardown fns that use my own API functions BEFORE here */
1888                 the_lnet.ln_refcount = 0;
1889
1890                 lnet_acceptor_stop();
1891                 lnet_destroy_routes();
1892                 lnet_shutdown_lndnis();
1893                 lnet_unprepare();
1894         }
1895
1896         LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1897         return 0;
1898 }
1899 EXPORT_SYMBOL(LNetNIFini);
1900
1901 /**
1902  * Grabs the ni data from the ni structure and fills the out
1903  * parameters
1904  *
1905  * \param[in] ni network        interface structure
1906  * \param[out] cpt_count        the number of cpts the ni is on
1907  * \param[out] nid              Network Interface ID
1908  * \param[out] peer_timeout     NI peer timeout
1909  * \param[out] peer_tx_crdits   NI peer transmit credits
1910  * \param[out] peer_rtr_credits NI peer router credits
1911  * \param[out] max_tx_credits   NI max transmit credit
1912  * \param[out] net_config       Network configuration
1913  */
1914 static void
1915 lnet_fill_ni_info(struct lnet_ni *ni, __u32 *cpt_count, __u64 *nid,
1916                   int *peer_timeout, int *peer_tx_credits,
1917                   int *peer_rtr_credits, int *max_tx_credits,
1918                   struct lnet_ioctl_net_config *net_config)
1919 {
1920         int i;
1921
1922         if (ni == NULL)
1923                 return;
1924
1925         if (net_config == NULL)
1926                 return;
1927
1928         CLASSERT(ARRAY_SIZE(ni->ni_interfaces) ==
1929                  ARRAY_SIZE(net_config->ni_interfaces));
1930
1931         if (ni->ni_interfaces[0] != NULL) {
1932                 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
1933                         if (ni->ni_interfaces[i] != NULL) {
1934                                 strncpy(net_config->ni_interfaces[i],
1935                                         ni->ni_interfaces[i],
1936                                         sizeof(net_config->ni_interfaces[i]));
1937                         }
1938                 }
1939         }
1940
1941         *nid = ni->ni_nid;
1942         *peer_timeout = ni->ni_peertimeout;
1943         *peer_tx_credits = ni->ni_peertxcredits;
1944         *peer_rtr_credits = ni->ni_peerrtrcredits;
1945         *max_tx_credits = ni->ni_maxtxcredits;
1946
1947         net_config->ni_status = ni->ni_status->ns_status;
1948
1949         for (i = 0;
1950              ni->ni_cpts != NULL && i < ni->ni_ncpts &&
1951              i < LNET_MAX_SHOW_NUM_CPT;
1952              i++)
1953                 net_config->ni_cpts[i] = ni->ni_cpts[i];
1954
1955         *cpt_count = ni->ni_ncpts;
1956 }
1957
1958 int
1959 lnet_get_net_config(int idx, __u32 *cpt_count, __u64 *nid, int *peer_timeout,
1960                     int *peer_tx_credits, int *peer_rtr_credits,
1961                     int *max_tx_credits,
1962                     struct lnet_ioctl_net_config *net_config)
1963 {
1964         struct lnet_ni          *ni;
1965         struct list_head        *tmp;
1966         int                     cpt;
1967         int                     rc = -ENOENT;
1968
1969         cpt = lnet_net_lock_current();
1970
1971         list_for_each(tmp, &the_lnet.ln_nis) {
1972                 ni = list_entry(tmp, lnet_ni_t, ni_list);
1973                 if (idx-- == 0) {
1974                         rc = 0;
1975                         lnet_ni_lock(ni);
1976                         lnet_fill_ni_info(ni, cpt_count, nid, peer_timeout,
1977                                           peer_tx_credits, peer_rtr_credits,
1978                                           max_tx_credits, net_config);
1979                         lnet_ni_unlock(ni);
1980                         break;
1981                 }
1982         }
1983
1984         lnet_net_unlock(cpt);
1985         return rc;
1986 }
1987
1988 int
1989 lnet_dyn_add_ni(lnet_pid_t requested_pid, char *nets,
1990                 __s32 peer_timeout, __s32 peer_cr, __s32 peer_buf_cr,
1991                 __s32 credits)
1992 {
1993         lnet_ping_info_t        *pinfo;
1994         lnet_handle_md_t        md_handle;
1995         struct lnet_ni          *ni;
1996         struct list_head        net_head;
1997         int                     rc;
1998         lnet_remotenet_t        *rnet;
1999
2000         INIT_LIST_HEAD(&net_head);
2001
2002         /* Create a ni structure for the network string */
2003         rc = lnet_parse_networks(&net_head, nets);
2004         if (rc <= 0)
2005                 return rc == 0 ? -EINVAL : rc;
2006
2007         LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2008
2009         if (rc > 1) {
2010                 rc = -EINVAL; /* only add one interface per call */
2011                 goto failed0;
2012         }
2013
2014         ni = list_entry(net_head.next, struct lnet_ni, ni_list);
2015
2016         lnet_net_lock(LNET_LOCK_EX);
2017         rnet = lnet_find_net_locked(LNET_NIDNET(ni->ni_nid));
2018         lnet_net_unlock(LNET_LOCK_EX);
2019         /* make sure that the net added doesn't invalidate the current
2020          * configuration LNet is keeping */
2021         if (rnet != NULL) {
2022                 CERROR("Adding net %s will invalidate routing configuration\n",
2023                        nets);
2024                 rc = -EUSERS;
2025                 goto failed0;
2026         }
2027
2028         rc = lnet_ping_info_setup(&pinfo, &md_handle, 1 + lnet_get_ni_count(),
2029                                   false);
2030         if (rc != 0)
2031                 goto failed0;
2032
2033         list_del_init(&ni->ni_list);
2034
2035         rc = lnet_startup_lndni(ni, peer_timeout, peer_cr,
2036                                 peer_buf_cr, credits);
2037         if (rc != 0)
2038                 goto failed1;
2039
2040         if (ni->ni_lnd->lnd_accept != NULL) {
2041                 rc = lnet_acceptor_start();
2042                 if (rc < 0) {
2043                         /* shutdown the ni that we just started */
2044                         CERROR("Failed to start up acceptor thread\n");
2045                         lnet_shutdown_lndni(ni);
2046                         goto failed1;
2047                 }
2048         }
2049
2050         lnet_ping_target_update(pinfo, md_handle);
2051         LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2052
2053         return 0;
2054
2055 failed1:
2056         lnet_ping_md_unlink(pinfo, &md_handle);
2057         lnet_ping_info_free(pinfo);
2058 failed0:
2059         LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2060         while (!list_empty(&net_head)) {
2061                 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
2062                 list_del_init(&ni->ni_list);
2063                 lnet_ni_free(ni);
2064         }
2065         return rc;
2066 }
2067
2068 int
2069 lnet_dyn_del_ni(__u32 net)
2070 {
2071         lnet_ni_t        *ni;
2072         lnet_ping_info_t *pinfo;
2073         lnet_handle_md_t  md_handle;
2074         int               rc;
2075
2076         /* don't allow userspace to shutdown the LOLND */
2077         if (LNET_NETTYP(net) == LOLND)
2078                 return -EINVAL;
2079
2080         LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2081         /* create and link a new ping info, before removing the old one */
2082         rc = lnet_ping_info_setup(&pinfo, &md_handle,
2083                                   lnet_get_ni_count() - 1, false);
2084         if (rc != 0)
2085                 goto out;
2086
2087         ni = lnet_net2ni(net);
2088         if (ni == NULL) {
2089                 rc = -EINVAL;
2090                 goto failed;
2091         }
2092
2093         /* decrement the reference counter taken by lnet_net2ni() */
2094         lnet_ni_decref_locked(ni, 0);
2095
2096         lnet_shutdown_lndni(ni);
2097
2098         if (lnet_count_acceptor_nis() == 0)
2099                 lnet_acceptor_stop();
2100
2101         lnet_ping_target_update(pinfo, md_handle);
2102         goto out;
2103 failed:
2104         lnet_ping_md_unlink(pinfo, &md_handle);
2105         lnet_ping_info_free(pinfo);
2106 out:
2107         LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2108
2109         return rc;
2110 }
2111
2112 /**
2113  * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
2114  * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
2115  * internal ioctl handler.
2116  *
2117  * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
2118  *
2119  * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
2120  * The data will be printed to system console. Don't use it excessively.
2121  * \param arg A pointer to lnet_process_id_t, process ID of the peer.
2122  *
2123  * \return Always return 0 when called by users directly (i.e., not via ioctl).
2124  */
2125 int
2126 LNetCtl(unsigned int cmd, void *arg)
2127 {
2128         struct libcfs_ioctl_data *data = arg;
2129         struct lnet_ioctl_config_data *config;
2130         lnet_process_id_t         id = {0};
2131         lnet_ni_t                *ni;
2132         int                       rc;
2133
2134         CLASSERT(LIBCFS_IOC_DATA_MAX >= sizeof(struct lnet_ioctl_net_config) +
2135                                         sizeof(struct lnet_ioctl_config_data));
2136         LASSERT(the_lnet.ln_init);
2137
2138         switch (cmd) {
2139         case IOC_LIBCFS_GET_NI:
2140                 rc = LNetGetId(data->ioc_count, &id);
2141                 data->ioc_nid = id.nid;
2142                 return rc;
2143
2144         case IOC_LIBCFS_FAIL_NID:
2145                 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
2146
2147         case IOC_LIBCFS_ADD_ROUTE:
2148                 config = arg;
2149
2150                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2151                         return -EINVAL;
2152
2153                 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2154                 rc = lnet_add_route(config->cfg_net,
2155                                     config->cfg_config_u.cfg_route.rtr_hop,
2156                                     config->cfg_nid,
2157                                     config->cfg_config_u.cfg_route.
2158                                         rtr_priority);
2159                 if (rc == 0) {
2160                         rc = lnet_check_routes();
2161                         if (rc != 0)
2162                                 lnet_del_route(config->cfg_net,
2163                                                config->cfg_nid);
2164                 }
2165                 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2166                 return rc;
2167
2168         case IOC_LIBCFS_DEL_ROUTE:
2169                 config = arg;
2170
2171                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2172                         return -EINVAL;
2173
2174                 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2175                 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
2176                 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2177                 return rc;
2178
2179         case IOC_LIBCFS_GET_ROUTE:
2180                 config = arg;
2181
2182                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2183                         return -EINVAL;
2184
2185                 return lnet_get_route(config->cfg_count,
2186                                       &config->cfg_net,
2187                                       &config->cfg_config_u.cfg_route.rtr_hop,
2188                                       &config->cfg_nid,
2189                                       &config->cfg_config_u.cfg_route.rtr_flags,
2190                                       &config->cfg_config_u.cfg_route.
2191                                         rtr_priority);
2192
2193         case IOC_LIBCFS_GET_NET: {
2194                 struct lnet_ioctl_net_config *net_config;
2195                 size_t total = sizeof(*config) + sizeof(*net_config);
2196
2197                 config = arg;
2198
2199                 if (config->cfg_hdr.ioc_len < total)
2200                         return -EINVAL;
2201
2202                 net_config = (struct lnet_ioctl_net_config *)
2203                         config->cfg_bulk;
2204                 if (config == NULL || net_config == NULL)
2205                         return -1;
2206
2207                 return lnet_get_net_config(config->cfg_count,
2208                                            &config->cfg_ncpts,
2209                                            &config->cfg_nid,
2210                                            &config->cfg_config_u.
2211                                                 cfg_net.net_peer_timeout,
2212                                            &config->cfg_config_u.cfg_net.
2213                                                 net_peer_tx_credits,
2214                                            &config->cfg_config_u.cfg_net.
2215                                                 net_peer_rtr_credits,
2216                                            &config->cfg_config_u.cfg_net.
2217                                                 net_max_tx_credits,
2218                                            net_config);
2219         }
2220
2221         case IOC_LIBCFS_GET_LNET_STATS:
2222         {
2223                 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
2224
2225                 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
2226                         return -EINVAL;
2227
2228                 lnet_counters_get(&lnet_stats->st_cntrs);
2229                 return 0;
2230         }
2231
2232 #if defined(__KERNEL__) && defined(LNET_ROUTER)
2233         case IOC_LIBCFS_CONFIG_RTR:
2234                 config = arg;
2235
2236                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2237                         return -EINVAL;
2238
2239                 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2240                 if (config->cfg_config_u.cfg_buffers.buf_enable) {
2241                         rc = lnet_rtrpools_enable();
2242                         LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2243                         return rc;
2244                 }
2245                 lnet_rtrpools_disable();
2246                 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2247                 return 0;
2248
2249         case IOC_LIBCFS_ADD_BUF:
2250                 config = arg;
2251
2252                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2253                         return -EINVAL;
2254
2255                 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
2256                 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
2257                                                 buf_tiny,
2258                                           config->cfg_config_u.cfg_buffers.
2259                                                 buf_small,
2260                                           config->cfg_config_u.cfg_buffers.
2261                                                 buf_large);
2262                 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2263                 return rc;
2264 #endif
2265
2266         case IOC_LIBCFS_GET_BUF: {
2267                 struct lnet_ioctl_pool_cfg *pool_cfg;
2268                 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2269
2270                 config = arg;
2271
2272                 if (config->cfg_hdr.ioc_len < total)
2273                         return -EINVAL;
2274
2275                 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2276                 return lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2277         }
2278
2279         case IOC_LIBCFS_GET_PEER_INFO: {
2280                 struct lnet_ioctl_peer *peer_info = arg;
2281
2282                 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2283                         return -EINVAL;
2284
2285                 return lnet_get_peer_info(
2286                    peer_info->pr_count,
2287                    &peer_info->pr_nid,
2288                    peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2289                    &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2290                    &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2291                    &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2292                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2293                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2294                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_rtr_credits,
2295                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2296         }
2297
2298         case IOC_LIBCFS_NOTIFY_ROUTER:
2299                 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2300                                    cfs_time_current() -
2301                                    cfs_time_seconds(cfs_time_current_sec() -
2302                                                     (time_t)data->ioc_u64[0]));
2303
2304         case IOC_LIBCFS_PORTALS_COMPATIBILITY:
2305                 /* This can be removed once lustre stops calling it */
2306                 return 0;
2307
2308         case IOC_LIBCFS_LNET_DIST:
2309                 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2310                 if (rc < 0 && rc != -EHOSTUNREACH)
2311                         return rc;
2312
2313                 data->ioc_u32[0] = rc;
2314                 return 0;
2315
2316         case IOC_LIBCFS_TESTPROTOCOMPAT:
2317                 lnet_net_lock(LNET_LOCK_EX);
2318                 the_lnet.ln_testprotocompat = data->ioc_flags;
2319                 lnet_net_unlock(LNET_LOCK_EX);
2320                 return 0;
2321
2322         case IOC_LIBCFS_LNET_FAULT:
2323                 return lnet_fault_ctl(data->ioc_flags, data);
2324
2325         case IOC_LIBCFS_PING:
2326                 id.nid = data->ioc_nid;
2327                 id.pid = data->ioc_u32[0];
2328                 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
2329                                (lnet_process_id_t __user *)data->ioc_pbuf1,
2330                                data->ioc_plen1/sizeof(lnet_process_id_t));
2331                 if (rc < 0)
2332                         return rc;
2333                 data->ioc_count = rc;
2334                 return 0;
2335
2336         case IOC_LIBCFS_DEBUG_PEER: {
2337                 /* CAVEAT EMPTOR: this one designed for calling directly; not
2338                  * via an ioctl */
2339                 id = *((lnet_process_id_t *) arg);
2340
2341                 lnet_debug_peer(id.nid);
2342
2343                 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2344                 if (ni == NULL) {
2345                         CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
2346                 } else {
2347                         if (ni->ni_lnd->lnd_ctl == NULL) {
2348                                 CDEBUG(D_WARNING, "No ctl for %s\n",
2349                                        libcfs_id2str(id));
2350                         } else {
2351                                 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2352                         }
2353
2354                         lnet_ni_decref(ni);
2355                 }
2356                 return 0;
2357         }
2358
2359         default:
2360                 ni = lnet_net2ni(data->ioc_net);
2361                 if (ni == NULL)
2362                         return -EINVAL;
2363
2364                 if (ni->ni_lnd->lnd_ctl == NULL)
2365                         rc = -EINVAL;
2366                 else
2367                         rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2368
2369                 lnet_ni_decref(ni);
2370                 return rc;
2371         }
2372         /* not reached */
2373 }
2374 EXPORT_SYMBOL(LNetCtl);
2375
2376 /**
2377  * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
2378  * all interfaces share a same PID, as requested by LNetNIInit().
2379  *
2380  * \param index Index of the interface to look up.
2381  * \param id On successful return, this location will hold the
2382  * lnet_process_id_t ID of the interface.
2383  *
2384  * \retval 0 If an interface exists at \a index.
2385  * \retval -ENOENT If no interface has been found.
2386  */
2387 int
2388 LNetGetId(unsigned int index, lnet_process_id_t *id)
2389 {
2390         struct lnet_ni   *ni;
2391         struct list_head *tmp;
2392         int               cpt;
2393         int               rc = -ENOENT;
2394
2395         LASSERT(the_lnet.ln_init);
2396         LASSERT(the_lnet.ln_refcount > 0);
2397
2398         cpt = lnet_net_lock_current();
2399
2400         list_for_each(tmp, &the_lnet.ln_nis) {
2401                 if (index-- != 0)
2402                         continue;
2403
2404                 ni = list_entry(tmp, lnet_ni_t, ni_list);
2405
2406                 id->nid = ni->ni_nid;
2407                 id->pid = the_lnet.ln_pid;
2408                 rc = 0;
2409                 break;
2410         }
2411
2412         lnet_net_unlock(cpt);
2413         return rc;
2414 }
2415 EXPORT_SYMBOL(LNetGetId);
2416
2417 /**
2418  * Print a string representation of handle \a h into buffer \a str of
2419  * \a len bytes.
2420  */
2421 void
2422 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
2423 {
2424         snprintf(str, len, LPX64, h.cookie);
2425 }
2426 EXPORT_SYMBOL(LNetSnprintHandle);
2427
2428 static int
2429 lnet_ping(lnet_process_id_t id, int timeout_ms, lnet_process_id_t __user *ids,
2430           int n_ids)
2431 {
2432         lnet_handle_eq_t     eqh;
2433         lnet_handle_md_t     mdh;
2434         lnet_event_t         event;
2435         lnet_md_t            md = { NULL };
2436         int                  which;
2437         int                  unlinked = 0;
2438         int                  replied = 0;
2439         const int            a_long_time = 60000; /* mS */
2440         int                  infosz;
2441         lnet_ping_info_t    *info;
2442         lnet_process_id_t    tmpid;
2443         int                  i;
2444         int                  nob;
2445         int                  rc;
2446         int                  rc2;
2447         sigset_t         blocked;
2448
2449         infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
2450
2451         if (n_ids <= 0 ||
2452             id.nid == LNET_NID_ANY ||
2453             timeout_ms > 500000 ||              /* arbitrary limit! */
2454             n_ids > 20)                         /* arbitrary limit! */
2455                 return -EINVAL;
2456
2457         if (id.pid == LNET_PID_ANY)
2458                 id.pid = LNET_PID_LUSTRE;
2459
2460         LIBCFS_ALLOC(info, infosz);
2461         if (info == NULL)
2462                 return -ENOMEM;
2463
2464         /* NB 2 events max (including any unlink event) */
2465         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
2466         if (rc != 0) {
2467                 CERROR("Can't allocate EQ: %d\n", rc);
2468                 goto out_0;
2469         }
2470
2471         /* initialize md content */
2472         md.start     = info;
2473         md.length    = infosz;
2474         md.threshold = 2; /*GET/REPLY*/
2475         md.max_size  = 0;
2476         md.options   = LNET_MD_TRUNCATE;
2477         md.user_ptr  = NULL;
2478         md.eq_handle = eqh;
2479
2480         rc = LNetMDBind(md, LNET_UNLINK, &mdh);
2481         if (rc != 0) {
2482                 CERROR("Can't bind MD: %d\n", rc);
2483                 goto out_1;
2484         }
2485
2486         rc = LNetGet(LNET_NID_ANY, mdh, id,
2487                      LNET_RESERVED_PORTAL,
2488                      LNET_PROTO_PING_MATCHBITS, 0);
2489
2490         if (rc != 0) {
2491                 /* Don't CERROR; this could be deliberate! */
2492
2493                 rc2 = LNetMDUnlink(mdh);
2494                 LASSERT(rc2 == 0);
2495
2496                 /* NB must wait for the UNLINK event below... */
2497                 unlinked = 1;
2498                 timeout_ms = a_long_time;
2499         }
2500
2501         do {
2502                 /* MUST block for unlink to complete */
2503                 if (unlinked)
2504                         blocked = cfs_block_allsigs();
2505
2506                 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
2507
2508                 if (unlinked)
2509                         cfs_restore_sigs(blocked);
2510
2511                 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
2512                        (rc2 <= 0) ? -1 : event.type,
2513                        (rc2 <= 0) ? -1 : event.status,
2514                        (rc2 > 0 && event.unlinked) ? " unlinked" : "");
2515
2516                 LASSERT(rc2 != -EOVERFLOW);     /* can't miss anything */
2517
2518                 if (rc2 <= 0 || event.status != 0) {
2519                         /* timeout or error */
2520                         if (!replied && rc == 0)
2521                                 rc = (rc2 < 0) ? rc2 :
2522                                      (rc2 == 0) ? -ETIMEDOUT :
2523                                      event.status;
2524
2525                         if (!unlinked) {
2526                                 /* Ensure completion in finite time... */
2527                                 LNetMDUnlink(mdh);
2528                                 /* No assertion (racing with network) */
2529                                 unlinked = 1;
2530                                 timeout_ms = a_long_time;
2531                         } else if (rc2 == 0) {
2532                                 /* timed out waiting for unlink */
2533                                 CWARN("ping %s: late network completion\n",
2534                                       libcfs_id2str(id));
2535                         }
2536                 } else if (event.type == LNET_EVENT_REPLY) {
2537                         replied = 1;
2538                         rc = event.mlength;
2539                 }
2540
2541         } while (rc2 <= 0 || !event.unlinked);
2542
2543         if (!replied) {
2544                 if (rc >= 0)
2545                         CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2546                               libcfs_id2str(id));
2547                 rc = -EIO;
2548                 goto out_1;
2549         }
2550
2551         nob = rc;
2552         LASSERT(nob >= 0 && nob <= infosz);
2553
2554         rc = -EPROTO;                           /* if I can't parse... */
2555
2556         if (nob < 8) {
2557                 /* can't check magic/version */
2558                 CERROR("%s: ping info too short %d\n",
2559                        libcfs_id2str(id), nob);
2560                 goto out_1;
2561         }
2562
2563         if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2564                 lnet_swap_pinginfo(info);
2565         } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2566                 CERROR("%s: Unexpected magic %08x\n",
2567                        libcfs_id2str(id), info->pi_magic);
2568                 goto out_1;
2569         }
2570
2571         if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
2572                 CERROR("%s: ping w/o NI status: 0x%x\n",
2573                        libcfs_id2str(id), info->pi_features);
2574                 goto out_1;
2575         }
2576
2577         if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2578                 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2579                        nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2580                 goto out_1;
2581         }
2582
2583         if (info->pi_nnis < n_ids)
2584                 n_ids = info->pi_nnis;
2585
2586         if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2587                 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2588                        nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2589                 goto out_1;
2590         }
2591
2592         rc = -EFAULT;                           /* If I SEGV... */
2593
2594         for (i = 0; i < n_ids; i++) {
2595                 tmpid.pid = info->pi_pid;
2596                 tmpid.nid = info->pi_ni[i].ns_nid;
2597                 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2598                         goto out_1;
2599         }
2600         rc = info->pi_nnis;
2601
2602  out_1:
2603         rc2 = LNetEQFree(eqh);
2604         if (rc2 != 0)
2605                 CERROR("rc2 %d\n", rc2);
2606         LASSERT(rc2 == 0);
2607
2608  out_0:
2609         LIBCFS_FREE(info, infosz);
2610         return rc;
2611 }