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