Whamcloud - gitweb
LU-9679 lnet: discard lnet_print_text_bufs()
[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, 2017, 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
35 #include <linux/ctype.h>
36 #include <linux/log2.h>
37 #include <linux/ktime.h>
38 #include <linux/moduleparam.h>
39 #include <linux/uaccess.h>
40
41 #include <lnet/lib-lnet.h>
42
43 #define D_LNI D_CONSOLE
44
45 /*
46  * initialize ln_api_mutex statically, since it needs to be used in
47  * discovery_set callback. That module parameter callback can be called
48  * before module init completes. The mutex needs to be ready for use then.
49  */
50 struct lnet the_lnet = {
51         .ln_api_mutex = __MUTEX_INITIALIZER(the_lnet.ln_api_mutex),
52 };              /* THE state of the network */
53 EXPORT_SYMBOL(the_lnet);
54
55 static char *ip2nets = "";
56 module_param(ip2nets, charp, 0444);
57 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
58
59 static char *networks = "";
60 module_param(networks, charp, 0444);
61 MODULE_PARM_DESC(networks, "local networks");
62
63 static char *routes = "";
64 module_param(routes, charp, 0444);
65 MODULE_PARM_DESC(routes, "routes to non-local networks");
66
67 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
68 module_param(rnet_htable_size, int, 0444);
69 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
70
71 static int use_tcp_bonding = false;
72 module_param(use_tcp_bonding, int, 0444);
73 MODULE_PARM_DESC(use_tcp_bonding,
74                  "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
75
76 unsigned int lnet_numa_range = 0;
77 module_param(lnet_numa_range, uint, 0444);
78 MODULE_PARM_DESC(lnet_numa_range,
79                 "NUMA range to consider during Multi-Rail selection");
80
81 /*
82  * lnet_health_sensitivity determines by how much we decrement the health
83  * value on sending error. The value defaults to 100, which means health
84  * interface health is decremented by 100 points every failure.
85  */
86 unsigned int lnet_health_sensitivity = 100;
87 static int sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
88 #ifdef HAVE_KERNEL_PARAM_OPS
89 static struct kernel_param_ops param_ops_health_sensitivity = {
90         .set = sensitivity_set,
91         .get = param_get_int,
92 };
93 #define param_check_health_sensitivity(name, p) \
94                 __param_check(name, p, int)
95 module_param(lnet_health_sensitivity, health_sensitivity, S_IRUGO|S_IWUSR);
96 #else
97 module_param_call(lnet_health_sensitivity, sensitivity_set, param_get_int,
98                   &lnet_health_sensitivity, S_IRUGO|S_IWUSR);
99 #endif
100 MODULE_PARM_DESC(lnet_health_sensitivity,
101                 "Value to decrement the health value by on error");
102
103 /*
104  * lnet_recovery_interval determines how often we should perform recovery
105  * on unhealthy interfaces.
106  */
107 unsigned int lnet_recovery_interval = 1;
108 static int recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp);
109 #ifdef HAVE_KERNEL_PARAM_OPS
110 static struct kernel_param_ops param_ops_recovery_interval = {
111         .set = recovery_interval_set,
112         .get = param_get_int,
113 };
114 #define param_check_recovery_interval(name, p) \
115                 __param_check(name, p, int)
116 module_param(lnet_recovery_interval, recovery_interval, S_IRUGO|S_IWUSR);
117 #else
118 module_param_call(lnet_recovery_interval, recovery_interval_set, param_get_int,
119                   &lnet_recovery_interval, S_IRUGO|S_IWUSR);
120 #endif
121 MODULE_PARM_DESC(lnet_recovery_interval,
122                 "Interval to recover unhealthy interfaces in seconds");
123
124 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
125 static int intf_max_set(const char *val, cfs_kernel_param_arg_t *kp);
126
127 static struct kernel_param_ops param_ops_interfaces_max = {
128         .set = intf_max_set,
129         .get = param_get_int,
130 };
131
132 #define param_check_interfaces_max(name, p) \
133                 __param_check(name, p, int)
134
135 #ifdef HAVE_KERNEL_PARAM_OPS
136 module_param(lnet_interfaces_max, interfaces_max, 0644);
137 #else
138 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
139                   &param_ops_interfaces_max, 0644);
140 #endif
141 MODULE_PARM_DESC(lnet_interfaces_max,
142                 "Maximum number of interfaces in a node.");
143
144 unsigned lnet_peer_discovery_disabled = 0;
145 static int discovery_set(const char *val, cfs_kernel_param_arg_t *kp);
146
147 static struct kernel_param_ops param_ops_discovery_disabled = {
148         .set = discovery_set,
149         .get = param_get_int,
150 };
151
152 #define param_check_discovery_disabled(name, p) \
153                 __param_check(name, p, int)
154 #ifdef HAVE_KERNEL_PARAM_OPS
155 module_param(lnet_peer_discovery_disabled, discovery_disabled, 0644);
156 #else
157 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
158                   &param_ops_discovery_disabled, 0644);
159 #endif
160 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
161                 "Set to 1 to disable peer discovery on this node.");
162
163 unsigned int lnet_drop_asym_route;
164 static int drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp);
165
166 static struct kernel_param_ops param_ops_drop_asym_route = {
167         .set = drop_asym_route_set,
168         .get = param_get_int,
169 };
170
171 #define param_check_drop_asym_route(name, p)    \
172         __param_check(name, p, int)
173 #ifdef HAVE_KERNEL_PARAM_OPS
174 module_param(lnet_drop_asym_route, drop_asym_route, 0644);
175 #else
176 module_param_call(lnet_drop_asym_route, drop_asym_route_set, param_get_int,
177                   &param_ops_drop_asym_route, 0644);
178 #endif
179 MODULE_PARM_DESC(lnet_drop_asym_route,
180                  "Set to 1 to drop asymmetrical route messages.");
181
182 #define LNET_TRANSACTION_TIMEOUT_NO_HEALTH_DEFAULT 50
183 #define LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT 10
184
185 unsigned lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT;
186 static int transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp);
187 #ifdef HAVE_KERNEL_PARAM_OPS
188 static struct kernel_param_ops param_ops_transaction_timeout = {
189         .set = transaction_to_set,
190         .get = param_get_int,
191 };
192
193 #define param_check_transaction_timeout(name, p) \
194                 __param_check(name, p, int)
195 module_param(lnet_transaction_timeout, transaction_timeout, S_IRUGO|S_IWUSR);
196 #else
197 module_param_call(lnet_transaction_timeout, transaction_to_set, param_get_int,
198                   &lnet_transaction_timeout, S_IRUGO|S_IWUSR);
199 #endif
200 MODULE_PARM_DESC(lnet_transaction_timeout,
201                 "Maximum number of seconds to wait for a peer response.");
202
203 #define LNET_RETRY_COUNT_HEALTH_DEFAULT 3
204 unsigned lnet_retry_count = LNET_RETRY_COUNT_HEALTH_DEFAULT;
205 static int retry_count_set(const char *val, cfs_kernel_param_arg_t *kp);
206 #ifdef HAVE_KERNEL_PARAM_OPS
207 static struct kernel_param_ops param_ops_retry_count = {
208         .set = retry_count_set,
209         .get = param_get_int,
210 };
211
212 #define param_check_retry_count(name, p) \
213                 __param_check(name, p, int)
214 module_param(lnet_retry_count, retry_count, S_IRUGO|S_IWUSR);
215 #else
216 module_param_call(lnet_retry_count, retry_count_set, param_get_int,
217                   &lnet_retry_count, S_IRUGO|S_IWUSR);
218 #endif
219 MODULE_PARM_DESC(lnet_retry_count,
220                  "Maximum number of times to retry transmitting a message");
221
222
223 unsigned lnet_lnd_timeout = LNET_LND_DEFAULT_TIMEOUT;
224 unsigned int lnet_current_net_count;
225
226 /*
227  * This sequence number keeps track of how many times DLC was used to
228  * update the local NIs. It is incremented when a NI is added or
229  * removed and checked when sending a message to determine if there is
230  * a need to re-run the selection algorithm. See lnet_select_pathway()
231  * for more details on its usage.
232  */
233 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
234
235 static int lnet_ping(struct lnet_process_id id, signed long timeout,
236                      struct lnet_process_id __user *ids, int n_ids);
237
238 static int lnet_discover(struct lnet_process_id id, __u32 force,
239                          struct lnet_process_id __user *ids, int n_ids);
240
241 static int
242 sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
243 {
244         int rc;
245         unsigned *sensitivity = (unsigned *)kp->arg;
246         unsigned long value;
247
248         rc = kstrtoul(val, 0, &value);
249         if (rc) {
250                 CERROR("Invalid module parameter value for 'lnet_health_sensitivity'\n");
251                 return rc;
252         }
253
254         /*
255          * The purpose of locking the api_mutex here is to ensure that
256          * the correct value ends up stored properly.
257          */
258         mutex_lock(&the_lnet.ln_api_mutex);
259
260         if (value > LNET_MAX_HEALTH_VALUE) {
261                 mutex_unlock(&the_lnet.ln_api_mutex);
262                 CERROR("Invalid health value. Maximum: %d value = %lu\n",
263                        LNET_MAX_HEALTH_VALUE, value);
264                 return -EINVAL;
265         }
266
267         /*
268          * if we're turning on health then use the health timeout
269          * defaults.
270          */
271         if (*sensitivity == 0 && value != 0) {
272                 lnet_transaction_timeout = LNET_TRANSACTION_TIMEOUT_HEALTH_DEFAULT;
273                 lnet_retry_count = LNET_RETRY_COUNT_HEALTH_DEFAULT;
274         /*
275          * if we're turning off health then use the no health timeout
276          * default.
277          */
278         } else if (*sensitivity != 0 && value == 0) {
279                 lnet_transaction_timeout =
280                         LNET_TRANSACTION_TIMEOUT_NO_HEALTH_DEFAULT;
281                 lnet_retry_count = 0;
282         }
283
284         *sensitivity = value;
285
286         mutex_unlock(&the_lnet.ln_api_mutex);
287
288         return 0;
289 }
290
291 static int
292 recovery_interval_set(const char *val, cfs_kernel_param_arg_t *kp)
293 {
294         int rc;
295         unsigned *interval = (unsigned *)kp->arg;
296         unsigned long value;
297
298         rc = kstrtoul(val, 0, &value);
299         if (rc) {
300                 CERROR("Invalid module parameter value for 'lnet_recovery_interval'\n");
301                 return rc;
302         }
303
304         if (value < 1) {
305                 CERROR("lnet_recovery_interval must be at least 1 second\n");
306                 return -EINVAL;
307         }
308
309         /*
310          * The purpose of locking the api_mutex here is to ensure that
311          * the correct value ends up stored properly.
312          */
313         mutex_lock(&the_lnet.ln_api_mutex);
314
315         *interval = value;
316
317         mutex_unlock(&the_lnet.ln_api_mutex);
318
319         return 0;
320 }
321
322 static int
323 discovery_set(const char *val, cfs_kernel_param_arg_t *kp)
324 {
325         int rc;
326         unsigned *discovery = (unsigned *)kp->arg;
327         unsigned long value;
328         struct lnet_ping_buffer *pbuf;
329
330         rc = kstrtoul(val, 0, &value);
331         if (rc) {
332                 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
333                 return rc;
334         }
335
336         value = (value) ? 1 : 0;
337
338         /*
339          * The purpose of locking the api_mutex here is to ensure that
340          * the correct value ends up stored properly.
341          */
342         mutex_lock(&the_lnet.ln_api_mutex);
343
344         if (value == *discovery) {
345                 mutex_unlock(&the_lnet.ln_api_mutex);
346                 return 0;
347         }
348
349         *discovery = value;
350
351         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
352                 mutex_unlock(&the_lnet.ln_api_mutex);
353                 return 0;
354         }
355
356         /* tell peers that discovery setting has changed */
357         lnet_net_lock(LNET_LOCK_EX);
358         pbuf = the_lnet.ln_ping_target;
359         if (value)
360                 pbuf->pb_info.pi_features &= ~LNET_PING_FEAT_DISCOVERY;
361         else
362                 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
363         lnet_net_unlock(LNET_LOCK_EX);
364
365         lnet_push_update_to_peers(1);
366
367         mutex_unlock(&the_lnet.ln_api_mutex);
368
369         return 0;
370 }
371
372 static int
373 drop_asym_route_set(const char *val, cfs_kernel_param_arg_t *kp)
374 {
375         int rc;
376         unsigned int *drop_asym_route = (unsigned int *)kp->arg;
377         unsigned long value;
378
379         rc = kstrtoul(val, 0, &value);
380         if (rc) {
381                 CERROR("Invalid module parameter value for "
382                        "'lnet_drop_asym_route'\n");
383                 return rc;
384         }
385
386         /*
387          * The purpose of locking the api_mutex here is to ensure that
388          * the correct value ends up stored properly.
389          */
390         mutex_lock(&the_lnet.ln_api_mutex);
391
392         if (value == *drop_asym_route) {
393                 mutex_unlock(&the_lnet.ln_api_mutex);
394                 return 0;
395         }
396
397         *drop_asym_route = value;
398
399         mutex_unlock(&the_lnet.ln_api_mutex);
400
401         return 0;
402 }
403
404 static int
405 transaction_to_set(const char *val, cfs_kernel_param_arg_t *kp)
406 {
407         int rc;
408         unsigned *transaction_to = (unsigned *)kp->arg;
409         unsigned long value;
410
411         rc = kstrtoul(val, 0, &value);
412         if (rc) {
413                 CERROR("Invalid module parameter value for 'lnet_transaction_timeout'\n");
414                 return rc;
415         }
416
417         /*
418          * The purpose of locking the api_mutex here is to ensure that
419          * the correct value ends up stored properly.
420          */
421         mutex_lock(&the_lnet.ln_api_mutex);
422
423         if (value < lnet_retry_count || value == 0) {
424                 mutex_unlock(&the_lnet.ln_api_mutex);
425                 CERROR("Invalid value for lnet_transaction_timeout (%lu). "
426                        "Has to be greater than lnet_retry_count (%u)\n",
427                        value, lnet_retry_count);
428                 return -EINVAL;
429         }
430
431         if (value == *transaction_to) {
432                 mutex_unlock(&the_lnet.ln_api_mutex);
433                 return 0;
434         }
435
436         *transaction_to = value;
437         if (lnet_retry_count == 0)
438                 lnet_lnd_timeout = value;
439         else
440                 lnet_lnd_timeout = value / lnet_retry_count;
441
442         mutex_unlock(&the_lnet.ln_api_mutex);
443
444         return 0;
445 }
446
447 static int
448 retry_count_set(const char *val, cfs_kernel_param_arg_t *kp)
449 {
450         int rc;
451         unsigned *retry_count = (unsigned *)kp->arg;
452         unsigned long value;
453
454         rc = kstrtoul(val, 0, &value);
455         if (rc) {
456                 CERROR("Invalid module parameter value for 'lnet_retry_count'\n");
457                 return rc;
458         }
459
460         /*
461          * The purpose of locking the api_mutex here is to ensure that
462          * the correct value ends up stored properly.
463          */
464         mutex_lock(&the_lnet.ln_api_mutex);
465
466         if (lnet_health_sensitivity == 0) {
467                 mutex_unlock(&the_lnet.ln_api_mutex);
468                 CERROR("Can not set retry_count when health feature is turned off\n");
469                 return -EINVAL;
470         }
471
472         if (value > lnet_transaction_timeout) {
473                 mutex_unlock(&the_lnet.ln_api_mutex);
474                 CERROR("Invalid value for lnet_retry_count (%lu). "
475                        "Has to be smaller than lnet_transaction_timeout (%u)\n",
476                        value, lnet_transaction_timeout);
477                 return -EINVAL;
478         }
479
480         *retry_count = value;
481
482         if (value == 0)
483                 lnet_lnd_timeout = lnet_transaction_timeout;
484         else
485                 lnet_lnd_timeout = lnet_transaction_timeout / value;
486
487         mutex_unlock(&the_lnet.ln_api_mutex);
488
489         return 0;
490 }
491
492 static int
493 intf_max_set(const char *val, cfs_kernel_param_arg_t *kp)
494 {
495         int value, rc;
496
497         rc = kstrtoint(val, 0, &value);
498         if (rc) {
499                 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
500                 return rc;
501         }
502
503         if (value < LNET_INTERFACES_MIN) {
504                 CWARN("max interfaces provided are too small, setting to %d\n",
505                       LNET_INTERFACES_MAX_DEFAULT);
506                 value = LNET_INTERFACES_MAX_DEFAULT;
507         }
508
509         *(int *)kp->arg = value;
510
511         return 0;
512 }
513
514 static char *
515 lnet_get_routes(void)
516 {
517         return routes;
518 }
519
520 static char *
521 lnet_get_networks(void)
522 {
523         char   *nets;
524         int     rc;
525
526         if (*networks != 0 && *ip2nets != 0) {
527                 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
528                                    "'ip2nets' but not both at once\n");
529                 return NULL;
530         }
531
532         if (*ip2nets != 0) {
533                 rc = lnet_parse_ip2nets(&nets, ip2nets);
534                 return (rc == 0) ? nets : NULL;
535         }
536
537         if (*networks != 0)
538                 return networks;
539
540         return "tcp";
541 }
542
543 static void
544 lnet_init_locks(void)
545 {
546         spin_lock_init(&the_lnet.ln_eq_wait_lock);
547         spin_lock_init(&the_lnet.ln_msg_resend_lock);
548         init_waitqueue_head(&the_lnet.ln_eq_waitq);
549         init_completion(&the_lnet.ln_mt_wait_complete);
550         mutex_init(&the_lnet.ln_lnd_mutex);
551 }
552
553 struct kmem_cache *lnet_mes_cachep;        /* MEs kmem_cache */
554 struct kmem_cache *lnet_small_mds_cachep;  /* <= LNET_SMALL_MD_SIZE bytes
555                                             *  MDs kmem_cache */
556 struct kmem_cache *lnet_rspt_cachep;       /* response tracker cache */
557 struct kmem_cache *lnet_msg_cachep;
558
559 static int
560 lnet_slab_setup(void)
561 {
562         /* create specific kmem_cache for MEs and small MDs (i.e., originally
563          * allocated in <size-xxx> kmem_cache).
564          */
565         lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
566                                             0, 0, NULL);
567         if (!lnet_mes_cachep)
568                 return -ENOMEM;
569
570         lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
571                                                   LNET_SMALL_MD_SIZE, 0, 0,
572                                                   NULL);
573         if (!lnet_small_mds_cachep)
574                 return -ENOMEM;
575
576         lnet_rspt_cachep = kmem_cache_create("lnet_rspt", sizeof(struct lnet_rsp_tracker),
577                                             0, 0, NULL);
578         if (!lnet_rspt_cachep)
579                 return -ENOMEM;
580
581         lnet_msg_cachep = kmem_cache_create("lnet_msg", sizeof(struct lnet_msg),
582                                             0, 0, NULL);
583         if (!lnet_msg_cachep)
584                 return -ENOMEM;
585
586         return 0;
587 }
588
589 static void
590 lnet_slab_cleanup(void)
591 {
592         if (lnet_msg_cachep) {
593                 kmem_cache_destroy(lnet_msg_cachep);
594                 lnet_msg_cachep = NULL;
595         }
596
597
598         if (lnet_rspt_cachep) {
599                 kmem_cache_destroy(lnet_rspt_cachep);
600                 lnet_rspt_cachep = NULL;
601         }
602
603         if (lnet_small_mds_cachep) {
604                 kmem_cache_destroy(lnet_small_mds_cachep);
605                 lnet_small_mds_cachep = NULL;
606         }
607
608         if (lnet_mes_cachep) {
609                 kmem_cache_destroy(lnet_mes_cachep);
610                 lnet_mes_cachep = NULL;
611         }
612 }
613
614 static int
615 lnet_create_remote_nets_table(void)
616 {
617         int               i;
618         struct list_head *hash;
619
620         LASSERT(the_lnet.ln_remote_nets_hash == NULL);
621         LASSERT(the_lnet.ln_remote_nets_hbits > 0);
622         LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
623         if (hash == NULL) {
624                 CERROR("Failed to create remote nets hash table\n");
625                 return -ENOMEM;
626         }
627
628         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
629                 INIT_LIST_HEAD(&hash[i]);
630         the_lnet.ln_remote_nets_hash = hash;
631         return 0;
632 }
633
634 static void
635 lnet_destroy_remote_nets_table(void)
636 {
637         int i;
638
639         if (the_lnet.ln_remote_nets_hash == NULL)
640                 return;
641
642         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
643                 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
644
645         LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
646                     LNET_REMOTE_NETS_HASH_SIZE *
647                     sizeof(the_lnet.ln_remote_nets_hash[0]));
648         the_lnet.ln_remote_nets_hash = NULL;
649 }
650
651 static void
652 lnet_destroy_locks(void)
653 {
654         if (the_lnet.ln_res_lock != NULL) {
655                 cfs_percpt_lock_free(the_lnet.ln_res_lock);
656                 the_lnet.ln_res_lock = NULL;
657         }
658
659         if (the_lnet.ln_net_lock != NULL) {
660                 cfs_percpt_lock_free(the_lnet.ln_net_lock);
661                 the_lnet.ln_net_lock = NULL;
662         }
663 }
664
665 static int
666 lnet_create_locks(void)
667 {
668         lnet_init_locks();
669
670         the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
671         if (the_lnet.ln_res_lock == NULL)
672                 goto failed;
673
674         the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
675         if (the_lnet.ln_net_lock == NULL)
676                 goto failed;
677
678         return 0;
679
680  failed:
681         lnet_destroy_locks();
682         return -ENOMEM;
683 }
684
685 static void lnet_assert_wire_constants(void)
686 {
687         /* Wire protocol assertions generated by 'wirecheck'
688          * running on Linux robert.bartonsoftware.com 2.6.8-1.521
689          * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
690          * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
691
692         /* Constants... */
693         BUILD_BUG_ON(LNET_PROTO_TCP_MAGIC != 0xeebc0ded);
694         BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MAJOR != 1);
695         BUILD_BUG_ON(LNET_PROTO_TCP_VERSION_MINOR != 0);
696         BUILD_BUG_ON(LNET_MSG_ACK != 0);
697         BUILD_BUG_ON(LNET_MSG_PUT != 1);
698         BUILD_BUG_ON(LNET_MSG_GET != 2);
699         BUILD_BUG_ON(LNET_MSG_REPLY != 3);
700         BUILD_BUG_ON(LNET_MSG_HELLO != 4);
701
702         /* Checks for struct lnet_handle_wire */
703         BUILD_BUG_ON((int)sizeof(struct lnet_handle_wire) != 16);
704         BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
705                                    wh_interface_cookie) != 0);
706         BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) != 8);
707         BUILD_BUG_ON((int)offsetof(struct lnet_handle_wire,
708                                    wh_object_cookie) != 8);
709         BUILD_BUG_ON((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) != 8);
710
711         /* Checks for struct struct lnet_magicversion */
712         BUILD_BUG_ON((int)sizeof(struct lnet_magicversion) != 8);
713         BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, magic) != 0);
714         BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->magic) != 4);
715         BUILD_BUG_ON((int)offsetof(struct lnet_magicversion, version_major) != 4);
716         BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_major) != 2);
717         BUILD_BUG_ON((int)offsetof(struct lnet_magicversion,
718                                    version_minor) != 6);
719         BUILD_BUG_ON((int)sizeof(((struct lnet_magicversion *)0)->version_minor) != 2);
720
721         /* Checks for struct struct lnet_hdr */
722         BUILD_BUG_ON((int)sizeof(struct lnet_hdr) != 72);
723         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_nid) != 0);
724         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_nid) != 8);
725         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_nid) != 8);
726         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_nid) != 8);
727         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, dest_pid) != 16);
728         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->dest_pid) != 4);
729         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, src_pid) != 20);
730         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->src_pid) != 4);
731         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, type) != 24);
732         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->type) != 4);
733         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, payload_length) != 28);
734         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->payload_length) != 4);
735         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg) != 32);
736         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg) != 40);
737
738         /* Ack */
739         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) != 32);
740         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) != 16);
741         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.match_bits) != 48);
742         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) != 8);
743         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.ack.mlength) != 56);
744         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) != 4);
745
746         /* Put */
747         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) != 32);
748         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) != 16);
749         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.match_bits) != 48);
750         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) != 8);
751         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.hdr_data) != 56);
752         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) != 8);
753         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.ptl_index) != 64);
754         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) != 4);
755         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.put.offset) != 68);
756         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) != 4);
757
758         /* Get */
759         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.return_wmd) != 32);
760         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) != 16);
761         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.match_bits) != 48);
762         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) != 8);
763         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.ptl_index) != 56);
764         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) != 4);
765         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.src_offset) != 60);
766         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) != 4);
767         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.get.sink_length) != 64);
768         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) != 4);
769
770         /* Reply */
771         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) != 32);
772         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) != 16);
773
774         /* Hello */
775         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.incarnation) != 32);
776         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) != 8);
777         BUILD_BUG_ON((int)offsetof(struct lnet_hdr, msg.hello.type) != 40);
778         BUILD_BUG_ON((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) != 4);
779
780         /* Checks for struct lnet_ni_status and related constants */
781         BUILD_BUG_ON(LNET_NI_STATUS_INVALID != 0x00000000);
782         BUILD_BUG_ON(LNET_NI_STATUS_UP != 0x15aac0de);
783         BUILD_BUG_ON(LNET_NI_STATUS_DOWN != 0xdeadface);
784
785         /* Checks for struct lnet_ni_status */
786         BUILD_BUG_ON((int)sizeof(struct lnet_ni_status) != 16);
787         BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_nid) != 0);
788         BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) != 8);
789         BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_status) != 8);
790         BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_status) != 4);
791         BUILD_BUG_ON((int)offsetof(struct lnet_ni_status, ns_unused) != 12);
792         BUILD_BUG_ON((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) != 4);
793
794         /* Checks for struct lnet_ping_info and related constants */
795         BUILD_BUG_ON(LNET_PROTO_PING_MAGIC != 0x70696E67);
796         BUILD_BUG_ON(LNET_PING_FEAT_INVAL != 0);
797         BUILD_BUG_ON(LNET_PING_FEAT_BASE != 1);
798         BUILD_BUG_ON(LNET_PING_FEAT_NI_STATUS != 2);
799         BUILD_BUG_ON(LNET_PING_FEAT_RTE_DISABLED != 4);
800         BUILD_BUG_ON(LNET_PING_FEAT_MULTI_RAIL != 8);
801         BUILD_BUG_ON(LNET_PING_FEAT_DISCOVERY != 16);
802         BUILD_BUG_ON(LNET_PING_FEAT_BITS != 31);
803
804         /* Checks for struct lnet_ping_info */
805         BUILD_BUG_ON((int)sizeof(struct lnet_ping_info) != 16);
806         BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_magic) != 0);
807         BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) != 4);
808         BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_features) != 4);
809         BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_features) != 4);
810         BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_pid) != 8);
811         BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) != 4);
812         BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_nnis) != 12);
813         BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) != 4);
814         BUILD_BUG_ON((int)offsetof(struct lnet_ping_info, pi_ni) != 16);
815         BUILD_BUG_ON((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) != 0);
816 }
817
818 static const struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
819 {
820         const struct lnet_lnd *lnd;
821
822         /* holding lnd mutex */
823         if (type >= NUM_LNDS)
824                 return NULL;
825         lnd = the_lnet.ln_lnds[type];
826         LASSERT(!lnd || lnd->lnd_type == type);
827
828         return lnd;
829 }
830
831 unsigned int
832 lnet_get_lnd_timeout(void)
833 {
834         return lnet_lnd_timeout;
835 }
836 EXPORT_SYMBOL(lnet_get_lnd_timeout);
837
838 void
839 lnet_register_lnd(const struct lnet_lnd *lnd)
840 {
841         mutex_lock(&the_lnet.ln_lnd_mutex);
842
843         LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
844         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
845
846         the_lnet.ln_lnds[lnd->lnd_type] = lnd;
847
848         CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
849
850         mutex_unlock(&the_lnet.ln_lnd_mutex);
851 }
852 EXPORT_SYMBOL(lnet_register_lnd);
853
854 void
855 lnet_unregister_lnd(const struct lnet_lnd *lnd)
856 {
857         mutex_lock(&the_lnet.ln_lnd_mutex);
858
859         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
860
861         the_lnet.ln_lnds[lnd->lnd_type] = NULL;
862         CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
863
864         mutex_unlock(&the_lnet.ln_lnd_mutex);
865 }
866 EXPORT_SYMBOL(lnet_unregister_lnd);
867
868 void
869 lnet_counters_get_common(struct lnet_counters_common *common)
870 {
871         struct lnet_counters *ctr;
872         int i;
873
874         memset(common, 0, sizeof(*common));
875
876         lnet_net_lock(LNET_LOCK_EX);
877
878         cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
879                 common->lcc_msgs_max     += ctr->lct_common.lcc_msgs_max;
880                 common->lcc_msgs_alloc   += ctr->lct_common.lcc_msgs_alloc;
881                 common->lcc_errors       += ctr->lct_common.lcc_errors;
882                 common->lcc_send_count   += ctr->lct_common.lcc_send_count;
883                 common->lcc_recv_count   += ctr->lct_common.lcc_recv_count;
884                 common->lcc_route_count  += ctr->lct_common.lcc_route_count;
885                 common->lcc_drop_count   += ctr->lct_common.lcc_drop_count;
886                 common->lcc_send_length  += ctr->lct_common.lcc_send_length;
887                 common->lcc_recv_length  += ctr->lct_common.lcc_recv_length;
888                 common->lcc_route_length += ctr->lct_common.lcc_route_length;
889                 common->lcc_drop_length  += ctr->lct_common.lcc_drop_length;
890         }
891         lnet_net_unlock(LNET_LOCK_EX);
892 }
893 EXPORT_SYMBOL(lnet_counters_get_common);
894
895 void
896 lnet_counters_get(struct lnet_counters *counters)
897 {
898         struct lnet_counters *ctr;
899         struct lnet_counters_health *health = &counters->lct_health;
900         int             i;
901
902         memset(counters, 0, sizeof(*counters));
903
904         lnet_counters_get_common(&counters->lct_common);
905
906         lnet_net_lock(LNET_LOCK_EX);
907
908         cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
909                 health->lch_rst_alloc    += ctr->lct_health.lch_rst_alloc;
910                 health->lch_resend_count += ctr->lct_health.lch_resend_count;
911                 health->lch_response_timeout_count +=
912                                 ctr->lct_health.lch_response_timeout_count;
913                 health->lch_local_interrupt_count +=
914                                 ctr->lct_health.lch_local_interrupt_count;
915                 health->lch_local_dropped_count +=
916                                 ctr->lct_health.lch_local_dropped_count;
917                 health->lch_local_aborted_count +=
918                                 ctr->lct_health.lch_local_aborted_count;
919                 health->lch_local_no_route_count +=
920                                 ctr->lct_health.lch_local_no_route_count;
921                 health->lch_local_timeout_count +=
922                                 ctr->lct_health.lch_local_timeout_count;
923                 health->lch_local_error_count +=
924                                 ctr->lct_health.lch_local_error_count;
925                 health->lch_remote_dropped_count +=
926                                 ctr->lct_health.lch_remote_dropped_count;
927                 health->lch_remote_error_count +=
928                                 ctr->lct_health.lch_remote_error_count;
929                 health->lch_remote_timeout_count +=
930                                 ctr->lct_health.lch_remote_timeout_count;
931                 health->lch_network_timeout_count +=
932                                 ctr->lct_health.lch_network_timeout_count;
933         }
934         lnet_net_unlock(LNET_LOCK_EX);
935 }
936 EXPORT_SYMBOL(lnet_counters_get);
937
938 void
939 lnet_counters_reset(void)
940 {
941         struct lnet_counters *counters;
942         int             i;
943
944         lnet_net_lock(LNET_LOCK_EX);
945
946         cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
947                 memset(counters, 0, sizeof(struct lnet_counters));
948
949         lnet_net_unlock(LNET_LOCK_EX);
950 }
951
952 static char *
953 lnet_res_type2str(int type)
954 {
955         switch (type) {
956         default:
957                 LBUG();
958         case LNET_COOKIE_TYPE_MD:
959                 return "MD";
960         case LNET_COOKIE_TYPE_ME:
961                 return "ME";
962         case LNET_COOKIE_TYPE_EQ:
963                 return "EQ";
964         }
965 }
966
967 static void
968 lnet_res_container_cleanup(struct lnet_res_container *rec)
969 {
970         int     count = 0;
971
972         if (rec->rec_type == 0) /* not set yet, it's uninitialized */
973                 return;
974
975         while (!list_empty(&rec->rec_active)) {
976                 struct list_head *e = rec->rec_active.next;
977
978                 list_del_init(e);
979                 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
980                         lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
981
982                 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
983                         lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
984
985                 } else { /* NB: Active MEs should be attached on portals */
986                         LBUG();
987                 }
988                 count++;
989         }
990
991         if (count > 0) {
992                 /* Found alive MD/ME/EQ, user really should unlink/free
993                  * all of them before finalize LNet, but if someone didn't,
994                  * we have to recycle garbage for him */
995                 CERROR("%d active elements on exit of %s container\n",
996                        count, lnet_res_type2str(rec->rec_type));
997         }
998
999         if (rec->rec_lh_hash != NULL) {
1000                 LIBCFS_FREE(rec->rec_lh_hash,
1001                             LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1002                 rec->rec_lh_hash = NULL;
1003         }
1004
1005         rec->rec_type = 0; /* mark it as finalized */
1006 }
1007
1008 static int
1009 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
1010 {
1011         int     rc = 0;
1012         int     i;
1013
1014         LASSERT(rec->rec_type == 0);
1015
1016         rec->rec_type = type;
1017         INIT_LIST_HEAD(&rec->rec_active);
1018
1019         rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
1020
1021         /* Arbitrary choice of hash table size */
1022         LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
1023                          LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
1024         if (rec->rec_lh_hash == NULL) {
1025                 rc = -ENOMEM;
1026                 goto out;
1027         }
1028
1029         for (i = 0; i < LNET_LH_HASH_SIZE; i++)
1030                 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
1031
1032         return 0;
1033
1034 out:
1035         CERROR("Failed to setup %s resource container\n",
1036                lnet_res_type2str(type));
1037         lnet_res_container_cleanup(rec);
1038         return rc;
1039 }
1040
1041 static void
1042 lnet_res_containers_destroy(struct lnet_res_container **recs)
1043 {
1044         struct lnet_res_container       *rec;
1045         int                             i;
1046
1047         cfs_percpt_for_each(rec, i, recs)
1048                 lnet_res_container_cleanup(rec);
1049
1050         cfs_percpt_free(recs);
1051 }
1052
1053 static struct lnet_res_container **
1054 lnet_res_containers_create(int type)
1055 {
1056         struct lnet_res_container       **recs;
1057         struct lnet_res_container       *rec;
1058         int                             rc;
1059         int                             i;
1060
1061         recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
1062         if (recs == NULL) {
1063                 CERROR("Failed to allocate %s resource containers\n",
1064                        lnet_res_type2str(type));
1065                 return NULL;
1066         }
1067
1068         cfs_percpt_for_each(rec, i, recs) {
1069                 rc = lnet_res_container_setup(rec, i, type);
1070                 if (rc != 0) {
1071                         lnet_res_containers_destroy(recs);
1072                         return NULL;
1073                 }
1074         }
1075
1076         return recs;
1077 }
1078
1079 struct lnet_libhandle *
1080 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
1081 {
1082         /* ALWAYS called with lnet_res_lock held */
1083         struct list_head        *head;
1084         struct lnet_libhandle   *lh;
1085         unsigned int            hash;
1086
1087         if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
1088                 return NULL;
1089
1090         hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
1091         head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
1092
1093         list_for_each_entry(lh, head, lh_hash_chain) {
1094                 if (lh->lh_cookie == cookie)
1095                         return lh;
1096         }
1097
1098         return NULL;
1099 }
1100
1101 void
1102 lnet_res_lh_initialize(struct lnet_res_container *rec,
1103                        struct lnet_libhandle *lh)
1104 {
1105         /* ALWAYS called with lnet_res_lock held */
1106         unsigned int    ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
1107         unsigned int    hash;
1108
1109         lh->lh_cookie = rec->rec_lh_cookie;
1110         rec->rec_lh_cookie += 1 << ibits;
1111
1112         hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
1113
1114         list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
1115 }
1116
1117 struct list_head **
1118 lnet_create_array_of_queues(void)
1119 {
1120         struct list_head **qs;
1121         struct list_head *q;
1122         int i;
1123
1124         qs = cfs_percpt_alloc(lnet_cpt_table(),
1125                               sizeof(struct list_head));
1126         if (!qs) {
1127                 CERROR("Failed to allocate queues\n");
1128                 return NULL;
1129         }
1130
1131         cfs_percpt_for_each(q, i, qs)
1132                 INIT_LIST_HEAD(q);
1133
1134         return qs;
1135 }
1136
1137 static int lnet_unprepare(void);
1138
1139 static int
1140 lnet_prepare(lnet_pid_t requested_pid)
1141 {
1142         /* Prepare to bring up the network */
1143         struct lnet_res_container **recs;
1144         int                       rc = 0;
1145
1146         if (requested_pid == LNET_PID_ANY) {
1147                 /* Don't instantiate LNET just for me */
1148                 return -ENETDOWN;
1149         }
1150
1151         LASSERT(the_lnet.ln_refcount == 0);
1152
1153         the_lnet.ln_routing = 0;
1154
1155         LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
1156         the_lnet.ln_pid = requested_pid;
1157
1158         INIT_LIST_HEAD(&the_lnet.ln_test_peers);
1159         INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
1160         INIT_LIST_HEAD(&the_lnet.ln_nets);
1161         INIT_LIST_HEAD(&the_lnet.ln_routers);
1162         INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
1163         INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
1164         INIT_LIST_HEAD(&the_lnet.ln_dc_request);
1165         INIT_LIST_HEAD(&the_lnet.ln_dc_working);
1166         INIT_LIST_HEAD(&the_lnet.ln_dc_expired);
1167         INIT_LIST_HEAD(&the_lnet.ln_mt_localNIRecovq);
1168         INIT_LIST_HEAD(&the_lnet.ln_mt_peerNIRecovq);
1169         init_waitqueue_head(&the_lnet.ln_dc_waitq);
1170         LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
1171         init_completion(&the_lnet.ln_started);
1172
1173         rc = lnet_slab_setup();
1174         if (rc != 0)
1175                 goto failed;
1176
1177         rc = lnet_create_remote_nets_table();
1178         if (rc != 0)
1179                 goto failed;
1180
1181         /*
1182          * NB the interface cookie in wire handles guards against delayed
1183          * replies and ACKs appearing valid after reboot.
1184          */
1185         the_lnet.ln_interface_cookie = ktime_get_real_ns();
1186
1187         the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
1188                                                 sizeof(struct lnet_counters));
1189         if (the_lnet.ln_counters == NULL) {
1190                 CERROR("Failed to allocate counters for LNet\n");
1191                 rc = -ENOMEM;
1192                 goto failed;
1193         }
1194
1195         rc = lnet_peer_tables_create();
1196         if (rc != 0)
1197                 goto failed;
1198
1199         rc = lnet_msg_containers_create();
1200         if (rc != 0)
1201                 goto failed;
1202
1203         rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
1204                                       LNET_COOKIE_TYPE_EQ);
1205         if (rc != 0)
1206                 goto failed;
1207
1208         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
1209         if (recs == NULL) {
1210                 rc = -ENOMEM;
1211                 goto failed;
1212         }
1213
1214         the_lnet.ln_md_containers = recs;
1215
1216         rc = lnet_portals_create();
1217         if (rc != 0) {
1218                 CERROR("Failed to create portals for LNet: %d\n", rc);
1219                 goto failed;
1220         }
1221
1222         the_lnet.ln_mt_zombie_rstqs = lnet_create_array_of_queues();
1223         if (!the_lnet.ln_mt_zombie_rstqs) {
1224                 rc = -ENOMEM;
1225                 goto failed;
1226         }
1227
1228         return 0;
1229
1230  failed:
1231         lnet_unprepare();
1232         return rc;
1233 }
1234
1235 static int
1236 lnet_unprepare (void)
1237 {
1238         int rc;
1239
1240         /* NB no LNET_LOCK since this is the last reference.  All LND instances
1241          * have shut down already, so it is safe to unlink and free all
1242          * descriptors, even those that appear committed to a network op (eg MD
1243          * with non-zero pending count) */
1244
1245         lnet_fail_nid(LNET_NID_ANY, 0);
1246
1247         LASSERT(the_lnet.ln_refcount == 0);
1248         LASSERT(list_empty(&the_lnet.ln_test_peers));
1249         LASSERT(list_empty(&the_lnet.ln_nets));
1250
1251         if (the_lnet.ln_mt_zombie_rstqs) {
1252                 lnet_clean_zombie_rstqs();
1253                 the_lnet.ln_mt_zombie_rstqs = NULL;
1254         }
1255
1256         if (!LNetEQHandleIsInvalid(the_lnet.ln_mt_eqh)) {
1257                 rc = LNetEQFree(the_lnet.ln_mt_eqh);
1258                 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
1259                 LASSERT(rc == 0);
1260         }
1261
1262         lnet_portals_destroy();
1263
1264         if (the_lnet.ln_md_containers != NULL) {
1265                 lnet_res_containers_destroy(the_lnet.ln_md_containers);
1266                 the_lnet.ln_md_containers = NULL;
1267         }
1268
1269         lnet_res_container_cleanup(&the_lnet.ln_eq_container);
1270
1271         lnet_msg_containers_destroy();
1272         lnet_peer_uninit();
1273         lnet_rtrpools_free(0);
1274
1275         if (the_lnet.ln_counters != NULL) {
1276                 cfs_percpt_free(the_lnet.ln_counters);
1277                 the_lnet.ln_counters = NULL;
1278         }
1279         lnet_destroy_remote_nets_table();
1280         lnet_slab_cleanup();
1281
1282         return 0;
1283 }
1284
1285 struct lnet_ni  *
1286 lnet_net2ni_locked(__u32 net_id, int cpt)
1287 {
1288         struct lnet_ni   *ni;
1289         struct lnet_net  *net;
1290
1291         LASSERT(cpt != LNET_LOCK_EX);
1292
1293         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1294                 if (net->net_id == net_id) {
1295                         ni = list_entry(net->net_ni_list.next, struct lnet_ni,
1296                                         ni_netlist);
1297                         return ni;
1298                 }
1299         }
1300
1301         return NULL;
1302 }
1303
1304 struct lnet_ni *
1305 lnet_net2ni_addref(__u32 net)
1306 {
1307         struct lnet_ni *ni;
1308
1309         lnet_net_lock(0);
1310         ni = lnet_net2ni_locked(net, 0);
1311         if (ni)
1312                 lnet_ni_addref_locked(ni, 0);
1313         lnet_net_unlock(0);
1314
1315         return ni;
1316 }
1317 EXPORT_SYMBOL(lnet_net2ni_addref);
1318
1319 struct lnet_net *
1320 lnet_get_net_locked(__u32 net_id)
1321 {
1322         struct lnet_net  *net;
1323
1324         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1325                 if (net->net_id == net_id)
1326                         return net;
1327         }
1328
1329         return NULL;
1330 }
1331
1332 unsigned int
1333 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
1334 {
1335         __u64           key = nid;
1336         unsigned int    val;
1337
1338         LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1339
1340         if (number == 1)
1341                 return 0;
1342
1343         val = hash_long(key, LNET_CPT_BITS);
1344         /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
1345         if (val < number)
1346                 return val;
1347
1348         return (unsigned int)(key + val + (val >> 1)) % number;
1349 }
1350
1351 int
1352 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
1353 {
1354         struct lnet_net *net;
1355
1356         /* must called with hold of lnet_net_lock */
1357         if (LNET_CPT_NUMBER == 1)
1358                 return 0; /* the only one */
1359
1360         /*
1361          * If NI is provided then use the CPT identified in the NI cpt
1362          * list if one exists. If one doesn't exist, then that NI is
1363          * associated with all CPTs and it follows that the net it belongs
1364          * to is implicitly associated with all CPTs, so just hash the nid
1365          * and return that.
1366          */
1367         if (ni != NULL) {
1368                 if (ni->ni_cpts != NULL)
1369                         return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1370                                                              ni->ni_ncpts)];
1371                 else
1372                         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1373         }
1374
1375         /* no NI provided so look at the net */
1376         net = lnet_get_net_locked(LNET_NIDNET(nid));
1377
1378         if (net != NULL && net->net_cpts != NULL) {
1379                 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1380         }
1381
1382         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1383 }
1384
1385 int
1386 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
1387 {
1388         int     cpt;
1389         int     cpt2;
1390
1391         if (LNET_CPT_NUMBER == 1)
1392                 return 0; /* the only one */
1393
1394         cpt = lnet_net_lock_current();
1395
1396         cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1397
1398         lnet_net_unlock(cpt);
1399
1400         return cpt2;
1401 }
1402 EXPORT_SYMBOL(lnet_cpt_of_nid);
1403
1404 int
1405 lnet_islocalnet_locked(__u32 net_id)
1406 {
1407         struct lnet_net *net;
1408         bool local;
1409
1410         net = lnet_get_net_locked(net_id);
1411
1412         local = net != NULL;
1413
1414         return local;
1415 }
1416
1417 int
1418 lnet_islocalnet(__u32 net_id)
1419 {
1420         int cpt;
1421         bool local;
1422
1423         cpt = lnet_net_lock_current();
1424
1425         local = lnet_islocalnet_locked(net_id);
1426
1427         lnet_net_unlock(cpt);
1428
1429         return local;
1430 }
1431
1432 struct lnet_ni  *
1433 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
1434 {
1435         struct lnet_net  *net;
1436         struct lnet_ni   *ni;
1437
1438         LASSERT(cpt != LNET_LOCK_EX);
1439
1440         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1441                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1442                         if (ni->ni_nid == nid)
1443                                 return ni;
1444                 }
1445         }
1446
1447         return NULL;
1448 }
1449
1450 struct lnet_ni *
1451 lnet_nid2ni_addref(lnet_nid_t nid)
1452 {
1453         struct lnet_ni *ni;
1454
1455         lnet_net_lock(0);
1456         ni = lnet_nid2ni_locked(nid, 0);
1457         if (ni)
1458                 lnet_ni_addref_locked(ni, 0);
1459         lnet_net_unlock(0);
1460
1461         return ni;
1462 }
1463 EXPORT_SYMBOL(lnet_nid2ni_addref);
1464
1465 int
1466 lnet_islocalnid(lnet_nid_t nid)
1467 {
1468         struct lnet_ni  *ni;
1469         int             cpt;
1470
1471         cpt = lnet_net_lock_current();
1472         ni = lnet_nid2ni_locked(nid, cpt);
1473         lnet_net_unlock(cpt);
1474
1475         return ni != NULL;
1476 }
1477
1478 int
1479 lnet_count_acceptor_nets(void)
1480 {
1481         /* Return the # of NIs that need the acceptor. */
1482         int              count = 0;
1483         struct lnet_net  *net;
1484         int              cpt;
1485
1486         cpt = lnet_net_lock_current();
1487         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1488                 /* all socklnd type networks should have the acceptor
1489                  * thread started */
1490                 if (net->net_lnd->lnd_accept != NULL)
1491                         count++;
1492         }
1493
1494         lnet_net_unlock(cpt);
1495
1496         return count;
1497 }
1498
1499 struct lnet_ping_buffer *
1500 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1501 {
1502         struct lnet_ping_buffer *pbuf;
1503
1504         LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1505         if (pbuf) {
1506                 pbuf->pb_nnis = nnis;
1507                 atomic_set(&pbuf->pb_refcnt, 1);
1508         }
1509
1510         return pbuf;
1511 }
1512
1513 void
1514 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1515 {
1516         LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1517         LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1518 }
1519
1520 static struct lnet_ping_buffer *
1521 lnet_ping_target_create(int nnis)
1522 {
1523         struct lnet_ping_buffer *pbuf;
1524
1525         pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1526         if (pbuf == NULL) {
1527                 CERROR("Can't allocate ping source [%d]\n", nnis);
1528                 return NULL;
1529         }
1530
1531         pbuf->pb_info.pi_nnis = nnis;
1532         pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1533         pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1534         pbuf->pb_info.pi_features =
1535                 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1536
1537         return pbuf;
1538 }
1539
1540 static inline int
1541 lnet_get_net_ni_count_locked(struct lnet_net *net)
1542 {
1543         struct lnet_ni  *ni;
1544         int             count = 0;
1545
1546         list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1547                 count++;
1548
1549         return count;
1550 }
1551
1552 static inline int
1553 lnet_get_net_ni_count_pre(struct lnet_net *net)
1554 {
1555         struct lnet_ni  *ni;
1556         int             count = 0;
1557
1558         list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1559                 count++;
1560
1561         return count;
1562 }
1563
1564 static inline int
1565 lnet_get_ni_count(void)
1566 {
1567         struct lnet_ni  *ni;
1568         struct lnet_net *net;
1569         int             count = 0;
1570
1571         lnet_net_lock(0);
1572
1573         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1574                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1575                         count++;
1576         }
1577
1578         lnet_net_unlock(0);
1579
1580         return count;
1581 }
1582
1583 int
1584 lnet_get_net_count(void)
1585 {
1586         struct lnet_net *net;
1587         int count = 0;
1588
1589         lnet_net_lock(0);
1590
1591         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1592                 count++;
1593         }
1594
1595         lnet_net_unlock(0);
1596
1597         return count;
1598 }
1599
1600 void
1601 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1602 {
1603         struct lnet_ni_status *stat;
1604         int nnis;
1605         int i;
1606
1607         __swab32s(&pbuf->pb_info.pi_magic);
1608         __swab32s(&pbuf->pb_info.pi_features);
1609         __swab32s(&pbuf->pb_info.pi_pid);
1610         __swab32s(&pbuf->pb_info.pi_nnis);
1611         nnis = pbuf->pb_info.pi_nnis;
1612         if (nnis > pbuf->pb_nnis)
1613                 nnis = pbuf->pb_nnis;
1614         for (i = 0; i < nnis; i++) {
1615                 stat = &pbuf->pb_info.pi_ni[i];
1616                 __swab64s(&stat->ns_nid);
1617                 __swab32s(&stat->ns_status);
1618         }
1619 }
1620
1621 int
1622 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1623 {
1624         if (!pinfo)
1625                 return -EINVAL;
1626         if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1627                 return -EPROTO;
1628         if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1629                 return -EPROTO;
1630         /* Loopback is guaranteed to be present */
1631         if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1632                 return -ERANGE;
1633         if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1634                 return -EPROTO;
1635         return 0;
1636 }
1637
1638 static void
1639 lnet_ping_target_destroy(void)
1640 {
1641         struct lnet_net *net;
1642         struct lnet_ni  *ni;
1643
1644         lnet_net_lock(LNET_LOCK_EX);
1645
1646         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1647                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1648                         lnet_ni_lock(ni);
1649                         ni->ni_status = NULL;
1650                         lnet_ni_unlock(ni);
1651                 }
1652         }
1653
1654         lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1655         the_lnet.ln_ping_target = NULL;
1656
1657         lnet_net_unlock(LNET_LOCK_EX);
1658 }
1659
1660 static void
1661 lnet_ping_target_event_handler(struct lnet_event *event)
1662 {
1663         struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1664
1665         if (event->unlinked)
1666                 lnet_ping_buffer_decref(pbuf);
1667 }
1668
1669 static int
1670 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1671                        struct lnet_handle_md *ping_mdh,
1672                        int ni_count, bool set_eq)
1673 {
1674         struct lnet_process_id id = {
1675                 .nid = LNET_NID_ANY,
1676                 .pid = LNET_PID_ANY
1677         };
1678         struct lnet_me *me;
1679         struct lnet_md md = { NULL };
1680         int rc, rc2;
1681
1682         if (set_eq) {
1683                 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1684                                  &the_lnet.ln_ping_target_eq);
1685                 if (rc != 0) {
1686                         CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1687                         return rc;
1688                 }
1689         }
1690
1691         *ppbuf = lnet_ping_target_create(ni_count);
1692         if (*ppbuf == NULL) {
1693                 rc = -ENOMEM;
1694                 goto fail_free_eq;
1695         }
1696
1697         /* Ping target ME/MD */
1698         me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1699                           LNET_PROTO_PING_MATCHBITS, 0,
1700                           LNET_UNLINK, LNET_INS_AFTER);
1701         if (IS_ERR(me)) {
1702                 rc = PTR_ERR(me);
1703                 CERROR("Can't create ping target ME: %d\n", rc);
1704                 goto fail_decref_ping_buffer;
1705         }
1706
1707         /* initialize md content */
1708         md.start     = &(*ppbuf)->pb_info;
1709         md.length    = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1710         md.threshold = LNET_MD_THRESH_INF;
1711         md.max_size  = 0;
1712         md.options   = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1713                        LNET_MD_MANAGE_REMOTE;
1714         md.eq_handle = the_lnet.ln_ping_target_eq;
1715         md.user_ptr  = *ppbuf;
1716
1717         rc = LNetMDAttach(me, md, LNET_RETAIN, ping_mdh);
1718         if (rc != 0) {
1719                 CERROR("Can't attach ping target MD: %d\n", rc);
1720                 goto fail_unlink_ping_me;
1721         }
1722         lnet_ping_buffer_addref(*ppbuf);
1723
1724         return 0;
1725
1726 fail_unlink_ping_me:
1727         LNetMEUnlink(me);
1728 fail_decref_ping_buffer:
1729         LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1730         lnet_ping_buffer_decref(*ppbuf);
1731         *ppbuf = NULL;
1732 fail_free_eq:
1733         if (set_eq) {
1734                 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1735                 LASSERT(rc2 == 0);
1736         }
1737         return rc;
1738 }
1739
1740 static void
1741 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1742                     struct lnet_handle_md *ping_mdh)
1743 {
1744         sigset_t        blocked = cfs_block_allsigs();
1745
1746         LNetMDUnlink(*ping_mdh);
1747         LNetInvalidateMDHandle(ping_mdh);
1748
1749         /* NB the MD could be busy; this just starts the unlink */
1750         while (lnet_ping_buffer_numref(pbuf) > 1) {
1751                 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1752                 set_current_state(TASK_UNINTERRUPTIBLE);
1753                 schedule_timeout(cfs_time_seconds(1));
1754         }
1755
1756         cfs_restore_sigs(blocked);
1757 }
1758
1759 static void
1760 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1761 {
1762         struct lnet_ni          *ni;
1763         struct lnet_net         *net;
1764         struct lnet_ni_status *ns;
1765         int                     i;
1766         int                     rc;
1767
1768         i = 0;
1769         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1770                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1771                         LASSERT(i < pbuf->pb_nnis);
1772
1773                         ns = &pbuf->pb_info.pi_ni[i];
1774
1775                         ns->ns_nid = ni->ni_nid;
1776
1777                         lnet_ni_lock(ni);
1778                         ns->ns_status = (ni->ni_status != NULL) ?
1779                                          ni->ni_status->ns_status :
1780                                                 LNET_NI_STATUS_UP;
1781                         ni->ni_status = ns;
1782                         lnet_ni_unlock(ni);
1783
1784                         i++;
1785                 }
1786         }
1787         /*
1788          * We (ab)use the ns_status of the loopback interface to
1789          * transmit the sequence number. The first interface listed
1790          * must be the loopback interface.
1791          */
1792         rc = lnet_ping_info_validate(&pbuf->pb_info);
1793         if (rc) {
1794                 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1795                 LBUG();
1796         }
1797         LNET_PING_BUFFER_SEQNO(pbuf) =
1798                 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1799 }
1800
1801 static void
1802 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1803                         struct lnet_handle_md ping_mdh)
1804 {
1805         struct lnet_ping_buffer *old_pbuf = NULL;
1806         struct lnet_handle_md old_ping_md;
1807
1808         /* switch the NIs to point to the new ping info created */
1809         lnet_net_lock(LNET_LOCK_EX);
1810
1811         if (!the_lnet.ln_routing)
1812                 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1813         if (!lnet_peer_discovery_disabled)
1814                 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
1815
1816         /* Ensure only known feature bits have been set. */
1817         LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1818         LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1819
1820         lnet_ping_target_install_locked(pbuf);
1821
1822         if (the_lnet.ln_ping_target) {
1823                 old_pbuf = the_lnet.ln_ping_target;
1824                 old_ping_md = the_lnet.ln_ping_target_md;
1825         }
1826         the_lnet.ln_ping_target_md = ping_mdh;
1827         the_lnet.ln_ping_target = pbuf;
1828
1829         lnet_net_unlock(LNET_LOCK_EX);
1830
1831         if (old_pbuf) {
1832                 /* unlink and free the old ping info */
1833                 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1834                 lnet_ping_buffer_decref(old_pbuf);
1835         }
1836
1837         lnet_push_update_to_peers(0);
1838 }
1839
1840 static void
1841 lnet_ping_target_fini(void)
1842 {
1843         int             rc;
1844
1845         lnet_ping_md_unlink(the_lnet.ln_ping_target,
1846                             &the_lnet.ln_ping_target_md);
1847
1848         rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1849         LASSERT(rc == 0);
1850
1851         lnet_ping_target_destroy();
1852 }
1853
1854 /* Resize the push target. */
1855 int lnet_push_target_resize(void)
1856 {
1857         struct lnet_process_id id = { LNET_NID_ANY, LNET_PID_ANY };
1858         struct lnet_md md = { NULL };
1859         struct lnet_me *me;
1860         struct lnet_handle_md mdh;
1861         struct lnet_handle_md old_mdh;
1862         struct lnet_ping_buffer *pbuf;
1863         struct lnet_ping_buffer *old_pbuf;
1864         int nnis = the_lnet.ln_push_target_nnis;
1865         int rc;
1866
1867         if (nnis <= 0) {
1868                 rc = -EINVAL;
1869                 goto fail_return;
1870         }
1871 again:
1872         pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1873         if (!pbuf) {
1874                 rc = -ENOMEM;
1875                 goto fail_return;
1876         }
1877
1878         me = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1879                           LNET_PROTO_PING_MATCHBITS, 0,
1880                           LNET_UNLINK, LNET_INS_AFTER);
1881
1882         if (IS_ERR(me)) {
1883                 rc = PTR_ERR(me);
1884                 CERROR("Can't create push target ME: %d\n", rc);
1885                 goto fail_decref_pbuf;
1886         }
1887
1888         /* initialize md content */
1889         md.start     = &pbuf->pb_info;
1890         md.length    = LNET_PING_INFO_SIZE(nnis);
1891         md.threshold = LNET_MD_THRESH_INF;
1892         md.max_size  = 0;
1893         md.options   = LNET_MD_OP_PUT | LNET_MD_TRUNCATE |
1894                        LNET_MD_MANAGE_REMOTE;
1895         md.user_ptr  = pbuf;
1896         md.eq_handle = the_lnet.ln_push_target_eq;
1897
1898         rc = LNetMDAttach(me, md, LNET_RETAIN, &mdh);
1899         if (rc) {
1900                 CERROR("Can't attach push MD: %d\n", rc);
1901                 goto fail_unlink_me;
1902         }
1903         lnet_ping_buffer_addref(pbuf);
1904
1905         lnet_net_lock(LNET_LOCK_EX);
1906         old_pbuf = the_lnet.ln_push_target;
1907         old_mdh = the_lnet.ln_push_target_md;
1908         the_lnet.ln_push_target = pbuf;
1909         the_lnet.ln_push_target_md = mdh;
1910         lnet_net_unlock(LNET_LOCK_EX);
1911
1912         if (old_pbuf) {
1913                 LNetMDUnlink(old_mdh);
1914                 lnet_ping_buffer_decref(old_pbuf);
1915         }
1916
1917         if (nnis < the_lnet.ln_push_target_nnis)
1918                 goto again;
1919
1920         CDEBUG(D_NET, "nnis %d success\n", nnis);
1921
1922         return 0;
1923
1924 fail_unlink_me:
1925         LNetMEUnlink(me);
1926 fail_decref_pbuf:
1927         lnet_ping_buffer_decref(pbuf);
1928 fail_return:
1929         CDEBUG(D_NET, "nnis %d error %d\n", nnis, rc);
1930         return rc;
1931 }
1932
1933 static void lnet_push_target_event_handler(struct lnet_event *ev)
1934 {
1935         struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1936
1937         if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
1938                 lnet_swap_pinginfo(pbuf);
1939
1940         lnet_peer_push_event(ev);
1941         if (ev->unlinked)
1942                 lnet_ping_buffer_decref(pbuf);
1943 }
1944
1945 /* Initialize the push target. */
1946 static int lnet_push_target_init(void)
1947 {
1948         int rc;
1949
1950         if (the_lnet.ln_push_target)
1951                 return -EALREADY;
1952
1953         rc = LNetEQAlloc(0, lnet_push_target_event_handler,
1954                          &the_lnet.ln_push_target_eq);
1955         if (rc) {
1956                 CERROR("Can't allocated push target EQ: %d\n", rc);
1957                 return rc;
1958         }
1959
1960         /* Start at the required minimum, we'll enlarge if required. */
1961         the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
1962
1963         rc = lnet_push_target_resize();
1964
1965         if (rc) {
1966                 LNetEQFree(the_lnet.ln_push_target_eq);
1967                 LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1968         }
1969
1970         return rc;
1971 }
1972
1973 /* Clean up the push target. */
1974 static void lnet_push_target_fini(void)
1975 {
1976         if (!the_lnet.ln_push_target)
1977                 return;
1978
1979         /* Unlink and invalidate to prevent new references. */
1980         LNetMDUnlink(the_lnet.ln_push_target_md);
1981         LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
1982
1983         /* Wait for the unlink to complete. */
1984         while (lnet_ping_buffer_numref(the_lnet.ln_push_target) > 1) {
1985                 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1986                 set_current_state(TASK_UNINTERRUPTIBLE);
1987                 schedule_timeout(cfs_time_seconds(1));
1988         }
1989
1990         lnet_ping_buffer_decref(the_lnet.ln_push_target);
1991         the_lnet.ln_push_target = NULL;
1992         the_lnet.ln_push_target_nnis = 0;
1993
1994         LNetEQFree(the_lnet.ln_push_target_eq);
1995         LNetInvalidateEQHandle(&the_lnet.ln_push_target_eq);
1996 }
1997
1998 static int
1999 lnet_ni_tq_credits(struct lnet_ni *ni)
2000 {
2001         int     credits;
2002
2003         LASSERT(ni->ni_ncpts >= 1);
2004
2005         if (ni->ni_ncpts == 1)
2006                 return ni->ni_net->net_tunables.lct_max_tx_credits;
2007
2008         credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2009         credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2010         credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2011
2012         return credits;
2013 }
2014
2015 static void
2016 lnet_ni_unlink_locked(struct lnet_ni *ni)
2017 {
2018         /* move it to zombie list and nobody can find it anymore */
2019         LASSERT(!list_empty(&ni->ni_netlist));
2020         list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2021         lnet_ni_decref_locked(ni, 0);
2022 }
2023
2024 static void
2025 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2026 {
2027         int             i;
2028         int             islo;
2029         struct lnet_ni  *ni;
2030         struct list_head *zombie_list = &net->net_ni_zombie;
2031
2032         /*
2033          * Now wait for the NIs I just nuked to show up on the zombie
2034          * list and shut them down in guaranteed thread context
2035          */
2036         i = 2;
2037         while (!list_empty(zombie_list)) {
2038                 int     *ref;
2039                 int     j;
2040
2041                 ni = list_entry(zombie_list->next,
2042                                 struct lnet_ni, ni_netlist);
2043                 list_del_init(&ni->ni_netlist);
2044                 /* the ni should be in deleting state. If it's not it's
2045                  * a bug */
2046                 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2047                 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2048                         if (*ref == 0)
2049                                 continue;
2050                         /* still busy, add it back to zombie list */
2051                         list_add(&ni->ni_netlist, zombie_list);
2052                         break;
2053                 }
2054
2055                 if (!list_empty(&ni->ni_netlist)) {
2056                         lnet_net_unlock(LNET_LOCK_EX);
2057                         ++i;
2058                         if ((i & (-i)) == i) {
2059                                 CDEBUG(D_WARNING,
2060                                        "Waiting for zombie LNI %s\n",
2061                                        libcfs_nid2str(ni->ni_nid));
2062                         }
2063                         set_current_state(TASK_UNINTERRUPTIBLE);
2064                         schedule_timeout(cfs_time_seconds(1));
2065                         lnet_net_lock(LNET_LOCK_EX);
2066                         continue;
2067                 }
2068
2069                 lnet_net_unlock(LNET_LOCK_EX);
2070
2071                 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2072
2073                 LASSERT(!in_interrupt());
2074                 /* Holding the mutex makes it safe for lnd_shutdown
2075                  * to call module_put(). Module unload cannot finish
2076                  * until lnet_unregister_lnd() completes, and that
2077                  * requires the mutex.
2078                  */
2079                 mutex_lock(&the_lnet.ln_lnd_mutex);
2080                 (net->net_lnd->lnd_shutdown)(ni);
2081                 mutex_unlock(&the_lnet.ln_lnd_mutex);
2082
2083                 if (!islo)
2084                         CDEBUG(D_LNI, "Removed LNI %s\n",
2085                               libcfs_nid2str(ni->ni_nid));
2086
2087                 lnet_ni_free(ni);
2088                 i = 2;
2089                 lnet_net_lock(LNET_LOCK_EX);
2090         }
2091 }
2092
2093 /* shutdown down the NI and release refcount */
2094 static void
2095 lnet_shutdown_lndni(struct lnet_ni *ni)
2096 {
2097         int i;
2098         struct lnet_net *net = ni->ni_net;
2099
2100         lnet_net_lock(LNET_LOCK_EX);
2101         lnet_ni_lock(ni);
2102         ni->ni_state = LNET_NI_STATE_DELETING;
2103         lnet_ni_unlock(ni);
2104         lnet_ni_unlink_locked(ni);
2105         lnet_incr_dlc_seq();
2106         lnet_net_unlock(LNET_LOCK_EX);
2107
2108         /* clear messages for this NI on the lazy portal */
2109         for (i = 0; i < the_lnet.ln_nportals; i++)
2110                 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2111
2112         lnet_net_lock(LNET_LOCK_EX);
2113         lnet_clear_zombies_nis_locked(net);
2114         lnet_net_unlock(LNET_LOCK_EX);
2115 }
2116
2117 static void
2118 lnet_shutdown_lndnet(struct lnet_net *net)
2119 {
2120         struct lnet_ni *ni;
2121
2122         lnet_net_lock(LNET_LOCK_EX);
2123
2124         list_del_init(&net->net_list);
2125
2126         while (!list_empty(&net->net_ni_list)) {
2127                 ni = list_entry(net->net_ni_list.next,
2128                                 struct lnet_ni, ni_netlist);
2129                 lnet_net_unlock(LNET_LOCK_EX);
2130                 lnet_shutdown_lndni(ni);
2131                 lnet_net_lock(LNET_LOCK_EX);
2132         }
2133
2134         lnet_net_unlock(LNET_LOCK_EX);
2135
2136         /* Do peer table cleanup for this net */
2137         lnet_peer_tables_cleanup(net);
2138
2139         lnet_net_free(net);
2140 }
2141
2142 static void
2143 lnet_shutdown_lndnets(void)
2144 {
2145         struct lnet_net *net;
2146         struct list_head resend;
2147         struct lnet_msg *msg, *tmp;
2148
2149         INIT_LIST_HEAD(&resend);
2150
2151         /* NB called holding the global mutex */
2152
2153         /* All quiet on the API front */
2154         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2155         LASSERT(the_lnet.ln_refcount == 0);
2156
2157         lnet_net_lock(LNET_LOCK_EX);
2158         the_lnet.ln_state = LNET_STATE_STOPPING;
2159
2160         while (!list_empty(&the_lnet.ln_nets)) {
2161                 /*
2162                  * move the nets to the zombie list to avoid them being
2163                  * picked up for new work. LONET is also included in the
2164                  * Nets that will be moved to the zombie list
2165                  */
2166                 net = list_entry(the_lnet.ln_nets.next,
2167                                  struct lnet_net, net_list);
2168                 list_move(&net->net_list, &the_lnet.ln_net_zombie);
2169         }
2170
2171         /* Drop the cached loopback Net. */
2172         if (the_lnet.ln_loni != NULL) {
2173                 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2174                 the_lnet.ln_loni = NULL;
2175         }
2176         lnet_net_unlock(LNET_LOCK_EX);
2177
2178         /* iterate through the net zombie list and delete each net */
2179         while (!list_empty(&the_lnet.ln_net_zombie)) {
2180                 net = list_entry(the_lnet.ln_net_zombie.next,
2181                                  struct lnet_net, net_list);
2182                 lnet_shutdown_lndnet(net);
2183         }
2184
2185         spin_lock(&the_lnet.ln_msg_resend_lock);
2186         list_splice(&the_lnet.ln_msg_resend, &resend);
2187         spin_unlock(&the_lnet.ln_msg_resend_lock);
2188
2189         list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2190                 list_del_init(&msg->msg_list);
2191                 msg->msg_no_resend = true;
2192                 lnet_finalize(msg, -ECANCELED);
2193         }
2194
2195         lnet_net_lock(LNET_LOCK_EX);
2196         the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2197         lnet_net_unlock(LNET_LOCK_EX);
2198 }
2199
2200 static int
2201 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2202 {
2203         int                     rc = -EINVAL;
2204         struct lnet_tx_queue    *tq;
2205         int                     i;
2206         struct lnet_net         *net = ni->ni_net;
2207
2208         mutex_lock(&the_lnet.ln_lnd_mutex);
2209
2210         if (tun) {
2211                 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2212                 ni->ni_lnd_tunables_set = true;
2213         }
2214
2215         rc = (net->net_lnd->lnd_startup)(ni);
2216
2217         mutex_unlock(&the_lnet.ln_lnd_mutex);
2218
2219         if (rc != 0) {
2220                 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2221                                    rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2222                 goto failed0;
2223         }
2224
2225         lnet_ni_lock(ni);
2226         ni->ni_state = LNET_NI_STATE_ACTIVE;
2227         lnet_ni_unlock(ni);
2228
2229         /* We keep a reference on the loopback net through the loopback NI */
2230         if (net->net_lnd->lnd_type == LOLND) {
2231                 lnet_ni_addref(ni);
2232                 LASSERT(the_lnet.ln_loni == NULL);
2233                 the_lnet.ln_loni = ni;
2234                 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2235                 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2236                 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2237                 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2238                 return 0;
2239         }
2240
2241         if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2242             ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2243                 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2244                                    libcfs_lnd2str(net->net_lnd->lnd_type),
2245                                    ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2246                                         "" : "per-peer ");
2247                 /* shutdown the NI since if we get here then it must've already
2248                  * been started
2249                  */
2250                 lnet_shutdown_lndni(ni);
2251                 return -EINVAL;
2252         }
2253
2254         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2255                 tq->tq_credits_min =
2256                 tq->tq_credits_max =
2257                 tq->tq_credits = lnet_ni_tq_credits(ni);
2258         }
2259
2260         atomic_set(&ni->ni_tx_credits,
2261                    lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2262         atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2263
2264         CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2265                 libcfs_nid2str(ni->ni_nid),
2266                 ni->ni_net->net_tunables.lct_peer_tx_credits,
2267                 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2268                 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2269                 ni->ni_net->net_tunables.lct_peer_timeout);
2270
2271         return 0;
2272 failed0:
2273         lnet_ni_free(ni);
2274         return rc;
2275 }
2276
2277 static int
2278 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2279 {
2280         struct lnet_ni *ni;
2281         struct lnet_net *net_l = NULL;
2282         struct list_head        local_ni_list;
2283         int                     rc;
2284         int                     ni_count = 0;
2285         __u32                   lnd_type;
2286         const struct lnet_lnd  *lnd;
2287         int                     peer_timeout =
2288                 net->net_tunables.lct_peer_timeout;
2289         int                     maxtxcredits =
2290                 net->net_tunables.lct_max_tx_credits;
2291         int                     peerrtrcredits =
2292                 net->net_tunables.lct_peer_rtr_credits;
2293
2294         INIT_LIST_HEAD(&local_ni_list);
2295
2296         /*
2297          * make sure that this net is unique. If it isn't then
2298          * we are adding interfaces to an already existing network, and
2299          * 'net' is just a convenient way to pass in the list.
2300          * if it is unique we need to find the LND and load it if
2301          * necessary.
2302          */
2303         if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2304                 lnd_type = LNET_NETTYP(net->net_id);
2305
2306                 mutex_lock(&the_lnet.ln_lnd_mutex);
2307                 lnd = lnet_find_lnd_by_type(lnd_type);
2308
2309                 if (lnd == NULL) {
2310                         mutex_unlock(&the_lnet.ln_lnd_mutex);
2311                         rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2312                         mutex_lock(&the_lnet.ln_lnd_mutex);
2313
2314                         lnd = lnet_find_lnd_by_type(lnd_type);
2315                         if (lnd == NULL) {
2316                                 mutex_unlock(&the_lnet.ln_lnd_mutex);
2317                                 CERROR("Can't load LND %s, module %s, rc=%d\n",
2318                                 libcfs_lnd2str(lnd_type),
2319                                 libcfs_lnd2modname(lnd_type), rc);
2320 #ifndef HAVE_MODULE_LOADING_SUPPORT
2321                                 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2322                                                 "compiled with kernel module "
2323                                                 "loading support.");
2324 #endif
2325                                 rc = -EINVAL;
2326                                 goto failed0;
2327                         }
2328                 }
2329
2330                 net->net_lnd = lnd;
2331
2332                 mutex_unlock(&the_lnet.ln_lnd_mutex);
2333
2334                 net_l = net;
2335         }
2336
2337         /*
2338          * net_l: if the network being added is unique then net_l
2339          *        will point to that network
2340          *        if the network being added is not unique then
2341          *        net_l points to the existing network.
2342          *
2343          * When we enter the loop below, we'll pick NIs off he
2344          * network beign added and start them up, then add them to
2345          * a local ni list. Once we've successfully started all
2346          * the NIs then we join the local NI list (of started up
2347          * networks) with the net_l->net_ni_list, which should
2348          * point to the correct network to add the new ni list to
2349          *
2350          * If any of the new NIs fail to start up, then we want to
2351          * iterate through the local ni list, which should include
2352          * any NIs which were successfully started up, and shut
2353          * them down.
2354          *
2355          * After than we want to delete the network being added,
2356          * to avoid a memory leak.
2357          */
2358
2359         /*
2360          * When a network uses TCP bonding then all its interfaces
2361          * must be specified when the network is first defined: the
2362          * TCP bonding code doesn't allow for interfaces to be added
2363          * or removed.
2364          */
2365         if (net_l != net && net_l != NULL && use_tcp_bonding &&
2366             LNET_NETTYP(net_l->net_id) == SOCKLND) {
2367                 rc = -EINVAL;
2368                 goto failed0;
2369         }
2370
2371         while (!list_empty(&net->net_ni_added)) {
2372                 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2373                                 ni_netlist);
2374                 list_del_init(&ni->ni_netlist);
2375
2376                 /* make sure that the the NI we're about to start
2377                  * up is actually unique. if it's not fail. */
2378                 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2379                                         ni->ni_interfaces[0])) {
2380                         rc = -EEXIST;
2381                         goto failed1;
2382                 }
2383
2384                 /* adjust the pointer the parent network, just in case it
2385                  * the net is a duplicate */
2386                 ni->ni_net = net_l;
2387
2388                 rc = lnet_startup_lndni(ni, tun);
2389
2390                 if (rc < 0)
2391                         goto failed1;
2392
2393                 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
2394                         ni->ni_net->net_lnd->lnd_query != NULL);
2395
2396                 lnet_ni_addref(ni);
2397                 list_add_tail(&ni->ni_netlist, &local_ni_list);
2398
2399                 ni_count++;
2400         }
2401
2402         lnet_net_lock(LNET_LOCK_EX);
2403         list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2404         lnet_incr_dlc_seq();
2405         lnet_net_unlock(LNET_LOCK_EX);
2406
2407         /* if the network is not unique then we don't want to keep
2408          * it around after we're done. Free it. Otherwise add that
2409          * net to the global the_lnet.ln_nets */
2410         if (net_l != net && net_l != NULL) {
2411                 /*
2412                  * TODO - note. currently the tunables can not be updated
2413                  * once added
2414                  */
2415                 lnet_net_free(net);
2416         } else {
2417                 /*
2418                  * restore tunables after it has been overwitten by the
2419                  * lnd
2420                  */
2421                 if (peer_timeout != -1)
2422                         net->net_tunables.lct_peer_timeout = peer_timeout;
2423                 if (maxtxcredits != -1)
2424                         net->net_tunables.lct_max_tx_credits = maxtxcredits;
2425                 if (peerrtrcredits != -1)
2426                         net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2427
2428                 lnet_net_lock(LNET_LOCK_EX);
2429                 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2430                 lnet_net_unlock(LNET_LOCK_EX);
2431         }
2432
2433         /* update net count */
2434         lnet_current_net_count = lnet_get_net_count();
2435
2436         return ni_count;
2437
2438 failed1:
2439         /*
2440          * shutdown the new NIs that are being started up
2441          * free the NET being started
2442          */
2443         while (!list_empty(&local_ni_list)) {
2444                 ni = list_entry(local_ni_list.next, struct lnet_ni,
2445                                 ni_netlist);
2446
2447                 lnet_shutdown_lndni(ni);
2448         }
2449
2450 failed0:
2451         lnet_net_free(net);
2452
2453         return rc;
2454 }
2455
2456 static int
2457 lnet_startup_lndnets(struct list_head *netlist)
2458 {
2459         struct lnet_net         *net;
2460         int                     rc;
2461         int                     ni_count = 0;
2462
2463         /*
2464          * Change to running state before bringing up the LNDs. This
2465          * allows lnet_shutdown_lndnets() to assert that we've passed
2466          * through here.
2467          */
2468         lnet_net_lock(LNET_LOCK_EX);
2469         the_lnet.ln_state = LNET_STATE_RUNNING;
2470         lnet_net_unlock(LNET_LOCK_EX);
2471
2472         while (!list_empty(netlist)) {
2473                 net = list_entry(netlist->next, struct lnet_net, net_list);
2474                 list_del_init(&net->net_list);
2475
2476                 rc = lnet_startup_lndnet(net, NULL);
2477
2478                 if (rc < 0)
2479                         goto failed;
2480
2481                 ni_count += rc;
2482         }
2483
2484         return ni_count;
2485 failed:
2486         lnet_shutdown_lndnets();
2487
2488         return rc;
2489 }
2490
2491 /**
2492  * Initialize LNet library.
2493  *
2494  * Automatically called at module loading time. Caller has to call
2495  * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2496  * latter returned 0. It must be called exactly once.
2497  *
2498  * \retval 0 on success
2499  * \retval -ve on failures.
2500  */
2501 int lnet_lib_init(void)
2502 {
2503         int rc;
2504
2505         lnet_assert_wire_constants();
2506
2507         /* refer to global cfs_cpt_table for now */
2508         the_lnet.ln_cpt_table   = cfs_cpt_table;
2509         the_lnet.ln_cpt_number  = cfs_cpt_number(cfs_cpt_table);
2510
2511         LASSERT(the_lnet.ln_cpt_number > 0);
2512         if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2513                 /* we are under risk of consuming all lh_cookie */
2514                 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2515                        "please change setting of CPT-table and retry\n",
2516                        the_lnet.ln_cpt_number, LNET_CPT_MAX);
2517                 return -E2BIG;
2518         }
2519
2520         while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2521                 the_lnet.ln_cpt_bits++;
2522
2523         rc = lnet_create_locks();
2524         if (rc != 0) {
2525                 CERROR("Can't create LNet global locks: %d\n", rc);
2526                 return rc;
2527         }
2528
2529         the_lnet.ln_refcount = 0;
2530         INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2531         INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2532
2533         /* The hash table size is the number of bits it takes to express the set
2534          * ln_num_routes, minus 1 (better to under estimate than over so we
2535          * don't waste memory). */
2536         if (rnet_htable_size <= 0)
2537                 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2538         else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2539                 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2540         the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2541                                            order_base_2(rnet_htable_size) - 1);
2542
2543         /* All LNDs apart from the LOLND are in separate modules.  They
2544          * register themselves when their module loads, and unregister
2545          * themselves when their module is unloaded. */
2546         lnet_register_lnd(&the_lolnd);
2547         return 0;
2548 }
2549
2550 /**
2551  * Finalize LNet library.
2552  *
2553  * \pre lnet_lib_init() called with success.
2554  * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2555  *
2556  * As this happens at module-unload, all lnds must already be unloaded,
2557  * so they must already be unregistered.
2558  */
2559 void lnet_lib_exit(void)
2560 {
2561         int i;
2562
2563         LASSERT(the_lnet.ln_refcount == 0);
2564         lnet_unregister_lnd(&the_lolnd);
2565         for (i = 0; i < NUM_LNDS; i++)
2566                 LASSERT(!the_lnet.ln_lnds[i]);
2567         lnet_destroy_locks();
2568 }
2569
2570 /**
2571  * Set LNet PID and start LNet interfaces, routing, and forwarding.
2572  *
2573  * Users must call this function at least once before any other functions.
2574  * For each successful call there must be a corresponding call to
2575  * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2576  * ignored.
2577  *
2578  * The PID used by LNet may be different from the one requested.
2579  * See LNetGetId().
2580  *
2581  * \param requested_pid PID requested by the caller.
2582  *
2583  * \return >= 0 on success, and < 0 error code on failures.
2584  */
2585 int
2586 LNetNIInit(lnet_pid_t requested_pid)
2587 {
2588         int                     im_a_router = 0;
2589         int                     rc;
2590         int                     ni_count;
2591         struct lnet_ping_buffer *pbuf;
2592         struct lnet_handle_md   ping_mdh;
2593         struct list_head        net_head;
2594         struct lnet_net         *net;
2595
2596         INIT_LIST_HEAD(&net_head);
2597
2598         mutex_lock(&the_lnet.ln_api_mutex);
2599
2600         CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2601
2602         if (the_lnet.ln_refcount > 0) {
2603                 rc = the_lnet.ln_refcount++;
2604                 mutex_unlock(&the_lnet.ln_api_mutex);
2605                 return rc;
2606         }
2607
2608         rc = lnet_prepare(requested_pid);
2609         if (rc != 0) {
2610                 mutex_unlock(&the_lnet.ln_api_mutex);
2611                 return rc;
2612         }
2613
2614         /* create a network for Loopback network */
2615         net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2616         if (net == NULL) {
2617                 rc = -ENOMEM;
2618                 goto err_empty_list;
2619         }
2620
2621         /* Add in the loopback NI */
2622         if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2623                 rc = -ENOMEM;
2624                 goto err_empty_list;
2625         }
2626
2627         /* If LNet is being initialized via DLC it is possible
2628          * that the user requests not to load module parameters (ones which
2629          * are supported by DLC) on initialization.  Therefore, make sure not
2630          * to load networks, routes and forwarding from module parameters
2631          * in this case.  On cleanup in case of failure only clean up
2632          * routes if it has been loaded */
2633         if (!the_lnet.ln_nis_from_mod_params) {
2634                 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
2635                                          use_tcp_bonding);
2636                 if (rc < 0)
2637                         goto err_empty_list;
2638         }
2639
2640         ni_count = lnet_startup_lndnets(&net_head);
2641         if (ni_count < 0) {
2642                 rc = ni_count;
2643                 goto err_empty_list;
2644         }
2645
2646         if (!the_lnet.ln_nis_from_mod_params) {
2647                 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2648                 if (rc != 0)
2649                         goto err_shutdown_lndnis;
2650
2651                 rc = lnet_rtrpools_alloc(im_a_router);
2652                 if (rc != 0)
2653                         goto err_destroy_routes;
2654         }
2655
2656         rc = lnet_acceptor_start();
2657         if (rc != 0)
2658                 goto err_destroy_routes;
2659
2660         the_lnet.ln_refcount = 1;
2661         /* Now I may use my own API functions... */
2662
2663         rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2664         if (rc != 0)
2665                 goto err_acceptor_stop;
2666
2667         lnet_ping_target_update(pbuf, ping_mdh);
2668
2669         rc = LNetEQAlloc(0, lnet_mt_event_handler, &the_lnet.ln_mt_eqh);
2670         if (rc != 0) {
2671                 CERROR("Can't allocate monitor thread EQ: %d\n", rc);
2672                 goto err_stop_ping;
2673         }
2674
2675         rc = lnet_push_target_init();
2676         if (rc != 0)
2677                 goto err_stop_ping;
2678
2679         rc = lnet_peer_discovery_start();
2680         if (rc != 0)
2681                 goto err_destroy_push_target;
2682
2683         rc = lnet_monitor_thr_start();
2684         if (rc != 0)
2685                 goto err_stop_discovery_thr;
2686
2687         lnet_fault_init();
2688         lnet_router_debugfs_init();
2689
2690         mutex_unlock(&the_lnet.ln_api_mutex);
2691
2692         complete_all(&the_lnet.ln_started);
2693
2694         /* wait for all routers to start */
2695         lnet_wait_router_start();
2696
2697         return 0;
2698
2699 err_stop_discovery_thr:
2700         lnet_peer_discovery_stop();
2701 err_destroy_push_target:
2702         lnet_push_target_fini();
2703 err_stop_ping:
2704         lnet_ping_target_fini();
2705 err_acceptor_stop:
2706         the_lnet.ln_refcount = 0;
2707         lnet_acceptor_stop();
2708 err_destroy_routes:
2709         if (!the_lnet.ln_nis_from_mod_params)
2710                 lnet_destroy_routes();
2711 err_shutdown_lndnis:
2712         lnet_shutdown_lndnets();
2713 err_empty_list:
2714         lnet_unprepare();
2715         LASSERT(rc < 0);
2716         mutex_unlock(&the_lnet.ln_api_mutex);
2717         while (!list_empty(&net_head)) {
2718                 struct lnet_net *net;
2719
2720                 net = list_entry(net_head.next, struct lnet_net, net_list);
2721                 list_del_init(&net->net_list);
2722                 lnet_net_free(net);
2723         }
2724         return rc;
2725 }
2726 EXPORT_SYMBOL(LNetNIInit);
2727
2728 /**
2729  * Stop LNet interfaces, routing, and forwarding.
2730  *
2731  * Users must call this function once for each successful call to LNetNIInit().
2732  * Once the LNetNIFini() operation has been started, the results of pending
2733  * API operations are undefined.
2734  *
2735  * \return always 0 for current implementation.
2736  */
2737 int
2738 LNetNIFini(void)
2739 {
2740         mutex_lock(&the_lnet.ln_api_mutex);
2741
2742         LASSERT(the_lnet.ln_refcount > 0);
2743
2744         if (the_lnet.ln_refcount != 1) {
2745                 the_lnet.ln_refcount--;
2746         } else {
2747                 LASSERT(!the_lnet.ln_niinit_self);
2748
2749                 lnet_fault_fini();
2750
2751                 lnet_router_debugfs_fini();
2752                 lnet_monitor_thr_stop();
2753                 lnet_peer_discovery_stop();
2754                 lnet_push_target_fini();
2755                 lnet_ping_target_fini();
2756
2757                 /* Teardown fns that use my own API functions BEFORE here */
2758                 the_lnet.ln_refcount = 0;
2759
2760                 lnet_acceptor_stop();
2761                 lnet_destroy_routes();
2762                 lnet_shutdown_lndnets();
2763                 lnet_unprepare();
2764         }
2765
2766         mutex_unlock(&the_lnet.ln_api_mutex);
2767         return 0;
2768 }
2769 EXPORT_SYMBOL(LNetNIFini);
2770
2771 /**
2772  * Grabs the ni data from the ni structure and fills the out
2773  * parameters
2774  *
2775  * \param[in] ni network        interface structure
2776  * \param[out] cfg_ni           NI config information
2777  * \param[out] tun              network and LND tunables
2778  */
2779 static void
2780 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2781                    struct lnet_ioctl_config_lnd_tunables *tun,
2782                    struct lnet_ioctl_element_stats *stats,
2783                    __u32 tun_size)
2784 {
2785         size_t min_size = 0;
2786         int i;
2787
2788         if (!ni || !cfg_ni || !tun)
2789                 return;
2790
2791         if (ni->ni_interfaces[0] != NULL) {
2792                 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2793                         if (ni->ni_interfaces[i] != NULL) {
2794                                 strncpy(cfg_ni->lic_ni_intf[i],
2795                                         ni->ni_interfaces[i],
2796                                         sizeof(cfg_ni->lic_ni_intf[i]));
2797                         }
2798                 }
2799         }
2800
2801         cfg_ni->lic_nid = ni->ni_nid;
2802         if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2803                 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2804         else
2805                 cfg_ni->lic_status = ni->ni_status->ns_status;
2806         cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2807         cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2808
2809         memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2810
2811         if (stats) {
2812                 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
2813                                                        LNET_STATS_TYPE_SEND);
2814                 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
2815                                                        LNET_STATS_TYPE_RECV);
2816                 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
2817                                                        LNET_STATS_TYPE_DROP);
2818         }
2819
2820         /*
2821          * tun->lt_tun will always be present, but in order to be
2822          * backwards compatible, we need to deal with the cases when
2823          * tun->lt_tun is smaller than what the kernel has, because it
2824          * comes from an older version of a userspace program, then we'll
2825          * need to copy as much information as we have available space.
2826          */
2827         min_size = tun_size - sizeof(tun->lt_cmn);
2828         memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2829
2830         /* copy over the cpts */
2831         if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2832             ni->ni_cpts == NULL)  {
2833                 for (i = 0; i < ni->ni_ncpts; i++)
2834                         cfg_ni->lic_cpts[i] = i;
2835         } else {
2836                 for (i = 0;
2837                      ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2838                      i < LNET_MAX_SHOW_NUM_CPT;
2839                      i++)
2840                         cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2841         }
2842         cfg_ni->lic_ncpts = ni->ni_ncpts;
2843 }
2844
2845 /**
2846  * NOTE: This is a legacy function left in the code to be backwards
2847  * compatible with older userspace programs. It should eventually be
2848  * removed.
2849  *
2850  * Grabs the ni data from the ni structure and fills the out
2851  * parameters
2852  *
2853  * \param[in] ni network        interface structure
2854  * \param[out] config           config information
2855  */
2856 static void
2857 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2858                          struct lnet_ioctl_config_data *config)
2859 {
2860         struct lnet_ioctl_net_config *net_config;
2861         struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2862         size_t min_size, tunable_size = 0;
2863         int i;
2864
2865         if (!ni || !config)
2866                 return;
2867
2868         net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2869         if (!net_config)
2870                 return;
2871
2872         BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2873                      ARRAY_SIZE(net_config->ni_interfaces));
2874
2875         for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2876                 if (!ni->ni_interfaces[i])
2877                         break;
2878
2879                 strncpy(net_config->ni_interfaces[i],
2880                         ni->ni_interfaces[i],
2881                         sizeof(net_config->ni_interfaces[i]));
2882         }
2883
2884         config->cfg_nid = ni->ni_nid;
2885         config->cfg_config_u.cfg_net.net_peer_timeout =
2886                 ni->ni_net->net_tunables.lct_peer_timeout;
2887         config->cfg_config_u.cfg_net.net_max_tx_credits =
2888                 ni->ni_net->net_tunables.lct_max_tx_credits;
2889         config->cfg_config_u.cfg_net.net_peer_tx_credits =
2890                 ni->ni_net->net_tunables.lct_peer_tx_credits;
2891         config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2892                 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2893
2894         if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2895                 net_config->ni_status = LNET_NI_STATUS_UP;
2896         else
2897                 net_config->ni_status = ni->ni_status->ns_status;
2898
2899         if (ni->ni_cpts) {
2900                 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2901
2902                 for (i = 0; i < num_cpts; i++)
2903                         net_config->ni_cpts[i] = ni->ni_cpts[i];
2904
2905                 config->cfg_ncpts = num_cpts;
2906         }
2907
2908         /*
2909          * See if user land tools sent in a newer and larger version
2910          * of struct lnet_tunables than what the kernel uses.
2911          */
2912         min_size = sizeof(*config) + sizeof(*net_config);
2913
2914         if (config->cfg_hdr.ioc_len > min_size)
2915                 tunable_size = config->cfg_hdr.ioc_len - min_size;
2916
2917         /* Don't copy too much data to user space */
2918         min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2919         lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2920
2921         if (lnd_cfg && min_size) {
2922                 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2923                 config->cfg_config_u.cfg_net.net_interface_count = 1;
2924
2925                 /* Tell user land that kernel side has less data */
2926                 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2927                         min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2928                         config->cfg_hdr.ioc_len -= min_size;
2929                 }
2930         }
2931 }
2932
2933 struct lnet_ni *
2934 lnet_get_ni_idx_locked(int idx)
2935 {
2936         struct lnet_ni          *ni;
2937         struct lnet_net         *net;
2938
2939         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2940                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2941                         if (idx-- == 0)
2942                                 return ni;
2943                 }
2944         }
2945
2946         return NULL;
2947 }
2948
2949 struct lnet_ni *
2950 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2951 {
2952         struct lnet_ni          *ni;
2953         struct lnet_net         *net = mynet;
2954
2955         /*
2956          * It is possible that the net has been cleaned out while there is
2957          * a message being sent. This function accessed the net without
2958          * checking if the list is empty
2959          */
2960         if (prev == NULL) {
2961                 if (net == NULL)
2962                         net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2963                                         net_list);
2964                 if (list_empty(&net->net_ni_list))
2965                         return NULL;
2966                 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2967                                 ni_netlist);
2968
2969                 return ni;
2970         }
2971
2972         if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2973                 /* if you reached the end of the ni list and the net is
2974                  * specified, then there are no more nis in that net */
2975                 if (net != NULL)
2976                         return NULL;
2977
2978                 /* we reached the end of this net ni list. move to the
2979                  * next net */
2980                 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2981                         /* no more nets and no more NIs. */
2982                         return NULL;
2983
2984                 /* get the next net */
2985                 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2986                                  net_list);
2987                 if (list_empty(&net->net_ni_list))
2988                         return NULL;
2989                 /* get the ni on it */
2990                 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2991                                 ni_netlist);
2992
2993                 return ni;
2994         }
2995
2996         if (list_empty(&prev->ni_netlist))
2997                 return NULL;
2998
2999         /* there are more nis left */
3000         ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3001
3002         return ni;
3003 }
3004
3005 int
3006 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3007 {
3008         struct lnet_ni *ni;
3009         int cpt;
3010         int rc = -ENOENT;
3011         int idx = config->cfg_count;
3012
3013         cpt = lnet_net_lock_current();
3014
3015         ni = lnet_get_ni_idx_locked(idx);
3016
3017         if (ni != NULL) {
3018                 rc = 0;
3019                 lnet_ni_lock(ni);
3020                 lnet_fill_ni_info_legacy(ni, config);
3021                 lnet_ni_unlock(ni);
3022         }
3023
3024         lnet_net_unlock(cpt);
3025         return rc;
3026 }
3027
3028 int
3029 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3030                    struct lnet_ioctl_config_lnd_tunables *tun,
3031                    struct lnet_ioctl_element_stats *stats,
3032                    __u32 tun_size)
3033 {
3034         struct lnet_ni          *ni;
3035         int                     cpt;
3036         int                     rc = -ENOENT;
3037
3038         if (!cfg_ni || !tun || !stats)
3039                 return -EINVAL;
3040
3041         cpt = lnet_net_lock_current();
3042
3043         ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3044
3045         if (ni) {
3046                 rc = 0;
3047                 lnet_ni_lock(ni);
3048                 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3049                 lnet_ni_unlock(ni);
3050         }
3051
3052         lnet_net_unlock(cpt);
3053         return rc;
3054 }
3055
3056 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3057 {
3058         struct lnet_ni *ni;
3059         int cpt;
3060         int rc = -ENOENT;
3061
3062         if (!msg_stats)
3063                 return -EINVAL;
3064
3065         cpt = lnet_net_lock_current();
3066
3067         ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3068
3069         if (ni) {
3070                 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3071                 rc = 0;
3072         }
3073
3074         lnet_net_unlock(cpt);
3075
3076         return rc;
3077 }
3078
3079 static int lnet_add_net_common(struct lnet_net *net,
3080                                struct lnet_ioctl_config_lnd_tunables *tun)
3081 {
3082         __u32                   net_id;
3083         struct lnet_ping_buffer *pbuf;
3084         struct lnet_handle_md   ping_mdh;
3085         int                     rc;
3086         struct lnet_remotenet *rnet;
3087         int                     net_ni_count;
3088         int                     num_acceptor_nets;
3089
3090         lnet_net_lock(LNET_LOCK_EX);
3091         rnet = lnet_find_rnet_locked(net->net_id);
3092         lnet_net_unlock(LNET_LOCK_EX);
3093         /*
3094          * make sure that the net added doesn't invalidate the current
3095          * configuration LNet is keeping
3096          */
3097         if (rnet) {
3098                 CERROR("Adding net %s will invalidate routing configuration\n",
3099                        libcfs_net2str(net->net_id));
3100                 lnet_net_free(net);
3101                 return -EUSERS;
3102         }
3103
3104         /*
3105          * make sure you calculate the correct number of slots in the ping
3106          * buffer. Since the ping info is a flattened list of all the NIs,
3107          * we should allocate enough slots to accomodate the number of NIs
3108          * which will be added.
3109          *
3110          * since ni hasn't been configured yet, use
3111          * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3112          */
3113         net_ni_count = lnet_get_net_ni_count_pre(net);
3114
3115         rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3116                                     net_ni_count + lnet_get_ni_count(),
3117                                     false);
3118         if (rc < 0) {
3119                 lnet_net_free(net);
3120                 return rc;
3121         }
3122
3123         if (tun)
3124                 memcpy(&net->net_tunables,
3125                        &tun->lt_cmn, sizeof(net->net_tunables));
3126         else
3127                 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3128
3129         /*
3130          * before starting this network get a count of the current TCP
3131          * networks which require the acceptor thread running. If that
3132          * count is == 0 before we start up this network, then we'd want to
3133          * start up the acceptor thread after starting up this network
3134          */
3135         num_acceptor_nets = lnet_count_acceptor_nets();
3136
3137         net_id = net->net_id;
3138
3139         rc = lnet_startup_lndnet(net,
3140                                  (tun) ? &tun->lt_tun : NULL);
3141         if (rc < 0)
3142                 goto failed;
3143
3144         lnet_net_lock(LNET_LOCK_EX);
3145         net = lnet_get_net_locked(net_id);
3146         lnet_net_unlock(LNET_LOCK_EX);
3147
3148         LASSERT(net);
3149
3150         /*
3151          * Start the acceptor thread if this is the first network
3152          * being added that requires the thread.
3153          */
3154         if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
3155                 rc = lnet_acceptor_start();
3156                 if (rc < 0) {
3157                         /* shutdown the net that we just started */
3158                         CERROR("Failed to start up acceptor thread\n");
3159                         lnet_shutdown_lndnet(net);
3160                         goto failed;
3161                 }
3162         }
3163
3164         lnet_net_lock(LNET_LOCK_EX);
3165         lnet_peer_net_added(net);
3166         lnet_net_unlock(LNET_LOCK_EX);
3167
3168         lnet_ping_target_update(pbuf, ping_mdh);
3169
3170         return 0;
3171
3172 failed:
3173         lnet_ping_md_unlink(pbuf, &ping_mdh);
3174         lnet_ping_buffer_decref(pbuf);
3175         return rc;
3176 }
3177
3178 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3179                                       struct lnet_ioctl_config_lnd_tunables *tun)
3180 {
3181         struct lnet_net *net;
3182         char *nets;
3183         int rc;
3184         struct list_head net_head;
3185
3186         INIT_LIST_HEAD(&net_head);
3187
3188         rc = lnet_parse_ip2nets(&nets, ip2nets);
3189         if (rc < 0)
3190                 return rc;
3191
3192         rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3193         if (rc < 0)
3194                 return rc;
3195
3196         mutex_lock(&the_lnet.ln_api_mutex);
3197         while (!list_empty(&net_head)) {
3198                 net = list_entry(net_head.next, struct lnet_net, net_list);
3199                 list_del_init(&net->net_list);
3200                 rc = lnet_add_net_common(net, tun);
3201                 if (rc < 0)
3202                         goto out;
3203         }
3204
3205 out:
3206         mutex_unlock(&the_lnet.ln_api_mutex);
3207
3208         while (!list_empty(&net_head)) {
3209                 net = list_entry(net_head.next, struct lnet_net, net_list);
3210                 list_del_init(&net->net_list);
3211                 lnet_net_free(net);
3212         }
3213         return rc;
3214 }
3215
3216 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3217 {
3218         struct lnet_net *net;
3219         struct lnet_ni *ni;
3220         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3221         int rc, i;
3222         __u32 net_id, lnd_type;
3223
3224         /* get the tunables if they are available */
3225         if (conf->lic_cfg_hdr.ioc_len >=
3226             sizeof(*conf) + sizeof(*tun))
3227                 tun = (struct lnet_ioctl_config_lnd_tunables *)
3228                         conf->lic_bulk;
3229
3230         /* handle legacy ip2nets from DLC */
3231         if (conf->lic_legacy_ip2nets[0] != '\0')
3232                 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3233                                                   tun);
3234
3235         net_id = LNET_NIDNET(conf->lic_nid);
3236         lnd_type = LNET_NETTYP(net_id);
3237
3238         if (!libcfs_isknown_lnd(lnd_type)) {
3239                 CERROR("No valid net and lnd information provided\n");
3240                 return -EINVAL;
3241         }
3242
3243         net = lnet_net_alloc(net_id, NULL);
3244         if (!net)
3245                 return -ENOMEM;
3246
3247         for (i = 0; i < conf->lic_ncpts; i++) {
3248                 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3249                         return -EINVAL;
3250         }
3251
3252         ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3253                                        conf->lic_ni_intf[0]);
3254         if (!ni)
3255                 return -ENOMEM;
3256
3257         mutex_lock(&the_lnet.ln_api_mutex);
3258
3259         rc = lnet_add_net_common(net, tun);
3260
3261         mutex_unlock(&the_lnet.ln_api_mutex);
3262
3263         return rc;
3264 }
3265
3266 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3267 {
3268         struct lnet_net  *net;
3269         struct lnet_ni *ni;
3270         __u32 net_id = LNET_NIDNET(conf->lic_nid);
3271         struct lnet_ping_buffer *pbuf;
3272         struct lnet_handle_md  ping_mdh;
3273         int               rc;
3274         int               net_count;
3275         __u32             addr;
3276
3277         /* don't allow userspace to shutdown the LOLND */
3278         if (LNET_NETTYP(net_id) == LOLND)
3279                 return -EINVAL;
3280
3281         mutex_lock(&the_lnet.ln_api_mutex);
3282
3283         lnet_net_lock(0);
3284
3285         net = lnet_get_net_locked(net_id);
3286         if (!net) {
3287                 CERROR("net %s not found\n",
3288                        libcfs_net2str(net_id));
3289                 rc = -ENOENT;
3290                 goto unlock_net;
3291         }
3292
3293         addr = LNET_NIDADDR(conf->lic_nid);
3294         if (addr == 0) {
3295                 /* remove the entire net */
3296                 net_count = lnet_get_net_ni_count_locked(net);
3297
3298                 lnet_net_unlock(0);
3299
3300                 /* create and link a new ping info, before removing the old one */
3301                 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3302                                         lnet_get_ni_count() - net_count,
3303                                         false);
3304                 if (rc != 0)
3305                         goto unlock_api_mutex;
3306
3307                 lnet_shutdown_lndnet(net);
3308
3309                 if (lnet_count_acceptor_nets() == 0)
3310                         lnet_acceptor_stop();
3311
3312                 lnet_ping_target_update(pbuf, ping_mdh);
3313
3314                 goto unlock_api_mutex;
3315         }
3316
3317         ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3318         if (!ni) {
3319                 CERROR("nid %s not found\n",
3320                        libcfs_nid2str(conf->lic_nid));
3321                 rc = -ENOENT;
3322                 goto unlock_net;
3323         }
3324
3325         net_count = lnet_get_net_ni_count_locked(net);
3326
3327         lnet_net_unlock(0);
3328
3329         /* create and link a new ping info, before removing the old one */
3330         rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3331                                   lnet_get_ni_count() - 1, false);
3332         if (rc != 0)
3333                 goto unlock_api_mutex;
3334
3335         lnet_shutdown_lndni(ni);
3336
3337         if (lnet_count_acceptor_nets() == 0)
3338                 lnet_acceptor_stop();
3339
3340         lnet_ping_target_update(pbuf, ping_mdh);
3341
3342         /* check if the net is empty and remove it if it is */
3343         if (net_count == 1)
3344                 lnet_shutdown_lndnet(net);
3345
3346         goto unlock_api_mutex;
3347
3348 unlock_net:
3349         lnet_net_unlock(0);
3350 unlock_api_mutex:
3351         mutex_unlock(&the_lnet.ln_api_mutex);
3352
3353         return rc;
3354 }
3355
3356 /*
3357  * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3358  * They are only expected to be called for unique networks.
3359  * That can be as a result of older DLC library
3360  * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3361  */
3362 int
3363 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3364 {
3365         struct lnet_net         *net;
3366         struct list_head        net_head;
3367         int                     rc;
3368         struct lnet_ioctl_config_lnd_tunables tun;
3369         char *nets = conf->cfg_config_u.cfg_net.net_intf;
3370
3371         INIT_LIST_HEAD(&net_head);
3372
3373         /* Create a net/ni structures for the network string */
3374         rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
3375         if (rc <= 0)
3376                 return rc == 0 ? -EINVAL : rc;
3377
3378         mutex_lock(&the_lnet.ln_api_mutex);
3379
3380         if (rc > 1) {
3381                 rc = -EINVAL; /* only add one network per call */
3382                 goto out_unlock_clean;
3383         }
3384
3385         net = list_entry(net_head.next, struct lnet_net, net_list);
3386         list_del_init(&net->net_list);
3387
3388         LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3389
3390         memset(&tun, 0, sizeof(tun));
3391
3392         tun.lt_cmn.lct_peer_timeout =
3393           conf->cfg_config_u.cfg_net.net_peer_timeout;
3394         tun.lt_cmn.lct_peer_tx_credits =
3395           conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3396         tun.lt_cmn.lct_peer_rtr_credits =
3397           conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3398         tun.lt_cmn.lct_max_tx_credits =
3399           conf->cfg_config_u.cfg_net.net_max_tx_credits;
3400
3401         rc = lnet_add_net_common(net, &tun);
3402
3403 out_unlock_clean:
3404         mutex_unlock(&the_lnet.ln_api_mutex);
3405         while (!list_empty(&net_head)) {
3406                 /* net_head list is empty in success case */
3407                 net = list_entry(net_head.next, struct lnet_net, net_list);
3408                 list_del_init(&net->net_list);
3409                 lnet_net_free(net);
3410         }
3411         return rc;
3412 }
3413
3414 int
3415 lnet_dyn_del_net(__u32 net_id)
3416 {
3417         struct lnet_net  *net;
3418         struct lnet_ping_buffer *pbuf;
3419         struct lnet_handle_md ping_mdh;
3420         int               rc;
3421         int               net_ni_count;
3422
3423         /* don't allow userspace to shutdown the LOLND */
3424         if (LNET_NETTYP(net_id) == LOLND)
3425                 return -EINVAL;
3426
3427         mutex_lock(&the_lnet.ln_api_mutex);
3428
3429         lnet_net_lock(0);
3430
3431         net = lnet_get_net_locked(net_id);
3432         if (net == NULL) {
3433                 lnet_net_unlock(0);
3434                 rc = -EINVAL;
3435                 goto out;
3436         }
3437
3438         net_ni_count = lnet_get_net_ni_count_locked(net);
3439
3440         lnet_net_unlock(0);
3441
3442         /* create and link a new ping info, before removing the old one */
3443         rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3444                                     lnet_get_ni_count() - net_ni_count, false);
3445         if (rc != 0)
3446                 goto out;
3447
3448         lnet_shutdown_lndnet(net);
3449
3450         if (lnet_count_acceptor_nets() == 0)
3451                 lnet_acceptor_stop();
3452
3453         lnet_ping_target_update(pbuf, ping_mdh);
3454
3455 out:
3456         mutex_unlock(&the_lnet.ln_api_mutex);
3457
3458         return rc;
3459 }
3460
3461 void lnet_incr_dlc_seq(void)
3462 {
3463         atomic_inc(&lnet_dlc_seq_no);
3464 }
3465
3466 __u32 lnet_get_dlc_seq_locked(void)
3467 {
3468         return atomic_read(&lnet_dlc_seq_no);
3469 }
3470
3471 static void
3472 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3473 {
3474         struct lnet_net *net;
3475         struct lnet_ni *ni;
3476
3477         lnet_net_lock(LNET_LOCK_EX);
3478         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3479                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3480                         if (ni->ni_nid == nid || all) {
3481                                 atomic_set(&ni->ni_healthv, value);
3482                                 if (list_empty(&ni->ni_recovery) &&
3483                                     value < LNET_MAX_HEALTH_VALUE) {
3484                                         CERROR("manually adding local NI %s to recovery\n",
3485                                                libcfs_nid2str(ni->ni_nid));
3486                                         list_add_tail(&ni->ni_recovery,
3487                                                       &the_lnet.ln_mt_localNIRecovq);
3488                                         lnet_ni_addref_locked(ni, 0);
3489                                 }
3490                                 if (!all) {
3491                                         lnet_net_unlock(LNET_LOCK_EX);
3492                                         return;
3493                                 }
3494                         }
3495                 }
3496         }
3497         lnet_net_unlock(LNET_LOCK_EX);
3498 }
3499
3500 static int
3501 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3502 {
3503         int cpt, rc = 0;
3504         struct lnet_ni *ni;
3505         lnet_nid_t nid = stats->hlni_nid;
3506
3507         cpt = lnet_net_lock_current();
3508         ni = lnet_nid2ni_locked(nid, cpt);
3509
3510         if (!ni) {
3511                 rc = -ENOENT;
3512                 goto unlock;
3513         }
3514
3515         stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3516         stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3517         stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3518         stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3519         stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3520         stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3521         stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3522
3523 unlock:
3524         lnet_net_unlock(cpt);
3525
3526         return rc;
3527 }
3528
3529 static int
3530 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3531 {
3532         struct lnet_ni *ni;
3533         int i = 0;
3534
3535         lnet_net_lock(LNET_LOCK_EX);
3536         list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3537                 list->rlst_nid_array[i] = ni->ni_nid;
3538                 i++;
3539                 if (i >= LNET_MAX_SHOW_NUM_NID)
3540                         break;
3541         }
3542         lnet_net_unlock(LNET_LOCK_EX);
3543         list->rlst_num_nids = i;
3544
3545         return 0;
3546 }
3547
3548 static int
3549 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3550 {
3551         struct lnet_peer_ni *lpni;
3552         int i = 0;
3553
3554         lnet_net_lock(LNET_LOCK_EX);
3555         list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3556                 list->rlst_nid_array[i] = lpni->lpni_nid;
3557                 i++;
3558                 if (i >= LNET_MAX_SHOW_NUM_NID)
3559                         break;
3560         }
3561         lnet_net_unlock(LNET_LOCK_EX);
3562         list->rlst_num_nids = i;
3563
3564         return 0;
3565 }
3566
3567 /**
3568  * LNet ioctl handler.
3569  *
3570  */
3571 int
3572 LNetCtl(unsigned int cmd, void *arg)
3573 {
3574         struct libcfs_ioctl_data *data = arg;
3575         struct lnet_ioctl_config_data *config;
3576         struct lnet_process_id    id = {0};
3577         struct lnet_ni           *ni;
3578         int                       rc;
3579
3580         BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3581                      sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3582
3583         switch (cmd) {
3584         case IOC_LIBCFS_GET_NI:
3585                 rc = LNetGetId(data->ioc_count, &id);
3586                 data->ioc_nid = id.nid;
3587                 return rc;
3588
3589         case IOC_LIBCFS_FAIL_NID:
3590                 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3591
3592         case IOC_LIBCFS_ADD_ROUTE: {
3593                 /* default router sensitivity to 1 */
3594                 unsigned int sensitivity = 1;
3595                 config = arg;
3596
3597                 if (config->cfg_hdr.ioc_len < sizeof(*config))
3598                         return -EINVAL;
3599
3600                 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3601                         sensitivity =
3602                           config->cfg_config_u.cfg_route.rtr_sensitivity;
3603                 }
3604
3605                 mutex_lock(&the_lnet.ln_api_mutex);
3606                 rc = lnet_add_route(config->cfg_net,
3607                                     config->cfg_config_u.cfg_route.rtr_hop,
3608                                     config->cfg_nid,
3609                                     config->cfg_config_u.cfg_route.
3610                                         rtr_priority, sensitivity);
3611                 mutex_unlock(&the_lnet.ln_api_mutex);
3612                 return rc;
3613         }
3614
3615         case IOC_LIBCFS_DEL_ROUTE:
3616                 config = arg;
3617
3618                 if (config->cfg_hdr.ioc_len < sizeof(*config))
3619                         return -EINVAL;
3620
3621                 mutex_lock(&the_lnet.ln_api_mutex);
3622                 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
3623                 mutex_unlock(&the_lnet.ln_api_mutex);
3624                 return rc;
3625
3626         case IOC_LIBCFS_GET_ROUTE:
3627                 config = arg;
3628
3629                 if (config->cfg_hdr.ioc_len < sizeof(*config))
3630                         return -EINVAL;
3631
3632                 mutex_lock(&the_lnet.ln_api_mutex);
3633                 rc = lnet_get_route(config->cfg_count,
3634                                     &config->cfg_net,
3635                                     &config->cfg_config_u.cfg_route.rtr_hop,
3636                                     &config->cfg_nid,
3637                                     &config->cfg_config_u.cfg_route.rtr_flags,
3638                                     &config->cfg_config_u.cfg_route.
3639                                         rtr_priority,
3640                                     &config->cfg_config_u.cfg_route.
3641                                         rtr_sensitivity);
3642                 mutex_unlock(&the_lnet.ln_api_mutex);
3643                 return rc;
3644
3645         case IOC_LIBCFS_GET_LOCAL_NI: {
3646                 struct lnet_ioctl_config_ni *cfg_ni;
3647                 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3648                 struct lnet_ioctl_element_stats *stats;
3649                 __u32 tun_size;
3650
3651                 cfg_ni = arg;
3652
3653                 /* get the tunables if they are available */
3654                 if (cfg_ni->lic_cfg_hdr.ioc_len <
3655                     sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
3656                         return -EINVAL;
3657
3658                 stats = (struct lnet_ioctl_element_stats *)
3659                         cfg_ni->lic_bulk;
3660                 tun = (struct lnet_ioctl_config_lnd_tunables *)
3661                                 (cfg_ni->lic_bulk + sizeof(*stats));
3662
3663                 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
3664                         sizeof(*stats);
3665
3666                 mutex_lock(&the_lnet.ln_api_mutex);
3667                 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
3668                 mutex_unlock(&the_lnet.ln_api_mutex);
3669                 return rc;
3670         }
3671
3672         case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
3673                 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
3674
3675                 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
3676                         return -EINVAL;
3677
3678                 mutex_lock(&the_lnet.ln_api_mutex);
3679                 rc = lnet_get_ni_stats(msg_stats);
3680                 mutex_unlock(&the_lnet.ln_api_mutex);
3681
3682                 return rc;
3683         }
3684
3685         case IOC_LIBCFS_GET_NET: {
3686                 size_t total = sizeof(*config) +
3687                                sizeof(struct lnet_ioctl_net_config);
3688                 config = arg;
3689
3690                 if (config->cfg_hdr.ioc_len < total)
3691                         return -EINVAL;
3692
3693                 mutex_lock(&the_lnet.ln_api_mutex);
3694                 rc = lnet_get_net_config(config);
3695                 mutex_unlock(&the_lnet.ln_api_mutex);
3696                 return rc;
3697         }
3698
3699         case IOC_LIBCFS_GET_LNET_STATS:
3700         {
3701                 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
3702
3703                 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
3704                         return -EINVAL;
3705
3706                 mutex_lock(&the_lnet.ln_api_mutex);
3707                 lnet_counters_get(&lnet_stats->st_cntrs);
3708                 mutex_unlock(&the_lnet.ln_api_mutex);
3709                 return 0;
3710         }
3711
3712         case IOC_LIBCFS_CONFIG_RTR:
3713                 config = arg;
3714
3715                 if (config->cfg_hdr.ioc_len < sizeof(*config))
3716                         return -EINVAL;
3717
3718                 mutex_lock(&the_lnet.ln_api_mutex);
3719                 if (config->cfg_config_u.cfg_buffers.buf_enable) {
3720                         rc = lnet_rtrpools_enable();
3721                         mutex_unlock(&the_lnet.ln_api_mutex);
3722                         return rc;
3723                 }
3724                 lnet_rtrpools_disable();
3725                 mutex_unlock(&the_lnet.ln_api_mutex);
3726                 return 0;
3727
3728         case IOC_LIBCFS_ADD_BUF:
3729                 config = arg;
3730
3731                 if (config->cfg_hdr.ioc_len < sizeof(*config))
3732                         return -EINVAL;
3733
3734                 mutex_lock(&the_lnet.ln_api_mutex);
3735                 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
3736                                                 buf_tiny,
3737                                           config->cfg_config_u.cfg_buffers.
3738                                                 buf_small,
3739                                           config->cfg_config_u.cfg_buffers.
3740                                                 buf_large);
3741                 mutex_unlock(&the_lnet.ln_api_mutex);
3742                 return rc;
3743
3744         case IOC_LIBCFS_SET_NUMA_RANGE: {
3745                 struct lnet_ioctl_set_value *numa;
3746                 numa = arg;
3747                 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3748                         return -EINVAL;
3749                 lnet_net_lock(LNET_LOCK_EX);
3750                 lnet_numa_range = numa->sv_value;
3751                 lnet_net_unlock(LNET_LOCK_EX);
3752                 return 0;
3753         }
3754
3755         case IOC_LIBCFS_GET_NUMA_RANGE: {
3756                 struct lnet_ioctl_set_value *numa;
3757                 numa = arg;
3758                 if (numa->sv_hdr.ioc_len != sizeof(*numa))
3759                         return -EINVAL;
3760                 numa->sv_value = lnet_numa_range;
3761                 return 0;
3762         }
3763
3764         case IOC_LIBCFS_GET_BUF: {
3765                 struct lnet_ioctl_pool_cfg *pool_cfg;
3766                 size_t total = sizeof(*config) + sizeof(*pool_cfg);
3767
3768                 config = arg;
3769
3770                 if (config->cfg_hdr.ioc_len < total)
3771                         return -EINVAL;
3772
3773                 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
3774
3775                 mutex_lock(&the_lnet.ln_api_mutex);
3776                 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
3777                 mutex_unlock(&the_lnet.ln_api_mutex);
3778                 return rc;
3779         }
3780
3781         case IOC_LIBCFS_GET_LOCAL_HSTATS: {
3782                 struct lnet_ioctl_local_ni_hstats *stats = arg;
3783
3784                 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
3785                         return -EINVAL;
3786
3787                 mutex_lock(&the_lnet.ln_api_mutex);
3788                 rc = lnet_get_local_ni_hstats(stats);
3789                 mutex_unlock(&the_lnet.ln_api_mutex);
3790
3791                 return rc;
3792         }
3793
3794         case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
3795                 struct lnet_ioctl_recovery_list *list = arg;
3796                 if (list->rlst_hdr.ioc_len < sizeof(*list))
3797                         return -EINVAL;
3798
3799                 mutex_lock(&the_lnet.ln_api_mutex);
3800                 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
3801                         rc = lnet_get_local_ni_recovery_list(list);
3802                 else
3803                         rc = lnet_get_peer_ni_recovery_list(list);
3804                 mutex_unlock(&the_lnet.ln_api_mutex);
3805                 return rc;
3806         }
3807
3808         case IOC_LIBCFS_ADD_PEER_NI: {
3809                 struct lnet_ioctl_peer_cfg *cfg = arg;
3810
3811                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3812                         return -EINVAL;
3813
3814                 mutex_lock(&the_lnet.ln_api_mutex);
3815                 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
3816                                       cfg->prcfg_cfg_nid,
3817                                       cfg->prcfg_mr);
3818                 mutex_unlock(&the_lnet.ln_api_mutex);
3819                 return rc;
3820         }
3821
3822         case IOC_LIBCFS_DEL_PEER_NI: {
3823                 struct lnet_ioctl_peer_cfg *cfg = arg;
3824
3825                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3826                         return -EINVAL;
3827
3828                 mutex_lock(&the_lnet.ln_api_mutex);
3829                 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
3830                                       cfg->prcfg_cfg_nid);
3831                 mutex_unlock(&the_lnet.ln_api_mutex);
3832                 return rc;
3833         }
3834
3835         case IOC_LIBCFS_GET_PEER_INFO: {
3836                 struct lnet_ioctl_peer *peer_info = arg;
3837
3838                 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
3839                         return -EINVAL;
3840
3841                 mutex_lock(&the_lnet.ln_api_mutex);
3842                 rc = lnet_get_peer_ni_info(
3843                    peer_info->pr_count,
3844                    &peer_info->pr_nid,
3845                    peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
3846                    &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
3847                    &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
3848                    &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
3849                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
3850                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
3851                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
3852                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
3853                 mutex_unlock(&the_lnet.ln_api_mutex);
3854                 return rc;
3855         }
3856
3857         case IOC_LIBCFS_GET_PEER_NI: {
3858                 struct lnet_ioctl_peer_cfg *cfg = arg;
3859
3860                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3861                         return -EINVAL;
3862
3863                 mutex_lock(&the_lnet.ln_api_mutex);
3864                 rc = lnet_get_peer_info(cfg,
3865                                         (void __user *)cfg->prcfg_bulk);
3866                 mutex_unlock(&the_lnet.ln_api_mutex);
3867                 return rc;
3868         }
3869
3870         case IOC_LIBCFS_GET_PEER_LIST: {
3871                 struct lnet_ioctl_peer_cfg *cfg = arg;
3872
3873                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
3874                         return -EINVAL;
3875
3876                 mutex_lock(&the_lnet.ln_api_mutex);
3877                 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
3878                                 (struct lnet_process_id __user *)cfg->prcfg_bulk);
3879                 mutex_unlock(&the_lnet.ln_api_mutex);
3880                 return rc;
3881         }
3882
3883         case IOC_LIBCFS_SET_HEALHV: {
3884                 struct lnet_ioctl_reset_health_cfg *cfg = arg;
3885                 int value;
3886                 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
3887                         return -EINVAL;
3888                 if (cfg->rh_value < 0 ||
3889                     cfg->rh_value > LNET_MAX_HEALTH_VALUE)
3890                         value = LNET_MAX_HEALTH_VALUE;
3891                 else
3892                         value = cfg->rh_value;
3893                 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
3894                        value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
3895                        "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
3896                 mutex_lock(&the_lnet.ln_api_mutex);
3897                 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
3898                         lnet_ni_set_healthv(cfg->rh_nid, value,
3899                                              cfg->rh_all);
3900                 else
3901                         lnet_peer_ni_set_healthv(cfg->rh_nid, value,
3902                                                   cfg->rh_all);
3903                 mutex_unlock(&the_lnet.ln_api_mutex);
3904                 return 0;
3905         }
3906
3907         case IOC_LIBCFS_NOTIFY_ROUTER: {
3908                 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
3909
3910                 /* The deadline passed in by the user should be some time in
3911                  * seconds in the future since the UNIX epoch. We have to map
3912                  * that deadline to the wall clock.
3913                  */
3914                 deadline += ktime_get_seconds();
3915                 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
3916                                    deadline);
3917         }
3918
3919         case IOC_LIBCFS_LNET_DIST:
3920                 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3921                 if (rc < 0 && rc != -EHOSTUNREACH)
3922                         return rc;
3923
3924                 data->ioc_u32[0] = rc;
3925                 return 0;
3926
3927         case IOC_LIBCFS_TESTPROTOCOMPAT:
3928                 the_lnet.ln_testprotocompat = data->ioc_flags;
3929                 return 0;
3930
3931         case IOC_LIBCFS_LNET_FAULT:
3932                 return lnet_fault_ctl(data->ioc_flags, data);
3933
3934         case IOC_LIBCFS_PING: {
3935                 signed long timeout;
3936
3937                 id.nid = data->ioc_nid;
3938                 id.pid = data->ioc_u32[0];
3939
3940                 /* If timeout is negative then set default of 3 minutes */
3941                 if (((s32)data->ioc_u32[1] <= 0) ||
3942                     data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3943                         timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3944                 else
3945                         timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
3946
3947                 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3948                                data->ioc_plen1 / sizeof(struct lnet_process_id));
3949
3950                 if (rc < 0)
3951                         return rc;
3952
3953                 data->ioc_count = rc;
3954                 return 0;
3955         }
3956
3957         case IOC_LIBCFS_PING_PEER: {
3958                 struct lnet_ioctl_ping_data *ping = arg;
3959                 struct lnet_peer *lp;
3960                 signed long timeout;
3961
3962                 /* If timeout is negative then set default of 3 minutes */
3963                 if (((s32)ping->op_param) <= 0 ||
3964                     ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
3965                         timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
3966                 else
3967                         timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
3968
3969                 rc = lnet_ping(ping->ping_id, timeout,
3970                                ping->ping_buf,
3971                                ping->ping_count);
3972                 if (rc < 0)
3973                         return rc;
3974
3975                 mutex_lock(&the_lnet.ln_api_mutex);
3976                 lp = lnet_find_peer(ping->ping_id.nid);
3977                 if (lp) {
3978                         ping->ping_id.nid = lp->lp_primary_nid;
3979                         ping->mr_info = lnet_peer_is_multi_rail(lp);
3980                         lnet_peer_decref_locked(lp);
3981                 }
3982                 mutex_unlock(&the_lnet.ln_api_mutex);
3983
3984                 ping->ping_count = rc;
3985                 return 0;
3986         }
3987
3988         case IOC_LIBCFS_DISCOVER: {
3989                 struct lnet_ioctl_ping_data *discover = arg;
3990                 struct lnet_peer *lp;
3991
3992                 rc = lnet_discover(discover->ping_id, discover->op_param,
3993                                    discover->ping_buf,
3994                                    discover->ping_count);
3995                 if (rc < 0)
3996                         return rc;
3997
3998                 mutex_lock(&the_lnet.ln_api_mutex);
3999                 lp = lnet_find_peer(discover->ping_id.nid);
4000                 if (lp) {
4001                         discover->ping_id.nid = lp->lp_primary_nid;
4002                         discover->mr_info = lnet_peer_is_multi_rail(lp);
4003                         lnet_peer_decref_locked(lp);
4004                 }
4005                 mutex_unlock(&the_lnet.ln_api_mutex);
4006
4007                 discover->ping_count = rc;
4008                 return 0;
4009         }
4010
4011         default:
4012                 ni = lnet_net2ni_addref(data->ioc_net);
4013                 if (ni == NULL)
4014                         return -EINVAL;
4015
4016                 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
4017                         rc = -EINVAL;
4018                 else
4019                         rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4020
4021                 lnet_ni_decref(ni);
4022                 return rc;
4023         }
4024         /* not reached */
4025 }
4026 EXPORT_SYMBOL(LNetCtl);
4027
4028 void LNetDebugPeer(struct lnet_process_id id)
4029 {
4030         lnet_debug_peer(id.nid);
4031 }
4032 EXPORT_SYMBOL(LNetDebugPeer);
4033
4034 /**
4035  * Determine if the specified peer \a nid is on the local node.
4036  *
4037  * \param nid   peer nid to check
4038  *
4039  * \retval true         If peer NID is on the local node.
4040  * \retval false        If peer NID is not on the local node.
4041  */
4042 bool LNetIsPeerLocal(lnet_nid_t nid)
4043 {
4044         struct lnet_net *net;
4045         struct lnet_ni *ni;
4046         int cpt;
4047
4048         cpt = lnet_net_lock_current();
4049         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4050                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4051                         if (ni->ni_nid == nid) {
4052                                 lnet_net_unlock(cpt);
4053                                 return true;
4054                         }
4055                 }
4056         }
4057         lnet_net_unlock(cpt);
4058
4059         return false;
4060 }
4061 EXPORT_SYMBOL(LNetIsPeerLocal);
4062
4063 /**
4064  * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4065  * Note that all interfaces share a same PID, as requested by LNetNIInit().
4066  *
4067  * \param index Index of the interface to look up.
4068  * \param id On successful return, this location will hold the
4069  * struct lnet_process_id ID of the interface.
4070  *
4071  * \retval 0 If an interface exists at \a index.
4072  * \retval -ENOENT If no interface has been found.
4073  */
4074 int
4075 LNetGetId(unsigned int index, struct lnet_process_id *id)
4076 {
4077         struct lnet_ni   *ni;
4078         struct lnet_net  *net;
4079         int               cpt;
4080         int               rc = -ENOENT;
4081
4082         LASSERT(the_lnet.ln_refcount > 0);
4083
4084         cpt = lnet_net_lock_current();
4085
4086         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4087                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4088                         if (index-- != 0)
4089                                 continue;
4090
4091                         id->nid = ni->ni_nid;
4092                         id->pid = the_lnet.ln_pid;
4093                         rc = 0;
4094                         break;
4095                 }
4096         }
4097
4098         lnet_net_unlock(cpt);
4099         return rc;
4100 }
4101 EXPORT_SYMBOL(LNetGetId);
4102
4103 static int lnet_ping(struct lnet_process_id id, signed long timeout,
4104                      struct lnet_process_id __user *ids, int n_ids)
4105 {
4106         struct lnet_handle_eq eqh;
4107         struct lnet_handle_md mdh;
4108         struct lnet_event event;
4109         struct lnet_md md = { NULL };
4110         int which;
4111         int unlinked = 0;
4112         int replied = 0;
4113         const signed long a_long_time = cfs_time_seconds(60);
4114         struct lnet_ping_buffer *pbuf;
4115         struct lnet_process_id tmpid;
4116         int i;
4117         int nob;
4118         int rc;
4119         int rc2;
4120         sigset_t blocked;
4121
4122         /* n_ids limit is arbitrary */
4123         if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4124                 return -EINVAL;
4125
4126         /*
4127          * if the user buffer has more space than the lnet_interfaces_max
4128          * then only fill it up to lnet_interfaces_max
4129          */
4130         if (n_ids > lnet_interfaces_max)
4131                 n_ids = lnet_interfaces_max;
4132
4133         if (id.pid == LNET_PID_ANY)
4134                 id.pid = LNET_PID_LUSTRE;
4135
4136         pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4137         if (!pbuf)
4138                 return -ENOMEM;
4139
4140         /* NB 2 events max (including any unlink event) */
4141         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
4142         if (rc != 0) {
4143                 CERROR("Can't allocate EQ: %d\n", rc);
4144                 goto fail_ping_buffer_decref;
4145         }
4146
4147         /* initialize md content */
4148         md.start     = &pbuf->pb_info;
4149         md.length    = LNET_PING_INFO_SIZE(n_ids);
4150         md.threshold = 2; /* GET/REPLY */
4151         md.max_size  = 0;
4152         md.options   = LNET_MD_TRUNCATE;
4153         md.user_ptr  = NULL;
4154         md.eq_handle = eqh;
4155
4156         rc = LNetMDBind(md, LNET_UNLINK, &mdh);
4157         if (rc != 0) {
4158                 CERROR("Can't bind MD: %d\n", rc);
4159                 goto fail_free_eq;
4160         }
4161
4162         rc = LNetGet(LNET_NID_ANY, mdh, id,
4163                      LNET_RESERVED_PORTAL,
4164                      LNET_PROTO_PING_MATCHBITS, 0, false);
4165
4166         if (rc != 0) {
4167                 /* Don't CERROR; this could be deliberate! */
4168                 rc2 = LNetMDUnlink(mdh);
4169                 LASSERT(rc2 == 0);
4170
4171                 /* NB must wait for the UNLINK event below... */
4172                 unlinked = 1;
4173                 timeout = a_long_time;
4174         }
4175
4176         do {
4177                 /* MUST block for unlink to complete */
4178                 if (unlinked)
4179                         blocked = cfs_block_allsigs();
4180
4181                 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
4182
4183                 if (unlinked)
4184                         cfs_restore_sigs(blocked);
4185
4186                 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
4187                        (rc2 <= 0) ? -1 : event.type,
4188                        (rc2 <= 0) ? -1 : event.status,
4189                        (rc2 > 0 && event.unlinked) ? " unlinked" : "");
4190
4191                 LASSERT(rc2 != -EOVERFLOW);     /* can't miss anything */
4192
4193                 if (rc2 <= 0 || event.status != 0) {
4194                         /* timeout or error */
4195                         if (!replied && rc == 0)
4196                                 rc = (rc2 < 0) ? rc2 :
4197                                      (rc2 == 0) ? -ETIMEDOUT :
4198                                      event.status;
4199
4200                         if (!unlinked) {
4201                                 /* Ensure completion in finite time... */
4202                                 LNetMDUnlink(mdh);
4203                                 /* No assertion (racing with network) */
4204                                 unlinked = 1;
4205                                 timeout = a_long_time;
4206                         } else if (rc2 == 0) {
4207                                 /* timed out waiting for unlink */
4208                                 CWARN("ping %s: late network completion\n",
4209                                       libcfs_id2str(id));
4210                         }
4211                 } else if (event.type == LNET_EVENT_REPLY) {
4212                         replied = 1;
4213                         rc = event.mlength;
4214                 }
4215         } while (rc2 <= 0 || !event.unlinked);
4216
4217         if (!replied) {
4218                 if (rc >= 0)
4219                         CWARN("%s: Unexpected rc >= 0 but no reply!\n",
4220                               libcfs_id2str(id));
4221                 rc = -EIO;
4222                 goto fail_free_eq;
4223         }
4224
4225         nob = rc;
4226         LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4227
4228         rc = -EPROTO;           /* if I can't parse... */
4229
4230         if (nob < 8) {
4231                 CERROR("%s: ping info too short %d\n",
4232                        libcfs_id2str(id), nob);
4233                 goto fail_free_eq;
4234         }
4235
4236         if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4237                 lnet_swap_pinginfo(pbuf);
4238         } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4239                 CERROR("%s: Unexpected magic %08x\n",
4240                        libcfs_id2str(id), pbuf->pb_info.pi_magic);
4241                 goto fail_free_eq;
4242         }
4243
4244         if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4245                 CERROR("%s: ping w/o NI status: 0x%x\n",
4246                        libcfs_id2str(id), pbuf->pb_info.pi_features);
4247                 goto fail_free_eq;
4248         }
4249
4250         if (nob < LNET_PING_INFO_SIZE(0)) {
4251                 CERROR("%s: Short reply %d(%d min)\n",
4252                        libcfs_id2str(id),
4253                        nob, (int)LNET_PING_INFO_SIZE(0));
4254                 goto fail_free_eq;
4255         }
4256
4257         if (pbuf->pb_info.pi_nnis < n_ids)
4258                 n_ids = pbuf->pb_info.pi_nnis;
4259
4260         if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4261                 CERROR("%s: Short reply %d(%d expected)\n",
4262                        libcfs_id2str(id),
4263                        nob, (int)LNET_PING_INFO_SIZE(n_ids));
4264                 goto fail_free_eq;
4265         }
4266
4267         rc = -EFAULT;           /* if I segv in copy_to_user()... */
4268
4269         memset(&tmpid, 0, sizeof(tmpid));
4270         for (i = 0; i < n_ids; i++) {
4271                 tmpid.pid = pbuf->pb_info.pi_pid;
4272                 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4273                 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4274                         goto fail_free_eq;
4275         }
4276         rc = pbuf->pb_info.pi_nnis;
4277
4278  fail_free_eq:
4279         rc2 = LNetEQFree(eqh);
4280         if (rc2 != 0)
4281                 CERROR("rc2 %d\n", rc2);
4282         LASSERT(rc2 == 0);
4283
4284  fail_ping_buffer_decref:
4285         lnet_ping_buffer_decref(pbuf);
4286         return rc;
4287 }
4288
4289 static int
4290 lnet_discover(struct lnet_process_id id, __u32 force,
4291               struct lnet_process_id __user *ids, int n_ids)
4292 {
4293         struct lnet_peer_ni *lpni;
4294         struct lnet_peer_ni *p;
4295         struct lnet_peer *lp;
4296         struct lnet_process_id *buf;
4297         int cpt;
4298         int i;
4299         int rc;
4300         int max_intf = lnet_interfaces_max;
4301         size_t buf_size;
4302
4303         if (n_ids <= 0 ||
4304             id.nid == LNET_NID_ANY)
4305                 return -EINVAL;
4306
4307         if (id.pid == LNET_PID_ANY)
4308                 id.pid = LNET_PID_LUSTRE;
4309
4310         /*
4311          * if the user buffer has more space than the max_intf
4312          * then only fill it up to max_intf
4313          */
4314         if (n_ids > max_intf)
4315                 n_ids = max_intf;
4316
4317         buf_size = n_ids * sizeof(*buf);
4318
4319         LIBCFS_ALLOC(buf, buf_size);
4320         if (!buf)
4321                 return -ENOMEM;
4322
4323         cpt = lnet_net_lock_current();
4324         lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4325         if (IS_ERR(lpni)) {
4326                 rc = PTR_ERR(lpni);
4327                 goto out;
4328         }
4329
4330         /*
4331          * Clearing the NIDS_UPTODATE flag ensures the peer will
4332          * be discovered, provided discovery has not been disabled.
4333          */
4334         lp = lpni->lpni_peer_net->lpn_peer;
4335         spin_lock(&lp->lp_lock);
4336         lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4337         /* If the force flag is set, force a PING and PUSH as well. */
4338         if (force)
4339                 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4340         spin_unlock(&lp->lp_lock);
4341         rc = lnet_discover_peer_locked(lpni, cpt, true);
4342         if (rc)
4343                 goto out_decref;
4344
4345         /* Peer may have changed. */
4346         lp = lpni->lpni_peer_net->lpn_peer;
4347         if (lp->lp_nnis < n_ids)
4348                 n_ids = lp->lp_nnis;
4349
4350         i = 0;
4351         p = NULL;
4352         while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4353                 buf[i].pid = id.pid;
4354                 buf[i].nid = p->lpni_nid;
4355                 if (++i >= n_ids)
4356                         break;
4357         }
4358
4359         lnet_net_unlock(cpt);
4360
4361         rc = -EFAULT;
4362         if (copy_to_user(ids, buf, n_ids * sizeof(*buf)))
4363                 goto out_relock;
4364         rc = n_ids;
4365 out_relock:
4366         lnet_net_lock(cpt);
4367 out_decref:
4368         lnet_peer_ni_decref_locked(lpni);
4369 out:
4370         lnet_net_unlock(cpt);
4371
4372         LIBCFS_FREE(buf, buf_size);
4373
4374         return rc;
4375 }
4376
4377 /**
4378  * Retrieve peer discovery status.
4379  *
4380  * \retval 1 if lnet_peer_discovery_disabled is 0
4381  * \retval 0 if lnet_peer_discovery_disabled is 1
4382  */
4383 int
4384 LNetGetPeerDiscoveryStatus(void)
4385 {
4386         return !lnet_peer_discovery_disabled;
4387 }
4388 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);