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