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