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