Whamcloud - gitweb
LU-14676 lnet: improve hash distribution across CPTs
[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, struct lnet_nid *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 _lnet_hdr_nid4 */
780         BUILD_BUG_ON((int)sizeof(struct _lnet_hdr_nid4) != 72);
781         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, dest_nid) != 0);
782         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->dest_nid) != 8);
783         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, src_nid) != 8);
784         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->src_nid) != 8);
785         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, dest_pid) != 16);
786         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->dest_pid) != 4);
787         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, src_pid) != 20);
788         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->src_pid) != 4);
789         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, type) != 24);
790         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->type) != 4);
791         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, payload_length) != 28);
792         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->payload_length) != 4);
793         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg) != 32);
794         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg) != 40);
795
796         /* Ack */
797         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.ack.dst_wmd) != 32);
798         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.ack.dst_wmd) != 16);
799         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.ack.match_bits) != 48);
800         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.ack.match_bits) != 8);
801         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.ack.mlength) != 56);
802         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.ack.mlength) != 4);
803
804         /* Put */
805         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.put.ack_wmd) != 32);
806         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.put.ack_wmd) != 16);
807         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.put.match_bits) != 48);
808         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.put.match_bits) != 8);
809         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.put.hdr_data) != 56);
810         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.put.hdr_data) != 8);
811         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.put.ptl_index) != 64);
812         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.put.ptl_index) != 4);
813         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.put.offset) != 68);
814         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.put.offset) != 4);
815
816         /* Get */
817         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.get.return_wmd) != 32);
818         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.get.return_wmd) != 16);
819         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.get.match_bits) != 48);
820         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.get.match_bits) != 8);
821         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.get.ptl_index) != 56);
822         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.get.ptl_index) != 4);
823         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.get.src_offset) != 60);
824         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.get.src_offset) != 4);
825         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.get.sink_length) != 64);
826         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.get.sink_length) != 4);
827
828         /* Reply */
829         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.reply.dst_wmd) != 32);
830         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.reply.dst_wmd) != 16);
831
832         /* Hello */
833         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.hello.incarnation) != 32);
834         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)0)->msg.hello.incarnation) != 8);
835         BUILD_BUG_ON((int)offsetof(struct _lnet_hdr_nid4, msg.hello.type) != 40);
836         BUILD_BUG_ON((int)sizeof(((struct _lnet_hdr_nid4 *)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         __u64 pair_bits = 0x0001000100010001LLU;
1525         __u64 mask = pair_bits * 0xFF;
1526         __u64 pair_sum;
1527
1528         /* Use (sum-by-multiplication of nid bytes) mod (number of CPTs)
1529          * to match nid to a CPT.
1530          */
1531         pair_sum = (key & mask) + ((key >> 8) & mask);
1532         pair_sum = (pair_sum * pair_bits) >> 48;
1533
1534         CDEBUG(D_NET, "Match nid %s to cpt %u\n",
1535                libcfs_nid2str(nid), (unsigned int)(pair_sum) % number);
1536
1537         return (unsigned int)(pair_sum) % number;
1538 }
1539
1540 unsigned int
1541 lnet_nid_cpt_hash(struct lnet_nid *nid, unsigned int number)
1542 {
1543         unsigned int val;
1544         u32 h = 0;
1545         int i;
1546
1547         LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
1548
1549         if (number == 1)
1550                 return 0;
1551
1552         if (nid_is_nid4(nid))
1553                 return lnet_nid4_cpt_hash(lnet_nid_to_nid4(nid), number);
1554
1555         for (i = 0; i < 4; i++)
1556                 h = hash_32(nid->nid_addr[i]^h, 32);
1557         val = hash_32(LNET_NID_NET(nid) ^ h, LNET_CPT_BITS);
1558         if (val < number)
1559                 return val;
1560         return (unsigned int)(h + val + (val >> 1)) % number;
1561 }
1562
1563 int
1564 lnet_cpt_of_nid_locked(struct lnet_nid *nid, struct lnet_ni *ni)
1565 {
1566         struct lnet_net *net;
1567
1568         /* must called with hold of lnet_net_lock */
1569         if (LNET_CPT_NUMBER == 1)
1570                 return 0; /* the only one */
1571
1572         /*
1573          * If NI is provided then use the CPT identified in the NI cpt
1574          * list if one exists. If one doesn't exist, then that NI is
1575          * associated with all CPTs and it follows that the net it belongs
1576          * to is implicitly associated with all CPTs, so just hash the nid
1577          * and return that.
1578          */
1579         if (ni != NULL) {
1580                 if (ni->ni_cpts != NULL)
1581                         return ni->ni_cpts[lnet_nid_cpt_hash(nid,
1582                                                              ni->ni_ncpts)];
1583                 else
1584                         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1585         }
1586
1587         /* no NI provided so look at the net */
1588         net = lnet_get_net_locked(LNET_NID_NET(nid));
1589
1590         if (net != NULL && net->net_cpts != NULL) {
1591                 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
1592         }
1593
1594         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
1595 }
1596
1597 int
1598 lnet_nid2cpt(struct lnet_nid *nid, struct lnet_ni *ni)
1599 {
1600         int     cpt;
1601         int     cpt2;
1602
1603         if (LNET_CPT_NUMBER == 1)
1604                 return 0; /* the only one */
1605
1606         cpt = lnet_net_lock_current();
1607
1608         cpt2 = lnet_cpt_of_nid_locked(nid, ni);
1609
1610         lnet_net_unlock(cpt);
1611
1612         return cpt2;
1613 }
1614 EXPORT_SYMBOL(lnet_nid2cpt);
1615
1616 int
1617 lnet_cpt_of_nid(lnet_nid_t nid4, struct lnet_ni *ni)
1618 {
1619         struct lnet_nid nid;
1620
1621         if (LNET_CPT_NUMBER == 1)
1622                 return 0; /* the only one */
1623
1624         lnet_nid4_to_nid(nid4, &nid);
1625         return lnet_nid2cpt(&nid, ni);
1626 }
1627 EXPORT_SYMBOL(lnet_cpt_of_nid);
1628
1629 int
1630 lnet_islocalnet_locked(__u32 net_id)
1631 {
1632         struct lnet_net *net;
1633         bool local;
1634
1635         net = lnet_get_net_locked(net_id);
1636
1637         local = net != NULL;
1638
1639         return local;
1640 }
1641
1642 int
1643 lnet_islocalnet(__u32 net_id)
1644 {
1645         int cpt;
1646         bool local;
1647
1648         cpt = lnet_net_lock_current();
1649
1650         local = lnet_islocalnet_locked(net_id);
1651
1652         lnet_net_unlock(cpt);
1653
1654         return local;
1655 }
1656
1657 struct lnet_ni  *
1658 lnet_nid_to_ni_locked(struct lnet_nid *nid, int cpt)
1659 {
1660         struct lnet_net  *net;
1661         struct lnet_ni *ni;
1662
1663         LASSERT(cpt != LNET_LOCK_EX);
1664
1665         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1666                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1667                         if (nid_same(&ni->ni_nid, nid))
1668                                 return ni;
1669                 }
1670         }
1671
1672         return NULL;
1673 }
1674
1675 struct lnet_ni  *
1676 lnet_nid2ni_locked(lnet_nid_t nid4, int cpt)
1677 {
1678         struct lnet_nid nid;
1679
1680         lnet_nid4_to_nid(nid4, &nid);
1681         return lnet_nid_to_ni_locked(&nid, cpt);
1682 }
1683
1684 struct lnet_ni *
1685 lnet_nid2ni_addref(lnet_nid_t nid4)
1686 {
1687         struct lnet_ni *ni;
1688         struct lnet_nid nid;
1689
1690         lnet_nid4_to_nid(nid4, &nid);
1691
1692         lnet_net_lock(0);
1693         ni = lnet_nid_to_ni_locked(&nid, 0);
1694         if (ni)
1695                 lnet_ni_addref_locked(ni, 0);
1696         lnet_net_unlock(0);
1697
1698         return ni;
1699 }
1700 EXPORT_SYMBOL(lnet_nid2ni_addref);
1701
1702 struct lnet_ni *
1703 lnet_nid_to_ni_addref(struct lnet_nid *nid)
1704 {
1705         struct lnet_ni *ni;
1706
1707         lnet_net_lock(0);
1708         ni = lnet_nid_to_ni_locked(nid, 0);
1709         if (ni)
1710                 lnet_ni_addref_locked(ni, 0);
1711         lnet_net_unlock(0);
1712
1713         return ni;
1714 }
1715 EXPORT_SYMBOL(lnet_nid_to_ni_addref);
1716
1717 int
1718 lnet_islocalnid(struct lnet_nid *nid)
1719 {
1720         struct lnet_ni  *ni;
1721         int             cpt;
1722
1723         cpt = lnet_net_lock_current();
1724         ni = lnet_nid_to_ni_locked(nid, cpt);
1725         lnet_net_unlock(cpt);
1726
1727         return ni != NULL;
1728 }
1729
1730 int
1731 lnet_count_acceptor_nets(void)
1732 {
1733         /* Return the # of NIs that need the acceptor. */
1734         int              count = 0;
1735         struct lnet_net  *net;
1736         int              cpt;
1737
1738         cpt = lnet_net_lock_current();
1739         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1740                 /* all socklnd type networks should have the acceptor
1741                  * thread started */
1742                 if (net->net_lnd->lnd_accept != NULL)
1743                         count++;
1744         }
1745
1746         lnet_net_unlock(cpt);
1747
1748         return count;
1749 }
1750
1751 struct lnet_ping_buffer *
1752 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1753 {
1754         struct lnet_ping_buffer *pbuf;
1755
1756         LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1757         if (pbuf) {
1758                 pbuf->pb_nnis = nnis;
1759                 pbuf->pb_needs_post = false;
1760                 atomic_set(&pbuf->pb_refcnt, 1);
1761         }
1762
1763         return pbuf;
1764 }
1765
1766 void
1767 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1768 {
1769         LASSERT(atomic_read(&pbuf->pb_refcnt) == 0);
1770         LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1771 }
1772
1773 static struct lnet_ping_buffer *
1774 lnet_ping_target_create(int nnis)
1775 {
1776         struct lnet_ping_buffer *pbuf;
1777
1778         pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1779         if (pbuf == NULL) {
1780                 CERROR("Can't allocate ping source [%d]\n", nnis);
1781                 return NULL;
1782         }
1783
1784         pbuf->pb_info.pi_nnis = nnis;
1785         pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1786         pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1787         pbuf->pb_info.pi_features =
1788                 LNET_PING_FEAT_NI_STATUS | LNET_PING_FEAT_MULTI_RAIL;
1789
1790         return pbuf;
1791 }
1792
1793 static inline int
1794 lnet_get_net_ni_count_locked(struct lnet_net *net)
1795 {
1796         struct lnet_ni  *ni;
1797         int             count = 0;
1798
1799         list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1800                 count++;
1801
1802         return count;
1803 }
1804
1805 static inline int
1806 lnet_get_net_ni_count_pre(struct lnet_net *net)
1807 {
1808         struct lnet_ni  *ni;
1809         int             count = 0;
1810
1811         list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1812                 count++;
1813
1814         return count;
1815 }
1816
1817 static inline int
1818 lnet_get_ni_count(void)
1819 {
1820         struct lnet_ni  *ni;
1821         struct lnet_net *net;
1822         int             count = 0;
1823
1824         lnet_net_lock(0);
1825
1826         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1827                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1828                         count++;
1829         }
1830
1831         lnet_net_unlock(0);
1832
1833         return count;
1834 }
1835
1836 void
1837 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
1838 {
1839         struct lnet_ni_status *stat;
1840         int nnis;
1841         int i;
1842
1843         __swab32s(&pbuf->pb_info.pi_magic);
1844         __swab32s(&pbuf->pb_info.pi_features);
1845         __swab32s(&pbuf->pb_info.pi_pid);
1846         __swab32s(&pbuf->pb_info.pi_nnis);
1847         nnis = pbuf->pb_info.pi_nnis;
1848         if (nnis > pbuf->pb_nnis)
1849                 nnis = pbuf->pb_nnis;
1850         for (i = 0; i < nnis; i++) {
1851                 stat = &pbuf->pb_info.pi_ni[i];
1852                 __swab64s(&stat->ns_nid);
1853                 __swab32s(&stat->ns_status);
1854         }
1855 }
1856
1857 int
1858 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1859 {
1860         if (!pinfo)
1861                 return -EINVAL;
1862         if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1863                 return -EPROTO;
1864         if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1865                 return -EPROTO;
1866         /* Loopback is guaranteed to be present */
1867         if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1868                 return -ERANGE;
1869         if (LNET_PING_INFO_LONI(pinfo) != LNET_NID_LO_0)
1870                 return -EPROTO;
1871         return 0;
1872 }
1873
1874 static void
1875 lnet_ping_target_destroy(void)
1876 {
1877         struct lnet_net *net;
1878         struct lnet_ni  *ni;
1879
1880         lnet_net_lock(LNET_LOCK_EX);
1881
1882         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1883                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1884                         lnet_ni_lock(ni);
1885                         ni->ni_status = NULL;
1886                         lnet_ni_unlock(ni);
1887                 }
1888         }
1889
1890         lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1891         the_lnet.ln_ping_target = NULL;
1892
1893         lnet_net_unlock(LNET_LOCK_EX);
1894 }
1895
1896 static void
1897 lnet_ping_target_event_handler(struct lnet_event *event)
1898 {
1899         struct lnet_ping_buffer *pbuf = event->md_user_ptr;
1900
1901         if (event->unlinked)
1902                 lnet_ping_buffer_decref(pbuf);
1903 }
1904
1905 static int
1906 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1907                        struct lnet_handle_md *ping_mdh,
1908                        int ni_count, bool set_eq)
1909 {
1910         struct lnet_processid id = {
1911                 .nid = LNET_ANY_NID,
1912                 .pid = LNET_PID_ANY
1913         };
1914         struct lnet_me *me;
1915         struct lnet_md md = { NULL };
1916         int rc;
1917
1918         if (set_eq)
1919                 the_lnet.ln_ping_target_handler =
1920                         lnet_ping_target_event_handler;
1921
1922         *ppbuf = lnet_ping_target_create(ni_count);
1923         if (*ppbuf == NULL) {
1924                 rc = -ENOMEM;
1925                 goto fail_free_eq;
1926         }
1927
1928         /* Ping target ME/MD */
1929         me = LNetMEAttach(LNET_RESERVED_PORTAL, &id,
1930                           LNET_PROTO_PING_MATCHBITS, 0,
1931                           LNET_UNLINK, LNET_INS_AFTER);
1932         if (IS_ERR(me)) {
1933                 rc = PTR_ERR(me);
1934                 CERROR("Can't create ping target ME: %d\n", rc);
1935                 goto fail_decref_ping_buffer;
1936         }
1937
1938         /* initialize md content */
1939         md.start     = &(*ppbuf)->pb_info;
1940         md.length    = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1941         md.threshold = LNET_MD_THRESH_INF;
1942         md.max_size  = 0;
1943         md.options   = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1944                        LNET_MD_MANAGE_REMOTE;
1945         md.handler   = the_lnet.ln_ping_target_handler;
1946         md.user_ptr  = *ppbuf;
1947
1948         rc = LNetMDAttach(me, &md, LNET_RETAIN, ping_mdh);
1949         if (rc != 0) {
1950                 CERROR("Can't attach ping target MD: %d\n", rc);
1951                 goto fail_decref_ping_buffer;
1952         }
1953         lnet_ping_buffer_addref(*ppbuf);
1954
1955         return 0;
1956
1957 fail_decref_ping_buffer:
1958         LASSERT(atomic_read(&(*ppbuf)->pb_refcnt) == 1);
1959         lnet_ping_buffer_decref(*ppbuf);
1960         *ppbuf = NULL;
1961 fail_free_eq:
1962         return rc;
1963 }
1964
1965 static void
1966 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1967                     struct lnet_handle_md *ping_mdh)
1968 {
1969         LNetMDUnlink(*ping_mdh);
1970         LNetInvalidateMDHandle(ping_mdh);
1971
1972         /* NB the MD could be busy; this just starts the unlink */
1973         wait_var_event_warning(&pbuf->pb_refcnt,
1974                                atomic_read(&pbuf->pb_refcnt) <= 1,
1975                                "Still waiting for ping data MD to unlink\n");
1976 }
1977
1978 static void
1979 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1980 {
1981         struct lnet_ni          *ni;
1982         struct lnet_net         *net;
1983         struct lnet_ni_status *ns;
1984         int                     i;
1985         int                     rc;
1986
1987         i = 0;
1988         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1989                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1990                         LASSERT(i < pbuf->pb_nnis);
1991
1992                         ns = &pbuf->pb_info.pi_ni[i];
1993
1994                         if (!nid_is_nid4(&ni->ni_nid))
1995                                 continue;
1996                         ns->ns_nid = lnet_nid_to_nid4(&ni->ni_nid);
1997
1998                         lnet_ni_lock(ni);
1999                         ns->ns_status = lnet_ni_get_status_locked(ni);
2000                         ni->ni_status = ns;
2001                         lnet_ni_unlock(ni);
2002
2003                         i++;
2004                 }
2005         }
2006         /*
2007          * We (ab)use the ns_status of the loopback interface to
2008          * transmit the sequence number. The first interface listed
2009          * must be the loopback interface.
2010          */
2011         rc = lnet_ping_info_validate(&pbuf->pb_info);
2012         if (rc) {
2013                 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
2014                 LBUG();
2015         }
2016         LNET_PING_BUFFER_SEQNO(pbuf) =
2017                 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
2018 }
2019
2020 static void
2021 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
2022                         struct lnet_handle_md ping_mdh)
2023 {
2024         struct lnet_ping_buffer *old_pbuf = NULL;
2025         struct lnet_handle_md old_ping_md;
2026
2027         /* switch the NIs to point to the new ping info created */
2028         lnet_net_lock(LNET_LOCK_EX);
2029
2030         if (!the_lnet.ln_routing)
2031                 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
2032         if (!lnet_peer_discovery_disabled)
2033                 pbuf->pb_info.pi_features |= LNET_PING_FEAT_DISCOVERY;
2034
2035         /* Ensure only known feature bits have been set. */
2036         LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
2037         LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
2038
2039         lnet_ping_target_install_locked(pbuf);
2040
2041         if (the_lnet.ln_ping_target) {
2042                 old_pbuf = the_lnet.ln_ping_target;
2043                 old_ping_md = the_lnet.ln_ping_target_md;
2044         }
2045         the_lnet.ln_ping_target_md = ping_mdh;
2046         the_lnet.ln_ping_target = pbuf;
2047
2048         lnet_net_unlock(LNET_LOCK_EX);
2049
2050         if (old_pbuf) {
2051                 /* unlink and free the old ping info */
2052                 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
2053                 lnet_ping_buffer_decref(old_pbuf);
2054         }
2055
2056         lnet_push_update_to_peers(0);
2057 }
2058
2059 static void
2060 lnet_ping_target_fini(void)
2061 {
2062         lnet_ping_md_unlink(the_lnet.ln_ping_target,
2063                             &the_lnet.ln_ping_target_md);
2064
2065         lnet_assert_handler_unused(the_lnet.ln_ping_target_handler);
2066         lnet_ping_target_destroy();
2067 }
2068
2069 /* Resize the push target. */
2070 int lnet_push_target_resize(void)
2071 {
2072         struct lnet_handle_md mdh;
2073         struct lnet_handle_md old_mdh;
2074         struct lnet_ping_buffer *pbuf;
2075         struct lnet_ping_buffer *old_pbuf;
2076         int nnis;
2077         int rc;
2078
2079 again:
2080         nnis = the_lnet.ln_push_target_nnis;
2081         if (nnis <= 0) {
2082                 CDEBUG(D_NET, "Invalid nnis %d\n", nnis);
2083                 return -EINVAL;
2084         }
2085
2086         /* NB: lnet_ping_buffer_alloc() sets pbuf refcount to 1. That ref is
2087          * dropped when we need to resize again (see "old_pbuf" below) or when
2088          * LNet is shutdown (see lnet_push_target_fini())
2089          */
2090         pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
2091         if (!pbuf) {
2092                 CDEBUG(D_NET, "Can't allocate pbuf for nnis %d\n", nnis);
2093                 return -ENOMEM;
2094         }
2095
2096         rc = lnet_push_target_post(pbuf, &mdh);
2097         if (rc) {
2098                 CDEBUG(D_NET, "Failed to post push target: %d\n", rc);
2099                 lnet_ping_buffer_decref(pbuf);
2100                 return rc;
2101         }
2102
2103         lnet_net_lock(LNET_LOCK_EX);
2104         old_pbuf = the_lnet.ln_push_target;
2105         old_mdh = the_lnet.ln_push_target_md;
2106         the_lnet.ln_push_target = pbuf;
2107         the_lnet.ln_push_target_md = mdh;
2108         lnet_net_unlock(LNET_LOCK_EX);
2109
2110         if (old_pbuf) {
2111                 LNetMDUnlink(old_mdh);
2112                 /* Drop ref set by lnet_ping_buffer_alloc() */
2113                 lnet_ping_buffer_decref(old_pbuf);
2114         }
2115
2116         /* Received another push or reply that requires a larger buffer */
2117         if (nnis < the_lnet.ln_push_target_nnis)
2118                 goto again;
2119
2120         CDEBUG(D_NET, "nnis %d success\n", nnis);
2121         return 0;
2122 }
2123
2124 int lnet_push_target_post(struct lnet_ping_buffer *pbuf,
2125                           struct lnet_handle_md *mdhp)
2126 {
2127         struct lnet_processid id = { LNET_ANY_NID, LNET_PID_ANY };
2128         struct lnet_md md = { NULL };
2129         struct lnet_me *me;
2130         int rc;
2131
2132         me = LNetMEAttach(LNET_RESERVED_PORTAL, &id,
2133                           LNET_PROTO_PING_MATCHBITS, 0,
2134                           LNET_UNLINK, LNET_INS_AFTER);
2135         if (IS_ERR(me)) {
2136                 rc = PTR_ERR(me);
2137                 CERROR("Can't create push target ME: %d\n", rc);
2138                 return rc;
2139         }
2140
2141         pbuf->pb_needs_post = false;
2142
2143         /* This reference is dropped by lnet_push_target_event_handler() */
2144         lnet_ping_buffer_addref(pbuf);
2145
2146         /* initialize md content */
2147         md.start     = &pbuf->pb_info;
2148         md.length    = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2149         md.threshold = 1;
2150         md.max_size  = 0;
2151         md.options   = LNET_MD_OP_PUT | LNET_MD_TRUNCATE;
2152         md.user_ptr  = pbuf;
2153         md.handler   = the_lnet.ln_push_target_handler;
2154
2155         rc = LNetMDAttach(me, &md, LNET_UNLINK, mdhp);
2156         if (rc) {
2157                 CERROR("Can't attach push MD: %d\n", rc);
2158                 lnet_ping_buffer_decref(pbuf);
2159                 pbuf->pb_needs_post = true;
2160                 return rc;
2161         }
2162
2163         CDEBUG(D_NET, "posted push target %p\n", pbuf);
2164
2165         return 0;
2166 }
2167
2168 static void lnet_push_target_event_handler(struct lnet_event *ev)
2169 {
2170         struct lnet_ping_buffer *pbuf = ev->md_user_ptr;
2171
2172         CDEBUG(D_NET, "type %d status %d unlinked %d\n", ev->type, ev->status,
2173                ev->unlinked);
2174
2175         if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2176                 lnet_swap_pinginfo(pbuf);
2177
2178         if (ev->type == LNET_EVENT_UNLINK) {
2179                 /* Drop ref added by lnet_push_target_post() */
2180                 lnet_ping_buffer_decref(pbuf);
2181                 return;
2182         }
2183
2184         lnet_peer_push_event(ev);
2185         if (ev->unlinked)
2186                 /* Drop ref added by lnet_push_target_post */
2187                 lnet_ping_buffer_decref(pbuf);
2188 }
2189
2190 /* Initialize the push target. */
2191 static int lnet_push_target_init(void)
2192 {
2193         int rc;
2194
2195         if (the_lnet.ln_push_target)
2196                 return -EALREADY;
2197
2198         the_lnet.ln_push_target_handler =
2199                 lnet_push_target_event_handler;
2200
2201         rc = LNetSetLazyPortal(LNET_RESERVED_PORTAL);
2202         LASSERT(rc == 0);
2203
2204         /* Start at the required minimum, we'll enlarge if required. */
2205         the_lnet.ln_push_target_nnis = LNET_INTERFACES_MIN;
2206
2207         rc = lnet_push_target_resize();
2208
2209         if (rc) {
2210                 LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2211                 the_lnet.ln_push_target_handler = NULL;
2212         }
2213
2214         return rc;
2215 }
2216
2217 /* Clean up the push target. */
2218 static void lnet_push_target_fini(void)
2219 {
2220         if (!the_lnet.ln_push_target)
2221                 return;
2222
2223         /* Unlink and invalidate to prevent new references. */
2224         LNetMDUnlink(the_lnet.ln_push_target_md);
2225         LNetInvalidateMDHandle(&the_lnet.ln_push_target_md);
2226
2227         /* Wait for the unlink to complete. */
2228         wait_var_event_warning(&the_lnet.ln_push_target->pb_refcnt,
2229                                atomic_read(&the_lnet.ln_push_target->pb_refcnt) <= 1,
2230                                "Still waiting for ping data MD to unlink\n");
2231
2232         /* Drop ref set by lnet_ping_buffer_alloc() */
2233         lnet_ping_buffer_decref(the_lnet.ln_push_target);
2234         the_lnet.ln_push_target = NULL;
2235         the_lnet.ln_push_target_nnis = 0;
2236
2237         LNetClearLazyPortal(LNET_RESERVED_PORTAL);
2238         lnet_assert_handler_unused(the_lnet.ln_push_target_handler);
2239         the_lnet.ln_push_target_handler = NULL;
2240 }
2241
2242 static int
2243 lnet_ni_tq_credits(struct lnet_ni *ni)
2244 {
2245         int     credits;
2246
2247         LASSERT(ni->ni_ncpts >= 1);
2248
2249         if (ni->ni_ncpts == 1)
2250                 return ni->ni_net->net_tunables.lct_max_tx_credits;
2251
2252         credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
2253         credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
2254         credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
2255
2256         return credits;
2257 }
2258
2259 static void
2260 lnet_ni_unlink_locked(struct lnet_ni *ni)
2261 {
2262         /* move it to zombie list and nobody can find it anymore */
2263         LASSERT(!list_empty(&ni->ni_netlist));
2264         list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
2265         lnet_ni_decref_locked(ni, 0);
2266 }
2267
2268 static void
2269 lnet_clear_zombies_nis_locked(struct lnet_net *net)
2270 {
2271         int             i;
2272         int             islo;
2273         struct lnet_ni  *ni;
2274         struct list_head *zombie_list = &net->net_ni_zombie;
2275
2276         /*
2277          * Now wait for the NIs I just nuked to show up on the zombie
2278          * list and shut them down in guaranteed thread context
2279          */
2280         i = 2;
2281         while (!list_empty(zombie_list)) {
2282                 int     *ref;
2283                 int     j;
2284
2285                 ni = list_entry(zombie_list->next,
2286                                 struct lnet_ni, ni_netlist);
2287                 list_del_init(&ni->ni_netlist);
2288                 /* the ni should be in deleting state. If it's not it's
2289                  * a bug */
2290                 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
2291                 cfs_percpt_for_each(ref, j, ni->ni_refs) {
2292                         if (*ref == 0)
2293                                 continue;
2294                         /* still busy, add it back to zombie list */
2295                         list_add(&ni->ni_netlist, zombie_list);
2296                         break;
2297                 }
2298
2299                 if (!list_empty(&ni->ni_netlist)) {
2300                         /* Unlock mutex while waiting to allow other
2301                          * threads to read the LNet state and fall through
2302                          * to avoid deadlock
2303                          */
2304                         lnet_net_unlock(LNET_LOCK_EX);
2305                         mutex_unlock(&the_lnet.ln_api_mutex);
2306
2307                         ++i;
2308                         if ((i & (-i)) == i) {
2309                                 CDEBUG(D_WARNING,
2310                                        "Waiting for zombie LNI %s\n",
2311                                        libcfs_nidstr(&ni->ni_nid));
2312                         }
2313                         schedule_timeout_uninterruptible(cfs_time_seconds(1));
2314
2315                         mutex_lock(&the_lnet.ln_api_mutex);
2316                         lnet_net_lock(LNET_LOCK_EX);
2317                         continue;
2318                 }
2319
2320                 lnet_net_unlock(LNET_LOCK_EX);
2321
2322                 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
2323
2324                 LASSERT(!in_interrupt());
2325                 /* Holding the mutex makes it safe for lnd_shutdown
2326                  * to call module_put(). Module unload cannot finish
2327                  * until lnet_unregister_lnd() completes, and that
2328                  * requires the mutex.
2329                  */
2330                 mutex_lock(&the_lnet.ln_lnd_mutex);
2331                 (net->net_lnd->lnd_shutdown)(ni);
2332                 mutex_unlock(&the_lnet.ln_lnd_mutex);
2333
2334                 if (!islo)
2335                         CDEBUG(D_LNI, "Removed LNI %s\n",
2336                               libcfs_nidstr(&ni->ni_nid));
2337
2338                 lnet_ni_free(ni);
2339                 i = 2;
2340                 lnet_net_lock(LNET_LOCK_EX);
2341         }
2342 }
2343
2344 /* shutdown down the NI and release refcount */
2345 static void
2346 lnet_shutdown_lndni(struct lnet_ni *ni)
2347 {
2348         int i;
2349         struct lnet_net *net = ni->ni_net;
2350
2351         lnet_net_lock(LNET_LOCK_EX);
2352         lnet_ni_lock(ni);
2353         ni->ni_state = LNET_NI_STATE_DELETING;
2354         lnet_ni_unlock(ni);
2355         lnet_ni_unlink_locked(ni);
2356         lnet_incr_dlc_seq();
2357         lnet_net_unlock(LNET_LOCK_EX);
2358
2359         /* clear messages for this NI on the lazy portal */
2360         for (i = 0; i < the_lnet.ln_nportals; i++)
2361                 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
2362
2363         lnet_net_lock(LNET_LOCK_EX);
2364         lnet_clear_zombies_nis_locked(net);
2365         lnet_net_unlock(LNET_LOCK_EX);
2366 }
2367
2368 static void
2369 lnet_shutdown_lndnet(struct lnet_net *net)
2370 {
2371         struct lnet_ni *ni;
2372
2373         lnet_net_lock(LNET_LOCK_EX);
2374
2375         list_del_init(&net->net_list);
2376
2377         while (!list_empty(&net->net_ni_list)) {
2378                 ni = list_entry(net->net_ni_list.next,
2379                                 struct lnet_ni, ni_netlist);
2380                 lnet_net_unlock(LNET_LOCK_EX);
2381                 lnet_shutdown_lndni(ni);
2382                 lnet_net_lock(LNET_LOCK_EX);
2383         }
2384
2385         lnet_net_unlock(LNET_LOCK_EX);
2386
2387         /* Do peer table cleanup for this net */
2388         lnet_peer_tables_cleanup(net);
2389
2390         lnet_net_free(net);
2391 }
2392
2393 static void
2394 lnet_shutdown_lndnets(void)
2395 {
2396         struct lnet_net *net;
2397         LIST_HEAD(resend);
2398         struct lnet_msg *msg, *tmp;
2399
2400         /* NB called holding the global mutex */
2401
2402         /* All quiet on the API front */
2403         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
2404         LASSERT(the_lnet.ln_refcount == 0);
2405
2406         lnet_net_lock(LNET_LOCK_EX);
2407         the_lnet.ln_state = LNET_STATE_STOPPING;
2408
2409         /*
2410          * move the nets to the zombie list to avoid them being
2411          * picked up for new work. LONET is also included in the
2412          * Nets that will be moved to the zombie list
2413          */
2414         list_splice_init(&the_lnet.ln_nets, &the_lnet.ln_net_zombie);
2415
2416         /* Drop the cached loopback Net. */
2417         if (the_lnet.ln_loni != NULL) {
2418                 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
2419                 the_lnet.ln_loni = NULL;
2420         }
2421         lnet_net_unlock(LNET_LOCK_EX);
2422
2423         /* iterate through the net zombie list and delete each net */
2424         while (!list_empty(&the_lnet.ln_net_zombie)) {
2425                 net = list_entry(the_lnet.ln_net_zombie.next,
2426                                  struct lnet_net, net_list);
2427                 lnet_shutdown_lndnet(net);
2428         }
2429
2430         spin_lock(&the_lnet.ln_msg_resend_lock);
2431         list_splice(&the_lnet.ln_msg_resend, &resend);
2432         spin_unlock(&the_lnet.ln_msg_resend_lock);
2433
2434         list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
2435                 list_del_init(&msg->msg_list);
2436                 msg->msg_no_resend = true;
2437                 lnet_finalize(msg, -ECANCELED);
2438         }
2439
2440         lnet_net_lock(LNET_LOCK_EX);
2441         the_lnet.ln_state = LNET_STATE_SHUTDOWN;
2442         lnet_net_unlock(LNET_LOCK_EX);
2443 }
2444
2445 static int
2446 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
2447 {
2448         int                     rc = -EINVAL;
2449         struct lnet_tx_queue    *tq;
2450         int                     i;
2451         struct lnet_net         *net = ni->ni_net;
2452
2453         mutex_lock(&the_lnet.ln_lnd_mutex);
2454
2455         if (tun) {
2456                 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
2457                 ni->ni_lnd_tunables_set = true;
2458         }
2459
2460         rc = (net->net_lnd->lnd_startup)(ni);
2461
2462         mutex_unlock(&the_lnet.ln_lnd_mutex);
2463
2464         if (rc != 0) {
2465                 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
2466                                    rc, libcfs_lnd2str(net->net_lnd->lnd_type));
2467                 goto failed0;
2468         }
2469
2470         lnet_ni_lock(ni);
2471         ni->ni_state = LNET_NI_STATE_ACTIVE;
2472         lnet_ni_unlock(ni);
2473
2474         /* We keep a reference on the loopback net through the loopback NI */
2475         if (net->net_lnd->lnd_type == LOLND) {
2476                 lnet_ni_addref(ni);
2477                 LASSERT(the_lnet.ln_loni == NULL);
2478                 the_lnet.ln_loni = ni;
2479                 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
2480                 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
2481                 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
2482                 ni->ni_net->net_tunables.lct_peer_timeout = 0;
2483                 return 0;
2484         }
2485
2486         if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
2487             ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
2488                 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
2489                                    libcfs_lnd2str(net->net_lnd->lnd_type),
2490                                    ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
2491                                         "" : "per-peer ");
2492                 /* shutdown the NI since if we get here then it must've already
2493                  * been started
2494                  */
2495                 lnet_shutdown_lndni(ni);
2496                 return -EINVAL;
2497         }
2498
2499         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
2500                 tq->tq_credits_min =
2501                 tq->tq_credits_max =
2502                 tq->tq_credits = lnet_ni_tq_credits(ni);
2503         }
2504
2505         atomic_set(&ni->ni_tx_credits,
2506                    lnet_ni_tq_credits(ni) * ni->ni_ncpts);
2507         atomic_set(&ni->ni_healthv, LNET_MAX_HEALTH_VALUE);
2508
2509         CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
2510                 libcfs_nidstr(&ni->ni_nid),
2511                 ni->ni_net->net_tunables.lct_peer_tx_credits,
2512                 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
2513                 ni->ni_net->net_tunables.lct_peer_rtr_credits,
2514                 ni->ni_net->net_tunables.lct_peer_timeout);
2515
2516         return 0;
2517 failed0:
2518         lnet_ni_free(ni);
2519         return rc;
2520 }
2521
2522 static int
2523 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
2524 {
2525         struct lnet_ni *ni;
2526         struct lnet_net *net_l = NULL;
2527         LIST_HEAD(local_ni_list);
2528         int rc;
2529         int ni_count = 0;
2530         __u32 lnd_type;
2531         const struct lnet_lnd  *lnd;
2532         int peer_timeout =
2533                 net->net_tunables.lct_peer_timeout;
2534         int maxtxcredits =
2535                 net->net_tunables.lct_max_tx_credits;
2536         int peerrtrcredits =
2537                 net->net_tunables.lct_peer_rtr_credits;
2538
2539         /*
2540          * make sure that this net is unique. If it isn't then
2541          * we are adding interfaces to an already existing network, and
2542          * 'net' is just a convenient way to pass in the list.
2543          * if it is unique we need to find the LND and load it if
2544          * necessary.
2545          */
2546         if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
2547                 lnd_type = LNET_NETTYP(net->net_id);
2548
2549                 mutex_lock(&the_lnet.ln_lnd_mutex);
2550                 lnd = lnet_find_lnd_by_type(lnd_type);
2551
2552                 if (lnd == NULL) {
2553                         mutex_unlock(&the_lnet.ln_lnd_mutex);
2554                         rc = request_module("%s", libcfs_lnd2modname(lnd_type));
2555                         mutex_lock(&the_lnet.ln_lnd_mutex);
2556
2557                         lnd = lnet_find_lnd_by_type(lnd_type);
2558                         if (lnd == NULL) {
2559                                 mutex_unlock(&the_lnet.ln_lnd_mutex);
2560                                 CERROR("Can't load LND %s, module %s, rc=%d\n",
2561                                 libcfs_lnd2str(lnd_type),
2562                                 libcfs_lnd2modname(lnd_type), rc);
2563 #ifndef HAVE_MODULE_LOADING_SUPPORT
2564                                 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
2565                                                 "compiled with kernel module "
2566                                                 "loading support.");
2567 #endif
2568                                 rc = -EINVAL;
2569                                 goto failed0;
2570                         }
2571                 }
2572
2573                 net->net_lnd = lnd;
2574
2575                 mutex_unlock(&the_lnet.ln_lnd_mutex);
2576
2577                 net_l = net;
2578         }
2579
2580         /*
2581          * net_l: if the network being added is unique then net_l
2582          *        will point to that network
2583          *        if the network being added is not unique then
2584          *        net_l points to the existing network.
2585          *
2586          * When we enter the loop below, we'll pick NIs off he
2587          * network beign added and start them up, then add them to
2588          * a local ni list. Once we've successfully started all
2589          * the NIs then we join the local NI list (of started up
2590          * networks) with the net_l->net_ni_list, which should
2591          * point to the correct network to add the new ni list to
2592          *
2593          * If any of the new NIs fail to start up, then we want to
2594          * iterate through the local ni list, which should include
2595          * any NIs which were successfully started up, and shut
2596          * them down.
2597          *
2598          * After than we want to delete the network being added,
2599          * to avoid a memory leak.
2600          */
2601         while (!list_empty(&net->net_ni_added)) {
2602                 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
2603                                 ni_netlist);
2604                 list_del_init(&ni->ni_netlist);
2605
2606                 /* make sure that the the NI we're about to start
2607                  * up is actually unique. if it's not fail. */
2608                 if (!lnet_ni_unique_net(&net_l->net_ni_list,
2609                                         ni->ni_interface)) {
2610                         rc = -EEXIST;
2611                         goto failed1;
2612                 }
2613
2614                 /* adjust the pointer the parent network, just in case it
2615                  * the net is a duplicate */
2616                 ni->ni_net = net_l;
2617
2618                 rc = lnet_startup_lndni(ni, tun);
2619
2620                 if (rc < 0)
2621                         goto failed1;
2622
2623                 lnet_ni_addref(ni);
2624                 list_add_tail(&ni->ni_netlist, &local_ni_list);
2625
2626                 ni_count++;
2627         }
2628
2629         lnet_net_lock(LNET_LOCK_EX);
2630         list_splice_tail(&local_ni_list, &net_l->net_ni_list);
2631         lnet_incr_dlc_seq();
2632         lnet_net_unlock(LNET_LOCK_EX);
2633
2634         /* if the network is not unique then we don't want to keep
2635          * it around after we're done. Free it. Otherwise add that
2636          * net to the global the_lnet.ln_nets */
2637         if (net_l != net && net_l != NULL) {
2638                 /*
2639                  * TODO - note. currently the tunables can not be updated
2640                  * once added
2641                  */
2642                 lnet_net_free(net);
2643         } else {
2644                 /*
2645                  * restore tunables after it has been overwitten by the
2646                  * lnd
2647                  */
2648                 if (peer_timeout != -1)
2649                         net->net_tunables.lct_peer_timeout = peer_timeout;
2650                 if (maxtxcredits != -1)
2651                         net->net_tunables.lct_max_tx_credits = maxtxcredits;
2652                 if (peerrtrcredits != -1)
2653                         net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
2654
2655                 lnet_net_lock(LNET_LOCK_EX);
2656                 list_add_tail(&net->net_list, &the_lnet.ln_nets);
2657                 lnet_net_unlock(LNET_LOCK_EX);
2658         }
2659
2660         return ni_count;
2661
2662 failed1:
2663         /*
2664          * shutdown the new NIs that are being started up
2665          * free the NET being started
2666          */
2667         while (!list_empty(&local_ni_list)) {
2668                 ni = list_entry(local_ni_list.next, struct lnet_ni,
2669                                 ni_netlist);
2670
2671                 lnet_shutdown_lndni(ni);
2672         }
2673
2674 failed0:
2675         lnet_net_free(net);
2676
2677         return rc;
2678 }
2679
2680 static int
2681 lnet_startup_lndnets(struct list_head *netlist)
2682 {
2683         struct lnet_net         *net;
2684         int                     rc;
2685         int                     ni_count = 0;
2686
2687         /*
2688          * Change to running state before bringing up the LNDs. This
2689          * allows lnet_shutdown_lndnets() to assert that we've passed
2690          * through here.
2691          */
2692         lnet_net_lock(LNET_LOCK_EX);
2693         the_lnet.ln_state = LNET_STATE_RUNNING;
2694         lnet_net_unlock(LNET_LOCK_EX);
2695
2696         while (!list_empty(netlist)) {
2697                 net = list_entry(netlist->next, struct lnet_net, net_list);
2698                 list_del_init(&net->net_list);
2699
2700                 rc = lnet_startup_lndnet(net, NULL);
2701
2702                 if (rc < 0)
2703                         goto failed;
2704
2705                 ni_count += rc;
2706         }
2707
2708         return ni_count;
2709 failed:
2710         lnet_shutdown_lndnets();
2711
2712         return rc;
2713 }
2714
2715 static int lnet_genl_parse_list(struct sk_buff *msg,
2716                                 const struct ln_key_list *data[], u16 idx)
2717 {
2718         const struct ln_key_list *list = data[idx];
2719         const struct ln_key_props *props;
2720         struct nlattr *node;
2721         u16 count;
2722
2723         if (!list)
2724                 return 0;
2725
2726         if (!list->lkl_maxattr)
2727                 return -ERANGE;
2728
2729         props = list->lkl_list;
2730         if (!props)
2731                 return -EINVAL;
2732
2733         node = nla_nest_start(msg, LN_SCALAR_ATTR_LIST);
2734         if (!node)
2735                 return -ENOBUFS;
2736
2737         for (count = 1; count <= list->lkl_maxattr; count++) {
2738                 struct nlattr *key = nla_nest_start(msg, count);
2739
2740                 if (count == 1)
2741                         nla_put_u16(msg, LN_SCALAR_ATTR_LIST_SIZE,
2742                                     list->lkl_maxattr);
2743
2744                 nla_put_u16(msg, LN_SCALAR_ATTR_INDEX, count);
2745                 if (props[count].lkp_value)
2746                         nla_put_string(msg, LN_SCALAR_ATTR_VALUE,
2747                                        props[count].lkp_value);
2748                 if (props[count].lkp_key_format)
2749                         nla_put_u16(msg, LN_SCALAR_ATTR_KEY_FORMAT,
2750                                     props[count].lkp_key_format);
2751                 nla_put_u16(msg, LN_SCALAR_ATTR_NLA_TYPE,
2752                             props[count].lkp_data_type);
2753                 if (props[count].lkp_data_type == NLA_NESTED) {
2754                         int rc;
2755
2756                         rc = lnet_genl_parse_list(msg, data, ++idx);
2757                         if (rc < 0)
2758                                 return rc;
2759                         idx = rc;
2760                 }
2761
2762                 nla_nest_end(msg, key);
2763         }
2764
2765         nla_nest_end(msg, node);
2766         return idx;
2767 }
2768
2769 int lnet_genl_send_scalar_list(struct sk_buff *msg, u32 portid, u32 seq,
2770                                const struct genl_family *family, int flags,
2771                                u8 cmd, const struct ln_key_list *data[])
2772 {
2773         int rc = 0;
2774         void *hdr;
2775
2776         if (!data[0])
2777                 return -EINVAL;
2778
2779         hdr = genlmsg_put(msg, portid, seq, family, flags, cmd);
2780         if (!hdr)
2781                 GOTO(canceled, rc = -EMSGSIZE);
2782
2783         rc = lnet_genl_parse_list(msg, data, 0);
2784         if (rc < 0)
2785                 GOTO(canceled, rc);
2786
2787         genlmsg_end(msg, hdr);
2788 canceled:
2789         if (rc < 0)
2790                 genlmsg_cancel(msg, hdr);
2791         return rc > 0 ? 0 : rc;
2792 }
2793 EXPORT_SYMBOL(lnet_genl_send_scalar_list);
2794
2795 /**
2796  * Initialize LNet library.
2797  *
2798  * Automatically called at module loading time. Caller has to call
2799  * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
2800  * latter returned 0. It must be called exactly once.
2801  *
2802  * \retval 0 on success
2803  * \retval -ve on failures.
2804  */
2805 int lnet_lib_init(void)
2806 {
2807         int rc;
2808
2809         lnet_assert_wire_constants();
2810
2811         /* refer to global cfs_cpt_table for now */
2812         the_lnet.ln_cpt_table = cfs_cpt_tab;
2813         the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_tab);
2814
2815         LASSERT(the_lnet.ln_cpt_number > 0);
2816         if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
2817                 /* we are under risk of consuming all lh_cookie */
2818                 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
2819                        "please change setting of CPT-table and retry\n",
2820                        the_lnet.ln_cpt_number, LNET_CPT_MAX);
2821                 return -E2BIG;
2822         }
2823
2824         while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
2825                 the_lnet.ln_cpt_bits++;
2826
2827         rc = lnet_create_locks();
2828         if (rc != 0) {
2829                 CERROR("Can't create LNet global locks: %d\n", rc);
2830                 return rc;
2831         }
2832
2833         the_lnet.ln_refcount = 0;
2834         INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
2835         INIT_LIST_HEAD(&the_lnet.ln_msg_resend);
2836
2837         /* The hash table size is the number of bits it takes to express the set
2838          * ln_num_routes, minus 1 (better to under estimate than over so we
2839          * don't waste memory). */
2840         if (rnet_htable_size <= 0)
2841                 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
2842         else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
2843                 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
2844         the_lnet.ln_remote_nets_hbits = max_t(int, 1,
2845                                            order_base_2(rnet_htable_size) - 1);
2846
2847         /* All LNDs apart from the LOLND are in separate modules.  They
2848          * register themselves when their module loads, and unregister
2849          * themselves when their module is unloaded. */
2850         lnet_register_lnd(&the_lolnd);
2851         return 0;
2852 }
2853
2854 /**
2855  * Finalize LNet library.
2856  *
2857  * \pre lnet_lib_init() called with success.
2858  * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
2859  *
2860  * As this happens at module-unload, all lnds must already be unloaded,
2861  * so they must already be unregistered.
2862  */
2863 void lnet_lib_exit(void)
2864 {
2865         int i;
2866
2867         LASSERT(the_lnet.ln_refcount == 0);
2868         lnet_unregister_lnd(&the_lolnd);
2869         for (i = 0; i < NUM_LNDS; i++)
2870                 LASSERT(!the_lnet.ln_lnds[i]);
2871         lnet_destroy_locks();
2872 }
2873
2874 /**
2875  * Set LNet PID and start LNet interfaces, routing, and forwarding.
2876  *
2877  * Users must call this function at least once before any other functions.
2878  * For each successful call there must be a corresponding call to
2879  * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
2880  * ignored.
2881  *
2882  * The PID used by LNet may be different from the one requested.
2883  * See LNetGetId().
2884  *
2885  * \param requested_pid PID requested by the caller.
2886  *
2887  * \return >= 0 on success, and < 0 error code on failures.
2888  */
2889 int
2890 LNetNIInit(lnet_pid_t requested_pid)
2891 {
2892         int                     im_a_router = 0;
2893         int                     rc;
2894         int                     ni_count;
2895         struct lnet_ping_buffer *pbuf;
2896         struct lnet_handle_md   ping_mdh;
2897         LIST_HEAD(net_head);
2898         struct lnet_net         *net;
2899
2900         mutex_lock(&the_lnet.ln_api_mutex);
2901
2902         CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
2903
2904         if (the_lnet.ln_refcount > 0) {
2905                 rc = the_lnet.ln_refcount++;
2906                 mutex_unlock(&the_lnet.ln_api_mutex);
2907                 return rc;
2908         }
2909
2910         rc = lnet_prepare(requested_pid);
2911         if (rc != 0) {
2912                 mutex_unlock(&the_lnet.ln_api_mutex);
2913                 return rc;
2914         }
2915
2916         /* create a network for Loopback network */
2917         net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
2918         if (net == NULL) {
2919                 rc = -ENOMEM;
2920                 goto err_empty_list;
2921         }
2922
2923         /* Add in the loopback NI */
2924         if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
2925                 rc = -ENOMEM;
2926                 goto err_empty_list;
2927         }
2928
2929         if (use_tcp_bonding)
2930                 CWARN("use_tcp_bonding has been removed. Use Multi-Rail and Dynamic Discovery instead, see LU-13641\n");
2931
2932         /* If LNet is being initialized via DLC it is possible
2933          * that the user requests not to load module parameters (ones which
2934          * are supported by DLC) on initialization.  Therefore, make sure not
2935          * to load networks, routes and forwarding from module parameters
2936          * in this case.  On cleanup in case of failure only clean up
2937          * routes if it has been loaded */
2938         if (!the_lnet.ln_nis_from_mod_params) {
2939                 rc = lnet_parse_networks(&net_head, lnet_get_networks());
2940                 if (rc < 0)
2941                         goto err_empty_list;
2942         }
2943
2944         ni_count = lnet_startup_lndnets(&net_head);
2945         if (ni_count < 0) {
2946                 rc = ni_count;
2947                 goto err_empty_list;
2948         }
2949
2950         if (!the_lnet.ln_nis_from_mod_params) {
2951                 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
2952                 if (rc != 0)
2953                         goto err_shutdown_lndnis;
2954
2955                 rc = lnet_rtrpools_alloc(im_a_router);
2956                 if (rc != 0)
2957                         goto err_destroy_routes;
2958         }
2959
2960         rc = lnet_acceptor_start();
2961         if (rc != 0)
2962                 goto err_destroy_routes;
2963
2964         the_lnet.ln_refcount = 1;
2965         /* Now I may use my own API functions... */
2966
2967         rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2968         if (rc != 0)
2969                 goto err_acceptor_stop;
2970
2971         lnet_ping_target_update(pbuf, ping_mdh);
2972
2973         the_lnet.ln_mt_handler = lnet_mt_event_handler;
2974
2975         rc = lnet_push_target_init();
2976         if (rc != 0)
2977                 goto err_stop_ping;
2978
2979         rc = lnet_peer_discovery_start();
2980         if (rc != 0)
2981                 goto err_destroy_push_target;
2982
2983         rc = lnet_monitor_thr_start();
2984         if (rc != 0)
2985                 goto err_stop_discovery_thr;
2986
2987         lnet_fault_init();
2988         lnet_router_debugfs_init();
2989
2990         mutex_unlock(&the_lnet.ln_api_mutex);
2991
2992         complete_all(&the_lnet.ln_started);
2993
2994         /* wait for all routers to start */
2995         lnet_wait_router_start();
2996
2997         return 0;
2998
2999 err_stop_discovery_thr:
3000         lnet_peer_discovery_stop();
3001 err_destroy_push_target:
3002         lnet_push_target_fini();
3003 err_stop_ping:
3004         lnet_ping_target_fini();
3005 err_acceptor_stop:
3006         the_lnet.ln_refcount = 0;
3007         lnet_acceptor_stop();
3008 err_destroy_routes:
3009         if (!the_lnet.ln_nis_from_mod_params)
3010                 lnet_destroy_routes();
3011 err_shutdown_lndnis:
3012         lnet_shutdown_lndnets();
3013 err_empty_list:
3014         lnet_unprepare();
3015         LASSERT(rc < 0);
3016         mutex_unlock(&the_lnet.ln_api_mutex);
3017         while (!list_empty(&net_head)) {
3018                 struct lnet_net *net;
3019
3020                 net = list_entry(net_head.next, struct lnet_net, net_list);
3021                 list_del_init(&net->net_list);
3022                 lnet_net_free(net);
3023         }
3024         return rc;
3025 }
3026 EXPORT_SYMBOL(LNetNIInit);
3027
3028 /**
3029  * Stop LNet interfaces, routing, and forwarding.
3030  *
3031  * Users must call this function once for each successful call to LNetNIInit().
3032  * Once the LNetNIFini() operation has been started, the results of pending
3033  * API operations are undefined.
3034  *
3035  * \return always 0 for current implementation.
3036  */
3037 int
3038 LNetNIFini(void)
3039 {
3040         mutex_lock(&the_lnet.ln_api_mutex);
3041
3042         LASSERT(the_lnet.ln_refcount > 0);
3043
3044         if (the_lnet.ln_refcount != 1) {
3045                 the_lnet.ln_refcount--;
3046         } else {
3047                 LASSERT(!the_lnet.ln_niinit_self);
3048
3049                 lnet_fault_fini();
3050
3051                 lnet_router_debugfs_fini();
3052                 lnet_monitor_thr_stop();
3053                 lnet_peer_discovery_stop();
3054                 lnet_push_target_fini();
3055                 lnet_ping_target_fini();
3056
3057                 /* Teardown fns that use my own API functions BEFORE here */
3058                 the_lnet.ln_refcount = 0;
3059
3060                 lnet_acceptor_stop();
3061                 lnet_destroy_routes();
3062                 lnet_shutdown_lndnets();
3063                 lnet_unprepare();
3064         }
3065
3066         mutex_unlock(&the_lnet.ln_api_mutex);
3067         return 0;
3068 }
3069 EXPORT_SYMBOL(LNetNIFini);
3070
3071 /**
3072  * Grabs the ni data from the ni structure and fills the out
3073  * parameters
3074  *
3075  * \param[in] ni network        interface structure
3076  * \param[out] cfg_ni           NI config information
3077  * \param[out] tun              network and LND tunables
3078  */
3079 static void
3080 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
3081                    struct lnet_ioctl_config_lnd_tunables *tun,
3082                    struct lnet_ioctl_element_stats *stats,
3083                    __u32 tun_size)
3084 {
3085         size_t min_size = 0;
3086         int i;
3087
3088         if (!ni || !cfg_ni || !tun || !nid_is_nid4(&ni->ni_nid))
3089                 return;
3090
3091         if (ni->ni_interface != NULL) {
3092                 strncpy(cfg_ni->lic_ni_intf,
3093                         ni->ni_interface,
3094                         sizeof(cfg_ni->lic_ni_intf));
3095         }
3096
3097         cfg_ni->lic_nid = lnet_nid_to_nid4(&ni->ni_nid);
3098         cfg_ni->lic_status = lnet_ni_get_status_locked(ni);
3099         cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
3100
3101         memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
3102
3103         if (stats) {
3104                 stats->iel_send_count = lnet_sum_stats(&ni->ni_stats,
3105                                                        LNET_STATS_TYPE_SEND);
3106                 stats->iel_recv_count = lnet_sum_stats(&ni->ni_stats,
3107                                                        LNET_STATS_TYPE_RECV);
3108                 stats->iel_drop_count = lnet_sum_stats(&ni->ni_stats,
3109                                                        LNET_STATS_TYPE_DROP);
3110         }
3111
3112         /*
3113          * tun->lt_tun will always be present, but in order to be
3114          * backwards compatible, we need to deal with the cases when
3115          * tun->lt_tun is smaller than what the kernel has, because it
3116          * comes from an older version of a userspace program, then we'll
3117          * need to copy as much information as we have available space.
3118          */
3119         min_size = tun_size - sizeof(tun->lt_cmn);
3120         memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
3121
3122         /* copy over the cpts */
3123         if (ni->ni_ncpts == LNET_CPT_NUMBER &&
3124             ni->ni_cpts == NULL)  {
3125                 for (i = 0; i < ni->ni_ncpts; i++)
3126                         cfg_ni->lic_cpts[i] = i;
3127         } else {
3128                 for (i = 0;
3129                      ni->ni_cpts != NULL && i < ni->ni_ncpts &&
3130                      i < LNET_MAX_SHOW_NUM_CPT;
3131                      i++)
3132                         cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
3133         }
3134         cfg_ni->lic_ncpts = ni->ni_ncpts;
3135 }
3136
3137 /**
3138  * NOTE: This is a legacy function left in the code to be backwards
3139  * compatible with older userspace programs. It should eventually be
3140  * removed.
3141  *
3142  * Grabs the ni data from the ni structure and fills the out
3143  * parameters
3144  *
3145  * \param[in] ni network        interface structure
3146  * \param[out] config           config information
3147  */
3148 static void
3149 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
3150                          struct lnet_ioctl_config_data *config)
3151 {
3152         struct lnet_ioctl_net_config *net_config;
3153         struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
3154         size_t min_size, tunable_size = 0;
3155         int i;
3156
3157         if (!ni || !config || !nid_is_nid4(&ni->ni_nid))
3158                 return;
3159
3160         net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
3161         if (!net_config)
3162                 return;
3163
3164         if (!ni->ni_interface)
3165                 return;
3166
3167         strncpy(net_config->ni_interface,
3168                 ni->ni_interface,
3169                 sizeof(net_config->ni_interface));
3170
3171         config->cfg_nid = lnet_nid_to_nid4(&ni->ni_nid);
3172         config->cfg_config_u.cfg_net.net_peer_timeout =
3173                 ni->ni_net->net_tunables.lct_peer_timeout;
3174         config->cfg_config_u.cfg_net.net_max_tx_credits =
3175                 ni->ni_net->net_tunables.lct_max_tx_credits;
3176         config->cfg_config_u.cfg_net.net_peer_tx_credits =
3177                 ni->ni_net->net_tunables.lct_peer_tx_credits;
3178         config->cfg_config_u.cfg_net.net_peer_rtr_credits =
3179                 ni->ni_net->net_tunables.lct_peer_rtr_credits;
3180
3181         net_config->ni_status = lnet_ni_get_status_locked(ni);
3182
3183         if (ni->ni_cpts) {
3184                 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
3185
3186                 for (i = 0; i < num_cpts; i++)
3187                         net_config->ni_cpts[i] = ni->ni_cpts[i];
3188
3189                 config->cfg_ncpts = num_cpts;
3190         }
3191
3192         /*
3193          * See if user land tools sent in a newer and larger version
3194          * of struct lnet_tunables than what the kernel uses.
3195          */
3196         min_size = sizeof(*config) + sizeof(*net_config);
3197
3198         if (config->cfg_hdr.ioc_len > min_size)
3199                 tunable_size = config->cfg_hdr.ioc_len - min_size;
3200
3201         /* Don't copy too much data to user space */
3202         min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
3203         lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
3204
3205         if (lnd_cfg && min_size) {
3206                 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
3207                 config->cfg_config_u.cfg_net.net_interface_count = 1;
3208
3209                 /* Tell user land that kernel side has less data */
3210                 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
3211                         min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
3212                         config->cfg_hdr.ioc_len -= min_size;
3213                 }
3214         }
3215 }
3216
3217 struct lnet_ni *
3218 lnet_get_ni_idx_locked(int idx)
3219 {
3220         struct lnet_ni          *ni;
3221         struct lnet_net         *net;
3222
3223         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3224                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3225                         if (idx-- == 0)
3226                                 return ni;
3227                 }
3228         }
3229
3230         return NULL;
3231 }
3232
3233 int lnet_get_net_healthv_locked(struct lnet_net *net)
3234 {
3235         struct lnet_ni *ni;
3236         int best_healthv = 0;
3237         int healthv, ni_fatal;
3238
3239         list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3240                 healthv = atomic_read(&ni->ni_healthv);
3241                 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
3242                 if (!ni_fatal && healthv > best_healthv)
3243                         best_healthv = healthv;
3244         }
3245
3246         return best_healthv;
3247 }
3248
3249 struct lnet_ni *
3250 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
3251 {
3252         struct lnet_ni          *ni;
3253         struct lnet_net         *net = mynet;
3254
3255         /*
3256          * It is possible that the net has been cleaned out while there is
3257          * a message being sent. This function accessed the net without
3258          * checking if the list is empty
3259          */
3260         if (prev == NULL) {
3261                 if (net == NULL)
3262                         net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
3263                                         net_list);
3264                 if (list_empty(&net->net_ni_list))
3265                         return NULL;
3266                 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3267                                 ni_netlist);
3268
3269                 return ni;
3270         }
3271
3272         if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
3273                 /* if you reached the end of the ni list and the net is
3274                  * specified, then there are no more nis in that net */
3275                 if (net != NULL)
3276                         return NULL;
3277
3278                 /* we reached the end of this net ni list. move to the
3279                  * next net */
3280                 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
3281                         /* no more nets and no more NIs. */
3282                         return NULL;
3283
3284                 /* get the next net */
3285                 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
3286                                  net_list);
3287                 if (list_empty(&net->net_ni_list))
3288                         return NULL;
3289                 /* get the ni on it */
3290                 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
3291                                 ni_netlist);
3292
3293                 return ni;
3294         }
3295
3296         if (list_empty(&prev->ni_netlist))
3297                 return NULL;
3298
3299         /* there are more nis left */
3300         ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
3301
3302         return ni;
3303 }
3304
3305 int
3306 lnet_get_net_config(struct lnet_ioctl_config_data *config)
3307 {
3308         struct lnet_ni *ni;
3309         int cpt;
3310         int rc = -ENOENT;
3311         int idx = config->cfg_count;
3312
3313         cpt = lnet_net_lock_current();
3314
3315         ni = lnet_get_ni_idx_locked(idx);
3316
3317         if (ni != NULL) {
3318                 rc = 0;
3319                 lnet_ni_lock(ni);
3320                 lnet_fill_ni_info_legacy(ni, config);
3321                 lnet_ni_unlock(ni);
3322         }
3323
3324         lnet_net_unlock(cpt);
3325         return rc;
3326 }
3327
3328 int
3329 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
3330                    struct lnet_ioctl_config_lnd_tunables *tun,
3331                    struct lnet_ioctl_element_stats *stats,
3332                    __u32 tun_size)
3333 {
3334         struct lnet_ni          *ni;
3335         int                     cpt;
3336         int                     rc = -ENOENT;
3337
3338         if (!cfg_ni || !tun || !stats)
3339                 return -EINVAL;
3340
3341         cpt = lnet_net_lock_current();
3342
3343         ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
3344
3345         if (ni) {
3346                 rc = 0;
3347                 lnet_ni_lock(ni);
3348                 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
3349                 lnet_ni_unlock(ni);
3350         }
3351
3352         lnet_net_unlock(cpt);
3353         return rc;
3354 }
3355
3356 int lnet_get_ni_stats(struct lnet_ioctl_element_msg_stats *msg_stats)
3357 {
3358         struct lnet_ni *ni;
3359         int cpt;
3360         int rc = -ENOENT;
3361
3362         if (!msg_stats)
3363                 return -EINVAL;
3364
3365         cpt = lnet_net_lock_current();
3366
3367         ni = lnet_get_ni_idx_locked(msg_stats->im_idx);
3368
3369         if (ni) {
3370                 lnet_usr_translate_stats(msg_stats, &ni->ni_stats);
3371                 rc = 0;
3372         }
3373
3374         lnet_net_unlock(cpt);
3375
3376         return rc;
3377 }
3378
3379 static int lnet_add_net_common(struct lnet_net *net,
3380                                struct lnet_ioctl_config_lnd_tunables *tun)
3381 {
3382         struct lnet_handle_md ping_mdh;
3383         struct lnet_ping_buffer *pbuf;
3384         struct lnet_remotenet *rnet;
3385         struct lnet_ni *ni;
3386         int net_ni_count;
3387         __u32 net_id;
3388         int rc;
3389
3390         lnet_net_lock(LNET_LOCK_EX);
3391         rnet = lnet_find_rnet_locked(net->net_id);
3392         lnet_net_unlock(LNET_LOCK_EX);
3393         /*
3394          * make sure that the net added doesn't invalidate the current
3395          * configuration LNet is keeping
3396          */
3397         if (rnet) {
3398                 CERROR("Adding net %s will invalidate routing configuration\n",
3399                        libcfs_net2str(net->net_id));
3400                 lnet_net_free(net);
3401                 return -EUSERS;
3402         }
3403
3404         /*
3405          * make sure you calculate the correct number of slots in the ping
3406          * buffer. Since the ping info is a flattened list of all the NIs,
3407          * we should allocate enough slots to accomodate the number of NIs
3408          * which will be added.
3409          *
3410          * since ni hasn't been configured yet, use
3411          * lnet_get_net_ni_count_pre() which checks the net_ni_added list
3412          */
3413         net_ni_count = lnet_get_net_ni_count_pre(net);
3414
3415         rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3416                                     net_ni_count + lnet_get_ni_count(),
3417                                     false);
3418         if (rc < 0) {
3419                 lnet_net_free(net);
3420                 return rc;
3421         }
3422
3423         if (tun)
3424                 memcpy(&net->net_tunables,
3425                        &tun->lt_cmn, sizeof(net->net_tunables));
3426         else
3427                 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
3428
3429         net_id = net->net_id;
3430
3431         rc = lnet_startup_lndnet(net,
3432                                  (tun) ? &tun->lt_tun : NULL);
3433         if (rc < 0)
3434                 goto failed;
3435
3436         lnet_net_lock(LNET_LOCK_EX);
3437         net = lnet_get_net_locked(net_id);
3438         LASSERT(net);
3439
3440         /* apply the UDSPs */
3441         rc = lnet_udsp_apply_policies_on_net(net);
3442         if (rc)
3443                 CERROR("Failed to apply UDSPs on local net %s\n",
3444                        libcfs_net2str(net->net_id));
3445
3446         /* At this point we lost track of which NI was just added, so we
3447          * just re-apply the policies on all of the NIs on this net
3448          */
3449         list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3450                 rc = lnet_udsp_apply_policies_on_ni(ni);
3451                 if (rc)
3452                         CERROR("Failed to apply UDSPs on ni %s\n",
3453                                libcfs_nidstr(&ni->ni_nid));
3454         }
3455         lnet_net_unlock(LNET_LOCK_EX);
3456
3457         /*
3458          * Start the acceptor thread if this is the first network
3459          * being added that requires the thread.
3460          */
3461         if (net->net_lnd->lnd_accept) {
3462                 rc = lnet_acceptor_start();
3463                 if (rc < 0) {
3464                         /* shutdown the net that we just started */
3465                         CERROR("Failed to start up acceptor thread\n");
3466                         lnet_shutdown_lndnet(net);
3467                         goto failed;
3468                 }
3469         }
3470
3471         lnet_net_lock(LNET_LOCK_EX);
3472         lnet_peer_net_added(net);
3473         lnet_net_unlock(LNET_LOCK_EX);
3474
3475         lnet_ping_target_update(pbuf, ping_mdh);
3476
3477         return 0;
3478
3479 failed:
3480         lnet_ping_md_unlink(pbuf, &ping_mdh);
3481         lnet_ping_buffer_decref(pbuf);
3482         return rc;
3483 }
3484
3485 static void
3486 lnet_set_tune_defaults(struct lnet_ioctl_config_lnd_tunables *tun)
3487 {
3488         if (tun) {
3489                 if (!tun->lt_cmn.lct_peer_timeout)
3490                         tun->lt_cmn.lct_peer_timeout = DEFAULT_PEER_TIMEOUT;
3491                 if (!tun->lt_cmn.lct_peer_tx_credits)
3492                         tun->lt_cmn.lct_peer_tx_credits = DEFAULT_PEER_CREDITS;
3493                 if (!tun->lt_cmn.lct_max_tx_credits)
3494                         tun->lt_cmn.lct_max_tx_credits = DEFAULT_CREDITS;
3495         }
3496 }
3497
3498 static int lnet_handle_legacy_ip2nets(char *ip2nets,
3499                                       struct lnet_ioctl_config_lnd_tunables *tun)
3500 {
3501         struct lnet_net *net;
3502         const char *nets;
3503         int rc;
3504         LIST_HEAD(net_head);
3505
3506         rc = lnet_parse_ip2nets(&nets, ip2nets);
3507         if (rc < 0)
3508                 return rc;
3509
3510         rc = lnet_parse_networks(&net_head, nets);
3511         if (rc < 0)
3512                 return rc;
3513
3514         lnet_set_tune_defaults(tun);
3515
3516         mutex_lock(&the_lnet.ln_api_mutex);
3517         while (!list_empty(&net_head)) {
3518                 net = list_entry(net_head.next, struct lnet_net, net_list);
3519                 list_del_init(&net->net_list);
3520                 rc = lnet_add_net_common(net, tun);
3521                 if (rc < 0)
3522                         goto out;
3523         }
3524
3525 out:
3526         mutex_unlock(&the_lnet.ln_api_mutex);
3527
3528         while (!list_empty(&net_head)) {
3529                 net = list_entry(net_head.next, struct lnet_net, net_list);
3530                 list_del_init(&net->net_list);
3531                 lnet_net_free(net);
3532         }
3533         return rc;
3534 }
3535
3536 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
3537 {
3538         struct lnet_net *net;
3539         struct lnet_ni *ni;
3540         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
3541         int rc, i;
3542         __u32 net_id, lnd_type;
3543
3544         /* get the tunables if they are available */
3545         if (conf->lic_cfg_hdr.ioc_len >=
3546             sizeof(*conf) + sizeof(*tun))
3547                 tun = (struct lnet_ioctl_config_lnd_tunables *)
3548                         conf->lic_bulk;
3549
3550         /* handle legacy ip2nets from DLC */
3551         if (conf->lic_legacy_ip2nets[0] != '\0')
3552                 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
3553                                                   tun);
3554
3555         net_id = LNET_NIDNET(conf->lic_nid);
3556         lnd_type = LNET_NETTYP(net_id);
3557
3558         if (!libcfs_isknown_lnd(lnd_type)) {
3559                 CERROR("No valid net and lnd information provided\n");
3560                 return -EINVAL;
3561         }
3562
3563         net = lnet_net_alloc(net_id, NULL);
3564         if (!net)
3565                 return -ENOMEM;
3566
3567         for (i = 0; i < conf->lic_ncpts; i++) {
3568                 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
3569                         return -EINVAL;
3570         }
3571
3572         ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
3573                                        conf->lic_ni_intf);
3574         if (!ni)
3575                 return -ENOMEM;
3576
3577         lnet_set_tune_defaults(tun);
3578
3579         mutex_lock(&the_lnet.ln_api_mutex);
3580
3581         rc = lnet_add_net_common(net, tun);
3582
3583         mutex_unlock(&the_lnet.ln_api_mutex);
3584
3585         return rc;
3586 }
3587
3588 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
3589 {
3590         struct lnet_net  *net;
3591         struct lnet_ni *ni;
3592         __u32 net_id = LNET_NIDNET(conf->lic_nid);
3593         struct lnet_ping_buffer *pbuf;
3594         struct lnet_handle_md  ping_mdh;
3595         int               rc;
3596         int               net_count;
3597         __u32             addr;
3598
3599         /* don't allow userspace to shutdown the LOLND */
3600         if (LNET_NETTYP(net_id) == LOLND)
3601                 return -EINVAL;
3602
3603         mutex_lock(&the_lnet.ln_api_mutex);
3604
3605         lnet_net_lock(0);
3606
3607         net = lnet_get_net_locked(net_id);
3608         if (!net) {
3609                 CERROR("net %s not found\n",
3610                        libcfs_net2str(net_id));
3611                 rc = -ENOENT;
3612                 goto unlock_net;
3613         }
3614
3615         addr = LNET_NIDADDR(conf->lic_nid);
3616         if (addr == 0) {
3617                 /* remove the entire net */
3618                 net_count = lnet_get_net_ni_count_locked(net);
3619
3620                 lnet_net_unlock(0);
3621
3622                 /* create and link a new ping info, before removing the old one */
3623                 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3624                                         lnet_get_ni_count() - net_count,
3625                                         false);
3626                 if (rc != 0)
3627                         goto unlock_api_mutex;
3628
3629                 lnet_shutdown_lndnet(net);
3630
3631                 lnet_acceptor_stop();
3632
3633                 lnet_ping_target_update(pbuf, ping_mdh);
3634
3635                 goto unlock_api_mutex;
3636         }
3637
3638         ni = lnet_nid2ni_locked(conf->lic_nid, 0);
3639         if (!ni) {
3640                 CERROR("nid %s not found\n",
3641                        libcfs_nid2str(conf->lic_nid));
3642                 rc = -ENOENT;
3643                 goto unlock_net;
3644         }
3645
3646         net_count = lnet_get_net_ni_count_locked(net);
3647
3648         lnet_net_unlock(0);
3649
3650         /* create and link a new ping info, before removing the old one */
3651         rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3652                                   lnet_get_ni_count() - 1, false);
3653         if (rc != 0)
3654                 goto unlock_api_mutex;
3655
3656         lnet_shutdown_lndni(ni);
3657
3658         lnet_acceptor_stop();
3659
3660         lnet_ping_target_update(pbuf, ping_mdh);
3661
3662         /* check if the net is empty and remove it if it is */
3663         if (net_count == 1)
3664                 lnet_shutdown_lndnet(net);
3665
3666         goto unlock_api_mutex;
3667
3668 unlock_net:
3669         lnet_net_unlock(0);
3670 unlock_api_mutex:
3671         mutex_unlock(&the_lnet.ln_api_mutex);
3672
3673         return rc;
3674 }
3675
3676 /*
3677  * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
3678  * They are only expected to be called for unique networks.
3679  * That can be as a result of older DLC library
3680  * calls. Multi-Rail DLC and beyond no longer uses these APIs.
3681  */
3682 int
3683 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
3684 {
3685         struct lnet_net *net;
3686         LIST_HEAD(net_head);
3687         int rc;
3688         struct lnet_ioctl_config_lnd_tunables tun;
3689         const char *nets = conf->cfg_config_u.cfg_net.net_intf;
3690
3691         /* Create a net/ni structures for the network string */
3692         rc = lnet_parse_networks(&net_head, nets);
3693         if (rc <= 0)
3694                 return rc == 0 ? -EINVAL : rc;
3695
3696         mutex_lock(&the_lnet.ln_api_mutex);
3697
3698         if (rc > 1) {
3699                 rc = -EINVAL; /* only add one network per call */
3700                 goto out_unlock_clean;
3701         }
3702
3703         net = list_entry(net_head.next, struct lnet_net, net_list);
3704         list_del_init(&net->net_list);
3705
3706         LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
3707
3708         memset(&tun, 0, sizeof(tun));
3709
3710         tun.lt_cmn.lct_peer_timeout =
3711           (!conf->cfg_config_u.cfg_net.net_peer_timeout) ? DEFAULT_PEER_TIMEOUT :
3712                 conf->cfg_config_u.cfg_net.net_peer_timeout;
3713         tun.lt_cmn.lct_peer_tx_credits =
3714           (!conf->cfg_config_u.cfg_net.net_peer_tx_credits) ? DEFAULT_PEER_CREDITS :
3715                 conf->cfg_config_u.cfg_net.net_peer_tx_credits;
3716         tun.lt_cmn.lct_peer_rtr_credits =
3717           conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
3718         tun.lt_cmn.lct_max_tx_credits =
3719           (!conf->cfg_config_u.cfg_net.net_max_tx_credits) ? DEFAULT_CREDITS :
3720                 conf->cfg_config_u.cfg_net.net_max_tx_credits;
3721
3722         rc = lnet_add_net_common(net, &tun);
3723
3724 out_unlock_clean:
3725         mutex_unlock(&the_lnet.ln_api_mutex);
3726         while (!list_empty(&net_head)) {
3727                 /* net_head list is empty in success case */
3728                 net = list_entry(net_head.next, struct lnet_net, net_list);
3729                 list_del_init(&net->net_list);
3730                 lnet_net_free(net);
3731         }
3732         return rc;
3733 }
3734
3735 int
3736 lnet_dyn_del_net(__u32 net_id)
3737 {
3738         struct lnet_net  *net;
3739         struct lnet_ping_buffer *pbuf;
3740         struct lnet_handle_md ping_mdh;
3741         int               rc;
3742         int               net_ni_count;
3743
3744         /* don't allow userspace to shutdown the LOLND */
3745         if (LNET_NETTYP(net_id) == LOLND)
3746                 return -EINVAL;
3747
3748         mutex_lock(&the_lnet.ln_api_mutex);
3749
3750         lnet_net_lock(0);
3751
3752         net = lnet_get_net_locked(net_id);
3753         if (net == NULL) {
3754                 lnet_net_unlock(0);
3755                 rc = -EINVAL;
3756                 goto out;
3757         }
3758
3759         net_ni_count = lnet_get_net_ni_count_locked(net);
3760
3761         lnet_net_unlock(0);
3762
3763         /* create and link a new ping info, before removing the old one */
3764         rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
3765                                     lnet_get_ni_count() - net_ni_count, false);
3766         if (rc != 0)
3767                 goto out;
3768
3769         lnet_shutdown_lndnet(net);
3770
3771         lnet_acceptor_stop();
3772
3773         lnet_ping_target_update(pbuf, ping_mdh);
3774
3775 out:
3776         mutex_unlock(&the_lnet.ln_api_mutex);
3777
3778         return rc;
3779 }
3780
3781 void lnet_incr_dlc_seq(void)
3782 {
3783         atomic_inc(&lnet_dlc_seq_no);
3784 }
3785
3786 __u32 lnet_get_dlc_seq_locked(void)
3787 {
3788         return atomic_read(&lnet_dlc_seq_no);
3789 }
3790
3791 static void
3792 lnet_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3793 {
3794         struct lnet_net *net;
3795         struct lnet_ni *ni;
3796
3797         lnet_net_lock(LNET_LOCK_EX);
3798         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3799                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3800                         if (all || (nid_is_nid4(&ni->ni_nid) &&
3801                                     lnet_nid_to_nid4(&ni->ni_nid) == nid)) {
3802                                 atomic_set(&ni->ni_healthv, value);
3803                                 if (list_empty(&ni->ni_recovery) &&
3804                                     value < LNET_MAX_HEALTH_VALUE) {
3805                                         CERROR("manually adding local NI %s to recovery\n",
3806                                                libcfs_nidstr(&ni->ni_nid));
3807                                         list_add_tail(&ni->ni_recovery,
3808                                                       &the_lnet.ln_mt_localNIRecovq);
3809                                         lnet_ni_addref_locked(ni, 0);
3810                                 }
3811                                 if (!all) {
3812                                         lnet_net_unlock(LNET_LOCK_EX);
3813                                         return;
3814                                 }
3815                         }
3816                 }
3817         }
3818         lnet_net_unlock(LNET_LOCK_EX);
3819 }
3820
3821 static void
3822 lnet_ni_set_conns_per_peer(lnet_nid_t nid, int value, bool all)
3823 {
3824         struct lnet_net *net;
3825         struct lnet_ni *ni;
3826
3827         lnet_net_lock(LNET_LOCK_EX);
3828         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3829                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3830                         if (lnet_nid_to_nid4(&ni->ni_nid) != nid && !all)
3831                                 continue;
3832                         if (LNET_NETTYP(net->net_id) == SOCKLND)
3833                                 ni->ni_lnd_tunables.lnd_tun_u.lnd_sock.lnd_conns_per_peer = value;
3834                         else if (LNET_NETTYP(net->net_id) == O2IBLND)
3835                                 ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib.lnd_conns_per_peer = value;
3836                         if (!all) {
3837                                 lnet_net_unlock(LNET_LOCK_EX);
3838                                 return;
3839                         }
3840                 }
3841         }
3842         lnet_net_unlock(LNET_LOCK_EX);
3843 }
3844
3845 static int
3846 lnet_get_local_ni_hstats(struct lnet_ioctl_local_ni_hstats *stats)
3847 {
3848         int cpt, rc = 0;
3849         struct lnet_ni *ni;
3850         lnet_nid_t nid = stats->hlni_nid;
3851
3852         cpt = lnet_net_lock_current();
3853         ni = lnet_nid2ni_locked(nid, cpt);
3854
3855         if (!ni) {
3856                 rc = -ENOENT;
3857                 goto unlock;
3858         }
3859
3860         stats->hlni_local_interrupt = atomic_read(&ni->ni_hstats.hlt_local_interrupt);
3861         stats->hlni_local_dropped = atomic_read(&ni->ni_hstats.hlt_local_dropped);
3862         stats->hlni_local_aborted = atomic_read(&ni->ni_hstats.hlt_local_aborted);
3863         stats->hlni_local_no_route = atomic_read(&ni->ni_hstats.hlt_local_no_route);
3864         stats->hlni_local_timeout = atomic_read(&ni->ni_hstats.hlt_local_timeout);
3865         stats->hlni_local_error = atomic_read(&ni->ni_hstats.hlt_local_error);
3866         stats->hlni_fatal_error = atomic_read(&ni->ni_fatal_error_on);
3867         stats->hlni_health_value = atomic_read(&ni->ni_healthv);
3868         stats->hlni_ping_count = ni->ni_ping_count;
3869         stats->hlni_next_ping = ni->ni_next_ping;
3870
3871 unlock:
3872         lnet_net_unlock(cpt);
3873
3874         return rc;
3875 }
3876
3877 static int
3878 lnet_get_local_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3879 {
3880         struct lnet_ni *ni;
3881         int i = 0;
3882
3883         lnet_net_lock(LNET_LOCK_EX);
3884         list_for_each_entry(ni, &the_lnet.ln_mt_localNIRecovq, ni_recovery) {
3885                 if (!nid_is_nid4(&ni->ni_nid))
3886                         continue;
3887                 list->rlst_nid_array[i] = lnet_nid_to_nid4(&ni->ni_nid);
3888                 i++;
3889                 if (i >= LNET_MAX_SHOW_NUM_NID)
3890                         break;
3891         }
3892         lnet_net_unlock(LNET_LOCK_EX);
3893         list->rlst_num_nids = i;
3894
3895         return 0;
3896 }
3897
3898 static int
3899 lnet_get_peer_ni_recovery_list(struct lnet_ioctl_recovery_list *list)
3900 {
3901         struct lnet_peer_ni *lpni;
3902         int i = 0;
3903
3904         lnet_net_lock(LNET_LOCK_EX);
3905         list_for_each_entry(lpni, &the_lnet.ln_mt_peerNIRecovq, lpni_recovery) {
3906                 list->rlst_nid_array[i] = lnet_nid_to_nid4(&lpni->lpni_nid);
3907                 i++;
3908                 if (i >= LNET_MAX_SHOW_NUM_NID)
3909                         break;
3910         }
3911         lnet_net_unlock(LNET_LOCK_EX);
3912         list->rlst_num_nids = i;
3913
3914         return 0;
3915 }
3916
3917 /**
3918  * LNet ioctl handler.
3919  *
3920  */
3921 int
3922 LNetCtl(unsigned int cmd, void *arg)
3923 {
3924         struct libcfs_ioctl_data *data = arg;
3925         struct lnet_ioctl_config_data *config;
3926         struct lnet_process_id    id4 = {};
3927         struct lnet_processid     id = {};
3928         struct lnet_ni           *ni;
3929         struct lnet_nid           nid;
3930         int                       rc;
3931
3932         BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
3933                      sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
3934
3935         switch (cmd) {
3936         case IOC_LIBCFS_GET_NI:
3937                 rc = LNetGetId(data->ioc_count, &id);
3938                 data->ioc_nid = lnet_nid_to_nid4(&id.nid);
3939                 return rc;
3940
3941         case IOC_LIBCFS_FAIL_NID:
3942                 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
3943
3944         case IOC_LIBCFS_ADD_ROUTE: {
3945                 /* default router sensitivity to 1 */
3946                 unsigned int sensitivity = 1;
3947                 config = arg;
3948
3949                 if (config->cfg_hdr.ioc_len < sizeof(*config))
3950                         return -EINVAL;
3951
3952                 if (config->cfg_config_u.cfg_route.rtr_sensitivity) {
3953                         sensitivity =
3954                           config->cfg_config_u.cfg_route.rtr_sensitivity;
3955                 }
3956
3957                 lnet_nid4_to_nid(config->cfg_nid, &nid);
3958                 mutex_lock(&the_lnet.ln_api_mutex);
3959                 rc = lnet_add_route(config->cfg_net,
3960                                     config->cfg_config_u.cfg_route.rtr_hop,
3961                                     &nid,
3962                                     config->cfg_config_u.cfg_route.
3963                                         rtr_priority, sensitivity);
3964                 mutex_unlock(&the_lnet.ln_api_mutex);
3965                 return rc;
3966         }
3967
3968         case IOC_LIBCFS_DEL_ROUTE:
3969                 config = arg;
3970
3971                 if (config->cfg_hdr.ioc_len < sizeof(*config))
3972                         return -EINVAL;
3973
3974                 lnet_nid4_to_nid(config->cfg_nid, &nid);
3975                 mutex_lock(&the_lnet.ln_api_mutex);
3976                 rc = lnet_del_route(config->cfg_net, &nid);
3977                 mutex_unlock(&the_lnet.ln_api_mutex);
3978                 return rc;
3979
3980         case IOC_LIBCFS_GET_ROUTE:
3981                 config = arg;
3982
3983                 if (config->cfg_hdr.ioc_len < sizeof(*config))
3984                         return -EINVAL;
3985
3986                 mutex_lock(&the_lnet.ln_api_mutex);
3987                 rc = lnet_get_route(config->cfg_count,
3988                                     &config->cfg_net,
3989                                     &config->cfg_config_u.cfg_route.rtr_hop,
3990                                     &config->cfg_nid,
3991                                     &config->cfg_config_u.cfg_route.rtr_flags,
3992                                     &config->cfg_config_u.cfg_route.
3993                                         rtr_priority,
3994                                     &config->cfg_config_u.cfg_route.
3995                                         rtr_sensitivity);
3996                 mutex_unlock(&the_lnet.ln_api_mutex);
3997                 return rc;
3998
3999         case IOC_LIBCFS_GET_LOCAL_NI: {
4000                 struct lnet_ioctl_config_ni *cfg_ni;
4001                 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
4002                 struct lnet_ioctl_element_stats *stats;
4003                 __u32 tun_size;
4004
4005                 cfg_ni = arg;
4006
4007                 /* get the tunables if they are available */
4008                 if (cfg_ni->lic_cfg_hdr.ioc_len <
4009                     sizeof(*cfg_ni) + sizeof(*stats) + sizeof(*tun))
4010                         return -EINVAL;
4011
4012                 stats = (struct lnet_ioctl_element_stats *)
4013                         cfg_ni->lic_bulk;
4014                 tun = (struct lnet_ioctl_config_lnd_tunables *)
4015                                 (cfg_ni->lic_bulk + sizeof(*stats));
4016
4017                 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
4018                         sizeof(*stats);
4019
4020                 mutex_lock(&the_lnet.ln_api_mutex);
4021                 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
4022                 mutex_unlock(&the_lnet.ln_api_mutex);
4023                 return rc;
4024         }
4025
4026         case IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS: {
4027                 struct lnet_ioctl_element_msg_stats *msg_stats = arg;
4028
4029                 if (msg_stats->im_hdr.ioc_len != sizeof(*msg_stats))
4030                         return -EINVAL;
4031
4032                 mutex_lock(&the_lnet.ln_api_mutex);
4033                 rc = lnet_get_ni_stats(msg_stats);
4034                 mutex_unlock(&the_lnet.ln_api_mutex);
4035
4036                 return rc;
4037         }
4038
4039         case IOC_LIBCFS_GET_NET: {
4040                 size_t total = sizeof(*config) +
4041                                sizeof(struct lnet_ioctl_net_config);
4042                 config = arg;
4043
4044                 if (config->cfg_hdr.ioc_len < total)
4045                         return -EINVAL;
4046
4047                 mutex_lock(&the_lnet.ln_api_mutex);
4048                 rc = lnet_get_net_config(config);
4049                 mutex_unlock(&the_lnet.ln_api_mutex);
4050                 return rc;
4051         }
4052
4053         case IOC_LIBCFS_GET_LNET_STATS:
4054         {
4055                 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
4056
4057                 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
4058                         return -EINVAL;
4059
4060                 mutex_lock(&the_lnet.ln_api_mutex);
4061                 rc = lnet_counters_get(&lnet_stats->st_cntrs);
4062                 mutex_unlock(&the_lnet.ln_api_mutex);
4063                 return rc;
4064         }
4065
4066         case IOC_LIBCFS_RESET_LNET_STATS:
4067         {
4068                 mutex_lock(&the_lnet.ln_api_mutex);
4069                 lnet_counters_reset();
4070                 mutex_unlock(&the_lnet.ln_api_mutex);
4071                 return 0;
4072         }
4073
4074         case IOC_LIBCFS_CONFIG_RTR:
4075                 config = arg;
4076
4077                 if (config->cfg_hdr.ioc_len < sizeof(*config))
4078                         return -EINVAL;
4079
4080                 mutex_lock(&the_lnet.ln_api_mutex);
4081                 if (config->cfg_config_u.cfg_buffers.buf_enable) {
4082                         rc = lnet_rtrpools_enable();
4083                         mutex_unlock(&the_lnet.ln_api_mutex);
4084                         return rc;
4085                 }
4086                 lnet_rtrpools_disable();
4087                 mutex_unlock(&the_lnet.ln_api_mutex);
4088                 return 0;
4089
4090         case IOC_LIBCFS_ADD_BUF:
4091                 config = arg;
4092
4093                 if (config->cfg_hdr.ioc_len < sizeof(*config))
4094                         return -EINVAL;
4095
4096                 mutex_lock(&the_lnet.ln_api_mutex);
4097                 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
4098                                                 buf_tiny,
4099                                           config->cfg_config_u.cfg_buffers.
4100                                                 buf_small,
4101                                           config->cfg_config_u.cfg_buffers.
4102                                                 buf_large);
4103                 mutex_unlock(&the_lnet.ln_api_mutex);
4104                 return rc;
4105
4106         case IOC_LIBCFS_SET_NUMA_RANGE: {
4107                 struct lnet_ioctl_set_value *numa;
4108                 numa = arg;
4109                 if (numa->sv_hdr.ioc_len != sizeof(*numa))
4110                         return -EINVAL;
4111                 lnet_net_lock(LNET_LOCK_EX);
4112                 lnet_numa_range = numa->sv_value;
4113                 lnet_net_unlock(LNET_LOCK_EX);
4114                 return 0;
4115         }
4116
4117         case IOC_LIBCFS_GET_NUMA_RANGE: {
4118                 struct lnet_ioctl_set_value *numa;
4119                 numa = arg;
4120                 if (numa->sv_hdr.ioc_len != sizeof(*numa))
4121                         return -EINVAL;
4122                 numa->sv_value = lnet_numa_range;
4123                 return 0;
4124         }
4125
4126         case IOC_LIBCFS_GET_BUF: {
4127                 struct lnet_ioctl_pool_cfg *pool_cfg;
4128                 size_t total = sizeof(*config) + sizeof(*pool_cfg);
4129
4130                 config = arg;
4131
4132                 if (config->cfg_hdr.ioc_len < total)
4133                         return -EINVAL;
4134
4135                 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
4136
4137                 mutex_lock(&the_lnet.ln_api_mutex);
4138                 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
4139                 mutex_unlock(&the_lnet.ln_api_mutex);
4140                 return rc;
4141         }
4142
4143         case IOC_LIBCFS_GET_LOCAL_HSTATS: {
4144                 struct lnet_ioctl_local_ni_hstats *stats = arg;
4145
4146                 if (stats->hlni_hdr.ioc_len < sizeof(*stats))
4147                         return -EINVAL;
4148
4149                 mutex_lock(&the_lnet.ln_api_mutex);
4150                 rc = lnet_get_local_ni_hstats(stats);
4151                 mutex_unlock(&the_lnet.ln_api_mutex);
4152
4153                 return rc;
4154         }
4155
4156         case IOC_LIBCFS_GET_RECOVERY_QUEUE: {
4157                 struct lnet_ioctl_recovery_list *list = arg;
4158                 if (list->rlst_hdr.ioc_len < sizeof(*list))
4159                         return -EINVAL;
4160
4161                 mutex_lock(&the_lnet.ln_api_mutex);
4162                 if (list->rlst_type == LNET_HEALTH_TYPE_LOCAL_NI)
4163                         rc = lnet_get_local_ni_recovery_list(list);
4164                 else
4165                         rc = lnet_get_peer_ni_recovery_list(list);
4166                 mutex_unlock(&the_lnet.ln_api_mutex);
4167                 return rc;
4168         }
4169
4170         case IOC_LIBCFS_ADD_PEER_NI: {
4171                 struct lnet_ioctl_peer_cfg *cfg = arg;
4172
4173                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4174                         return -EINVAL;
4175
4176                 mutex_lock(&the_lnet.ln_api_mutex);
4177                 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
4178                                       cfg->prcfg_cfg_nid,
4179                                       cfg->prcfg_mr, false);
4180                 mutex_unlock(&the_lnet.ln_api_mutex);
4181                 return rc;
4182         }
4183
4184         case IOC_LIBCFS_DEL_PEER_NI: {
4185                 struct lnet_ioctl_peer_cfg *cfg = arg;
4186
4187                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4188                         return -EINVAL;
4189
4190                 mutex_lock(&the_lnet.ln_api_mutex);
4191                 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
4192                                       cfg->prcfg_cfg_nid);
4193                 mutex_unlock(&the_lnet.ln_api_mutex);
4194                 return rc;
4195         }
4196
4197         case IOC_LIBCFS_GET_PEER_INFO: {
4198                 struct lnet_ioctl_peer *peer_info = arg;
4199
4200                 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
4201                         return -EINVAL;
4202
4203                 mutex_lock(&the_lnet.ln_api_mutex);
4204                 rc = lnet_get_peer_ni_info(
4205                    peer_info->pr_count,
4206                    &peer_info->pr_nid,
4207                    peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
4208                    &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
4209                    &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
4210                    &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
4211                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
4212                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
4213                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
4214                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
4215                 mutex_unlock(&the_lnet.ln_api_mutex);
4216                 return rc;
4217         }
4218
4219         case IOC_LIBCFS_GET_PEER_NI: {
4220                 struct lnet_ioctl_peer_cfg *cfg = arg;
4221
4222                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4223                         return -EINVAL;
4224
4225                 mutex_lock(&the_lnet.ln_api_mutex);
4226                 rc = lnet_get_peer_info(cfg,
4227                                         (void __user *)cfg->prcfg_bulk);
4228                 mutex_unlock(&the_lnet.ln_api_mutex);
4229                 return rc;
4230         }
4231
4232         case IOC_LIBCFS_GET_PEER_LIST: {
4233                 struct lnet_ioctl_peer_cfg *cfg = arg;
4234
4235                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
4236                         return -EINVAL;
4237
4238                 mutex_lock(&the_lnet.ln_api_mutex);
4239                 rc = lnet_get_peer_list(&cfg->prcfg_count, &cfg->prcfg_size,
4240                                 (struct lnet_process_id __user *)cfg->prcfg_bulk);
4241                 mutex_unlock(&the_lnet.ln_api_mutex);
4242                 return rc;
4243         }
4244
4245         case IOC_LIBCFS_SET_HEALHV: {
4246                 struct lnet_ioctl_reset_health_cfg *cfg = arg;
4247                 int value;
4248                 if (cfg->rh_hdr.ioc_len < sizeof(*cfg))
4249                         return -EINVAL;
4250                 if (cfg->rh_value < 0 ||
4251                     cfg->rh_value > LNET_MAX_HEALTH_VALUE)
4252                         value = LNET_MAX_HEALTH_VALUE;
4253                 else
4254                         value = cfg->rh_value;
4255                 CDEBUG(D_NET, "Manually setting healthv to %d for %s:%s. all = %d\n",
4256                        value, (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI) ?
4257                        "local" : "peer", libcfs_nid2str(cfg->rh_nid), cfg->rh_all);
4258                 mutex_lock(&the_lnet.ln_api_mutex);
4259                 if (cfg->rh_type == LNET_HEALTH_TYPE_LOCAL_NI)
4260                         lnet_ni_set_healthv(cfg->rh_nid, value,
4261                                              cfg->rh_all);
4262                 else
4263                         lnet_peer_ni_set_healthv(cfg->rh_nid, value,
4264                                                   cfg->rh_all);
4265                 mutex_unlock(&the_lnet.ln_api_mutex);
4266                 return 0;
4267         }
4268
4269         case IOC_LIBCFS_SET_CONNS_PER_PEER: {
4270                 struct lnet_ioctl_reset_conns_per_peer_cfg *cfg = arg;
4271                 int value;
4272
4273                 if (cfg->rcpp_hdr.ioc_len < sizeof(*cfg))
4274                         return -EINVAL;
4275                 if (cfg->rcpp_value < 0)
4276                         value = 1;
4277                 else
4278                         value = cfg->rcpp_value;
4279                 CDEBUG(D_NET,
4280                        "Setting conns_per_peer to %d for %s. all = %d\n",
4281                        value, libcfs_nid2str(cfg->rcpp_nid), cfg->rcpp_all);
4282                 mutex_lock(&the_lnet.ln_api_mutex);
4283                 lnet_ni_set_conns_per_peer(cfg->rcpp_nid, value, cfg->rcpp_all);
4284                 mutex_unlock(&the_lnet.ln_api_mutex);
4285                 return 0;
4286         }
4287
4288         case IOC_LIBCFS_NOTIFY_ROUTER: {
4289                 time64_t deadline = ktime_get_real_seconds() - data->ioc_u64[0];
4290
4291                 /* The deadline passed in by the user should be some time in
4292                  * seconds in the future since the UNIX epoch. We have to map
4293                  * that deadline to the wall clock.
4294                  */
4295                 deadline += ktime_get_seconds();
4296                 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags, false,
4297                                    deadline);
4298         }
4299
4300         case IOC_LIBCFS_LNET_DIST:
4301                 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
4302                 if (rc < 0 && rc != -EHOSTUNREACH)
4303                         return rc;
4304
4305                 data->ioc_u32[0] = rc;
4306                 return 0;
4307
4308         case IOC_LIBCFS_TESTPROTOCOMPAT:
4309                 the_lnet.ln_testprotocompat = data->ioc_flags;
4310                 return 0;
4311
4312         case IOC_LIBCFS_LNET_FAULT:
4313                 return lnet_fault_ctl(data->ioc_flags, data);
4314
4315         case IOC_LIBCFS_PING: {
4316                 signed long timeout;
4317
4318                 id4.nid = data->ioc_nid;
4319                 id4.pid = data->ioc_u32[0];
4320
4321                 /* If timeout is negative then set default of 3 minutes */
4322                 if (((s32)data->ioc_u32[1] <= 0) ||
4323                     data->ioc_u32[1] > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
4324                         timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
4325                 else
4326                         timeout = nsecs_to_jiffies(data->ioc_u32[1] * NSEC_PER_MSEC);
4327
4328                 rc = lnet_ping(id4, &LNET_ANY_NID, timeout, data->ioc_pbuf1,
4329                                data->ioc_plen1 / sizeof(struct lnet_process_id));
4330
4331                 if (rc < 0)
4332                         return rc;
4333
4334                 data->ioc_count = rc;
4335                 return 0;
4336         }
4337
4338         case IOC_LIBCFS_PING_PEER: {
4339                 struct lnet_ioctl_ping_data *ping = arg;
4340                 struct lnet_nid src_nid = LNET_ANY_NID;
4341                 struct lnet_peer *lp;
4342                 signed long timeout;
4343
4344                 /* Check if the supplied ping data supports source nid
4345                  * NB: This check is sufficient if lnet_ioctl_ping_data has
4346                  * additional fields added, but if they are re-ordered or
4347                  * fields removed then this will break. It is expected that
4348                  * these ioctls will be replaced with netlink implementation, so
4349                  * it is probably not worth coming up with a more robust version
4350                  * compatibility scheme.
4351                  */
4352                 if (ping->ping_hdr.ioc_len >= sizeof(struct lnet_ioctl_ping_data))
4353                         lnet_nid4_to_nid(ping->ping_src, &src_nid);
4354
4355                 /* If timeout is negative then set default of 3 minutes */
4356                 if (((s32)ping->op_param) <= 0 ||
4357                     ping->op_param > (DEFAULT_PEER_TIMEOUT * MSEC_PER_SEC))
4358                         timeout = cfs_time_seconds(DEFAULT_PEER_TIMEOUT);
4359                 else
4360                         timeout = nsecs_to_jiffies(ping->op_param * NSEC_PER_MSEC);
4361
4362                 rc = lnet_ping(ping->ping_id, &src_nid, timeout,
4363                                ping->ping_buf,
4364                                ping->ping_count);
4365                 if (rc < 0)
4366                         return rc;
4367
4368                 mutex_lock(&the_lnet.ln_api_mutex);
4369                 lp = lnet_find_peer4(ping->ping_id.nid);
4370                 if (lp) {
4371                         ping->ping_id.nid =
4372                                 lnet_nid_to_nid4(&lp->lp_primary_nid);
4373                         ping->mr_info = lnet_peer_is_multi_rail(lp);
4374                         lnet_peer_decref_locked(lp);
4375                 }
4376                 mutex_unlock(&the_lnet.ln_api_mutex);
4377
4378                 ping->ping_count = rc;
4379                 return 0;
4380         }
4381
4382         case IOC_LIBCFS_DISCOVER: {
4383                 struct lnet_ioctl_ping_data *discover = arg;
4384                 struct lnet_peer *lp;
4385
4386                 rc = lnet_discover(discover->ping_id, discover->op_param,
4387                                    discover->ping_buf,
4388                                    discover->ping_count);
4389                 if (rc < 0)
4390                         return rc;
4391
4392                 mutex_lock(&the_lnet.ln_api_mutex);
4393                 lp = lnet_find_peer4(discover->ping_id.nid);
4394                 if (lp) {
4395                         discover->ping_id.nid =
4396                                 lnet_nid_to_nid4(&lp->lp_primary_nid);
4397                         discover->mr_info = lnet_peer_is_multi_rail(lp);
4398                         lnet_peer_decref_locked(lp);
4399                 }
4400                 mutex_unlock(&the_lnet.ln_api_mutex);
4401
4402                 discover->ping_count = rc;
4403                 return 0;
4404         }
4405
4406         case IOC_LIBCFS_ADD_UDSP: {
4407                 struct lnet_ioctl_udsp *ioc_udsp = arg;
4408                 __u32 bulk_size = ioc_udsp->iou_hdr.ioc_len;
4409
4410                 mutex_lock(&the_lnet.ln_api_mutex);
4411                 rc = lnet_udsp_demarshal_add(arg, bulk_size);
4412                 if (!rc) {
4413                         rc = lnet_udsp_apply_policies(NULL, false);
4414                         CDEBUG(D_NET, "policy application returned %d\n", rc);
4415                         rc = 0;
4416                 }
4417                 mutex_unlock(&the_lnet.ln_api_mutex);
4418
4419                 return rc;
4420         }
4421
4422         case IOC_LIBCFS_DEL_UDSP: {
4423                 struct lnet_ioctl_udsp *ioc_udsp = arg;
4424                 int idx = ioc_udsp->iou_idx;
4425
4426                 if (ioc_udsp->iou_hdr.ioc_len < sizeof(*ioc_udsp))
4427                         return -EINVAL;
4428
4429                 mutex_lock(&the_lnet.ln_api_mutex);
4430                 rc = lnet_udsp_del_policy(idx);
4431                 if (!rc) {
4432                         rc = lnet_udsp_apply_policies(NULL, false);
4433                         CDEBUG(D_NET, "policy re-application returned %d\n",
4434                                rc);
4435                         rc = 0;
4436                 }
4437                 mutex_unlock(&the_lnet.ln_api_mutex);
4438
4439                 return rc;
4440         }
4441
4442         case IOC_LIBCFS_GET_UDSP_SIZE: {
4443                 struct lnet_ioctl_udsp *ioc_udsp = arg;
4444                 struct lnet_udsp *udsp;
4445
4446                 if (ioc_udsp->iou_hdr.ioc_len < sizeof(*ioc_udsp))
4447                         return -EINVAL;
4448
4449                 rc = 0;
4450
4451                 mutex_lock(&the_lnet.ln_api_mutex);
4452                 udsp = lnet_udsp_get_policy(ioc_udsp->iou_idx);
4453                 if (!udsp) {
4454                         rc = -ENOENT;
4455                 } else {
4456                         /* coming in iou_idx will hold the idx of the udsp
4457                          * to get the size of. going out the iou_idx will
4458                          * hold the size of the UDSP found at the passed
4459                          * in index.
4460                          */
4461                         ioc_udsp->iou_idx = lnet_get_udsp_size(udsp);
4462                         if (ioc_udsp->iou_idx < 0)
4463                                 rc = -EINVAL;
4464                 }
4465                 mutex_unlock(&the_lnet.ln_api_mutex);
4466
4467                 return rc;
4468         }
4469
4470         case IOC_LIBCFS_GET_UDSP: {
4471                 struct lnet_ioctl_udsp *ioc_udsp = arg;
4472                 struct lnet_udsp *udsp;
4473
4474                 if (ioc_udsp->iou_hdr.ioc_len < sizeof(*ioc_udsp))
4475                         return -EINVAL;
4476
4477                 rc = 0;
4478
4479                 mutex_lock(&the_lnet.ln_api_mutex);
4480                 udsp = lnet_udsp_get_policy(ioc_udsp->iou_idx);
4481                 if (!udsp)
4482                         rc = -ENOENT;
4483                 else
4484                         rc = lnet_udsp_marshal(udsp, ioc_udsp);
4485                 mutex_unlock(&the_lnet.ln_api_mutex);
4486
4487                 return rc;
4488         }
4489
4490         case IOC_LIBCFS_GET_CONST_UDSP_INFO: {
4491                 struct lnet_ioctl_construct_udsp_info *info = arg;
4492
4493                 if (info->cud_hdr.ioc_len < sizeof(*info))
4494                         return -EINVAL;
4495
4496                 CDEBUG(D_NET, "GET_UDSP_INFO for %s\n",
4497                        libcfs_nid2str(info->cud_nid));
4498
4499                 mutex_lock(&the_lnet.ln_api_mutex);
4500                 lnet_udsp_get_construct_info(info);
4501                 mutex_unlock(&the_lnet.ln_api_mutex);
4502
4503                 return 0;
4504         }
4505
4506         default:
4507                 ni = lnet_net2ni_addref(data->ioc_net);
4508                 if (ni == NULL)
4509                         return -EINVAL;
4510
4511                 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
4512                         rc = -EINVAL;
4513                 else
4514                         rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
4515
4516                 lnet_ni_decref(ni);
4517                 return rc;
4518         }
4519         /* not reached */
4520 }
4521 EXPORT_SYMBOL(LNetCtl);
4522
4523 void LNetDebugPeer(struct lnet_processid *id)
4524 {
4525         lnet_debug_peer(lnet_nid_to_nid4(&id->nid));
4526 }
4527 EXPORT_SYMBOL(LNetDebugPeer);
4528
4529 /**
4530  * Determine if the specified peer \a nid is on the local node.
4531  *
4532  * \param nid   peer nid to check
4533  *
4534  * \retval true         If peer NID is on the local node.
4535  * \retval false        If peer NID is not on the local node.
4536  */
4537 bool LNetIsPeerLocal(lnet_nid_t nid)
4538 {
4539         struct lnet_net *net;
4540         struct lnet_ni *ni;
4541         int cpt;
4542
4543         cpt = lnet_net_lock_current();
4544         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4545                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4546                         if (lnet_nid_to_nid4(&ni->ni_nid) == nid) {
4547                                 lnet_net_unlock(cpt);
4548                                 return true;
4549                         }
4550                 }
4551         }
4552         lnet_net_unlock(cpt);
4553
4554         return false;
4555 }
4556 EXPORT_SYMBOL(LNetIsPeerLocal);
4557
4558 /**
4559  * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
4560  * Note that all interfaces share a same PID, as requested by LNetNIInit().
4561  *
4562  * \param index Index of the interface to look up.
4563  * \param id On successful return, this location will hold the
4564  * struct lnet_process_id ID of the interface.
4565  *
4566  * \retval 0 If an interface exists at \a index.
4567  * \retval -ENOENT If no interface has been found.
4568  */
4569 int
4570 LNetGetId(unsigned int index, struct lnet_processid *id)
4571 {
4572         struct lnet_ni   *ni;
4573         struct lnet_net  *net;
4574         int               cpt;
4575         int               rc = -ENOENT;
4576
4577         LASSERT(the_lnet.ln_refcount > 0);
4578
4579         cpt = lnet_net_lock_current();
4580
4581         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
4582                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
4583                         if (!nid_is_nid4(&ni->ni_nid))
4584                                 /* FIXME this needs to be handled */
4585                                 continue;
4586                         if (index-- != 0)
4587                                 continue;
4588
4589                         id->nid = ni->ni_nid;
4590                         id->pid = the_lnet.ln_pid;
4591                         rc = 0;
4592                         break;
4593                 }
4594         }
4595
4596         lnet_net_unlock(cpt);
4597         return rc;
4598 }
4599 EXPORT_SYMBOL(LNetGetId);
4600
4601 struct ping_data {
4602         int rc;
4603         int replied;
4604         struct lnet_handle_md mdh;
4605         struct completion completion;
4606 };
4607
4608 static void
4609 lnet_ping_event_handler(struct lnet_event *event)
4610 {
4611         struct ping_data *pd = event->md_user_ptr;
4612
4613         CDEBUG(D_NET, "ping event (%d %d)%s\n",
4614                event->type, event->status,
4615                event->unlinked ? " unlinked" : "");
4616
4617         if (event->status) {
4618                 if (!pd->rc)
4619                         pd->rc = event->status;
4620         } else if (event->type == LNET_EVENT_REPLY) {
4621                 pd->replied = 1;
4622                 pd->rc = event->mlength;
4623         }
4624         if (event->unlinked)
4625                 complete(&pd->completion);
4626 }
4627
4628 static int lnet_ping(struct lnet_process_id id, struct lnet_nid *src_nid,
4629                      signed long timeout, struct lnet_process_id __user *ids,
4630                      int n_ids)
4631 {
4632         struct lnet_md md = { NULL };
4633         struct ping_data pd = { 0 };
4634         struct lnet_ping_buffer *pbuf;
4635         struct lnet_process_id tmpid;
4636         int i;
4637         int nob;
4638         int rc;
4639         int rc2;
4640
4641         /* n_ids limit is arbitrary */
4642         if (n_ids <= 0 || id.nid == LNET_NID_ANY)
4643                 return -EINVAL;
4644
4645         /*
4646          * if the user buffer has more space than the lnet_interfaces_max
4647          * then only fill it up to lnet_interfaces_max
4648          */
4649         if (n_ids > lnet_interfaces_max)
4650                 n_ids = lnet_interfaces_max;
4651
4652         if (id.pid == LNET_PID_ANY)
4653                 id.pid = LNET_PID_LUSTRE;
4654
4655         pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
4656         if (!pbuf)
4657                 return -ENOMEM;
4658
4659         /* initialize md content */
4660         md.start     = &pbuf->pb_info;
4661         md.length    = LNET_PING_INFO_SIZE(n_ids);
4662         md.threshold = 2; /* GET/REPLY */
4663         md.max_size  = 0;
4664         md.options   = LNET_MD_TRUNCATE;
4665         md.user_ptr  = &pd;
4666         md.handler   = lnet_ping_event_handler;
4667
4668         init_completion(&pd.completion);
4669
4670         rc = LNetMDBind(&md, LNET_UNLINK, &pd.mdh);
4671         if (rc != 0) {
4672                 CERROR("Can't bind MD: %d\n", rc);
4673                 goto fail_ping_buffer_decref;
4674         }
4675
4676         rc = LNetGet(lnet_nid_to_nid4(src_nid), pd.mdh, id,
4677                      LNET_RESERVED_PORTAL,
4678                      LNET_PROTO_PING_MATCHBITS, 0, false);
4679
4680         if (rc != 0) {
4681                 /* Don't CERROR; this could be deliberate! */
4682                 rc2 = LNetMDUnlink(pd.mdh);
4683                 LASSERT(rc2 == 0);
4684
4685                 /* NB must wait for the UNLINK event below... */
4686         }
4687
4688         if (wait_for_completion_timeout(&pd.completion, timeout) == 0) {
4689                 /* Ensure completion in finite time... */
4690                 LNetMDUnlink(pd.mdh);
4691                 wait_for_completion(&pd.completion);
4692         }
4693         if (!pd.replied) {
4694                 rc = -EIO;
4695                 goto fail_ping_buffer_decref;
4696         }
4697
4698         nob = pd.rc;
4699         LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
4700
4701         rc = -EPROTO;           /* if I can't parse... */
4702
4703         if (nob < 8) {
4704                 CERROR("%s: ping info too short %d\n",
4705                        libcfs_id2str(id), nob);
4706                 goto fail_ping_buffer_decref;
4707         }
4708
4709         if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
4710                 lnet_swap_pinginfo(pbuf);
4711         } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
4712                 CERROR("%s: Unexpected magic %08x\n",
4713                        libcfs_id2str(id), pbuf->pb_info.pi_magic);
4714                 goto fail_ping_buffer_decref;
4715         }
4716
4717         if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
4718                 CERROR("%s: ping w/o NI status: 0x%x\n",
4719                        libcfs_id2str(id), pbuf->pb_info.pi_features);
4720                 goto fail_ping_buffer_decref;
4721         }
4722
4723         if (nob < LNET_PING_INFO_SIZE(0)) {
4724                 CERROR("%s: Short reply %d(%d min)\n",
4725                        libcfs_id2str(id),
4726                        nob, (int)LNET_PING_INFO_SIZE(0));
4727                 goto fail_ping_buffer_decref;
4728         }
4729
4730         if (pbuf->pb_info.pi_nnis < n_ids)
4731                 n_ids = pbuf->pb_info.pi_nnis;
4732
4733         if (nob < LNET_PING_INFO_SIZE(n_ids)) {
4734                 CERROR("%s: Short reply %d(%d expected)\n",
4735                        libcfs_id2str(id),
4736                        nob, (int)LNET_PING_INFO_SIZE(n_ids));
4737                 goto fail_ping_buffer_decref;
4738         }
4739
4740         rc = -EFAULT;           /* if I segv in copy_to_user()... */
4741
4742         memset(&tmpid, 0, sizeof(tmpid));
4743         for (i = 0; i < n_ids; i++) {
4744                 tmpid.pid = pbuf->pb_info.pi_pid;
4745                 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
4746                 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
4747                         goto fail_ping_buffer_decref;
4748         }
4749         rc = pbuf->pb_info.pi_nnis;
4750
4751  fail_ping_buffer_decref:
4752         lnet_ping_buffer_decref(pbuf);
4753         return rc;
4754 }
4755
4756 static int
4757 lnet_discover(struct lnet_process_id id, __u32 force,
4758               struct lnet_process_id __user *ids, int n_ids)
4759 {
4760         struct lnet_peer_ni *lpni;
4761         struct lnet_peer_ni *p;
4762         struct lnet_peer *lp;
4763         struct lnet_process_id *buf;
4764         int cpt;
4765         int i;
4766         int rc;
4767
4768         if (n_ids <= 0 ||
4769             id.nid == LNET_NID_ANY)
4770                 return -EINVAL;
4771
4772         if (id.pid == LNET_PID_ANY)
4773                 id.pid = LNET_PID_LUSTRE;
4774
4775         /*
4776          * If the user buffer has more space than the lnet_interfaces_max,
4777          * then only fill it up to lnet_interfaces_max.
4778          */
4779         if (n_ids > lnet_interfaces_max)
4780                 n_ids = lnet_interfaces_max;
4781
4782         CFS_ALLOC_PTR_ARRAY(buf, n_ids);
4783         if (!buf)
4784                 return -ENOMEM;
4785
4786         cpt = lnet_net_lock_current();
4787         lpni = lnet_nid2peerni_locked(id.nid, LNET_NID_ANY, cpt);
4788         if (IS_ERR(lpni)) {
4789                 rc = PTR_ERR(lpni);
4790                 goto out;
4791         }
4792
4793         /*
4794          * Clearing the NIDS_UPTODATE flag ensures the peer will
4795          * be discovered, provided discovery has not been disabled.
4796          */
4797         lp = lpni->lpni_peer_net->lpn_peer;
4798         spin_lock(&lp->lp_lock);
4799         lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
4800         /* If the force flag is set, force a PING and PUSH as well. */
4801         if (force)
4802                 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
4803         spin_unlock(&lp->lp_lock);
4804         rc = lnet_discover_peer_locked(lpni, cpt, true);
4805         if (rc)
4806                 goto out_decref;
4807
4808         /* The lpni (or lp) for this NID may have changed and our ref is
4809          * the only thing keeping the old one around. Release the ref
4810          * and lookup the lpni again
4811          */
4812         lnet_peer_ni_decref_locked(lpni);
4813         lpni = lnet_find_peer_ni_locked(id.nid);
4814         if (!lpni) {
4815                 rc = -ENOENT;
4816                 goto out;
4817         }
4818         lp = lpni->lpni_peer_net->lpn_peer;
4819
4820         i = 0;
4821         p = NULL;
4822         while ((p = lnet_get_next_peer_ni_locked(lp, NULL, p)) != NULL) {
4823                 buf[i].pid = id.pid;
4824                 buf[i].nid = lnet_nid_to_nid4(&p->lpni_nid);
4825                 if (++i >= n_ids)
4826                         break;
4827         }
4828         rc = i;
4829
4830 out_decref:
4831         lnet_peer_ni_decref_locked(lpni);
4832 out:
4833         lnet_net_unlock(cpt);
4834
4835         if (rc >= 0)
4836                 if (copy_to_user(ids, buf, rc * sizeof(*buf)))
4837                         rc = -EFAULT;
4838         CFS_FREE_PTR_ARRAY(buf, n_ids);
4839
4840         return rc;
4841 }
4842
4843 /**
4844  * Retrieve peer discovery status.
4845  *
4846  * \retval 1 if lnet_peer_discovery_disabled is 0
4847  * \retval 0 if lnet_peer_discovery_disabled is 1
4848  */
4849 int
4850 LNetGetPeerDiscoveryStatus(void)
4851 {
4852         return !lnet_peer_discovery_disabled;
4853 }
4854 EXPORT_SYMBOL(LNetGetPeerDiscoveryStatus);