Whamcloud - gitweb
LU-8935 ptlrpc: missing barrier before wake_up
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LNET
34 #include <linux/log2.h>
35 #include <linux/ktime.h>
36
37 #include <lnet/lib-lnet.h>
38
39 #define D_LNI D_CONSOLE
40
41 struct lnet the_lnet;           /* THE state of the network */
42 EXPORT_SYMBOL(the_lnet);
43
44 static char *ip2nets = "";
45 module_param(ip2nets, charp, 0444);
46 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
47
48 static char *networks = "";
49 module_param(networks, charp, 0444);
50 MODULE_PARM_DESC(networks, "local networks");
51
52 static char *routes = "";
53 module_param(routes, charp, 0444);
54 MODULE_PARM_DESC(routes, "routes to non-local networks");
55
56 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
57 module_param(rnet_htable_size, int, 0444);
58 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
59
60 static int use_tcp_bonding = false;
61 module_param(use_tcp_bonding, int, 0444);
62 MODULE_PARM_DESC(use_tcp_bonding,
63                  "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
64
65 unsigned int lnet_numa_range = 0;
66 module_param(lnet_numa_range, uint, 0444);
67 MODULE_PARM_DESC(lnet_numa_range,
68                 "NUMA range to consider during Multi-Rail selection");
69
70 /*
71  * This sequence number keeps track of how many times DLC was used to
72  * update the local NIs. It is incremented when a NI is added or
73  * removed and checked when sending a message to determine if there is
74  * a need to re-run the selection algorithm. See lnet_select_pathway()
75  * for more details on its usage.
76  */
77 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
78
79 static int lnet_ping(struct lnet_process_id id, signed long timeout,
80                      struct lnet_process_id __user *ids, int n_ids);
81
82 static char *
83 lnet_get_routes(void)
84 {
85         return routes;
86 }
87
88 static char *
89 lnet_get_networks(void)
90 {
91         char   *nets;
92         int     rc;
93
94         if (*networks != 0 && *ip2nets != 0) {
95                 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
96                                    "'ip2nets' but not both at once\n");
97                 return NULL;
98         }
99
100         if (*ip2nets != 0) {
101                 rc = lnet_parse_ip2nets(&nets, ip2nets);
102                 return (rc == 0) ? nets : NULL;
103         }
104
105         if (*networks != 0)
106                 return networks;
107
108         return "tcp";
109 }
110
111 static void
112 lnet_init_locks(void)
113 {
114         spin_lock_init(&the_lnet.ln_eq_wait_lock);
115         init_waitqueue_head(&the_lnet.ln_eq_waitq);
116         init_waitqueue_head(&the_lnet.ln_rc_waitq);
117         mutex_init(&the_lnet.ln_lnd_mutex);
118         mutex_init(&the_lnet.ln_api_mutex);
119 }
120
121 static void
122 lnet_fini_locks(void)
123 {
124 }
125
126 struct kmem_cache *lnet_mes_cachep;        /* MEs kmem_cache */
127 struct kmem_cache *lnet_small_mds_cachep;  /* <= LNET_SMALL_MD_SIZE bytes
128                                             *  MDs kmem_cache */
129
130 static int
131 lnet_descriptor_setup(void)
132 {
133         /* create specific kmem_cache for MEs and small MDs (i.e., originally
134          * allocated in <size-xxx> kmem_cache).
135          */
136         lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
137                                             0, 0, NULL);
138         if (!lnet_mes_cachep)
139                 return -ENOMEM;
140
141         lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
142                                                   LNET_SMALL_MD_SIZE, 0, 0,
143                                                   NULL);
144         if (!lnet_small_mds_cachep)
145                 return -ENOMEM;
146
147         return 0;
148 }
149
150 static void
151 lnet_descriptor_cleanup(void)
152 {
153
154         if (lnet_small_mds_cachep) {
155                 kmem_cache_destroy(lnet_small_mds_cachep);
156                 lnet_small_mds_cachep = NULL;
157         }
158
159         if (lnet_mes_cachep) {
160                 kmem_cache_destroy(lnet_mes_cachep);
161                 lnet_mes_cachep = NULL;
162         }
163 }
164
165 static int
166 lnet_create_remote_nets_table(void)
167 {
168         int               i;
169         struct list_head *hash;
170
171         LASSERT(the_lnet.ln_remote_nets_hash == NULL);
172         LASSERT(the_lnet.ln_remote_nets_hbits > 0);
173         LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
174         if (hash == NULL) {
175                 CERROR("Failed to create remote nets hash table\n");
176                 return -ENOMEM;
177         }
178
179         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
180                 INIT_LIST_HEAD(&hash[i]);
181         the_lnet.ln_remote_nets_hash = hash;
182         return 0;
183 }
184
185 static void
186 lnet_destroy_remote_nets_table(void)
187 {
188         int i;
189
190         if (the_lnet.ln_remote_nets_hash == NULL)
191                 return;
192
193         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
194                 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
195
196         LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
197                     LNET_REMOTE_NETS_HASH_SIZE *
198                     sizeof(the_lnet.ln_remote_nets_hash[0]));
199         the_lnet.ln_remote_nets_hash = NULL;
200 }
201
202 static void
203 lnet_destroy_locks(void)
204 {
205         if (the_lnet.ln_res_lock != NULL) {
206                 cfs_percpt_lock_free(the_lnet.ln_res_lock);
207                 the_lnet.ln_res_lock = NULL;
208         }
209
210         if (the_lnet.ln_net_lock != NULL) {
211                 cfs_percpt_lock_free(the_lnet.ln_net_lock);
212                 the_lnet.ln_net_lock = NULL;
213         }
214
215         lnet_fini_locks();
216 }
217
218 static int
219 lnet_create_locks(void)
220 {
221         lnet_init_locks();
222
223         the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
224         if (the_lnet.ln_res_lock == NULL)
225                 goto failed;
226
227         the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
228         if (the_lnet.ln_net_lock == NULL)
229                 goto failed;
230
231         return 0;
232
233  failed:
234         lnet_destroy_locks();
235         return -ENOMEM;
236 }
237
238 static void lnet_assert_wire_constants(void)
239 {
240         /* Wire protocol assertions generated by 'wirecheck'
241          * running on Linux robert.bartonsoftware.com 2.6.8-1.521
242          * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
243          * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
244
245         /* Constants... */
246         CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
247         CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
248         CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
249         CLASSERT(LNET_MSG_ACK == 0);
250         CLASSERT(LNET_MSG_PUT == 1);
251         CLASSERT(LNET_MSG_GET == 2);
252         CLASSERT(LNET_MSG_REPLY == 3);
253         CLASSERT(LNET_MSG_HELLO == 4);
254
255         /* Checks for struct lnet_handle_wire */
256         CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
257         CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
258         CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
259         CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
260         CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
261
262         /* Checks for struct struct lnet_magicversion */
263         CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
264         CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
265         CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
266         CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
267         CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
268         CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
269         CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
270
271         /* Checks for struct struct lnet_hdr */
272         CLASSERT((int)sizeof(struct lnet_hdr) == 72);
273         CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
274         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
275         CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
276         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
277         CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
278         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
279         CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
280         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
281         CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
282         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
283         CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
284         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
285         CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
286         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
287
288         /* Ack */
289         CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
290         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
291         CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
292         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
293         CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
294         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
295
296         /* Put */
297         CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
298         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
299         CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
300         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
301         CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
302         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
303         CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
304         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
305         CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
306         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
307
308         /* Get */
309         CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
310         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
311         CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
312         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
313         CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
314         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
315         CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
316         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
317         CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
318         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
319
320         /* Reply */
321         CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
322         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
323
324         /* Hello */
325         CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
326         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
327         CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
328         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
329 }
330
331 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
332 {
333         struct lnet_lnd *lnd;
334         struct list_head *tmp;
335
336         /* holding lnd mutex */
337         list_for_each(tmp, &the_lnet.ln_lnds) {
338                 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
339
340                 if (lnd->lnd_type == type)
341                         return lnd;
342         }
343         return NULL;
344 }
345
346 void
347 lnet_register_lnd(struct lnet_lnd *lnd)
348 {
349         mutex_lock(&the_lnet.ln_lnd_mutex);
350
351         LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
352         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
353
354         list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
355         lnd->lnd_refcount = 0;
356
357         CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
358
359         mutex_unlock(&the_lnet.ln_lnd_mutex);
360 }
361 EXPORT_SYMBOL(lnet_register_lnd);
362
363 void
364 lnet_unregister_lnd(struct lnet_lnd *lnd)
365 {
366         mutex_lock(&the_lnet.ln_lnd_mutex);
367
368         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
369         LASSERT(lnd->lnd_refcount == 0);
370
371         list_del(&lnd->lnd_list);
372         CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
373
374         mutex_unlock(&the_lnet.ln_lnd_mutex);
375 }
376 EXPORT_SYMBOL(lnet_unregister_lnd);
377
378 void
379 lnet_counters_get(struct lnet_counters *counters)
380 {
381         struct lnet_counters *ctr;
382         int             i;
383
384         memset(counters, 0, sizeof(*counters));
385
386         lnet_net_lock(LNET_LOCK_EX);
387
388         cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
389                 counters->msgs_max     += ctr->msgs_max;
390                 counters->msgs_alloc   += ctr->msgs_alloc;
391                 counters->errors       += ctr->errors;
392                 counters->send_count   += ctr->send_count;
393                 counters->recv_count   += ctr->recv_count;
394                 counters->route_count  += ctr->route_count;
395                 counters->drop_count   += ctr->drop_count;
396                 counters->send_length  += ctr->send_length;
397                 counters->recv_length  += ctr->recv_length;
398                 counters->route_length += ctr->route_length;
399                 counters->drop_length  += ctr->drop_length;
400
401         }
402         lnet_net_unlock(LNET_LOCK_EX);
403 }
404 EXPORT_SYMBOL(lnet_counters_get);
405
406 void
407 lnet_counters_reset(void)
408 {
409         struct lnet_counters *counters;
410         int             i;
411
412         lnet_net_lock(LNET_LOCK_EX);
413
414         cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
415                 memset(counters, 0, sizeof(struct lnet_counters));
416
417         lnet_net_unlock(LNET_LOCK_EX);
418 }
419
420 static char *
421 lnet_res_type2str(int type)
422 {
423         switch (type) {
424         default:
425                 LBUG();
426         case LNET_COOKIE_TYPE_MD:
427                 return "MD";
428         case LNET_COOKIE_TYPE_ME:
429                 return "ME";
430         case LNET_COOKIE_TYPE_EQ:
431                 return "EQ";
432         }
433 }
434
435 static void
436 lnet_res_container_cleanup(struct lnet_res_container *rec)
437 {
438         int     count = 0;
439
440         if (rec->rec_type == 0) /* not set yet, it's uninitialized */
441                 return;
442
443         while (!list_empty(&rec->rec_active)) {
444                 struct list_head *e = rec->rec_active.next;
445
446                 list_del_init(e);
447                 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
448                         lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
449
450                 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
451                         lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
452
453                 } else { /* NB: Active MEs should be attached on portals */
454                         LBUG();
455                 }
456                 count++;
457         }
458
459         if (count > 0) {
460                 /* Found alive MD/ME/EQ, user really should unlink/free
461                  * all of them before finalize LNet, but if someone didn't,
462                  * we have to recycle garbage for him */
463                 CERROR("%d active elements on exit of %s container\n",
464                        count, lnet_res_type2str(rec->rec_type));
465         }
466
467         if (rec->rec_lh_hash != NULL) {
468                 LIBCFS_FREE(rec->rec_lh_hash,
469                             LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
470                 rec->rec_lh_hash = NULL;
471         }
472
473         rec->rec_type = 0; /* mark it as finalized */
474 }
475
476 static int
477 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
478 {
479         int     rc = 0;
480         int     i;
481
482         LASSERT(rec->rec_type == 0);
483
484         rec->rec_type = type;
485         INIT_LIST_HEAD(&rec->rec_active);
486
487         rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
488
489         /* Arbitrary choice of hash table size */
490         LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
491                          LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
492         if (rec->rec_lh_hash == NULL) {
493                 rc = -ENOMEM;
494                 goto out;
495         }
496
497         for (i = 0; i < LNET_LH_HASH_SIZE; i++)
498                 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
499
500         return 0;
501
502 out:
503         CERROR("Failed to setup %s resource container\n",
504                lnet_res_type2str(type));
505         lnet_res_container_cleanup(rec);
506         return rc;
507 }
508
509 static void
510 lnet_res_containers_destroy(struct lnet_res_container **recs)
511 {
512         struct lnet_res_container       *rec;
513         int                             i;
514
515         cfs_percpt_for_each(rec, i, recs)
516                 lnet_res_container_cleanup(rec);
517
518         cfs_percpt_free(recs);
519 }
520
521 static struct lnet_res_container **
522 lnet_res_containers_create(int type)
523 {
524         struct lnet_res_container       **recs;
525         struct lnet_res_container       *rec;
526         int                             rc;
527         int                             i;
528
529         recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
530         if (recs == NULL) {
531                 CERROR("Failed to allocate %s resource containers\n",
532                        lnet_res_type2str(type));
533                 return NULL;
534         }
535
536         cfs_percpt_for_each(rec, i, recs) {
537                 rc = lnet_res_container_setup(rec, i, type);
538                 if (rc != 0) {
539                         lnet_res_containers_destroy(recs);
540                         return NULL;
541                 }
542         }
543
544         return recs;
545 }
546
547 struct lnet_libhandle *
548 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
549 {
550         /* ALWAYS called with lnet_res_lock held */
551         struct list_head        *head;
552         struct lnet_libhandle   *lh;
553         unsigned int            hash;
554
555         if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
556                 return NULL;
557
558         hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
559         head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
560
561         list_for_each_entry(lh, head, lh_hash_chain) {
562                 if (lh->lh_cookie == cookie)
563                         return lh;
564         }
565
566         return NULL;
567 }
568
569 void
570 lnet_res_lh_initialize(struct lnet_res_container *rec,
571                        struct lnet_libhandle *lh)
572 {
573         /* ALWAYS called with lnet_res_lock held */
574         unsigned int    ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
575         unsigned int    hash;
576
577         lh->lh_cookie = rec->rec_lh_cookie;
578         rec->rec_lh_cookie += 1 << ibits;
579
580         hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
581
582         list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
583 }
584
585 static int lnet_unprepare(void);
586
587 static int
588 lnet_prepare(lnet_pid_t requested_pid)
589 {
590         /* Prepare to bring up the network */
591         struct lnet_res_container **recs;
592         int                       rc = 0;
593
594         if (requested_pid == LNET_PID_ANY) {
595                 /* Don't instantiate LNET just for me */
596                 return -ENETDOWN;
597         }
598
599         LASSERT(the_lnet.ln_refcount == 0);
600
601         the_lnet.ln_routing = 0;
602
603         LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
604         the_lnet.ln_pid = requested_pid;
605
606         INIT_LIST_HEAD(&the_lnet.ln_test_peers);
607         INIT_LIST_HEAD(&the_lnet.ln_peers);
608         INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
609         INIT_LIST_HEAD(&the_lnet.ln_nets);
610         INIT_LIST_HEAD(&the_lnet.ln_routers);
611         INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
612         INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
613
614         rc = lnet_descriptor_setup();
615         if (rc != 0)
616                 goto failed;
617
618         rc = lnet_create_remote_nets_table();
619         if (rc != 0)
620                 goto failed;
621
622         /*
623          * NB the interface cookie in wire handles guards against delayed
624          * replies and ACKs appearing valid after reboot.
625          */
626         the_lnet.ln_interface_cookie = ktime_get_real_ns();
627
628         the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
629                                                 sizeof(struct lnet_counters));
630         if (the_lnet.ln_counters == NULL) {
631                 CERROR("Failed to allocate counters for LNet\n");
632                 rc = -ENOMEM;
633                 goto failed;
634         }
635
636         rc = lnet_peer_tables_create();
637         if (rc != 0)
638                 goto failed;
639
640         rc = lnet_msg_containers_create();
641         if (rc != 0)
642                 goto failed;
643
644         rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
645                                       LNET_COOKIE_TYPE_EQ);
646         if (rc != 0)
647                 goto failed;
648
649         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
650         if (recs == NULL) {
651                 rc = -ENOMEM;
652                 goto failed;
653         }
654
655         the_lnet.ln_me_containers = recs;
656
657         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
658         if (recs == NULL) {
659                 rc = -ENOMEM;
660                 goto failed;
661         }
662
663         the_lnet.ln_md_containers = recs;
664
665         rc = lnet_portals_create();
666         if (rc != 0) {
667                 CERROR("Failed to create portals for LNet: %d\n", rc);
668                 goto failed;
669         }
670
671         return 0;
672
673  failed:
674         lnet_unprepare();
675         return rc;
676 }
677
678 static int
679 lnet_unprepare (void)
680 {
681         /* NB no LNET_LOCK since this is the last reference.  All LND instances
682          * have shut down already, so it is safe to unlink and free all
683          * descriptors, even those that appear committed to a network op (eg MD
684          * with non-zero pending count) */
685
686         lnet_fail_nid(LNET_NID_ANY, 0);
687
688         LASSERT(the_lnet.ln_refcount == 0);
689         LASSERT(list_empty(&the_lnet.ln_test_peers));
690         LASSERT(list_empty(&the_lnet.ln_nets));
691
692         lnet_portals_destroy();
693
694         if (the_lnet.ln_md_containers != NULL) {
695                 lnet_res_containers_destroy(the_lnet.ln_md_containers);
696                 the_lnet.ln_md_containers = NULL;
697         }
698
699         if (the_lnet.ln_me_containers != NULL) {
700                 lnet_res_containers_destroy(the_lnet.ln_me_containers);
701                 the_lnet.ln_me_containers = NULL;
702         }
703
704         lnet_res_container_cleanup(&the_lnet.ln_eq_container);
705
706         lnet_msg_containers_destroy();
707         lnet_peer_uninit();
708         lnet_rtrpools_free(0);
709
710         if (the_lnet.ln_counters != NULL) {
711                 cfs_percpt_free(the_lnet.ln_counters);
712                 the_lnet.ln_counters = NULL;
713         }
714         lnet_destroy_remote_nets_table();
715         lnet_descriptor_cleanup();
716
717         return 0;
718 }
719
720 struct lnet_ni  *
721 lnet_net2ni_locked(__u32 net_id, int cpt)
722 {
723         struct lnet_ni   *ni;
724         struct lnet_net  *net;
725
726         LASSERT(cpt != LNET_LOCK_EX);
727
728         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
729                 if (net->net_id == net_id) {
730                         ni = list_entry(net->net_ni_list.next, struct lnet_ni,
731                                         ni_netlist);
732                         return ni;
733                 }
734         }
735
736         return NULL;
737 }
738
739 struct lnet_ni *
740 lnet_net2ni_addref(__u32 net)
741 {
742         struct lnet_ni *ni;
743
744         lnet_net_lock(0);
745         ni = lnet_net2ni_locked(net, 0);
746         if (ni)
747                 lnet_ni_addref_locked(ni, 0);
748         lnet_net_unlock(0);
749
750         return ni;
751 }
752 EXPORT_SYMBOL(lnet_net2ni_addref);
753
754 struct lnet_net *
755 lnet_get_net_locked(__u32 net_id)
756 {
757         struct lnet_net  *net;
758
759         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
760                 if (net->net_id == net_id)
761                         return net;
762         }
763
764         return NULL;
765 }
766
767 unsigned int
768 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
769 {
770         __u64           key = nid;
771         unsigned int    val;
772
773         LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
774
775         if (number == 1)
776                 return 0;
777
778         val = hash_long(key, LNET_CPT_BITS);
779         /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
780         if (val < number)
781                 return val;
782
783         return (unsigned int)(key + val + (val >> 1)) % number;
784 }
785
786 int
787 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
788 {
789         struct lnet_net *net;
790
791         /* must called with hold of lnet_net_lock */
792         if (LNET_CPT_NUMBER == 1)
793                 return 0; /* the only one */
794
795         /*
796          * If NI is provided then use the CPT identified in the NI cpt
797          * list if one exists. If one doesn't exist, then that NI is
798          * associated with all CPTs and it follows that the net it belongs
799          * to is implicitly associated with all CPTs, so just hash the nid
800          * and return that.
801          */
802         if (ni != NULL) {
803                 if (ni->ni_cpts != NULL)
804                         return ni->ni_cpts[lnet_nid_cpt_hash(nid,
805                                                              ni->ni_ncpts)];
806                 else
807                         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
808         }
809
810         /* no NI provided so look at the net */
811         net = lnet_get_net_locked(LNET_NIDNET(nid));
812
813         if (net != NULL && net->net_cpts != NULL) {
814                 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
815         }
816
817         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
818 }
819
820 int
821 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
822 {
823         int     cpt;
824         int     cpt2;
825
826         if (LNET_CPT_NUMBER == 1)
827                 return 0; /* the only one */
828
829         cpt = lnet_net_lock_current();
830
831         cpt2 = lnet_cpt_of_nid_locked(nid, ni);
832
833         lnet_net_unlock(cpt);
834
835         return cpt2;
836 }
837 EXPORT_SYMBOL(lnet_cpt_of_nid);
838
839 int
840 lnet_islocalnet(__u32 net_id)
841 {
842         struct lnet_net *net;
843         int             cpt;
844         bool            local;
845
846         cpt = lnet_net_lock_current();
847
848         net = lnet_get_net_locked(net_id);
849
850         local = net != NULL;
851
852         lnet_net_unlock(cpt);
853
854         return local;
855 }
856
857 bool
858 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
859 {
860         if (ni->ni_state == LNET_NI_STATE_ACTIVE ||
861             ni->ni_state == LNET_NI_STATE_DEGRADED)
862                 return true;
863
864         return false;
865 }
866
867 struct lnet_ni  *
868 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
869 {
870         struct lnet_net  *net;
871         struct lnet_ni   *ni;
872
873         LASSERT(cpt != LNET_LOCK_EX);
874
875         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
876                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
877                         if (ni->ni_nid == nid)
878                                 return ni;
879                 }
880         }
881
882         return NULL;
883 }
884
885 struct lnet_ni *
886 lnet_nid2ni_addref(lnet_nid_t nid)
887 {
888         struct lnet_ni *ni;
889
890         lnet_net_lock(0);
891         ni = lnet_nid2ni_locked(nid, 0);
892         if (ni)
893                 lnet_ni_addref_locked(ni, 0);
894         lnet_net_unlock(0);
895
896         return ni;
897 }
898 EXPORT_SYMBOL(lnet_nid2ni_addref);
899
900 int
901 lnet_islocalnid(lnet_nid_t nid)
902 {
903         struct lnet_ni  *ni;
904         int             cpt;
905
906         cpt = lnet_net_lock_current();
907         ni = lnet_nid2ni_locked(nid, cpt);
908         lnet_net_unlock(cpt);
909
910         return ni != NULL;
911 }
912
913 int
914 lnet_count_acceptor_nets(void)
915 {
916         /* Return the # of NIs that need the acceptor. */
917         int              count = 0;
918         struct lnet_net  *net;
919         int              cpt;
920
921         cpt = lnet_net_lock_current();
922         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
923                 /* all socklnd type networks should have the acceptor
924                  * thread started */
925                 if (net->net_lnd->lnd_accept != NULL)
926                         count++;
927         }
928
929         lnet_net_unlock(cpt);
930
931         return count;
932 }
933
934 static struct lnet_ping_info *
935 lnet_ping_info_create(int num_ni)
936 {
937         struct lnet_ping_info *ping_info;
938         unsigned int     infosz;
939
940         infosz = offsetof(struct lnet_ping_info, pi_ni[num_ni]);
941         LIBCFS_ALLOC(ping_info, infosz);
942         if (ping_info == NULL) {
943                 CERROR("Can't allocate ping info[%d]\n", num_ni);
944                 return NULL;
945         }
946
947         ping_info->pi_nnis = num_ni;
948         ping_info->pi_pid = the_lnet.ln_pid;
949         ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
950         ping_info->pi_features = LNET_PING_FEAT_NI_STATUS;
951
952         return ping_info;
953 }
954
955 static inline int
956 lnet_get_net_ni_count_locked(struct lnet_net *net)
957 {
958         struct lnet_ni  *ni;
959         int             count = 0;
960
961         list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
962                 count++;
963
964         return count;
965 }
966
967 static inline int
968 lnet_get_net_ni_count_pre(struct lnet_net *net)
969 {
970         struct lnet_ni  *ni;
971         int             count = 0;
972
973         list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
974                 count++;
975
976         return count;
977 }
978
979 static inline int
980 lnet_get_ni_count(void)
981 {
982         struct lnet_ni  *ni;
983         struct lnet_net *net;
984         int             count = 0;
985
986         lnet_net_lock(0);
987
988         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
989                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
990                         count++;
991         }
992
993         lnet_net_unlock(0);
994
995         return count;
996 }
997
998 static inline void
999 lnet_ping_info_free(struct lnet_ping_info *pinfo)
1000 {
1001         LIBCFS_FREE(pinfo,
1002                     offsetof(struct lnet_ping_info,
1003                              pi_ni[pinfo->pi_nnis]));
1004 }
1005
1006 static void
1007 lnet_ping_info_destroy(void)
1008 {
1009         struct lnet_net *net;
1010         struct lnet_ni  *ni;
1011
1012         lnet_net_lock(LNET_LOCK_EX);
1013
1014         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1015                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1016                         lnet_ni_lock(ni);
1017                         ni->ni_status = NULL;
1018                         lnet_ni_unlock(ni);
1019                 }
1020         }
1021
1022         lnet_ping_info_free(the_lnet.ln_ping_info);
1023         the_lnet.ln_ping_info = NULL;
1024
1025         lnet_net_unlock(LNET_LOCK_EX);
1026 }
1027
1028 static void
1029 lnet_ping_event_handler(struct lnet_event *event)
1030 {
1031         struct lnet_ping_info *pinfo = event->md.user_ptr;
1032
1033         if (event->unlinked)
1034                 pinfo->pi_features = LNET_PING_FEAT_INVAL;
1035 }
1036
1037 static int
1038 lnet_ping_info_setup(struct lnet_ping_info **ppinfo,
1039                      struct lnet_handle_md *md_handle,
1040                      int ni_count, bool set_eq)
1041 {
1042         struct lnet_process_id id = {
1043                 .nid = LNET_NID_ANY,
1044                 .pid = LNET_PID_ANY
1045         };
1046         struct lnet_handle_me me_handle;
1047         struct lnet_md md = { NULL };
1048         int rc, rc2;
1049
1050         if (set_eq) {
1051                 rc = LNetEQAlloc(0, lnet_ping_event_handler,
1052                                  &the_lnet.ln_ping_target_eq);
1053                 if (rc != 0) {
1054                         CERROR("Can't allocate ping EQ: %d\n", rc);
1055                         return rc;
1056                 }
1057         }
1058
1059         *ppinfo = lnet_ping_info_create(ni_count);
1060         if (*ppinfo == NULL) {
1061                 rc = -ENOMEM;
1062                 goto failed_0;
1063         }
1064
1065         rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1066                           LNET_PROTO_PING_MATCHBITS, 0,
1067                           LNET_UNLINK, LNET_INS_AFTER,
1068                           &me_handle);
1069         if (rc != 0) {
1070                 CERROR("Can't create ping ME: %d\n", rc);
1071                 goto failed_1;
1072         }
1073
1074         /* initialize md content */
1075         md.start     = *ppinfo;
1076         md.length    = offsetof(struct lnet_ping_info,
1077                                 pi_ni[(*ppinfo)->pi_nnis]);
1078         md.threshold = LNET_MD_THRESH_INF;
1079         md.max_size  = 0;
1080         md.options   = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1081                        LNET_MD_MANAGE_REMOTE;
1082         md.user_ptr  = NULL;
1083         md.eq_handle = the_lnet.ln_ping_target_eq;
1084         md.user_ptr = *ppinfo;
1085
1086         rc = LNetMDAttach(me_handle, md, LNET_RETAIN, md_handle);
1087         if (rc != 0) {
1088                 CERROR("Can't attach ping MD: %d\n", rc);
1089                 goto failed_2;
1090         }
1091
1092         return 0;
1093
1094 failed_2:
1095         rc2 = LNetMEUnlink(me_handle);
1096         LASSERT(rc2 == 0);
1097 failed_1:
1098         lnet_ping_info_free(*ppinfo);
1099         *ppinfo = NULL;
1100 failed_0:
1101         if (set_eq)
1102                 LNetEQFree(the_lnet.ln_ping_target_eq);
1103         return rc;
1104 }
1105
1106 static void
1107 lnet_ping_md_unlink(struct lnet_ping_info *pinfo, struct lnet_handle_md *md_handle)
1108 {
1109         sigset_t        blocked = cfs_block_allsigs();
1110
1111         LNetMDUnlink(*md_handle);
1112         LNetInvalidateMDHandle(md_handle);
1113
1114         /* NB md could be busy; this just starts the unlink */
1115         while (pinfo->pi_features != LNET_PING_FEAT_INVAL) {
1116                 CDEBUG(D_NET, "Still waiting for ping MD to unlink\n");
1117                 set_current_state(TASK_UNINTERRUPTIBLE);
1118                 schedule_timeout(cfs_time_seconds(1));
1119         }
1120
1121         cfs_restore_sigs(blocked);
1122 }
1123
1124 static void
1125 lnet_ping_info_install_locked(struct lnet_ping_info *ping_info)
1126 {
1127         int                     i;
1128         struct lnet_ni          *ni;
1129         struct lnet_net         *net;
1130         struct lnet_ni_status *ns;
1131
1132         i = 0;
1133         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1134                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1135                         LASSERT(i < ping_info->pi_nnis);
1136
1137                         ns = &ping_info->pi_ni[i];
1138
1139                         ns->ns_nid = ni->ni_nid;
1140
1141                         lnet_ni_lock(ni);
1142                         ns->ns_status = (ni->ni_status != NULL) ?
1143                                         ni->ni_status->ns_status :
1144                                                 LNET_NI_STATUS_UP;
1145                         ni->ni_status = ns;
1146                         lnet_ni_unlock(ni);
1147
1148                         i++;
1149                 }
1150
1151         }
1152 }
1153
1154 static void
1155 lnet_ping_target_update(struct lnet_ping_info *pinfo,
1156                         struct lnet_handle_md md_handle)
1157 {
1158         struct lnet_ping_info *old_pinfo = NULL;
1159         struct lnet_handle_md old_md;
1160
1161         /* switch the NIs to point to the new ping info created */
1162         lnet_net_lock(LNET_LOCK_EX);
1163
1164         if (!the_lnet.ln_routing)
1165                 pinfo->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1166         lnet_ping_info_install_locked(pinfo);
1167
1168         if (the_lnet.ln_ping_info != NULL) {
1169                 old_pinfo = the_lnet.ln_ping_info;
1170                 old_md = the_lnet.ln_ping_target_md;
1171         }
1172         the_lnet.ln_ping_target_md = md_handle;
1173         the_lnet.ln_ping_info = pinfo;
1174
1175         lnet_net_unlock(LNET_LOCK_EX);
1176
1177         if (old_pinfo != NULL) {
1178                 /* unlink the old ping info */
1179                 lnet_ping_md_unlink(old_pinfo, &old_md);
1180                 lnet_ping_info_free(old_pinfo);
1181         }
1182 }
1183
1184 static void
1185 lnet_ping_target_fini(void)
1186 {
1187         int             rc;
1188
1189         lnet_ping_md_unlink(the_lnet.ln_ping_info,
1190                             &the_lnet.ln_ping_target_md);
1191
1192         rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1193         LASSERT(rc == 0);
1194
1195         lnet_ping_info_destroy();
1196 }
1197
1198 static int
1199 lnet_ni_tq_credits(struct lnet_ni *ni)
1200 {
1201         int     credits;
1202
1203         LASSERT(ni->ni_ncpts >= 1);
1204
1205         if (ni->ni_ncpts == 1)
1206                 return ni->ni_net->net_tunables.lct_max_tx_credits;
1207
1208         credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1209         credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1210         credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1211
1212         return credits;
1213 }
1214
1215 static void
1216 lnet_ni_unlink_locked(struct lnet_ni *ni)
1217 {
1218         if (!list_empty(&ni->ni_cptlist)) {
1219                 list_del_init(&ni->ni_cptlist);
1220                 lnet_ni_decref_locked(ni, 0);
1221         }
1222
1223         /* move it to zombie list and nobody can find it anymore */
1224         LASSERT(!list_empty(&ni->ni_netlist));
1225         list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1226         lnet_ni_decref_locked(ni, 0);
1227 }
1228
1229 static void
1230 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1231 {
1232         int             i;
1233         int             islo;
1234         struct lnet_ni  *ni;
1235         struct list_head *zombie_list = &net->net_ni_zombie;
1236
1237         /*
1238          * Now wait for the NIs I just nuked to show up on the zombie
1239          * list and shut them down in guaranteed thread context
1240          */
1241         i = 2;
1242         while (!list_empty(zombie_list)) {
1243                 int     *ref;
1244                 int     j;
1245
1246                 ni = list_entry(zombie_list->next,
1247                                 struct lnet_ni, ni_netlist);
1248                 list_del_init(&ni->ni_netlist);
1249                 /* the ni should be in deleting state. If it's not it's
1250                  * a bug */
1251                 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
1252                 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1253                         if (*ref == 0)
1254                                 continue;
1255                         /* still busy, add it back to zombie list */
1256                         list_add(&ni->ni_netlist, zombie_list);
1257                         break;
1258                 }
1259
1260                 if (!list_empty(&ni->ni_netlist)) {
1261                         lnet_net_unlock(LNET_LOCK_EX);
1262                         ++i;
1263                         if ((i & (-i)) == i) {
1264                                 CDEBUG(D_WARNING,
1265                                        "Waiting for zombie LNI %s\n",
1266                                        libcfs_nid2str(ni->ni_nid));
1267                         }
1268                         set_current_state(TASK_UNINTERRUPTIBLE);
1269                         schedule_timeout(cfs_time_seconds(1));
1270                         lnet_net_lock(LNET_LOCK_EX);
1271                         continue;
1272                 }
1273
1274                 lnet_net_unlock(LNET_LOCK_EX);
1275
1276                 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1277
1278                 LASSERT(!in_interrupt());
1279                 (net->net_lnd->lnd_shutdown)(ni);
1280
1281                 if (!islo)
1282                         CDEBUG(D_LNI, "Removed LNI %s\n",
1283                               libcfs_nid2str(ni->ni_nid));
1284
1285                 lnet_ni_free(ni);
1286                 i = 2;
1287                 lnet_net_lock(LNET_LOCK_EX);
1288         }
1289 }
1290
1291 /* shutdown down the NI and release refcount */
1292 static void
1293 lnet_shutdown_lndni(struct lnet_ni *ni)
1294 {
1295         int i;
1296         struct lnet_net *net = ni->ni_net;
1297
1298         lnet_net_lock(LNET_LOCK_EX);
1299         ni->ni_state = LNET_NI_STATE_DELETING;
1300         lnet_ni_unlink_locked(ni);
1301         lnet_incr_dlc_seq();
1302         lnet_net_unlock(LNET_LOCK_EX);
1303
1304         /* clear messages for this NI on the lazy portal */
1305         for (i = 0; i < the_lnet.ln_nportals; i++)
1306                 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1307
1308         lnet_net_lock(LNET_LOCK_EX);
1309         lnet_clear_zombies_nis_locked(net);
1310         lnet_net_unlock(LNET_LOCK_EX);
1311 }
1312
1313 static void
1314 lnet_shutdown_lndnet(struct lnet_net *net)
1315 {
1316         struct lnet_ni *ni;
1317
1318         lnet_net_lock(LNET_LOCK_EX);
1319
1320         net->net_state = LNET_NET_STATE_DELETING;
1321
1322         list_del_init(&net->net_list);
1323
1324         while (!list_empty(&net->net_ni_list)) {
1325                 ni = list_entry(net->net_ni_list.next,
1326                                 struct lnet_ni, ni_netlist);
1327                 lnet_net_unlock(LNET_LOCK_EX);
1328                 lnet_shutdown_lndni(ni);
1329                 lnet_net_lock(LNET_LOCK_EX);
1330         }
1331
1332         lnet_net_unlock(LNET_LOCK_EX);
1333
1334         /* Do peer table cleanup for this net */
1335         lnet_peer_tables_cleanup(net);
1336
1337         lnet_net_lock(LNET_LOCK_EX);
1338         /*
1339          * decrement ref count on lnd only when the entire network goes
1340          * away
1341          */
1342         net->net_lnd->lnd_refcount--;
1343
1344         lnet_net_unlock(LNET_LOCK_EX);
1345
1346         lnet_net_free(net);
1347 }
1348
1349 static void
1350 lnet_shutdown_lndnets(void)
1351 {
1352         struct lnet_net *net;
1353
1354         /* NB called holding the global mutex */
1355
1356         /* All quiet on the API front */
1357         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
1358         LASSERT(the_lnet.ln_refcount == 0);
1359
1360         lnet_net_lock(LNET_LOCK_EX);
1361         the_lnet.ln_state = LNET_STATE_STOPPING;
1362
1363         while (!list_empty(&the_lnet.ln_nets)) {
1364                 /*
1365                  * move the nets to the zombie list to avoid them being
1366                  * picked up for new work. LONET is also included in the
1367                  * Nets that will be moved to the zombie list
1368                  */
1369                 net = list_entry(the_lnet.ln_nets.next,
1370                                  struct lnet_net, net_list);
1371                 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1372         }
1373
1374         /* Drop the cached loopback Net. */
1375         if (the_lnet.ln_loni != NULL) {
1376                 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1377                 the_lnet.ln_loni = NULL;
1378         }
1379         lnet_net_unlock(LNET_LOCK_EX);
1380
1381         /* iterate through the net zombie list and delete each net */
1382         while (!list_empty(&the_lnet.ln_net_zombie)) {
1383                 net = list_entry(the_lnet.ln_net_zombie.next,
1384                                  struct lnet_net, net_list);
1385                 lnet_shutdown_lndnet(net);
1386         }
1387
1388         lnet_net_lock(LNET_LOCK_EX);
1389         the_lnet.ln_state = LNET_STATE_SHUTDOWN;
1390         lnet_net_unlock(LNET_LOCK_EX);
1391 }
1392
1393 static int
1394 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1395 {
1396         int                     rc = -EINVAL;
1397         struct lnet_tx_queue    *tq;
1398         int                     i;
1399         struct lnet_net         *net = ni->ni_net;
1400
1401         mutex_lock(&the_lnet.ln_lnd_mutex);
1402
1403         if (tun) {
1404                 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1405                 ni->ni_lnd_tunables_set = true;
1406         }
1407
1408         rc = (net->net_lnd->lnd_startup)(ni);
1409
1410         mutex_unlock(&the_lnet.ln_lnd_mutex);
1411
1412         if (rc != 0) {
1413                 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1414                                    rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1415                 lnet_net_lock(LNET_LOCK_EX);
1416                 net->net_lnd->lnd_refcount--;
1417                 lnet_net_unlock(LNET_LOCK_EX);
1418                 goto failed0;
1419         }
1420
1421         ni->ni_state = LNET_NI_STATE_ACTIVE;
1422
1423         /* We keep a reference on the loopback net through the loopback NI */
1424         if (net->net_lnd->lnd_type == LOLND) {
1425                 lnet_ni_addref(ni);
1426                 LASSERT(the_lnet.ln_loni == NULL);
1427                 the_lnet.ln_loni = ni;
1428                 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
1429                 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
1430                 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
1431                 ni->ni_net->net_tunables.lct_peer_timeout = 0;
1432                 return 0;
1433         }
1434
1435         if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
1436             ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
1437                 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1438                                    libcfs_lnd2str(net->net_lnd->lnd_type),
1439                                    ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
1440                                         "" : "per-peer ");
1441                 /* shutdown the NI since if we get here then it must've already
1442                  * been started
1443                  */
1444                 lnet_shutdown_lndni(ni);
1445                 return -EINVAL;
1446         }
1447
1448         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1449                 tq->tq_credits_min =
1450                 tq->tq_credits_max =
1451                 tq->tq_credits = lnet_ni_tq_credits(ni);
1452         }
1453
1454         atomic_set(&ni->ni_tx_credits,
1455                    lnet_ni_tq_credits(ni) * ni->ni_ncpts);
1456
1457         CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1458                 libcfs_nid2str(ni->ni_nid),
1459                 ni->ni_net->net_tunables.lct_peer_tx_credits,
1460                 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1461                 ni->ni_net->net_tunables.lct_peer_rtr_credits,
1462                 ni->ni_net->net_tunables.lct_peer_timeout);
1463
1464         return 0;
1465 failed0:
1466         lnet_ni_free(ni);
1467         return rc;
1468 }
1469
1470 static int
1471 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
1472 {
1473         struct lnet_ni *ni;
1474         struct lnet_net *net_l = NULL;
1475         struct list_head        local_ni_list;
1476         int                     rc;
1477         int                     ni_count = 0;
1478         __u32                   lnd_type;
1479         struct lnet_lnd *lnd;
1480         int                     peer_timeout =
1481                 net->net_tunables.lct_peer_timeout;
1482         int                     maxtxcredits =
1483                 net->net_tunables.lct_max_tx_credits;
1484         int                     peerrtrcredits =
1485                 net->net_tunables.lct_peer_rtr_credits;
1486
1487         INIT_LIST_HEAD(&local_ni_list);
1488
1489         /*
1490          * make sure that this net is unique. If it isn't then
1491          * we are adding interfaces to an already existing network, and
1492          * 'net' is just a convenient way to pass in the list.
1493          * if it is unique we need to find the LND and load it if
1494          * necessary.
1495          */
1496         if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
1497                 lnd_type = LNET_NETTYP(net->net_id);
1498
1499                 LASSERT(libcfs_isknown_lnd(lnd_type));
1500
1501                 mutex_lock(&the_lnet.ln_lnd_mutex);
1502                 lnd = lnet_find_lnd_by_type(lnd_type);
1503
1504                 if (lnd == NULL) {
1505                         mutex_unlock(&the_lnet.ln_lnd_mutex);
1506                         rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1507                         mutex_lock(&the_lnet.ln_lnd_mutex);
1508
1509                         lnd = lnet_find_lnd_by_type(lnd_type);
1510                         if (lnd == NULL) {
1511                                 mutex_unlock(&the_lnet.ln_lnd_mutex);
1512                                 CERROR("Can't load LND %s, module %s, rc=%d\n",
1513                                 libcfs_lnd2str(lnd_type),
1514                                 libcfs_lnd2modname(lnd_type), rc);
1515 #ifndef HAVE_MODULE_LOADING_SUPPORT
1516                                 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1517                                                 "compiled with kernel module "
1518                                                 "loading support.");
1519 #endif
1520                                 rc = -EINVAL;
1521                                 goto failed0;
1522                         }
1523                 }
1524
1525                 lnet_net_lock(LNET_LOCK_EX);
1526                 lnd->lnd_refcount++;
1527                 lnet_net_unlock(LNET_LOCK_EX);
1528
1529                 net->net_lnd = lnd;
1530
1531                 mutex_unlock(&the_lnet.ln_lnd_mutex);
1532
1533                 net_l = net;
1534         }
1535
1536         /*
1537          * net_l: if the network being added is unique then net_l
1538          *        will point to that network
1539          *        if the network being added is not unique then
1540          *        net_l points to the existing network.
1541          *
1542          * When we enter the loop below, we'll pick NIs off he
1543          * network beign added and start them up, then add them to
1544          * a local ni list. Once we've successfully started all
1545          * the NIs then we join the local NI list (of started up
1546          * networks) with the net_l->net_ni_list, which should
1547          * point to the correct network to add the new ni list to
1548          *
1549          * If any of the new NIs fail to start up, then we want to
1550          * iterate through the local ni list, which should include
1551          * any NIs which were successfully started up, and shut
1552          * them down.
1553          *
1554          * After than we want to delete the network being added,
1555          * to avoid a memory leak.
1556          */
1557
1558         /*
1559          * When a network uses TCP bonding then all its interfaces
1560          * must be specified when the network is first defined: the
1561          * TCP bonding code doesn't allow for interfaces to be added
1562          * or removed.
1563          */
1564         if (net_l != net && net_l != NULL && use_tcp_bonding &&
1565             LNET_NETTYP(net_l->net_id) == SOCKLND) {
1566                 rc = -EINVAL;
1567                 goto failed0;
1568         }
1569
1570         while (!list_empty(&net->net_ni_added)) {
1571                 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
1572                                 ni_netlist);
1573                 list_del_init(&ni->ni_netlist);
1574
1575                 /* make sure that the the NI we're about to start
1576                  * up is actually unique. if it's not fail. */
1577                 if (!lnet_ni_unique_net(&net_l->net_ni_list,
1578                                         ni->ni_interfaces[0])) {
1579                         rc = -EINVAL;
1580                         goto failed1;
1581                 }
1582
1583                 /* adjust the pointer the parent network, just in case it
1584                  * the net is a duplicate */
1585                 ni->ni_net = net_l;
1586
1587                 rc = lnet_startup_lndni(ni, tun);
1588
1589                 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
1590                         ni->ni_net->net_lnd->lnd_query != NULL);
1591
1592                 if (rc < 0)
1593                         goto failed1;
1594
1595                 lnet_ni_addref(ni);
1596                 list_add_tail(&ni->ni_netlist, &local_ni_list);
1597
1598                 ni_count++;
1599         }
1600
1601         lnet_net_lock(LNET_LOCK_EX);
1602         list_splice_tail(&local_ni_list, &net_l->net_ni_list);
1603         lnet_incr_dlc_seq();
1604         lnet_net_unlock(LNET_LOCK_EX);
1605
1606         /* if the network is not unique then we don't want to keep
1607          * it around after we're done. Free it. Otherwise add that
1608          * net to the global the_lnet.ln_nets */
1609         if (net_l != net && net_l != NULL) {
1610                 /*
1611                  * TODO - note. currently the tunables can not be updated
1612                  * once added
1613                  */
1614                 lnet_net_free(net);
1615         } else {
1616                 net->net_state = LNET_NET_STATE_ACTIVE;
1617                 /*
1618                  * restore tunables after it has been overwitten by the
1619                  * lnd
1620                  */
1621                 if (peer_timeout != -1)
1622                         net->net_tunables.lct_peer_timeout = peer_timeout;
1623                 if (maxtxcredits != -1)
1624                         net->net_tunables.lct_max_tx_credits = maxtxcredits;
1625                 if (peerrtrcredits != -1)
1626                         net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
1627
1628                 lnet_net_lock(LNET_LOCK_EX);
1629                 list_add_tail(&net->net_list, &the_lnet.ln_nets);
1630                 lnet_net_unlock(LNET_LOCK_EX);
1631         }
1632
1633         return ni_count;
1634
1635 failed1:
1636         /*
1637          * shutdown the new NIs that are being started up
1638          * free the NET being started
1639          */
1640         while (!list_empty(&local_ni_list)) {
1641                 ni = list_entry(local_ni_list.next, struct lnet_ni,
1642                                 ni_netlist);
1643
1644                 lnet_shutdown_lndni(ni);
1645         }
1646
1647 failed0:
1648         lnet_net_free(net);
1649
1650         return rc;
1651 }
1652
1653 static int
1654 lnet_startup_lndnets(struct list_head *netlist)
1655 {
1656         struct lnet_net         *net;
1657         int                     rc;
1658         int                     ni_count = 0;
1659
1660         /*
1661          * Change to running state before bringing up the LNDs. This
1662          * allows lnet_shutdown_lndnets() to assert that we've passed
1663          * through here.
1664          */
1665         lnet_net_lock(LNET_LOCK_EX);
1666         the_lnet.ln_state = LNET_STATE_RUNNING;
1667         lnet_net_unlock(LNET_LOCK_EX);
1668
1669         while (!list_empty(netlist)) {
1670                 net = list_entry(netlist->next, struct lnet_net, net_list);
1671                 list_del_init(&net->net_list);
1672
1673                 rc = lnet_startup_lndnet(net, NULL);
1674
1675                 if (rc < 0)
1676                         goto failed;
1677
1678                 ni_count += rc;
1679         }
1680
1681         return ni_count;
1682 failed:
1683         lnet_shutdown_lndnets();
1684
1685         return rc;
1686 }
1687
1688 /**
1689  * Initialize LNet library.
1690  *
1691  * Automatically called at module loading time. Caller has to call
1692  * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
1693  * latter returned 0. It must be called exactly once.
1694  *
1695  * \retval 0 on success
1696  * \retval -ve on failures.
1697  */
1698 int lnet_lib_init(void)
1699 {
1700         int rc;
1701
1702         lnet_assert_wire_constants();
1703
1704         memset(&the_lnet, 0, sizeof(the_lnet));
1705
1706         /* refer to global cfs_cpt_table for now */
1707         the_lnet.ln_cpt_table   = cfs_cpt_table;
1708         the_lnet.ln_cpt_number  = cfs_cpt_number(cfs_cpt_table);
1709
1710         LASSERT(the_lnet.ln_cpt_number > 0);
1711         if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1712                 /* we are under risk of consuming all lh_cookie */
1713                 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1714                        "please change setting of CPT-table and retry\n",
1715                        the_lnet.ln_cpt_number, LNET_CPT_MAX);
1716                 return -E2BIG;
1717         }
1718
1719         while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1720                 the_lnet.ln_cpt_bits++;
1721
1722         rc = lnet_create_locks();
1723         if (rc != 0) {
1724                 CERROR("Can't create LNet global locks: %d\n", rc);
1725                 return rc;
1726         }
1727
1728         the_lnet.ln_refcount = 0;
1729         LNetInvalidateEQHandle(&the_lnet.ln_rc_eqh);
1730         INIT_LIST_HEAD(&the_lnet.ln_lnds);
1731         INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
1732         INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1733         INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1734
1735         /* The hash table size is the number of bits it takes to express the set
1736          * ln_num_routes, minus 1 (better to under estimate than over so we
1737          * don't waste memory). */
1738         if (rnet_htable_size <= 0)
1739                 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1740         else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1741                 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1742         the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1743                                            order_base_2(rnet_htable_size) - 1);
1744
1745         /* All LNDs apart from the LOLND are in separate modules.  They
1746          * register themselves when their module loads, and unregister
1747          * themselves when their module is unloaded. */
1748         lnet_register_lnd(&the_lolnd);
1749         return 0;
1750 }
1751
1752 /**
1753  * Finalize LNet library.
1754  *
1755  * \pre lnet_lib_init() called with success.
1756  * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1757  */
1758 void lnet_lib_exit(void)
1759 {
1760         LASSERT(the_lnet.ln_refcount == 0);
1761
1762         while (!list_empty(&the_lnet.ln_lnds))
1763                 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1764                                                struct lnet_lnd, lnd_list));
1765         lnet_destroy_locks();
1766 }
1767
1768 /**
1769  * Set LNet PID and start LNet interfaces, routing, and forwarding.
1770  *
1771  * Users must call this function at least once before any other functions.
1772  * For each successful call there must be a corresponding call to
1773  * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1774  * ignored.
1775  *
1776  * The PID used by LNet may be different from the one requested.
1777  * See LNetGetId().
1778  *
1779  * \param requested_pid PID requested by the caller.
1780  *
1781  * \return >= 0 on success, and < 0 error code on failures.
1782  */
1783 int
1784 LNetNIInit(lnet_pid_t requested_pid)
1785 {
1786         int                     im_a_router = 0;
1787         int                     rc;
1788         int                     ni_count;
1789         struct lnet_ping_info   *pinfo;
1790         struct lnet_handle_md   md_handle;
1791         struct list_head        net_head;
1792         struct lnet_net         *net;
1793
1794         INIT_LIST_HEAD(&net_head);
1795
1796         mutex_lock(&the_lnet.ln_api_mutex);
1797
1798         CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1799
1800         if (the_lnet.ln_refcount > 0) {
1801                 rc = the_lnet.ln_refcount++;
1802                 mutex_unlock(&the_lnet.ln_api_mutex);
1803                 return rc;
1804         }
1805
1806         rc = lnet_prepare(requested_pid);
1807         if (rc != 0) {
1808                 mutex_unlock(&the_lnet.ln_api_mutex);
1809                 return rc;
1810         }
1811
1812         /* create a network for Loopback network */
1813         net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
1814         if (net == NULL) {
1815                 rc = -ENOMEM;
1816                 goto err_empty_list;
1817         }
1818
1819         /* Add in the loopback NI */
1820         if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
1821                 rc = -ENOMEM;
1822                 goto err_empty_list;
1823         }
1824
1825         /* If LNet is being initialized via DLC it is possible
1826          * that the user requests not to load module parameters (ones which
1827          * are supported by DLC) on initialization.  Therefore, make sure not
1828          * to load networks, routes and forwarding from module parameters
1829          * in this case.  On cleanup in case of failure only clean up
1830          * routes if it has been loaded */
1831         if (!the_lnet.ln_nis_from_mod_params) {
1832                 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
1833                                          use_tcp_bonding);
1834                 if (rc < 0)
1835                         goto err_empty_list;
1836         }
1837
1838         ni_count = lnet_startup_lndnets(&net_head);
1839         if (ni_count < 0) {
1840                 rc = ni_count;
1841                 goto err_empty_list;
1842         }
1843
1844         if (!the_lnet.ln_nis_from_mod_params) {
1845                 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1846                 if (rc != 0)
1847                         goto err_shutdown_lndnis;
1848
1849                 rc = lnet_check_routes();
1850                 if (rc != 0)
1851                         goto err_destroy_routes;
1852
1853                 rc = lnet_rtrpools_alloc(im_a_router);
1854                 if (rc != 0)
1855                         goto err_destroy_routes;
1856         }
1857
1858         rc = lnet_acceptor_start();
1859         if (rc != 0)
1860                 goto err_destroy_routes;
1861
1862         the_lnet.ln_refcount = 1;
1863         /* Now I may use my own API functions... */
1864
1865         rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
1866         if (rc != 0)
1867                 goto err_acceptor_stop;
1868
1869         lnet_ping_target_update(pinfo, md_handle);
1870
1871         rc = lnet_router_checker_start();
1872         if (rc != 0)
1873                 goto err_stop_ping;
1874
1875         lnet_fault_init();
1876         lnet_proc_init();
1877
1878         mutex_unlock(&the_lnet.ln_api_mutex);
1879
1880         return 0;
1881
1882 err_stop_ping:
1883         lnet_ping_target_fini();
1884 err_acceptor_stop:
1885         the_lnet.ln_refcount = 0;
1886         lnet_acceptor_stop();
1887 err_destroy_routes:
1888         if (!the_lnet.ln_nis_from_mod_params)
1889                 lnet_destroy_routes();
1890 err_shutdown_lndnis:
1891         lnet_shutdown_lndnets();
1892 err_empty_list:
1893         lnet_unprepare();
1894         LASSERT(rc < 0);
1895         mutex_unlock(&the_lnet.ln_api_mutex);
1896         while (!list_empty(&net_head)) {
1897                 struct lnet_net *net;
1898
1899                 net = list_entry(net_head.next, struct lnet_net, net_list);
1900                 list_del_init(&net->net_list);
1901                 lnet_net_free(net);
1902         }
1903         return rc;
1904 }
1905 EXPORT_SYMBOL(LNetNIInit);
1906
1907 /**
1908  * Stop LNet interfaces, routing, and forwarding.
1909  *
1910  * Users must call this function once for each successful call to LNetNIInit().
1911  * Once the LNetNIFini() operation has been started, the results of pending
1912  * API operations are undefined.
1913  *
1914  * \return always 0 for current implementation.
1915  */
1916 int
1917 LNetNIFini()
1918 {
1919         mutex_lock(&the_lnet.ln_api_mutex);
1920
1921         LASSERT(the_lnet.ln_refcount > 0);
1922
1923         if (the_lnet.ln_refcount != 1) {
1924                 the_lnet.ln_refcount--;
1925         } else {
1926                 LASSERT(!the_lnet.ln_niinit_self);
1927
1928                 lnet_fault_fini();
1929
1930                 lnet_proc_fini();
1931                 lnet_router_checker_stop();
1932                 lnet_ping_target_fini();
1933
1934                 /* Teardown fns that use my own API functions BEFORE here */
1935                 the_lnet.ln_refcount = 0;
1936
1937                 lnet_acceptor_stop();
1938                 lnet_destroy_routes();
1939                 lnet_shutdown_lndnets();
1940                 lnet_unprepare();
1941         }
1942
1943         mutex_unlock(&the_lnet.ln_api_mutex);
1944         return 0;
1945 }
1946 EXPORT_SYMBOL(LNetNIFini);
1947
1948 /**
1949  * Grabs the ni data from the ni structure and fills the out
1950  * parameters
1951  *
1952  * \param[in] ni network        interface structure
1953  * \param[out] cfg_ni           NI config information
1954  * \param[out] tun              network and LND tunables
1955  */
1956 static void
1957 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
1958                    struct lnet_ioctl_config_lnd_tunables *tun,
1959                    struct lnet_ioctl_element_stats *stats,
1960                    __u32 tun_size)
1961 {
1962         size_t min_size = 0;
1963         int i;
1964
1965         if (!ni || !cfg_ni || !tun)
1966                 return;
1967
1968         if (ni->ni_interfaces[0] != NULL) {
1969                 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
1970                         if (ni->ni_interfaces[i] != NULL) {
1971                                 strncpy(cfg_ni->lic_ni_intf[i],
1972                                         ni->ni_interfaces[i],
1973                                         sizeof(cfg_ni->lic_ni_intf[i]));
1974                         }
1975                 }
1976         }
1977
1978         cfg_ni->lic_nid = ni->ni_nid;
1979         cfg_ni->lic_status = ni->ni_status->ns_status;
1980         cfg_ni->lic_tcp_bonding = use_tcp_bonding;
1981         cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
1982
1983         memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
1984
1985         if (stats) {
1986                 stats->iel_send_count = atomic_read(&ni->ni_stats.send_count);
1987                 stats->iel_recv_count = atomic_read(&ni->ni_stats.recv_count);
1988         }
1989
1990         /*
1991          * tun->lt_tun will always be present, but in order to be
1992          * backwards compatible, we need to deal with the cases when
1993          * tun->lt_tun is smaller than what the kernel has, because it
1994          * comes from an older version of a userspace program, then we'll
1995          * need to copy as much information as we have available space.
1996          */
1997         min_size = tun_size - sizeof(tun->lt_cmn);
1998         memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
1999
2000         /* copy over the cpts */
2001         if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2002             ni->ni_cpts == NULL)  {
2003                 for (i = 0; i < ni->ni_ncpts; i++)
2004                         cfg_ni->lic_cpts[i] = i;
2005         } else {
2006                 for (i = 0;
2007                      ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2008                      i < LNET_MAX_SHOW_NUM_CPT;
2009                      i++)
2010                         cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2011         }
2012         cfg_ni->lic_ncpts = ni->ni_ncpts;
2013 }
2014
2015 /**
2016  * NOTE: This is a legacy function left in the code to be backwards
2017  * compatible with older userspace programs. It should eventually be
2018  * removed.
2019  *
2020  * Grabs the ni data from the ni structure and fills the out
2021  * parameters
2022  *
2023  * \param[in] ni network        interface structure
2024  * \param[out] config           config information
2025  */
2026 static void
2027 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2028                          struct lnet_ioctl_config_data *config)
2029 {
2030         struct lnet_ioctl_net_config *net_config;
2031         struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2032         size_t min_size, tunable_size = 0;
2033         int i;
2034
2035         if (!ni || !config)
2036                 return;
2037
2038         net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2039         if (!net_config)
2040                 return;
2041
2042         BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2043                      ARRAY_SIZE(net_config->ni_interfaces));
2044
2045         for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2046                 if (!ni->ni_interfaces[i])
2047                         break;
2048
2049                 strncpy(net_config->ni_interfaces[i],
2050                         ni->ni_interfaces[i],
2051                         sizeof(net_config->ni_interfaces[i]));
2052         }
2053
2054         config->cfg_nid = ni->ni_nid;
2055         config->cfg_config_u.cfg_net.net_peer_timeout =
2056                 ni->ni_net->net_tunables.lct_peer_timeout;
2057         config->cfg_config_u.cfg_net.net_max_tx_credits =
2058                 ni->ni_net->net_tunables.lct_max_tx_credits;
2059         config->cfg_config_u.cfg_net.net_peer_tx_credits =
2060                 ni->ni_net->net_tunables.lct_peer_tx_credits;
2061         config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2062                 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2063
2064         net_config->ni_status = ni->ni_status->ns_status;
2065
2066         if (ni->ni_cpts) {
2067                 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2068
2069                 for (i = 0; i < num_cpts; i++)
2070                         net_config->ni_cpts[i] = ni->ni_cpts[i];
2071
2072                 config->cfg_ncpts = num_cpts;
2073         }
2074
2075         /*
2076          * See if user land tools sent in a newer and larger version
2077          * of struct lnet_tunables than what the kernel uses.
2078          */
2079         min_size = sizeof(*config) + sizeof(*net_config);
2080
2081         if (config->cfg_hdr.ioc_len > min_size)
2082                 tunable_size = config->cfg_hdr.ioc_len - min_size;
2083
2084         /* Don't copy too much data to user space */
2085         min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2086         lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2087
2088         if (lnd_cfg && min_size) {
2089                 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2090                 config->cfg_config_u.cfg_net.net_interface_count = 1;
2091
2092                 /* Tell user land that kernel side has less data */
2093                 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2094                         min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2095                         config->cfg_hdr.ioc_len -= min_size;
2096                 }
2097         }
2098 }
2099
2100 struct lnet_ni *
2101 lnet_get_ni_idx_locked(int idx)
2102 {
2103         struct lnet_ni          *ni;
2104         struct lnet_net         *net;
2105
2106         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2107                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2108                         if (idx-- == 0)
2109                                 return ni;
2110                 }
2111         }
2112
2113         return NULL;
2114 }
2115
2116 struct lnet_ni *
2117 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2118 {
2119         struct lnet_ni          *ni;
2120         struct lnet_net         *net = mynet;
2121
2122         if (prev == NULL) {
2123                 if (net == NULL)
2124                         net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2125                                         net_list);
2126                 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2127                                 ni_netlist);
2128
2129                 return ni;
2130         }
2131
2132         if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2133                 /* if you reached the end of the ni list and the net is
2134                  * specified, then there are no more nis in that net */
2135                 if (net != NULL)
2136                         return NULL;
2137
2138                 /* we reached the end of this net ni list. move to the
2139                  * next net */
2140                 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2141                         /* no more nets and no more NIs. */
2142                         return NULL;
2143
2144                 /* get the next net */
2145                 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2146                                  net_list);
2147                 /* get the ni on it */
2148                 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2149                                 ni_netlist);
2150
2151                 return ni;
2152         }
2153
2154         /* there are more nis left */
2155         ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2156
2157         return ni;
2158 }
2159
2160 int
2161 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2162 {
2163         struct lnet_ni *ni;
2164         int cpt;
2165         int rc = -ENOENT;
2166         int idx = config->cfg_count;
2167
2168         cpt = lnet_net_lock_current();
2169
2170         ni = lnet_get_ni_idx_locked(idx);
2171
2172         if (ni != NULL) {
2173                 rc = 0;
2174                 lnet_ni_lock(ni);
2175                 lnet_fill_ni_info_legacy(ni, config);
2176                 lnet_ni_unlock(ni);
2177         }
2178
2179         lnet_net_unlock(cpt);
2180         return rc;
2181 }
2182
2183 int
2184 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2185                    struct lnet_ioctl_config_lnd_tunables *tun,
2186                    struct lnet_ioctl_element_stats *stats,
2187                    __u32 tun_size)
2188 {
2189         struct lnet_ni          *ni;
2190         int                     cpt;
2191         int                     rc = -ENOENT;
2192
2193         if (!cfg_ni || !tun || !stats)
2194                 return -EINVAL;
2195
2196         cpt = lnet_net_lock_current();
2197
2198         ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2199
2200         if (ni) {
2201                 rc = 0;
2202                 lnet_ni_lock(ni);
2203                 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
2204                 lnet_ni_unlock(ni);
2205         }
2206
2207         lnet_net_unlock(cpt);
2208         return rc;
2209 }
2210
2211 static int lnet_add_net_common(struct lnet_net *net,
2212                                struct lnet_ioctl_config_lnd_tunables *tun)
2213 {
2214         __u32                   net_id;
2215         struct lnet_ping_info   *pinfo;
2216         struct lnet_handle_md   md_handle;
2217         int                     rc;
2218         struct lnet_remotenet *rnet;
2219         int                     net_ni_count;
2220         int                     num_acceptor_nets;
2221
2222         lnet_net_lock(LNET_LOCK_EX);
2223         rnet = lnet_find_rnet_locked(net->net_id);
2224         lnet_net_unlock(LNET_LOCK_EX);
2225         /*
2226          * make sure that the net added doesn't invalidate the current
2227          * configuration LNet is keeping
2228          */
2229         if (rnet) {
2230                 CERROR("Adding net %s will invalidate routing configuration\n",
2231                        libcfs_net2str(net->net_id));
2232                 lnet_net_free(net);
2233                 return -EUSERS;
2234         }
2235
2236         /*
2237          * make sure you calculate the correct number of slots in the ping
2238          * info. Since the ping info is a flattened list of all the NIs,
2239          * we should allocate enough slots to accomodate the number of NIs
2240          * which will be added.
2241          *
2242          * since ni hasn't been configured yet, use
2243          * lnet_get_net_ni_count_pre() which checks the net_ni_added list
2244          */
2245         net_ni_count = lnet_get_net_ni_count_pre(net);
2246
2247         rc = lnet_ping_info_setup(&pinfo, &md_handle,
2248                                   net_ni_count + lnet_get_ni_count(),
2249                                   false);
2250         if (rc < 0) {
2251                 lnet_net_free(net);
2252                 return rc;
2253         }
2254
2255         if (tun)
2256                 memcpy(&net->net_tunables,
2257                        &tun->lt_cmn, sizeof(net->net_tunables));
2258         else
2259                 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
2260
2261         /*
2262          * before starting this network get a count of the current TCP
2263          * networks which require the acceptor thread running. If that
2264          * count is == 0 before we start up this network, then we'd want to
2265          * start up the acceptor thread after starting up this network
2266          */
2267         num_acceptor_nets = lnet_count_acceptor_nets();
2268
2269         net_id = net->net_id;
2270
2271         rc = lnet_startup_lndnet(net,
2272                                  (tun) ? &tun->lt_tun : NULL);
2273         if (rc < 0)
2274                 goto failed;
2275
2276         lnet_net_lock(LNET_LOCK_EX);
2277         net = lnet_get_net_locked(net_id);
2278         lnet_net_unlock(LNET_LOCK_EX);
2279
2280         LASSERT(net);
2281
2282         /*
2283          * Start the acceptor thread if this is the first network
2284          * being added that requires the thread.
2285          */
2286         if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
2287                 rc = lnet_acceptor_start();
2288                 if (rc < 0) {
2289                         /* shutdown the net that we just started */
2290                         CERROR("Failed to start up acceptor thread\n");
2291                         lnet_shutdown_lndnet(net);
2292                         goto failed;
2293                 }
2294         }
2295
2296         lnet_net_lock(LNET_LOCK_EX);
2297         lnet_peer_net_added(net);
2298         lnet_net_unlock(LNET_LOCK_EX);
2299
2300         lnet_ping_target_update(pinfo, md_handle);
2301
2302         return 0;
2303
2304 failed:
2305         lnet_ping_md_unlink(pinfo, &md_handle);
2306         lnet_ping_info_free(pinfo);
2307         return rc;
2308 }
2309
2310 static int lnet_handle_legacy_ip2nets(char *ip2nets,
2311                                       struct lnet_ioctl_config_lnd_tunables *tun)
2312 {
2313         struct lnet_net *net;
2314         char *nets;
2315         int rc;
2316         struct list_head net_head;
2317
2318         INIT_LIST_HEAD(&net_head);
2319
2320         rc = lnet_parse_ip2nets(&nets, ip2nets);
2321         if (rc < 0)
2322                 return rc;
2323
2324         rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2325         if (rc < 0)
2326                 return rc;
2327
2328         mutex_lock(&the_lnet.ln_api_mutex);
2329         while (!list_empty(&net_head)) {
2330                 net = list_entry(net_head.next, struct lnet_net, net_list);
2331                 list_del_init(&net->net_list);
2332                 rc = lnet_add_net_common(net, tun);
2333                 if (rc < 0)
2334                         goto out;
2335         }
2336
2337 out:
2338         mutex_unlock(&the_lnet.ln_api_mutex);
2339
2340         while (!list_empty(&net_head)) {
2341                 net = list_entry(net_head.next, struct lnet_net, net_list);
2342                 list_del_init(&net->net_list);
2343                 lnet_net_free(net);
2344         }
2345         return rc;
2346 }
2347
2348 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
2349 {
2350         struct lnet_net *net;
2351         struct lnet_ni *ni;
2352         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2353         int rc, i;
2354         __u32 net_id;
2355
2356         /* get the tunables if they are available */
2357         if (conf->lic_cfg_hdr.ioc_len >=
2358             sizeof(*conf) + sizeof(*tun))
2359                 tun = (struct lnet_ioctl_config_lnd_tunables *)
2360                         conf->lic_bulk;
2361
2362         /* handle legacy ip2nets from DLC */
2363         if (conf->lic_legacy_ip2nets[0] != '\0')
2364                 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
2365                                                   tun);
2366
2367         net_id = LNET_NIDNET(conf->lic_nid);
2368
2369         net = lnet_net_alloc(net_id, NULL);
2370         if (!net)
2371                 return -ENOMEM;
2372
2373         for (i = 0; i < conf->lic_ncpts; i++) {
2374                 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
2375                         return -EINVAL;
2376         }
2377
2378         ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
2379                                        conf->lic_ni_intf[0]);
2380         if (!ni)
2381                 return -ENOMEM;
2382
2383         mutex_lock(&the_lnet.ln_api_mutex);
2384
2385         rc = lnet_add_net_common(net, tun);
2386
2387         mutex_unlock(&the_lnet.ln_api_mutex);
2388
2389         return rc;
2390 }
2391
2392 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
2393 {
2394         struct lnet_net  *net;
2395         struct lnet_ni *ni;
2396         __u32 net_id = LNET_NIDNET(conf->lic_nid);
2397         struct lnet_ping_info *pinfo;
2398         struct lnet_handle_md md_handle;
2399         int               rc;
2400         int               net_count;
2401         __u32             addr;
2402
2403         /* don't allow userspace to shutdown the LOLND */
2404         if (LNET_NETTYP(net_id) == LOLND)
2405                 return -EINVAL;
2406
2407         mutex_lock(&the_lnet.ln_api_mutex);
2408
2409         lnet_net_lock(0);
2410
2411         net = lnet_get_net_locked(net_id);
2412         if (!net) {
2413                 CERROR("net %s not found\n",
2414                        libcfs_net2str(net_id));
2415                 rc = -ENOENT;
2416                 goto net_unlock;
2417         }
2418
2419         addr = LNET_NIDADDR(conf->lic_nid);
2420         if (addr == 0) {
2421                 /* remove the entire net */
2422                 net_count = lnet_get_net_ni_count_locked(net);
2423
2424                 lnet_net_unlock(0);
2425
2426                 /* create and link a new ping info, before removing the old one */
2427                 rc = lnet_ping_info_setup(&pinfo, &md_handle,
2428                                         lnet_get_ni_count() - net_count,
2429                                         false);
2430                 if (rc != 0)
2431                         goto out;
2432
2433                 lnet_shutdown_lndnet(net);
2434
2435                 if (lnet_count_acceptor_nets() == 0)
2436                         lnet_acceptor_stop();
2437
2438                 lnet_ping_target_update(pinfo, md_handle);
2439
2440                 goto out;
2441         }
2442
2443         ni = lnet_nid2ni_locked(conf->lic_nid, 0);
2444         if (!ni) {
2445                 CERROR("nid %s not found \n",
2446                        libcfs_nid2str(conf->lic_nid));
2447                 rc = -ENOENT;
2448                 goto net_unlock;
2449         }
2450
2451         net_count = lnet_get_net_ni_count_locked(net);
2452
2453         lnet_net_unlock(0);
2454
2455         /* create and link a new ping info, before removing the old one */
2456         rc = lnet_ping_info_setup(&pinfo, &md_handle,
2457                                   lnet_get_ni_count() - 1, false);
2458         if (rc != 0)
2459                 goto out;
2460
2461         lnet_shutdown_lndni(ni);
2462
2463         if (lnet_count_acceptor_nets() == 0)
2464                 lnet_acceptor_stop();
2465
2466         lnet_ping_target_update(pinfo, md_handle);
2467
2468         /* check if the net is empty and remove it if it is */
2469         if (net_count == 1)
2470                 lnet_shutdown_lndnet(net);
2471
2472         goto out;
2473
2474 net_unlock:
2475         lnet_net_unlock(0);
2476 out:
2477         mutex_unlock(&the_lnet.ln_api_mutex);
2478
2479         return rc;
2480 }
2481
2482 /*
2483  * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
2484  * They are only expected to be called for unique networks.
2485  * That can be as a result of older DLC library
2486  * calls. Multi-Rail DLC and beyond no longer uses these APIs.
2487  */
2488 int
2489 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
2490 {
2491         struct lnet_net         *net;
2492         struct list_head        net_head;
2493         int                     rc;
2494         struct lnet_ioctl_config_lnd_tunables tun;
2495         char *nets = conf->cfg_config_u.cfg_net.net_intf;
2496
2497         INIT_LIST_HEAD(&net_head);
2498
2499         /* Create a net/ni structures for the network string */
2500         rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2501         if (rc <= 0)
2502                 return rc == 0 ? -EINVAL : rc;
2503
2504         mutex_lock(&the_lnet.ln_api_mutex);
2505
2506         if (rc > 1) {
2507                 rc = -EINVAL; /* only add one network per call */
2508                 goto out_unlock_clean;
2509         }
2510
2511         net = list_entry(net_head.next, struct lnet_net, net_list);
2512         list_del_init(&net->net_list);
2513
2514         LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
2515
2516         memset(&tun, 0, sizeof(tun));
2517
2518         tun.lt_cmn.lct_peer_timeout =
2519           conf->cfg_config_u.cfg_net.net_peer_timeout;
2520         tun.lt_cmn.lct_peer_tx_credits =
2521           conf->cfg_config_u.cfg_net.net_peer_tx_credits;
2522         tun.lt_cmn.lct_peer_rtr_credits =
2523           conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
2524         tun.lt_cmn.lct_max_tx_credits =
2525           conf->cfg_config_u.cfg_net.net_max_tx_credits;
2526
2527         rc = lnet_add_net_common(net, &tun);
2528
2529 out_unlock_clean:
2530         mutex_unlock(&the_lnet.ln_api_mutex);
2531         while (!list_empty(&net_head)) {
2532                 /* net_head list is empty in success case */
2533                 net = list_entry(net_head.next, struct lnet_net, net_list);
2534                 list_del_init(&net->net_list);
2535                 lnet_net_free(net);
2536         }
2537         return rc;
2538 }
2539
2540 int
2541 lnet_dyn_del_net(__u32 net_id)
2542 {
2543         struct lnet_net  *net;
2544         struct lnet_ping_info *pinfo;
2545         struct lnet_handle_md md_handle;
2546         int               rc;
2547         int               net_ni_count;
2548
2549         /* don't allow userspace to shutdown the LOLND */
2550         if (LNET_NETTYP(net_id) == LOLND)
2551                 return -EINVAL;
2552
2553         mutex_lock(&the_lnet.ln_api_mutex);
2554
2555         lnet_net_lock(0);
2556
2557         net = lnet_get_net_locked(net_id);
2558         if (net == NULL) {
2559                 lnet_net_unlock(0);
2560                 rc = -EINVAL;
2561                 goto out;
2562         }
2563
2564         net_ni_count = lnet_get_net_ni_count_locked(net);
2565
2566         lnet_net_unlock(0);
2567
2568         /* create and link a new ping info, before removing the old one */
2569         rc = lnet_ping_info_setup(&pinfo, &md_handle,
2570                                   lnet_get_ni_count() - net_ni_count, false);
2571         if (rc != 0)
2572                 goto out;
2573
2574         lnet_shutdown_lndnet(net);
2575
2576         if (lnet_count_acceptor_nets() == 0)
2577                 lnet_acceptor_stop();
2578
2579         lnet_ping_target_update(pinfo, md_handle);
2580
2581 out:
2582         mutex_unlock(&the_lnet.ln_api_mutex);
2583
2584         return rc;
2585 }
2586
2587 void lnet_incr_dlc_seq(void)
2588 {
2589         atomic_inc(&lnet_dlc_seq_no);
2590 }
2591
2592 __u32 lnet_get_dlc_seq_locked(void)
2593 {
2594         return atomic_read(&lnet_dlc_seq_no);
2595 }
2596
2597 /**
2598  * LNet ioctl handler.
2599  *
2600  */
2601 int
2602 LNetCtl(unsigned int cmd, void *arg)
2603 {
2604         struct libcfs_ioctl_data *data = arg;
2605         struct lnet_ioctl_config_data *config;
2606         struct lnet_process_id    id = {0};
2607         struct lnet_ni           *ni;
2608         int                       rc;
2609
2610         BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
2611                      sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
2612
2613         switch (cmd) {
2614         case IOC_LIBCFS_GET_NI:
2615                 rc = LNetGetId(data->ioc_count, &id);
2616                 data->ioc_nid = id.nid;
2617                 return rc;
2618
2619         case IOC_LIBCFS_FAIL_NID:
2620                 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
2621
2622         case IOC_LIBCFS_ADD_ROUTE:
2623                 config = arg;
2624
2625                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2626                         return -EINVAL;
2627
2628                 mutex_lock(&the_lnet.ln_api_mutex);
2629                 rc = lnet_add_route(config->cfg_net,
2630                                     config->cfg_config_u.cfg_route.rtr_hop,
2631                                     config->cfg_nid,
2632                                     config->cfg_config_u.cfg_route.
2633                                         rtr_priority);
2634                 if (rc == 0) {
2635                         rc = lnet_check_routes();
2636                         if (rc != 0)
2637                                 lnet_del_route(config->cfg_net,
2638                                                config->cfg_nid);
2639                 }
2640                 mutex_unlock(&the_lnet.ln_api_mutex);
2641                 return rc;
2642
2643         case IOC_LIBCFS_DEL_ROUTE:
2644                 config = arg;
2645
2646                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2647                         return -EINVAL;
2648
2649                 mutex_lock(&the_lnet.ln_api_mutex);
2650                 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
2651                 mutex_unlock(&the_lnet.ln_api_mutex);
2652                 return rc;
2653
2654         case IOC_LIBCFS_GET_ROUTE:
2655                 config = arg;
2656
2657                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2658                         return -EINVAL;
2659
2660                 mutex_lock(&the_lnet.ln_api_mutex);
2661                 rc = lnet_get_route(config->cfg_count,
2662                                     &config->cfg_net,
2663                                     &config->cfg_config_u.cfg_route.rtr_hop,
2664                                     &config->cfg_nid,
2665                                     &config->cfg_config_u.cfg_route.rtr_flags,
2666                                     &config->cfg_config_u.cfg_route.
2667                                         rtr_priority);
2668                 mutex_unlock(&the_lnet.ln_api_mutex);
2669                 return rc;
2670
2671         case IOC_LIBCFS_GET_LOCAL_NI: {
2672                 struct lnet_ioctl_config_ni *cfg_ni;
2673                 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2674                 struct lnet_ioctl_element_stats *stats;
2675                 __u32 tun_size;
2676
2677                 cfg_ni = arg;
2678                 /* get the tunables if they are available */
2679                 if (cfg_ni->lic_cfg_hdr.ioc_len <
2680                     sizeof(*cfg_ni) + sizeof(*stats)+ sizeof(*tun))
2681                         return -EINVAL;
2682
2683                 stats = (struct lnet_ioctl_element_stats *)
2684                         cfg_ni->lic_bulk;
2685                 tun = (struct lnet_ioctl_config_lnd_tunables *)
2686                                 (cfg_ni->lic_bulk + sizeof(*stats));
2687
2688                 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
2689                         sizeof(*stats);
2690
2691                 mutex_lock(&the_lnet.ln_api_mutex);
2692                 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
2693                 mutex_unlock(&the_lnet.ln_api_mutex);
2694                 return rc;
2695         }
2696
2697         case IOC_LIBCFS_GET_NET: {
2698                 size_t total = sizeof(*config) +
2699                                sizeof(struct lnet_ioctl_net_config);
2700                 config = arg;
2701
2702                 if (config->cfg_hdr.ioc_len < total)
2703                         return -EINVAL;
2704
2705                 mutex_lock(&the_lnet.ln_api_mutex);
2706                 rc = lnet_get_net_config(config);
2707                 mutex_unlock(&the_lnet.ln_api_mutex);
2708                 return rc;
2709         }
2710
2711         case IOC_LIBCFS_GET_LNET_STATS:
2712         {
2713                 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
2714
2715                 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
2716                         return -EINVAL;
2717
2718                 mutex_lock(&the_lnet.ln_api_mutex);
2719                 lnet_counters_get(&lnet_stats->st_cntrs);
2720                 mutex_unlock(&the_lnet.ln_api_mutex);
2721                 return 0;
2722         }
2723
2724         case IOC_LIBCFS_CONFIG_RTR:
2725                 config = arg;
2726
2727                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2728                         return -EINVAL;
2729
2730                 mutex_lock(&the_lnet.ln_api_mutex);
2731                 if (config->cfg_config_u.cfg_buffers.buf_enable) {
2732                         rc = lnet_rtrpools_enable();
2733                         mutex_unlock(&the_lnet.ln_api_mutex);
2734                         return rc;
2735                 }
2736                 lnet_rtrpools_disable();
2737                 mutex_unlock(&the_lnet.ln_api_mutex);
2738                 return 0;
2739
2740         case IOC_LIBCFS_ADD_BUF:
2741                 config = arg;
2742
2743                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2744                         return -EINVAL;
2745
2746                 mutex_lock(&the_lnet.ln_api_mutex);
2747                 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
2748                                                 buf_tiny,
2749                                           config->cfg_config_u.cfg_buffers.
2750                                                 buf_small,
2751                                           config->cfg_config_u.cfg_buffers.
2752                                                 buf_large);
2753                 mutex_unlock(&the_lnet.ln_api_mutex);
2754                 return rc;
2755
2756         case IOC_LIBCFS_SET_NUMA_RANGE: {
2757                 struct lnet_ioctl_numa_range *numa;
2758                 numa = arg;
2759                 if (numa->nr_hdr.ioc_len != sizeof(*numa))
2760                         return -EINVAL;
2761                 mutex_lock(&the_lnet.ln_api_mutex);
2762                 lnet_numa_range = numa->nr_range;
2763                 mutex_unlock(&the_lnet.ln_api_mutex);
2764                 return 0;
2765         }
2766
2767         case IOC_LIBCFS_GET_NUMA_RANGE: {
2768                 struct lnet_ioctl_numa_range *numa;
2769                 numa = arg;
2770                 if (numa->nr_hdr.ioc_len != sizeof(*numa))
2771                         return -EINVAL;
2772                 numa->nr_range = lnet_numa_range;
2773                 return 0;
2774         }
2775
2776         case IOC_LIBCFS_GET_BUF: {
2777                 struct lnet_ioctl_pool_cfg *pool_cfg;
2778                 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2779
2780                 config = arg;
2781
2782                 if (config->cfg_hdr.ioc_len < total)
2783                         return -EINVAL;
2784
2785                 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2786
2787                 mutex_lock(&the_lnet.ln_api_mutex);
2788                 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2789                 mutex_unlock(&the_lnet.ln_api_mutex);
2790                 return rc;
2791         }
2792
2793         case IOC_LIBCFS_ADD_PEER_NI: {
2794                 struct lnet_ioctl_peer_cfg *cfg = arg;
2795
2796                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
2797                         return -EINVAL;
2798
2799                 mutex_lock(&the_lnet.ln_api_mutex);
2800                 rc = lnet_add_peer_ni_to_peer(cfg->prcfg_prim_nid,
2801                                               cfg->prcfg_cfg_nid,
2802                                               cfg->prcfg_mr);
2803                 mutex_unlock(&the_lnet.ln_api_mutex);
2804                 return rc;
2805         }
2806
2807         case IOC_LIBCFS_DEL_PEER_NI: {
2808                 struct lnet_ioctl_peer_cfg *cfg = arg;
2809
2810                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
2811                         return -EINVAL;
2812
2813                 mutex_lock(&the_lnet.ln_api_mutex);
2814                 rc = lnet_del_peer_ni_from_peer(cfg->prcfg_prim_nid,
2815                                                 cfg->prcfg_cfg_nid);
2816                 mutex_unlock(&the_lnet.ln_api_mutex);
2817                 return rc;
2818         }
2819
2820         case IOC_LIBCFS_GET_PEER_INFO: {
2821                 struct lnet_ioctl_peer *peer_info = arg;
2822
2823                 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2824                         return -EINVAL;
2825
2826                 mutex_lock(&the_lnet.ln_api_mutex);
2827                 rc = lnet_get_peer_ni_info(
2828                    peer_info->pr_count,
2829                    &peer_info->pr_nid,
2830                    peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2831                    &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2832                    &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2833                    &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2834                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2835                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2836                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
2837                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2838                 mutex_unlock(&the_lnet.ln_api_mutex);
2839                 return rc;
2840         }
2841
2842         case IOC_LIBCFS_GET_PEER_NI: {
2843                 struct lnet_ioctl_peer_cfg *cfg = arg;
2844                 struct lnet_peer_ni_credit_info __user *lpni_cri;
2845                 struct lnet_ioctl_element_stats __user *lpni_stats;
2846                 size_t usr_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2847
2848                 if ((cfg->prcfg_hdr.ioc_len != sizeof(*cfg)) ||
2849                     (cfg->prcfg_size != usr_size))
2850                         return -EINVAL;
2851
2852                 lpni_cri = cfg->prcfg_bulk;
2853                 lpni_stats = cfg->prcfg_bulk + sizeof(*lpni_cri);
2854
2855                 mutex_lock(&the_lnet.ln_api_mutex);
2856                 rc = lnet_get_peer_info(cfg->prcfg_count, &cfg->prcfg_prim_nid,
2857                                         &cfg->prcfg_cfg_nid, &cfg->prcfg_mr,
2858                                         lpni_cri, lpni_stats);
2859                 mutex_unlock(&the_lnet.ln_api_mutex);
2860                 return rc;
2861         }
2862
2863         case IOC_LIBCFS_NOTIFY_ROUTER: {
2864                 unsigned long jiffies_passed;
2865
2866                 jiffies_passed = ktime_get_real_seconds() - data->ioc_u64[0];
2867                 jiffies_passed = cfs_time_seconds(jiffies_passed);
2868
2869                 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2870                                    jiffies - jiffies_passed);
2871         }
2872
2873         case IOC_LIBCFS_LNET_DIST:
2874                 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2875                 if (rc < 0 && rc != -EHOSTUNREACH)
2876                         return rc;
2877
2878                 data->ioc_u32[0] = rc;
2879                 return 0;
2880
2881         case IOC_LIBCFS_TESTPROTOCOMPAT:
2882                 lnet_net_lock(LNET_LOCK_EX);
2883                 the_lnet.ln_testprotocompat = data->ioc_flags;
2884                 lnet_net_unlock(LNET_LOCK_EX);
2885                 return 0;
2886
2887         case IOC_LIBCFS_LNET_FAULT:
2888                 return lnet_fault_ctl(data->ioc_flags, data);
2889
2890         case IOC_LIBCFS_PING: {
2891                 signed long timeout;
2892
2893                 id.nid = data->ioc_nid;
2894                 id.pid = data->ioc_u32[0];
2895
2896                 /* Don't block longer than 2 minutes */
2897                 if (data->ioc_u32[1] > 120 * MSEC_PER_SEC)
2898                         return -EINVAL;
2899
2900                 /* If timestamp is negative then disable timeout */
2901                 if ((s32)data->ioc_u32[1] < 0)
2902                         timeout = MAX_SCHEDULE_TIMEOUT;
2903                 else
2904                         timeout = msecs_to_jiffies(data->ioc_u32[1]);
2905
2906                 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
2907                                data->ioc_plen1 / sizeof(struct lnet_process_id));
2908                 if (rc < 0)
2909                         return rc;
2910                 data->ioc_count = rc;
2911                 return 0;
2912         }
2913
2914         default:
2915                 ni = lnet_net2ni_addref(data->ioc_net);
2916                 if (ni == NULL)
2917                         return -EINVAL;
2918
2919                 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
2920                         rc = -EINVAL;
2921                 else
2922                         rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
2923
2924                 lnet_ni_decref(ni);
2925                 return rc;
2926         }
2927         /* not reached */
2928 }
2929 EXPORT_SYMBOL(LNetCtl);
2930
2931 void LNetDebugPeer(struct lnet_process_id id)
2932 {
2933         lnet_debug_peer(id.nid);
2934 }
2935 EXPORT_SYMBOL(LNetDebugPeer);
2936
2937 /**
2938  * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
2939  * Note that all interfaces share a same PID, as requested by LNetNIInit().
2940  *
2941  * \param index Index of the interface to look up.
2942  * \param id On successful return, this location will hold the
2943  * struct lnet_process_id ID of the interface.
2944  *
2945  * \retval 0 If an interface exists at \a index.
2946  * \retval -ENOENT If no interface has been found.
2947  */
2948 int
2949 LNetGetId(unsigned int index, struct lnet_process_id *id)
2950 {
2951         struct lnet_ni   *ni;
2952         struct lnet_net  *net;
2953         int               cpt;
2954         int               rc = -ENOENT;
2955
2956         LASSERT(the_lnet.ln_refcount > 0);
2957
2958         cpt = lnet_net_lock_current();
2959
2960         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2961                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2962                         if (index-- != 0)
2963                                 continue;
2964
2965                         id->nid = ni->ni_nid;
2966                         id->pid = the_lnet.ln_pid;
2967                         rc = 0;
2968                         break;
2969                 }
2970         }
2971
2972         lnet_net_unlock(cpt);
2973         return rc;
2974 }
2975 EXPORT_SYMBOL(LNetGetId);
2976
2977 static int lnet_ping(struct lnet_process_id id, signed long timeout,
2978                      struct lnet_process_id __user *ids, int n_ids)
2979 {
2980         struct lnet_handle_eq eqh;
2981         struct lnet_handle_md mdh;
2982         struct lnet_event event;
2983         struct lnet_md md = { NULL };
2984         int                  which;
2985         int                  unlinked = 0;
2986         int                  replied = 0;
2987         const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
2988         int                  infosz;
2989         struct lnet_ping_info *info;
2990         struct lnet_process_id tmpid;
2991         int                  i;
2992         int                  nob;
2993         int                  rc;
2994         int                  rc2;
2995         sigset_t         blocked;
2996
2997         infosz = offsetof(struct lnet_ping_info, pi_ni[n_ids]);
2998
2999         /* n_ids limit is arbitrary */
3000         if (n_ids <= 0 || n_ids > 20 || id.nid == LNET_NID_ANY)
3001                 return -EINVAL;
3002
3003         if (id.pid == LNET_PID_ANY)
3004                 id.pid = LNET_PID_LUSTRE;
3005
3006         LIBCFS_ALLOC(info, infosz);
3007         if (info == NULL)
3008                 return -ENOMEM;
3009
3010         /* NB 2 events max (including any unlink event) */
3011         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
3012         if (rc != 0) {
3013                 CERROR("Can't allocate EQ: %d\n", rc);
3014                 goto out_0;
3015         }
3016
3017         /* initialize md content */
3018         md.start     = info;
3019         md.length    = infosz;
3020         md.threshold = 2; /*GET/REPLY*/
3021         md.max_size  = 0;
3022         md.options   = LNET_MD_TRUNCATE;
3023         md.user_ptr  = NULL;
3024         md.eq_handle = eqh;
3025
3026         rc = LNetMDBind(md, LNET_UNLINK, &mdh);
3027         if (rc != 0) {
3028                 CERROR("Can't bind MD: %d\n", rc);
3029                 goto out_1;
3030         }
3031
3032         rc = LNetGet(LNET_NID_ANY, mdh, id,
3033                      LNET_RESERVED_PORTAL,
3034                      LNET_PROTO_PING_MATCHBITS, 0);
3035
3036         if (rc != 0) {
3037                 /* Don't CERROR; this could be deliberate! */
3038
3039                 rc2 = LNetMDUnlink(mdh);
3040                 LASSERT(rc2 == 0);
3041
3042                 /* NB must wait for the UNLINK event below... */
3043                 unlinked = 1;
3044                 timeout = a_long_time;
3045         }
3046
3047         do {
3048                 /* MUST block for unlink to complete */
3049                 if (unlinked)
3050                         blocked = cfs_block_allsigs();
3051
3052                 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
3053
3054                 if (unlinked)
3055                         cfs_restore_sigs(blocked);
3056
3057                 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
3058                        (rc2 <= 0) ? -1 : event.type,
3059                        (rc2 <= 0) ? -1 : event.status,
3060                        (rc2 > 0 && event.unlinked) ? " unlinked" : "");
3061
3062                 LASSERT(rc2 != -EOVERFLOW);     /* can't miss anything */
3063
3064                 if (rc2 <= 0 || event.status != 0) {
3065                         /* timeout or error */
3066                         if (!replied && rc == 0)
3067                                 rc = (rc2 < 0) ? rc2 :
3068                                      (rc2 == 0) ? -ETIMEDOUT :
3069                                      event.status;
3070
3071                         if (!unlinked) {
3072                                 /* Ensure completion in finite time... */
3073                                 LNetMDUnlink(mdh);
3074                                 /* No assertion (racing with network) */
3075                                 unlinked = 1;
3076                                 timeout = a_long_time;
3077                         } else if (rc2 == 0) {
3078                                 /* timed out waiting for unlink */
3079                                 CWARN("ping %s: late network completion\n",
3080                                       libcfs_id2str(id));
3081                         }
3082                 } else if (event.type == LNET_EVENT_REPLY) {
3083                         replied = 1;
3084                         rc = event.mlength;
3085                 }
3086
3087         } while (rc2 <= 0 || !event.unlinked);
3088
3089         if (!replied) {
3090                 if (rc >= 0)
3091                         CWARN("%s: Unexpected rc >= 0 but no reply!\n",
3092                               libcfs_id2str(id));
3093                 rc = -EIO;
3094                 goto out_1;
3095         }
3096
3097         nob = rc;
3098         LASSERT(nob >= 0 && nob <= infosz);
3099
3100         rc = -EPROTO;                           /* if I can't parse... */
3101
3102         if (nob < 8) {
3103                 /* can't check magic/version */
3104                 CERROR("%s: ping info too short %d\n",
3105                        libcfs_id2str(id), nob);
3106                 goto out_1;
3107         }
3108
3109         if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
3110                 lnet_swap_pinginfo(info);
3111         } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
3112                 CERROR("%s: Unexpected magic %08x\n",
3113                        libcfs_id2str(id), info->pi_magic);
3114                 goto out_1;
3115         }
3116
3117         if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
3118                 CERROR("%s: ping w/o NI status: 0x%x\n",
3119                        libcfs_id2str(id), info->pi_features);
3120                 goto out_1;
3121         }
3122
3123         if (nob < offsetof(struct lnet_ping_info, pi_ni[0])) {
3124                 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
3125                        nob, (int)offsetof(struct lnet_ping_info, pi_ni[0]));
3126                 goto out_1;
3127         }
3128
3129         if (info->pi_nnis < n_ids)
3130                 n_ids = info->pi_nnis;
3131
3132         if (nob < offsetof(struct lnet_ping_info, pi_ni[n_ids])) {
3133                 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
3134                        nob, (int)offsetof(struct lnet_ping_info, pi_ni[n_ids]));
3135                 goto out_1;
3136         }
3137
3138         rc = -EFAULT;                           /* If I SEGV... */
3139
3140         memset(&tmpid, 0, sizeof(tmpid));
3141         for (i = 0; i < n_ids; i++) {
3142                 tmpid.pid = info->pi_pid;
3143                 tmpid.nid = info->pi_ni[i].ns_nid;
3144                 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
3145                         goto out_1;
3146         }
3147         rc = info->pi_nnis;
3148
3149  out_1:
3150         rc2 = LNetEQFree(eqh);
3151         if (rc2 != 0)
3152                 CERROR("rc2 %d\n", rc2);
3153         LASSERT(rc2 == 0);
3154
3155  out_0:
3156         LIBCFS_FREE(info, infosz);
3157         return rc;
3158 }