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