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