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