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