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