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