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