Whamcloud - gitweb
f5acbc9b09d4edf8f2516e6c6692e97d88ba0040
[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, 2016, 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 #include <linux/log2.h>
35 #include <linux/ktime.h>
36 #include <linux/moduleparam.h>
37
38 #include <lnet/lib-lnet.h>
39
40 #define D_LNI D_CONSOLE
41
42 struct lnet the_lnet;           /* THE state of the network */
43 EXPORT_SYMBOL(the_lnet);
44
45 static char *ip2nets = "";
46 module_param(ip2nets, charp, 0444);
47 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
48
49 static char *networks = "";
50 module_param(networks, charp, 0444);
51 MODULE_PARM_DESC(networks, "local networks");
52
53 static char *routes = "";
54 module_param(routes, charp, 0444);
55 MODULE_PARM_DESC(routes, "routes to non-local networks");
56
57 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
58 module_param(rnet_htable_size, int, 0444);
59 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
60
61 static int use_tcp_bonding = false;
62 module_param(use_tcp_bonding, int, 0444);
63 MODULE_PARM_DESC(use_tcp_bonding,
64                  "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
65
66 unsigned int lnet_numa_range = 0;
67 module_param(lnet_numa_range, uint, 0444);
68 MODULE_PARM_DESC(lnet_numa_range,
69                 "NUMA range to consider during Multi-Rail selection");
70
71 static int lnet_interfaces_max = LNET_INTERFACES_MAX_DEFAULT;
72 static int intf_max_set(const char *val, struct kernel_param *kp);
73 module_param_call(lnet_interfaces_max, intf_max_set, param_get_int,
74                   &lnet_interfaces_max, S_IRUGO|S_IWUSR);
75 MODULE_PARM_DESC(lnet_interfaces_max,
76                 "Maximum number of interfaces in a node.");
77
78 unsigned lnet_peer_discovery_disabled = 0;
79 static int discovery_set(const char *val, struct kernel_param *kp);
80 module_param_call(lnet_peer_discovery_disabled, discovery_set, param_get_int,
81                   &lnet_peer_discovery_disabled, S_IRUGO|S_IWUSR);
82 MODULE_PARM_DESC(lnet_peer_discovery_disabled,
83                 "Set to 1 to disable peer discovery on this node.");
84
85 /*
86  * This sequence number keeps track of how many times DLC was used to
87  * update the local NIs. It is incremented when a NI is added or
88  * removed and checked when sending a message to determine if there is
89  * a need to re-run the selection algorithm. See lnet_select_pathway()
90  * for more details on its usage.
91  */
92 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
93
94 static int lnet_ping(struct lnet_process_id id, signed long timeout,
95                      struct lnet_process_id __user *ids, int n_ids);
96
97 static int
98 discovery_set(const char *val, struct kernel_param *kp)
99 {
100         int rc;
101         unsigned long value;
102
103         rc = kstrtoul(val, 0, &value);
104         if (rc) {
105                 CERROR("Invalid module parameter value for 'lnet_peer_discovery_disabled'\n");
106                 return rc;
107         }
108
109         *(unsigned *)kp->arg = (value) ? 1 : 0;
110
111         return 0;
112 }
113
114 static int
115 intf_max_set(const char *val, struct kernel_param *kp)
116 {
117         int value, rc;
118
119         rc = kstrtoint(val, 0, &value);
120         if (rc) {
121                 CERROR("Invalid module parameter value for 'lnet_interfaces_max'\n");
122                 return rc;
123         }
124
125         if (value < LNET_INTERFACES_MIN) {
126                 CWARN("max interfaces provided are too small, setting to %d\n",
127                       LNET_INTERFACES_MIN);
128                 value = LNET_INTERFACES_MIN;
129         }
130
131         *(int *)kp->arg = value;
132
133         return 0;
134 }
135
136 static char *
137 lnet_get_routes(void)
138 {
139         return routes;
140 }
141
142 static char *
143 lnet_get_networks(void)
144 {
145         char   *nets;
146         int     rc;
147
148         if (*networks != 0 && *ip2nets != 0) {
149                 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
150                                    "'ip2nets' but not both at once\n");
151                 return NULL;
152         }
153
154         if (*ip2nets != 0) {
155                 rc = lnet_parse_ip2nets(&nets, ip2nets);
156                 return (rc == 0) ? nets : NULL;
157         }
158
159         if (*networks != 0)
160                 return networks;
161
162         return "tcp";
163 }
164
165 static void
166 lnet_init_locks(void)
167 {
168         spin_lock_init(&the_lnet.ln_eq_wait_lock);
169         init_waitqueue_head(&the_lnet.ln_eq_waitq);
170         init_waitqueue_head(&the_lnet.ln_rc_waitq);
171         mutex_init(&the_lnet.ln_lnd_mutex);
172         mutex_init(&the_lnet.ln_api_mutex);
173 }
174
175 static void
176 lnet_fini_locks(void)
177 {
178 }
179
180 struct kmem_cache *lnet_mes_cachep;        /* MEs kmem_cache */
181 struct kmem_cache *lnet_small_mds_cachep;  /* <= LNET_SMALL_MD_SIZE bytes
182                                             *  MDs kmem_cache */
183
184 static int
185 lnet_descriptor_setup(void)
186 {
187         /* create specific kmem_cache for MEs and small MDs (i.e., originally
188          * allocated in <size-xxx> kmem_cache).
189          */
190         lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(struct lnet_me),
191                                             0, 0, NULL);
192         if (!lnet_mes_cachep)
193                 return -ENOMEM;
194
195         lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
196                                                   LNET_SMALL_MD_SIZE, 0, 0,
197                                                   NULL);
198         if (!lnet_small_mds_cachep)
199                 return -ENOMEM;
200
201         return 0;
202 }
203
204 static void
205 lnet_descriptor_cleanup(void)
206 {
207
208         if (lnet_small_mds_cachep) {
209                 kmem_cache_destroy(lnet_small_mds_cachep);
210                 lnet_small_mds_cachep = NULL;
211         }
212
213         if (lnet_mes_cachep) {
214                 kmem_cache_destroy(lnet_mes_cachep);
215                 lnet_mes_cachep = NULL;
216         }
217 }
218
219 static int
220 lnet_create_remote_nets_table(void)
221 {
222         int               i;
223         struct list_head *hash;
224
225         LASSERT(the_lnet.ln_remote_nets_hash == NULL);
226         LASSERT(the_lnet.ln_remote_nets_hbits > 0);
227         LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
228         if (hash == NULL) {
229                 CERROR("Failed to create remote nets hash table\n");
230                 return -ENOMEM;
231         }
232
233         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
234                 INIT_LIST_HEAD(&hash[i]);
235         the_lnet.ln_remote_nets_hash = hash;
236         return 0;
237 }
238
239 static void
240 lnet_destroy_remote_nets_table(void)
241 {
242         int i;
243
244         if (the_lnet.ln_remote_nets_hash == NULL)
245                 return;
246
247         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
248                 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
249
250         LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
251                     LNET_REMOTE_NETS_HASH_SIZE *
252                     sizeof(the_lnet.ln_remote_nets_hash[0]));
253         the_lnet.ln_remote_nets_hash = NULL;
254 }
255
256 static void
257 lnet_destroy_locks(void)
258 {
259         if (the_lnet.ln_res_lock != NULL) {
260                 cfs_percpt_lock_free(the_lnet.ln_res_lock);
261                 the_lnet.ln_res_lock = NULL;
262         }
263
264         if (the_lnet.ln_net_lock != NULL) {
265                 cfs_percpt_lock_free(the_lnet.ln_net_lock);
266                 the_lnet.ln_net_lock = NULL;
267         }
268
269         lnet_fini_locks();
270 }
271
272 static int
273 lnet_create_locks(void)
274 {
275         lnet_init_locks();
276
277         the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
278         if (the_lnet.ln_res_lock == NULL)
279                 goto failed;
280
281         the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
282         if (the_lnet.ln_net_lock == NULL)
283                 goto failed;
284
285         return 0;
286
287  failed:
288         lnet_destroy_locks();
289         return -ENOMEM;
290 }
291
292 static void lnet_assert_wire_constants(void)
293 {
294         /* Wire protocol assertions generated by 'wirecheck'
295          * running on Linux robert.bartonsoftware.com 2.6.8-1.521
296          * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
297          * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
298
299         /* Constants... */
300         CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
301         CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
302         CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
303         CLASSERT(LNET_MSG_ACK == 0);
304         CLASSERT(LNET_MSG_PUT == 1);
305         CLASSERT(LNET_MSG_GET == 2);
306         CLASSERT(LNET_MSG_REPLY == 3);
307         CLASSERT(LNET_MSG_HELLO == 4);
308
309         /* Checks for struct lnet_handle_wire */
310         CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
311         CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
312         CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
313         CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
314         CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
315
316         /* Checks for struct struct lnet_magicversion */
317         CLASSERT((int)sizeof(struct lnet_magicversion) == 8);
318         CLASSERT((int)offsetof(struct lnet_magicversion, magic) == 0);
319         CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->magic) == 4);
320         CLASSERT((int)offsetof(struct lnet_magicversion, version_major) == 4);
321         CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_major) == 2);
322         CLASSERT((int)offsetof(struct lnet_magicversion, version_minor) == 6);
323         CLASSERT((int)sizeof(((struct lnet_magicversion *)0)->version_minor) == 2);
324
325         /* Checks for struct struct lnet_hdr */
326         CLASSERT((int)sizeof(struct lnet_hdr) == 72);
327         CLASSERT((int)offsetof(struct lnet_hdr, dest_nid) == 0);
328         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_nid) == 8);
329         CLASSERT((int)offsetof(struct lnet_hdr, src_nid) == 8);
330         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_nid) == 8);
331         CLASSERT((int)offsetof(struct lnet_hdr, dest_pid) == 16);
332         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->dest_pid) == 4);
333         CLASSERT((int)offsetof(struct lnet_hdr, src_pid) == 20);
334         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->src_pid) == 4);
335         CLASSERT((int)offsetof(struct lnet_hdr, type) == 24);
336         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->type) == 4);
337         CLASSERT((int)offsetof(struct lnet_hdr, payload_length) == 28);
338         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->payload_length) == 4);
339         CLASSERT((int)offsetof(struct lnet_hdr, msg) == 32);
340         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg) == 40);
341
342         /* Ack */
343         CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.dst_wmd) == 32);
344         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.dst_wmd) == 16);
345         CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.match_bits) == 48);
346         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.match_bits) == 8);
347         CLASSERT((int)offsetof(struct lnet_hdr, msg.ack.mlength) == 56);
348         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.ack.mlength) == 4);
349
350         /* Put */
351         CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ack_wmd) == 32);
352         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ack_wmd) == 16);
353         CLASSERT((int)offsetof(struct lnet_hdr, msg.put.match_bits) == 48);
354         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.match_bits) == 8);
355         CLASSERT((int)offsetof(struct lnet_hdr, msg.put.hdr_data) == 56);
356         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.hdr_data) == 8);
357         CLASSERT((int)offsetof(struct lnet_hdr, msg.put.ptl_index) == 64);
358         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.ptl_index) == 4);
359         CLASSERT((int)offsetof(struct lnet_hdr, msg.put.offset) == 68);
360         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.put.offset) == 4);
361
362         /* Get */
363         CLASSERT((int)offsetof(struct lnet_hdr, msg.get.return_wmd) == 32);
364         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.return_wmd) == 16);
365         CLASSERT((int)offsetof(struct lnet_hdr, msg.get.match_bits) == 48);
366         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.match_bits) == 8);
367         CLASSERT((int)offsetof(struct lnet_hdr, msg.get.ptl_index) == 56);
368         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.ptl_index) == 4);
369         CLASSERT((int)offsetof(struct lnet_hdr, msg.get.src_offset) == 60);
370         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.src_offset) == 4);
371         CLASSERT((int)offsetof(struct lnet_hdr, msg.get.sink_length) == 64);
372         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.get.sink_length) == 4);
373
374         /* Reply */
375         CLASSERT((int)offsetof(struct lnet_hdr, msg.reply.dst_wmd) == 32);
376         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.reply.dst_wmd) == 16);
377
378         /* Hello */
379         CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.incarnation) == 32);
380         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.incarnation) == 8);
381         CLASSERT((int)offsetof(struct lnet_hdr, msg.hello.type) == 40);
382         CLASSERT((int)sizeof(((struct lnet_hdr *)0)->msg.hello.type) == 4);
383
384         /* Checks for struct lnet_ni_status and related constants */
385         CLASSERT(LNET_NI_STATUS_INVALID == 0x00000000);
386         CLASSERT(LNET_NI_STATUS_UP == 0x15aac0de);
387         CLASSERT(LNET_NI_STATUS_DOWN == 0xdeadface);
388
389         /* Checks for struct lnet_ni_status */
390         CLASSERT((int)sizeof(struct lnet_ni_status) == 16);
391         CLASSERT((int)offsetof(struct lnet_ni_status, ns_nid) == 0);
392         CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_nid) == 8);
393         CLASSERT((int)offsetof(struct lnet_ni_status, ns_status) == 8);
394         CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_status) == 4);
395         CLASSERT((int)offsetof(struct lnet_ni_status, ns_unused) == 12);
396         CLASSERT((int)sizeof(((struct lnet_ni_status *)0)->ns_unused) == 4);
397
398         /* Checks for struct lnet_ping_info and related constants */
399         CLASSERT(LNET_PROTO_PING_MAGIC == 0x70696E67);
400         CLASSERT(LNET_PING_FEAT_INVAL == 0);
401         CLASSERT(LNET_PING_FEAT_BASE == 1);
402         CLASSERT(LNET_PING_FEAT_NI_STATUS == 2);
403         CLASSERT(LNET_PING_FEAT_RTE_DISABLED == 4);
404         CLASSERT(LNET_PING_FEAT_MULTI_RAIL == 8);
405         CLASSERT(LNET_PING_FEAT_DISCOVERY == 16);
406         CLASSERT(LNET_PING_FEAT_BITS == 31);
407
408         /* Checks for struct lnet_ping_info */
409         CLASSERT((int)sizeof(struct lnet_ping_info) == 16);
410         CLASSERT((int)offsetof(struct lnet_ping_info, pi_magic) == 0);
411         CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_magic) == 4);
412         CLASSERT((int)offsetof(struct lnet_ping_info, pi_features) == 4);
413         CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_features) == 4);
414         CLASSERT((int)offsetof(struct lnet_ping_info, pi_pid) == 8);
415         CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_pid) == 4);
416         CLASSERT((int)offsetof(struct lnet_ping_info, pi_nnis) == 12);
417         CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_nnis) == 4);
418         CLASSERT((int)offsetof(struct lnet_ping_info, pi_ni) == 16);
419         CLASSERT((int)sizeof(((struct lnet_ping_info *)0)->pi_ni) == 0);
420 }
421
422 static struct lnet_lnd *lnet_find_lnd_by_type(__u32 type)
423 {
424         struct lnet_lnd *lnd;
425         struct list_head *tmp;
426
427         /* holding lnd mutex */
428         list_for_each(tmp, &the_lnet.ln_lnds) {
429                 lnd = list_entry(tmp, struct lnet_lnd, lnd_list);
430
431                 if (lnd->lnd_type == type)
432                         return lnd;
433         }
434         return NULL;
435 }
436
437 void
438 lnet_register_lnd(struct lnet_lnd *lnd)
439 {
440         mutex_lock(&the_lnet.ln_lnd_mutex);
441
442         LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
443         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
444
445         list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
446         lnd->lnd_refcount = 0;
447
448         CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
449
450         mutex_unlock(&the_lnet.ln_lnd_mutex);
451 }
452 EXPORT_SYMBOL(lnet_register_lnd);
453
454 void
455 lnet_unregister_lnd(struct lnet_lnd *lnd)
456 {
457         mutex_lock(&the_lnet.ln_lnd_mutex);
458
459         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
460         LASSERT(lnd->lnd_refcount == 0);
461
462         list_del(&lnd->lnd_list);
463         CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
464
465         mutex_unlock(&the_lnet.ln_lnd_mutex);
466 }
467 EXPORT_SYMBOL(lnet_unregister_lnd);
468
469 void
470 lnet_counters_get(struct lnet_counters *counters)
471 {
472         struct lnet_counters *ctr;
473         int             i;
474
475         memset(counters, 0, sizeof(*counters));
476
477         lnet_net_lock(LNET_LOCK_EX);
478
479         cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
480                 counters->msgs_max     += ctr->msgs_max;
481                 counters->msgs_alloc   += ctr->msgs_alloc;
482                 counters->errors       += ctr->errors;
483                 counters->send_count   += ctr->send_count;
484                 counters->recv_count   += ctr->recv_count;
485                 counters->route_count  += ctr->route_count;
486                 counters->drop_count   += ctr->drop_count;
487                 counters->send_length  += ctr->send_length;
488                 counters->recv_length  += ctr->recv_length;
489                 counters->route_length += ctr->route_length;
490                 counters->drop_length  += ctr->drop_length;
491
492         }
493         lnet_net_unlock(LNET_LOCK_EX);
494 }
495 EXPORT_SYMBOL(lnet_counters_get);
496
497 void
498 lnet_counters_reset(void)
499 {
500         struct lnet_counters *counters;
501         int             i;
502
503         lnet_net_lock(LNET_LOCK_EX);
504
505         cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
506                 memset(counters, 0, sizeof(struct lnet_counters));
507
508         lnet_net_unlock(LNET_LOCK_EX);
509 }
510
511 static char *
512 lnet_res_type2str(int type)
513 {
514         switch (type) {
515         default:
516                 LBUG();
517         case LNET_COOKIE_TYPE_MD:
518                 return "MD";
519         case LNET_COOKIE_TYPE_ME:
520                 return "ME";
521         case LNET_COOKIE_TYPE_EQ:
522                 return "EQ";
523         }
524 }
525
526 static void
527 lnet_res_container_cleanup(struct lnet_res_container *rec)
528 {
529         int     count = 0;
530
531         if (rec->rec_type == 0) /* not set yet, it's uninitialized */
532                 return;
533
534         while (!list_empty(&rec->rec_active)) {
535                 struct list_head *e = rec->rec_active.next;
536
537                 list_del_init(e);
538                 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
539                         lnet_eq_free(list_entry(e, struct lnet_eq, eq_list));
540
541                 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
542                         lnet_md_free(list_entry(e, struct lnet_libmd, md_list));
543
544                 } else { /* NB: Active MEs should be attached on portals */
545                         LBUG();
546                 }
547                 count++;
548         }
549
550         if (count > 0) {
551                 /* Found alive MD/ME/EQ, user really should unlink/free
552                  * all of them before finalize LNet, but if someone didn't,
553                  * we have to recycle garbage for him */
554                 CERROR("%d active elements on exit of %s container\n",
555                        count, lnet_res_type2str(rec->rec_type));
556         }
557
558         if (rec->rec_lh_hash != NULL) {
559                 LIBCFS_FREE(rec->rec_lh_hash,
560                             LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
561                 rec->rec_lh_hash = NULL;
562         }
563
564         rec->rec_type = 0; /* mark it as finalized */
565 }
566
567 static int
568 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
569 {
570         int     rc = 0;
571         int     i;
572
573         LASSERT(rec->rec_type == 0);
574
575         rec->rec_type = type;
576         INIT_LIST_HEAD(&rec->rec_active);
577
578         rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
579
580         /* Arbitrary choice of hash table size */
581         LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
582                          LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
583         if (rec->rec_lh_hash == NULL) {
584                 rc = -ENOMEM;
585                 goto out;
586         }
587
588         for (i = 0; i < LNET_LH_HASH_SIZE; i++)
589                 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
590
591         return 0;
592
593 out:
594         CERROR("Failed to setup %s resource container\n",
595                lnet_res_type2str(type));
596         lnet_res_container_cleanup(rec);
597         return rc;
598 }
599
600 static void
601 lnet_res_containers_destroy(struct lnet_res_container **recs)
602 {
603         struct lnet_res_container       *rec;
604         int                             i;
605
606         cfs_percpt_for_each(rec, i, recs)
607                 lnet_res_container_cleanup(rec);
608
609         cfs_percpt_free(recs);
610 }
611
612 static struct lnet_res_container **
613 lnet_res_containers_create(int type)
614 {
615         struct lnet_res_container       **recs;
616         struct lnet_res_container       *rec;
617         int                             rc;
618         int                             i;
619
620         recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
621         if (recs == NULL) {
622                 CERROR("Failed to allocate %s resource containers\n",
623                        lnet_res_type2str(type));
624                 return NULL;
625         }
626
627         cfs_percpt_for_each(rec, i, recs) {
628                 rc = lnet_res_container_setup(rec, i, type);
629                 if (rc != 0) {
630                         lnet_res_containers_destroy(recs);
631                         return NULL;
632                 }
633         }
634
635         return recs;
636 }
637
638 struct lnet_libhandle *
639 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
640 {
641         /* ALWAYS called with lnet_res_lock held */
642         struct list_head        *head;
643         struct lnet_libhandle   *lh;
644         unsigned int            hash;
645
646         if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
647                 return NULL;
648
649         hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
650         head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
651
652         list_for_each_entry(lh, head, lh_hash_chain) {
653                 if (lh->lh_cookie == cookie)
654                         return lh;
655         }
656
657         return NULL;
658 }
659
660 void
661 lnet_res_lh_initialize(struct lnet_res_container *rec,
662                        struct lnet_libhandle *lh)
663 {
664         /* ALWAYS called with lnet_res_lock held */
665         unsigned int    ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
666         unsigned int    hash;
667
668         lh->lh_cookie = rec->rec_lh_cookie;
669         rec->rec_lh_cookie += 1 << ibits;
670
671         hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
672
673         list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
674 }
675
676 static int lnet_unprepare(void);
677
678 static int
679 lnet_prepare(lnet_pid_t requested_pid)
680 {
681         /* Prepare to bring up the network */
682         struct lnet_res_container **recs;
683         int                       rc = 0;
684
685         if (requested_pid == LNET_PID_ANY) {
686                 /* Don't instantiate LNET just for me */
687                 return -ENETDOWN;
688         }
689
690         LASSERT(the_lnet.ln_refcount == 0);
691
692         the_lnet.ln_routing = 0;
693
694         LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
695         the_lnet.ln_pid = requested_pid;
696
697         INIT_LIST_HEAD(&the_lnet.ln_test_peers);
698         INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
699         INIT_LIST_HEAD(&the_lnet.ln_nets);
700         INIT_LIST_HEAD(&the_lnet.ln_routers);
701         INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
702         INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
703
704         rc = lnet_descriptor_setup();
705         if (rc != 0)
706                 goto failed;
707
708         rc = lnet_create_remote_nets_table();
709         if (rc != 0)
710                 goto failed;
711
712         /*
713          * NB the interface cookie in wire handles guards against delayed
714          * replies and ACKs appearing valid after reboot.
715          */
716         the_lnet.ln_interface_cookie = ktime_get_real_ns();
717
718         the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
719                                                 sizeof(struct lnet_counters));
720         if (the_lnet.ln_counters == NULL) {
721                 CERROR("Failed to allocate counters for LNet\n");
722                 rc = -ENOMEM;
723                 goto failed;
724         }
725
726         rc = lnet_peer_tables_create();
727         if (rc != 0)
728                 goto failed;
729
730         rc = lnet_msg_containers_create();
731         if (rc != 0)
732                 goto failed;
733
734         rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
735                                       LNET_COOKIE_TYPE_EQ);
736         if (rc != 0)
737                 goto failed;
738
739         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
740         if (recs == NULL) {
741                 rc = -ENOMEM;
742                 goto failed;
743         }
744
745         the_lnet.ln_me_containers = recs;
746
747         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
748         if (recs == NULL) {
749                 rc = -ENOMEM;
750                 goto failed;
751         }
752
753         the_lnet.ln_md_containers = recs;
754
755         rc = lnet_portals_create();
756         if (rc != 0) {
757                 CERROR("Failed to create portals for LNet: %d\n", rc);
758                 goto failed;
759         }
760
761         return 0;
762
763  failed:
764         lnet_unprepare();
765         return rc;
766 }
767
768 static int
769 lnet_unprepare (void)
770 {
771         /* NB no LNET_LOCK since this is the last reference.  All LND instances
772          * have shut down already, so it is safe to unlink and free all
773          * descriptors, even those that appear committed to a network op (eg MD
774          * with non-zero pending count) */
775
776         lnet_fail_nid(LNET_NID_ANY, 0);
777
778         LASSERT(the_lnet.ln_refcount == 0);
779         LASSERT(list_empty(&the_lnet.ln_test_peers));
780         LASSERT(list_empty(&the_lnet.ln_nets));
781
782         lnet_portals_destroy();
783
784         if (the_lnet.ln_md_containers != NULL) {
785                 lnet_res_containers_destroy(the_lnet.ln_md_containers);
786                 the_lnet.ln_md_containers = NULL;
787         }
788
789         if (the_lnet.ln_me_containers != NULL) {
790                 lnet_res_containers_destroy(the_lnet.ln_me_containers);
791                 the_lnet.ln_me_containers = NULL;
792         }
793
794         lnet_res_container_cleanup(&the_lnet.ln_eq_container);
795
796         lnet_msg_containers_destroy();
797         lnet_peer_uninit();
798         lnet_rtrpools_free(0);
799
800         if (the_lnet.ln_counters != NULL) {
801                 cfs_percpt_free(the_lnet.ln_counters);
802                 the_lnet.ln_counters = NULL;
803         }
804         lnet_destroy_remote_nets_table();
805         lnet_descriptor_cleanup();
806
807         return 0;
808 }
809
810 struct lnet_ni  *
811 lnet_net2ni_locked(__u32 net_id, int cpt)
812 {
813         struct lnet_ni   *ni;
814         struct lnet_net  *net;
815
816         LASSERT(cpt != LNET_LOCK_EX);
817
818         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
819                 if (net->net_id == net_id) {
820                         ni = list_entry(net->net_ni_list.next, struct lnet_ni,
821                                         ni_netlist);
822                         return ni;
823                 }
824         }
825
826         return NULL;
827 }
828
829 struct lnet_ni *
830 lnet_net2ni_addref(__u32 net)
831 {
832         struct lnet_ni *ni;
833
834         lnet_net_lock(0);
835         ni = lnet_net2ni_locked(net, 0);
836         if (ni)
837                 lnet_ni_addref_locked(ni, 0);
838         lnet_net_unlock(0);
839
840         return ni;
841 }
842 EXPORT_SYMBOL(lnet_net2ni_addref);
843
844 struct lnet_net *
845 lnet_get_net_locked(__u32 net_id)
846 {
847         struct lnet_net  *net;
848
849         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
850                 if (net->net_id == net_id)
851                         return net;
852         }
853
854         return NULL;
855 }
856
857 unsigned int
858 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
859 {
860         __u64           key = nid;
861         unsigned int    val;
862
863         LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
864
865         if (number == 1)
866                 return 0;
867
868         val = hash_long(key, LNET_CPT_BITS);
869         /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
870         if (val < number)
871                 return val;
872
873         return (unsigned int)(key + val + (val >> 1)) % number;
874 }
875
876 int
877 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
878 {
879         struct lnet_net *net;
880
881         /* must called with hold of lnet_net_lock */
882         if (LNET_CPT_NUMBER == 1)
883                 return 0; /* the only one */
884
885         /*
886          * If NI is provided then use the CPT identified in the NI cpt
887          * list if one exists. If one doesn't exist, then that NI is
888          * associated with all CPTs and it follows that the net it belongs
889          * to is implicitly associated with all CPTs, so just hash the nid
890          * and return that.
891          */
892         if (ni != NULL) {
893                 if (ni->ni_cpts != NULL)
894                         return ni->ni_cpts[lnet_nid_cpt_hash(nid,
895                                                              ni->ni_ncpts)];
896                 else
897                         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
898         }
899
900         /* no NI provided so look at the net */
901         net = lnet_get_net_locked(LNET_NIDNET(nid));
902
903         if (net != NULL && net->net_cpts != NULL) {
904                 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
905         }
906
907         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
908 }
909
910 int
911 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
912 {
913         int     cpt;
914         int     cpt2;
915
916         if (LNET_CPT_NUMBER == 1)
917                 return 0; /* the only one */
918
919         cpt = lnet_net_lock_current();
920
921         cpt2 = lnet_cpt_of_nid_locked(nid, ni);
922
923         lnet_net_unlock(cpt);
924
925         return cpt2;
926 }
927 EXPORT_SYMBOL(lnet_cpt_of_nid);
928
929 int
930 lnet_islocalnet(__u32 net_id)
931 {
932         struct lnet_net *net;
933         int             cpt;
934         bool            local;
935
936         cpt = lnet_net_lock_current();
937
938         net = lnet_get_net_locked(net_id);
939
940         local = net != NULL;
941
942         lnet_net_unlock(cpt);
943
944         return local;
945 }
946
947 bool
948 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
949 {
950         if (ni->ni_state == LNET_NI_STATE_ACTIVE ||
951             ni->ni_state == LNET_NI_STATE_DEGRADED)
952                 return true;
953
954         return false;
955 }
956
957 struct lnet_ni  *
958 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
959 {
960         struct lnet_net  *net;
961         struct lnet_ni   *ni;
962
963         LASSERT(cpt != LNET_LOCK_EX);
964
965         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
966                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
967                         if (ni->ni_nid == nid)
968                                 return ni;
969                 }
970         }
971
972         return NULL;
973 }
974
975 struct lnet_ni *
976 lnet_nid2ni_addref(lnet_nid_t nid)
977 {
978         struct lnet_ni *ni;
979
980         lnet_net_lock(0);
981         ni = lnet_nid2ni_locked(nid, 0);
982         if (ni)
983                 lnet_ni_addref_locked(ni, 0);
984         lnet_net_unlock(0);
985
986         return ni;
987 }
988 EXPORT_SYMBOL(lnet_nid2ni_addref);
989
990 int
991 lnet_islocalnid(lnet_nid_t nid)
992 {
993         struct lnet_ni  *ni;
994         int             cpt;
995
996         cpt = lnet_net_lock_current();
997         ni = lnet_nid2ni_locked(nid, cpt);
998         lnet_net_unlock(cpt);
999
1000         return ni != NULL;
1001 }
1002
1003 int
1004 lnet_count_acceptor_nets(void)
1005 {
1006         /* Return the # of NIs that need the acceptor. */
1007         int              count = 0;
1008         struct lnet_net  *net;
1009         int              cpt;
1010
1011         cpt = lnet_net_lock_current();
1012         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1013                 /* all socklnd type networks should have the acceptor
1014                  * thread started */
1015                 if (net->net_lnd->lnd_accept != NULL)
1016                         count++;
1017         }
1018
1019         lnet_net_unlock(cpt);
1020
1021         return count;
1022 }
1023
1024 struct lnet_ping_buffer *
1025 lnet_ping_buffer_alloc(int nnis, gfp_t gfp)
1026 {
1027         struct lnet_ping_buffer *pbuf;
1028
1029         LIBCFS_ALLOC_GFP(pbuf, LNET_PING_BUFFER_SIZE(nnis), gfp);
1030         if (pbuf) {
1031                 pbuf->pb_nnis = nnis;
1032                 atomic_set(&pbuf->pb_refcnt, 1);
1033         }
1034
1035         return pbuf;
1036 }
1037
1038 void
1039 lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf)
1040 {
1041         LASSERT(lnet_ping_buffer_numref(pbuf) == 0);
1042         LIBCFS_FREE(pbuf, LNET_PING_BUFFER_SIZE(pbuf->pb_nnis));
1043 }
1044
1045 static struct lnet_ping_buffer *
1046 lnet_ping_target_create(int nnis)
1047 {
1048         struct lnet_ping_buffer *pbuf;
1049
1050         pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
1051         if (pbuf == NULL) {
1052                 CERROR("Can't allocate ping source [%d]\n", nnis);
1053                 return NULL;
1054         }
1055
1056         pbuf->pb_info.pi_nnis = nnis;
1057         pbuf->pb_info.pi_pid = the_lnet.ln_pid;
1058         pbuf->pb_info.pi_magic = LNET_PROTO_PING_MAGIC;
1059         pbuf->pb_info.pi_features = LNET_PING_FEAT_NI_STATUS;
1060
1061         return pbuf;
1062 }
1063
1064 static inline int
1065 lnet_get_net_ni_count_locked(struct lnet_net *net)
1066 {
1067         struct lnet_ni  *ni;
1068         int             count = 0;
1069
1070         list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1071                 count++;
1072
1073         return count;
1074 }
1075
1076 static inline int
1077 lnet_get_net_ni_count_pre(struct lnet_net *net)
1078 {
1079         struct lnet_ni  *ni;
1080         int             count = 0;
1081
1082         list_for_each_entry(ni, &net->net_ni_added, ni_netlist)
1083                 count++;
1084
1085         return count;
1086 }
1087
1088 static inline int
1089 lnet_get_ni_count(void)
1090 {
1091         struct lnet_ni  *ni;
1092         struct lnet_net *net;
1093         int             count = 0;
1094
1095         lnet_net_lock(0);
1096
1097         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1098                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1099                         count++;
1100         }
1101
1102         lnet_net_unlock(0);
1103
1104         return count;
1105 }
1106
1107 int
1108 lnet_ping_info_validate(struct lnet_ping_info *pinfo)
1109 {
1110         if (!pinfo)
1111                 return -EINVAL;
1112         if (pinfo->pi_magic != LNET_PROTO_PING_MAGIC)
1113                 return -EPROTO;
1114         if (!(pinfo->pi_features & LNET_PING_FEAT_NI_STATUS))
1115                 return -EPROTO;
1116         /* Loopback is guaranteed to be present */
1117         if (pinfo->pi_nnis < 1 || pinfo->pi_nnis > lnet_interfaces_max)
1118                 return -ERANGE;
1119         if (LNET_NETTYP(LNET_NIDNET(LNET_PING_INFO_LONI(pinfo))) != LOLND)
1120                 return -EPROTO;
1121         return 0;
1122 }
1123
1124 static void
1125 lnet_ping_target_destroy(void)
1126 {
1127         struct lnet_net *net;
1128         struct lnet_ni  *ni;
1129
1130         lnet_net_lock(LNET_LOCK_EX);
1131
1132         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1133                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1134                         lnet_ni_lock(ni);
1135                         ni->ni_status = NULL;
1136                         lnet_ni_unlock(ni);
1137                 }
1138         }
1139
1140         lnet_ping_buffer_decref(the_lnet.ln_ping_target);
1141         the_lnet.ln_ping_target = NULL;
1142
1143         lnet_net_unlock(LNET_LOCK_EX);
1144 }
1145
1146 static void
1147 lnet_ping_target_event_handler(struct lnet_event *event)
1148 {
1149         struct lnet_ping_buffer *pbuf = event->md.user_ptr;
1150
1151         if (event->unlinked)
1152                 lnet_ping_buffer_decref(pbuf);
1153 }
1154
1155 static int
1156 lnet_ping_target_setup(struct lnet_ping_buffer **ppbuf,
1157                        struct lnet_handle_md *ping_mdh,
1158                        int ni_count, bool set_eq)
1159 {
1160         struct lnet_process_id id = {
1161                 .nid = LNET_NID_ANY,
1162                 .pid = LNET_PID_ANY
1163         };
1164         struct lnet_handle_me me_handle;
1165         struct lnet_md md = { NULL };
1166         int rc, rc2;
1167
1168         if (set_eq) {
1169                 rc = LNetEQAlloc(0, lnet_ping_target_event_handler,
1170                                  &the_lnet.ln_ping_target_eq);
1171                 if (rc != 0) {
1172                         CERROR("Can't allocate ping buffer EQ: %d\n", rc);
1173                         return rc;
1174                 }
1175         }
1176
1177         *ppbuf = lnet_ping_target_create(ni_count);
1178         if (*ppbuf == NULL) {
1179                 rc = -ENOMEM;
1180                 goto fail_free_eq;
1181         }
1182
1183         /* Ping target ME/MD */
1184         rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1185                           LNET_PROTO_PING_MATCHBITS, 0,
1186                           LNET_UNLINK, LNET_INS_AFTER,
1187                           &me_handle);
1188         if (rc != 0) {
1189                 CERROR("Can't create ping target ME: %d\n", rc);
1190                 goto fail_decref_ping_buffer;
1191         }
1192
1193         /* initialize md content */
1194         md.start     = &(*ppbuf)->pb_info;
1195         md.length    = LNET_PING_INFO_SIZE((*ppbuf)->pb_nnis);
1196         md.threshold = LNET_MD_THRESH_INF;
1197         md.max_size  = 0;
1198         md.options   = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1199                        LNET_MD_MANAGE_REMOTE;
1200         md.eq_handle = the_lnet.ln_ping_target_eq;
1201         md.user_ptr  = *ppbuf;
1202
1203         rc = LNetMDAttach(me_handle, md, LNET_RETAIN, ping_mdh);
1204         if (rc != 0) {
1205                 CERROR("Can't attach ping target MD: %d\n", rc);
1206                 goto fail_unlink_ping_me;
1207         }
1208         lnet_ping_buffer_addref(*ppbuf);
1209
1210         return 0;
1211
1212 fail_unlink_ping_me:
1213         rc2 = LNetMEUnlink(me_handle);
1214         LASSERT(rc2 == 0);
1215 fail_decref_ping_buffer:
1216         LASSERT(lnet_ping_buffer_numref(*ppbuf) == 1);
1217         lnet_ping_buffer_decref(*ppbuf);
1218         *ppbuf = NULL;
1219 fail_free_eq:
1220         if (set_eq) {
1221                 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1222                 LASSERT(rc2 == 0);
1223         }
1224         return rc;
1225 }
1226
1227 static void
1228 lnet_ping_md_unlink(struct lnet_ping_buffer *pbuf,
1229                     struct lnet_handle_md *ping_mdh)
1230 {
1231         sigset_t        blocked = cfs_block_allsigs();
1232
1233         LNetMDUnlink(*ping_mdh);
1234         LNetInvalidateMDHandle(ping_mdh);
1235
1236         /* NB the MD could be busy; this just starts the unlink */
1237         while (lnet_ping_buffer_numref(pbuf) > 1) {
1238                 CDEBUG(D_NET, "Still waiting for ping data MD to unlink\n");
1239                 set_current_state(TASK_UNINTERRUPTIBLE);
1240                 schedule_timeout(cfs_time_seconds(1));
1241         }
1242
1243         cfs_restore_sigs(blocked);
1244 }
1245
1246 static void
1247 lnet_ping_target_install_locked(struct lnet_ping_buffer *pbuf)
1248 {
1249         struct lnet_ni          *ni;
1250         struct lnet_net         *net;
1251         struct lnet_ni_status *ns;
1252         int                     i;
1253         int                     rc;
1254
1255         i = 0;
1256         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1257                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1258                         LASSERT(i < pbuf->pb_nnis);
1259
1260                         ns = &pbuf->pb_info.pi_ni[i];
1261
1262                         ns->ns_nid = ni->ni_nid;
1263
1264                         lnet_ni_lock(ni);
1265                         ns->ns_status = (ni->ni_status != NULL) ?
1266                                          ni->ni_status->ns_status :
1267                                                 LNET_NI_STATUS_UP;
1268                         ni->ni_status = ns;
1269                         lnet_ni_unlock(ni);
1270
1271                         i++;
1272                 }
1273         }
1274         /*
1275          * We (ab)use the ns_status of the loopback interface to
1276          * transmit the sequence number. The first interface listed
1277          * must be the loopback interface.
1278          */
1279         rc = lnet_ping_info_validate(&pbuf->pb_info);
1280         if (rc) {
1281                 LCONSOLE_EMERG("Invalid ping target: %d\n", rc);
1282                 LBUG();
1283         }
1284         LNET_PING_BUFFER_SEQNO(pbuf) =
1285                 atomic_inc_return(&the_lnet.ln_ping_target_seqno);
1286 }
1287
1288 static void
1289 lnet_ping_target_update(struct lnet_ping_buffer *pbuf,
1290                         struct lnet_handle_md ping_mdh)
1291 {
1292         struct lnet_ping_buffer *old_pbuf = NULL;
1293         struct lnet_handle_md old_ping_md;
1294
1295         /* switch the NIs to point to the new ping info created */
1296         lnet_net_lock(LNET_LOCK_EX);
1297
1298         if (!the_lnet.ln_routing)
1299                 pbuf->pb_info.pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1300
1301         /* Ensure only known feature bits have been set. */
1302         LASSERT(pbuf->pb_info.pi_features & LNET_PING_FEAT_BITS);
1303         LASSERT(!(pbuf->pb_info.pi_features & ~LNET_PING_FEAT_BITS));
1304
1305         lnet_ping_target_install_locked(pbuf);
1306
1307         if (the_lnet.ln_ping_target) {
1308                 old_pbuf = the_lnet.ln_ping_target;
1309                 old_ping_md = the_lnet.ln_ping_target_md;
1310         }
1311         the_lnet.ln_ping_target_md = ping_mdh;
1312         the_lnet.ln_ping_target = pbuf;
1313
1314         lnet_net_unlock(LNET_LOCK_EX);
1315
1316         if (old_pbuf) {
1317                 /* unlink and free the old ping info */
1318                 lnet_ping_md_unlink(old_pbuf, &old_ping_md);
1319                 lnet_ping_buffer_decref(old_pbuf);
1320         }
1321 }
1322
1323 static void
1324 lnet_ping_target_fini(void)
1325 {
1326         int             rc;
1327
1328         lnet_ping_md_unlink(the_lnet.ln_ping_target,
1329                             &the_lnet.ln_ping_target_md);
1330
1331         rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1332         LASSERT(rc == 0);
1333
1334         lnet_ping_target_destroy();
1335 }
1336
1337 static int
1338 lnet_ni_tq_credits(struct lnet_ni *ni)
1339 {
1340         int     credits;
1341
1342         LASSERT(ni->ni_ncpts >= 1);
1343
1344         if (ni->ni_ncpts == 1)
1345                 return ni->ni_net->net_tunables.lct_max_tx_credits;
1346
1347         credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1348         credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1349         credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1350
1351         return credits;
1352 }
1353
1354 static void
1355 lnet_ni_unlink_locked(struct lnet_ni *ni)
1356 {
1357         if (!list_empty(&ni->ni_cptlist)) {
1358                 list_del_init(&ni->ni_cptlist);
1359                 lnet_ni_decref_locked(ni, 0);
1360         }
1361
1362         /* move it to zombie list and nobody can find it anymore */
1363         LASSERT(!list_empty(&ni->ni_netlist));
1364         list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1365         lnet_ni_decref_locked(ni, 0);
1366 }
1367
1368 static void
1369 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1370 {
1371         int             i;
1372         int             islo;
1373         struct lnet_ni  *ni;
1374         struct list_head *zombie_list = &net->net_ni_zombie;
1375
1376         /*
1377          * Now wait for the NIs I just nuked to show up on the zombie
1378          * list and shut them down in guaranteed thread context
1379          */
1380         i = 2;
1381         while (!list_empty(zombie_list)) {
1382                 int     *ref;
1383                 int     j;
1384
1385                 ni = list_entry(zombie_list->next,
1386                                 struct lnet_ni, ni_netlist);
1387                 list_del_init(&ni->ni_netlist);
1388                 /* the ni should be in deleting state. If it's not it's
1389                  * a bug */
1390                 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
1391                 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1392                         if (*ref == 0)
1393                                 continue;
1394                         /* still busy, add it back to zombie list */
1395                         list_add(&ni->ni_netlist, zombie_list);
1396                         break;
1397                 }
1398
1399                 if (!list_empty(&ni->ni_netlist)) {
1400                         lnet_net_unlock(LNET_LOCK_EX);
1401                         ++i;
1402                         if ((i & (-i)) == i) {
1403                                 CDEBUG(D_WARNING,
1404                                        "Waiting for zombie LNI %s\n",
1405                                        libcfs_nid2str(ni->ni_nid));
1406                         }
1407                         set_current_state(TASK_UNINTERRUPTIBLE);
1408                         schedule_timeout(cfs_time_seconds(1));
1409                         lnet_net_lock(LNET_LOCK_EX);
1410                         continue;
1411                 }
1412
1413                 lnet_net_unlock(LNET_LOCK_EX);
1414
1415                 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1416
1417                 LASSERT(!in_interrupt());
1418                 (net->net_lnd->lnd_shutdown)(ni);
1419
1420                 if (!islo)
1421                         CDEBUG(D_LNI, "Removed LNI %s\n",
1422                               libcfs_nid2str(ni->ni_nid));
1423
1424                 lnet_ni_free(ni);
1425                 i = 2;
1426                 lnet_net_lock(LNET_LOCK_EX);
1427         }
1428 }
1429
1430 /* shutdown down the NI and release refcount */
1431 static void
1432 lnet_shutdown_lndni(struct lnet_ni *ni)
1433 {
1434         int i;
1435         struct lnet_net *net = ni->ni_net;
1436
1437         lnet_net_lock(LNET_LOCK_EX);
1438         ni->ni_state = LNET_NI_STATE_DELETING;
1439         lnet_ni_unlink_locked(ni);
1440         lnet_incr_dlc_seq();
1441         lnet_net_unlock(LNET_LOCK_EX);
1442
1443         /* clear messages for this NI on the lazy portal */
1444         for (i = 0; i < the_lnet.ln_nportals; i++)
1445                 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1446
1447         lnet_net_lock(LNET_LOCK_EX);
1448         lnet_clear_zombies_nis_locked(net);
1449         lnet_net_unlock(LNET_LOCK_EX);
1450 }
1451
1452 static void
1453 lnet_shutdown_lndnet(struct lnet_net *net)
1454 {
1455         struct lnet_ni *ni;
1456
1457         lnet_net_lock(LNET_LOCK_EX);
1458
1459         net->net_state = LNET_NET_STATE_DELETING;
1460
1461         list_del_init(&net->net_list);
1462
1463         while (!list_empty(&net->net_ni_list)) {
1464                 ni = list_entry(net->net_ni_list.next,
1465                                 struct lnet_ni, ni_netlist);
1466                 lnet_net_unlock(LNET_LOCK_EX);
1467                 lnet_shutdown_lndni(ni);
1468                 lnet_net_lock(LNET_LOCK_EX);
1469         }
1470
1471         lnet_net_unlock(LNET_LOCK_EX);
1472
1473         /* Do peer table cleanup for this net */
1474         lnet_peer_tables_cleanup(net);
1475
1476         lnet_net_lock(LNET_LOCK_EX);
1477         /*
1478          * decrement ref count on lnd only when the entire network goes
1479          * away
1480          */
1481         net->net_lnd->lnd_refcount--;
1482
1483         lnet_net_unlock(LNET_LOCK_EX);
1484
1485         lnet_net_free(net);
1486 }
1487
1488 static void
1489 lnet_shutdown_lndnets(void)
1490 {
1491         struct lnet_net *net;
1492
1493         /* NB called holding the global mutex */
1494
1495         /* All quiet on the API front */
1496         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
1497         LASSERT(the_lnet.ln_refcount == 0);
1498
1499         lnet_net_lock(LNET_LOCK_EX);
1500         the_lnet.ln_state = LNET_STATE_STOPPING;
1501
1502         while (!list_empty(&the_lnet.ln_nets)) {
1503                 /*
1504                  * move the nets to the zombie list to avoid them being
1505                  * picked up for new work. LONET is also included in the
1506                  * Nets that will be moved to the zombie list
1507                  */
1508                 net = list_entry(the_lnet.ln_nets.next,
1509                                  struct lnet_net, net_list);
1510                 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1511         }
1512
1513         /* Drop the cached loopback Net. */
1514         if (the_lnet.ln_loni != NULL) {
1515                 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1516                 the_lnet.ln_loni = NULL;
1517         }
1518         lnet_net_unlock(LNET_LOCK_EX);
1519
1520         /* iterate through the net zombie list and delete each net */
1521         while (!list_empty(&the_lnet.ln_net_zombie)) {
1522                 net = list_entry(the_lnet.ln_net_zombie.next,
1523                                  struct lnet_net, net_list);
1524                 lnet_shutdown_lndnet(net);
1525         }
1526
1527         lnet_net_lock(LNET_LOCK_EX);
1528         the_lnet.ln_state = LNET_STATE_SHUTDOWN;
1529         lnet_net_unlock(LNET_LOCK_EX);
1530 }
1531
1532 static int
1533 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1534 {
1535         int                     rc = -EINVAL;
1536         struct lnet_tx_queue    *tq;
1537         int                     i;
1538         struct lnet_net         *net = ni->ni_net;
1539
1540         mutex_lock(&the_lnet.ln_lnd_mutex);
1541
1542         if (tun) {
1543                 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1544                 ni->ni_lnd_tunables_set = true;
1545         }
1546
1547         rc = (net->net_lnd->lnd_startup)(ni);
1548
1549         mutex_unlock(&the_lnet.ln_lnd_mutex);
1550
1551         if (rc != 0) {
1552                 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1553                                    rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1554                 lnet_net_lock(LNET_LOCK_EX);
1555                 net->net_lnd->lnd_refcount--;
1556                 lnet_net_unlock(LNET_LOCK_EX);
1557                 goto failed0;
1558         }
1559
1560         ni->ni_state = LNET_NI_STATE_ACTIVE;
1561
1562         /* We keep a reference on the loopback net through the loopback NI */
1563         if (net->net_lnd->lnd_type == LOLND) {
1564                 lnet_ni_addref(ni);
1565                 LASSERT(the_lnet.ln_loni == NULL);
1566                 the_lnet.ln_loni = ni;
1567                 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
1568                 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
1569                 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
1570                 ni->ni_net->net_tunables.lct_peer_timeout = 0;
1571                 return 0;
1572         }
1573
1574         if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
1575             ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
1576                 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1577                                    libcfs_lnd2str(net->net_lnd->lnd_type),
1578                                    ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
1579                                         "" : "per-peer ");
1580                 /* shutdown the NI since if we get here then it must've already
1581                  * been started
1582                  */
1583                 lnet_shutdown_lndni(ni);
1584                 return -EINVAL;
1585         }
1586
1587         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1588                 tq->tq_credits_min =
1589                 tq->tq_credits_max =
1590                 tq->tq_credits = lnet_ni_tq_credits(ni);
1591         }
1592
1593         atomic_set(&ni->ni_tx_credits,
1594                    lnet_ni_tq_credits(ni) * ni->ni_ncpts);
1595
1596         CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1597                 libcfs_nid2str(ni->ni_nid),
1598                 ni->ni_net->net_tunables.lct_peer_tx_credits,
1599                 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1600                 ni->ni_net->net_tunables.lct_peer_rtr_credits,
1601                 ni->ni_net->net_tunables.lct_peer_timeout);
1602
1603         return 0;
1604 failed0:
1605         lnet_ni_free(ni);
1606         return rc;
1607 }
1608
1609 static int
1610 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
1611 {
1612         struct lnet_ni *ni;
1613         struct lnet_net *net_l = NULL;
1614         struct list_head        local_ni_list;
1615         int                     rc;
1616         int                     ni_count = 0;
1617         __u32                   lnd_type;
1618         struct lnet_lnd *lnd;
1619         int                     peer_timeout =
1620                 net->net_tunables.lct_peer_timeout;
1621         int                     maxtxcredits =
1622                 net->net_tunables.lct_max_tx_credits;
1623         int                     peerrtrcredits =
1624                 net->net_tunables.lct_peer_rtr_credits;
1625
1626         INIT_LIST_HEAD(&local_ni_list);
1627
1628         /*
1629          * make sure that this net is unique. If it isn't then
1630          * we are adding interfaces to an already existing network, and
1631          * 'net' is just a convenient way to pass in the list.
1632          * if it is unique we need to find the LND and load it if
1633          * necessary.
1634          */
1635         if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
1636                 lnd_type = LNET_NETTYP(net->net_id);
1637
1638                 LASSERT(libcfs_isknown_lnd(lnd_type));
1639
1640                 mutex_lock(&the_lnet.ln_lnd_mutex);
1641                 lnd = lnet_find_lnd_by_type(lnd_type);
1642
1643                 if (lnd == NULL) {
1644                         mutex_unlock(&the_lnet.ln_lnd_mutex);
1645                         rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1646                         mutex_lock(&the_lnet.ln_lnd_mutex);
1647
1648                         lnd = lnet_find_lnd_by_type(lnd_type);
1649                         if (lnd == NULL) {
1650                                 mutex_unlock(&the_lnet.ln_lnd_mutex);
1651                                 CERROR("Can't load LND %s, module %s, rc=%d\n",
1652                                 libcfs_lnd2str(lnd_type),
1653                                 libcfs_lnd2modname(lnd_type), rc);
1654 #ifndef HAVE_MODULE_LOADING_SUPPORT
1655                                 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1656                                                 "compiled with kernel module "
1657                                                 "loading support.");
1658 #endif
1659                                 rc = -EINVAL;
1660                                 goto failed0;
1661                         }
1662                 }
1663
1664                 lnet_net_lock(LNET_LOCK_EX);
1665                 lnd->lnd_refcount++;
1666                 lnet_net_unlock(LNET_LOCK_EX);
1667
1668                 net->net_lnd = lnd;
1669
1670                 mutex_unlock(&the_lnet.ln_lnd_mutex);
1671
1672                 net_l = net;
1673         }
1674
1675         /*
1676          * net_l: if the network being added is unique then net_l
1677          *        will point to that network
1678          *        if the network being added is not unique then
1679          *        net_l points to the existing network.
1680          *
1681          * When we enter the loop below, we'll pick NIs off he
1682          * network beign added and start them up, then add them to
1683          * a local ni list. Once we've successfully started all
1684          * the NIs then we join the local NI list (of started up
1685          * networks) with the net_l->net_ni_list, which should
1686          * point to the correct network to add the new ni list to
1687          *
1688          * If any of the new NIs fail to start up, then we want to
1689          * iterate through the local ni list, which should include
1690          * any NIs which were successfully started up, and shut
1691          * them down.
1692          *
1693          * After than we want to delete the network being added,
1694          * to avoid a memory leak.
1695          */
1696
1697         /*
1698          * When a network uses TCP bonding then all its interfaces
1699          * must be specified when the network is first defined: the
1700          * TCP bonding code doesn't allow for interfaces to be added
1701          * or removed.
1702          */
1703         if (net_l != net && net_l != NULL && use_tcp_bonding &&
1704             LNET_NETTYP(net_l->net_id) == SOCKLND) {
1705                 rc = -EINVAL;
1706                 goto failed0;
1707         }
1708
1709         while (!list_empty(&net->net_ni_added)) {
1710                 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
1711                                 ni_netlist);
1712                 list_del_init(&ni->ni_netlist);
1713
1714                 /* make sure that the the NI we're about to start
1715                  * up is actually unique. if it's not fail. */
1716                 if (!lnet_ni_unique_net(&net_l->net_ni_list,
1717                                         ni->ni_interfaces[0])) {
1718                         rc = -EINVAL;
1719                         goto failed1;
1720                 }
1721
1722                 /* adjust the pointer the parent network, just in case it
1723                  * the net is a duplicate */
1724                 ni->ni_net = net_l;
1725
1726                 rc = lnet_startup_lndni(ni, tun);
1727
1728                 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
1729                         ni->ni_net->net_lnd->lnd_query != NULL);
1730
1731                 if (rc < 0)
1732                         goto failed1;
1733
1734                 lnet_ni_addref(ni);
1735                 list_add_tail(&ni->ni_netlist, &local_ni_list);
1736
1737                 ni_count++;
1738         }
1739
1740         lnet_net_lock(LNET_LOCK_EX);
1741         list_splice_tail(&local_ni_list, &net_l->net_ni_list);
1742         lnet_incr_dlc_seq();
1743         lnet_net_unlock(LNET_LOCK_EX);
1744
1745         /* if the network is not unique then we don't want to keep
1746          * it around after we're done. Free it. Otherwise add that
1747          * net to the global the_lnet.ln_nets */
1748         if (net_l != net && net_l != NULL) {
1749                 /*
1750                  * TODO - note. currently the tunables can not be updated
1751                  * once added
1752                  */
1753                 lnet_net_free(net);
1754         } else {
1755                 net->net_state = LNET_NET_STATE_ACTIVE;
1756                 /*
1757                  * restore tunables after it has been overwitten by the
1758                  * lnd
1759                  */
1760                 if (peer_timeout != -1)
1761                         net->net_tunables.lct_peer_timeout = peer_timeout;
1762                 if (maxtxcredits != -1)
1763                         net->net_tunables.lct_max_tx_credits = maxtxcredits;
1764                 if (peerrtrcredits != -1)
1765                         net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
1766
1767                 lnet_net_lock(LNET_LOCK_EX);
1768                 list_add_tail(&net->net_list, &the_lnet.ln_nets);
1769                 lnet_net_unlock(LNET_LOCK_EX);
1770         }
1771
1772         return ni_count;
1773
1774 failed1:
1775         /*
1776          * shutdown the new NIs that are being started up
1777          * free the NET being started
1778          */
1779         while (!list_empty(&local_ni_list)) {
1780                 ni = list_entry(local_ni_list.next, struct lnet_ni,
1781                                 ni_netlist);
1782
1783                 lnet_shutdown_lndni(ni);
1784         }
1785
1786 failed0:
1787         lnet_net_free(net);
1788
1789         return rc;
1790 }
1791
1792 static int
1793 lnet_startup_lndnets(struct list_head *netlist)
1794 {
1795         struct lnet_net         *net;
1796         int                     rc;
1797         int                     ni_count = 0;
1798
1799         /*
1800          * Change to running state before bringing up the LNDs. This
1801          * allows lnet_shutdown_lndnets() to assert that we've passed
1802          * through here.
1803          */
1804         lnet_net_lock(LNET_LOCK_EX);
1805         the_lnet.ln_state = LNET_STATE_RUNNING;
1806         lnet_net_unlock(LNET_LOCK_EX);
1807
1808         while (!list_empty(netlist)) {
1809                 net = list_entry(netlist->next, struct lnet_net, net_list);
1810                 list_del_init(&net->net_list);
1811
1812                 rc = lnet_startup_lndnet(net, NULL);
1813
1814                 if (rc < 0)
1815                         goto failed;
1816
1817                 ni_count += rc;
1818         }
1819
1820         return ni_count;
1821 failed:
1822         lnet_shutdown_lndnets();
1823
1824         return rc;
1825 }
1826
1827 /**
1828  * Initialize LNet library.
1829  *
1830  * Automatically called at module loading time. Caller has to call
1831  * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
1832  * latter returned 0. It must be called exactly once.
1833  *
1834  * \retval 0 on success
1835  * \retval -ve on failures.
1836  */
1837 int lnet_lib_init(void)
1838 {
1839         int rc;
1840
1841         lnet_assert_wire_constants();
1842
1843         memset(&the_lnet, 0, sizeof(the_lnet));
1844
1845         /* refer to global cfs_cpt_table for now */
1846         the_lnet.ln_cpt_table   = cfs_cpt_table;
1847         the_lnet.ln_cpt_number  = cfs_cpt_number(cfs_cpt_table);
1848
1849         LASSERT(the_lnet.ln_cpt_number > 0);
1850         if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1851                 /* we are under risk of consuming all lh_cookie */
1852                 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1853                        "please change setting of CPT-table and retry\n",
1854                        the_lnet.ln_cpt_number, LNET_CPT_MAX);
1855                 return -E2BIG;
1856         }
1857
1858         while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1859                 the_lnet.ln_cpt_bits++;
1860
1861         rc = lnet_create_locks();
1862         if (rc != 0) {
1863                 CERROR("Can't create LNet global locks: %d\n", rc);
1864                 return rc;
1865         }
1866
1867         the_lnet.ln_refcount = 0;
1868         LNetInvalidateEQHandle(&the_lnet.ln_rc_eqh);
1869         INIT_LIST_HEAD(&the_lnet.ln_lnds);
1870         INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
1871         INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1872         INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1873
1874         /* The hash table size is the number of bits it takes to express the set
1875          * ln_num_routes, minus 1 (better to under estimate than over so we
1876          * don't waste memory). */
1877         if (rnet_htable_size <= 0)
1878                 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1879         else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1880                 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1881         the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1882                                            order_base_2(rnet_htable_size) - 1);
1883
1884         /* All LNDs apart from the LOLND are in separate modules.  They
1885          * register themselves when their module loads, and unregister
1886          * themselves when their module is unloaded. */
1887         lnet_register_lnd(&the_lolnd);
1888         return 0;
1889 }
1890
1891 /**
1892  * Finalize LNet library.
1893  *
1894  * \pre lnet_lib_init() called with success.
1895  * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1896  */
1897 void lnet_lib_exit(void)
1898 {
1899         LASSERT(the_lnet.ln_refcount == 0);
1900
1901         while (!list_empty(&the_lnet.ln_lnds))
1902                 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1903                                                struct lnet_lnd, lnd_list));
1904         lnet_destroy_locks();
1905 }
1906
1907 /**
1908  * Set LNet PID and start LNet interfaces, routing, and forwarding.
1909  *
1910  * Users must call this function at least once before any other functions.
1911  * For each successful call there must be a corresponding call to
1912  * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1913  * ignored.
1914  *
1915  * The PID used by LNet may be different from the one requested.
1916  * See LNetGetId().
1917  *
1918  * \param requested_pid PID requested by the caller.
1919  *
1920  * \return >= 0 on success, and < 0 error code on failures.
1921  */
1922 int
1923 LNetNIInit(lnet_pid_t requested_pid)
1924 {
1925         int                     im_a_router = 0;
1926         int                     rc;
1927         int                     ni_count;
1928         struct lnet_ping_buffer *pbuf;
1929         struct lnet_handle_md   ping_mdh;
1930         struct list_head        net_head;
1931         struct lnet_net         *net;
1932
1933         INIT_LIST_HEAD(&net_head);
1934
1935         mutex_lock(&the_lnet.ln_api_mutex);
1936
1937         CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1938
1939         if (the_lnet.ln_refcount > 0) {
1940                 rc = the_lnet.ln_refcount++;
1941                 mutex_unlock(&the_lnet.ln_api_mutex);
1942                 return rc;
1943         }
1944
1945         rc = lnet_prepare(requested_pid);
1946         if (rc != 0) {
1947                 mutex_unlock(&the_lnet.ln_api_mutex);
1948                 return rc;
1949         }
1950
1951         /* create a network for Loopback network */
1952         net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
1953         if (net == NULL) {
1954                 rc = -ENOMEM;
1955                 goto err_empty_list;
1956         }
1957
1958         /* Add in the loopback NI */
1959         if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
1960                 rc = -ENOMEM;
1961                 goto err_empty_list;
1962         }
1963
1964         /* If LNet is being initialized via DLC it is possible
1965          * that the user requests not to load module parameters (ones which
1966          * are supported by DLC) on initialization.  Therefore, make sure not
1967          * to load networks, routes and forwarding from module parameters
1968          * in this case.  On cleanup in case of failure only clean up
1969          * routes if it has been loaded */
1970         if (!the_lnet.ln_nis_from_mod_params) {
1971                 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
1972                                          use_tcp_bonding);
1973                 if (rc < 0)
1974                         goto err_empty_list;
1975         }
1976
1977         ni_count = lnet_startup_lndnets(&net_head);
1978         if (ni_count < 0) {
1979                 rc = ni_count;
1980                 goto err_empty_list;
1981         }
1982
1983         if (!the_lnet.ln_nis_from_mod_params) {
1984                 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1985                 if (rc != 0)
1986                         goto err_shutdown_lndnis;
1987
1988                 rc = lnet_check_routes();
1989                 if (rc != 0)
1990                         goto err_destroy_routes;
1991
1992                 rc = lnet_rtrpools_alloc(im_a_router);
1993                 if (rc != 0)
1994                         goto err_destroy_routes;
1995         }
1996
1997         rc = lnet_acceptor_start();
1998         if (rc != 0)
1999                 goto err_destroy_routes;
2000
2001         the_lnet.ln_refcount = 1;
2002         /* Now I may use my own API functions... */
2003
2004         rc = lnet_ping_target_setup(&pbuf, &ping_mdh, ni_count, true);
2005         if (rc != 0)
2006                 goto err_acceptor_stop;
2007
2008         lnet_ping_target_update(pbuf, ping_mdh);
2009
2010         rc = lnet_router_checker_start();
2011         if (rc != 0)
2012                 goto err_stop_ping;
2013
2014         rc = lnet_peer_discovery_start();
2015         if (rc != 0)
2016                 goto err_stop_router_checker;
2017
2018         lnet_fault_init();
2019         lnet_proc_init();
2020
2021         mutex_unlock(&the_lnet.ln_api_mutex);
2022
2023         return 0;
2024
2025 err_stop_router_checker:
2026         lnet_router_checker_stop();
2027 err_stop_ping:
2028         lnet_ping_target_fini();
2029 err_acceptor_stop:
2030         the_lnet.ln_refcount = 0;
2031         lnet_acceptor_stop();
2032 err_destroy_routes:
2033         if (!the_lnet.ln_nis_from_mod_params)
2034                 lnet_destroy_routes();
2035 err_shutdown_lndnis:
2036         lnet_shutdown_lndnets();
2037 err_empty_list:
2038         lnet_unprepare();
2039         LASSERT(rc < 0);
2040         mutex_unlock(&the_lnet.ln_api_mutex);
2041         while (!list_empty(&net_head)) {
2042                 struct lnet_net *net;
2043
2044                 net = list_entry(net_head.next, struct lnet_net, net_list);
2045                 list_del_init(&net->net_list);
2046                 lnet_net_free(net);
2047         }
2048         return rc;
2049 }
2050 EXPORT_SYMBOL(LNetNIInit);
2051
2052 /**
2053  * Stop LNet interfaces, routing, and forwarding.
2054  *
2055  * Users must call this function once for each successful call to LNetNIInit().
2056  * Once the LNetNIFini() operation has been started, the results of pending
2057  * API operations are undefined.
2058  *
2059  * \return always 0 for current implementation.
2060  */
2061 int
2062 LNetNIFini()
2063 {
2064         mutex_lock(&the_lnet.ln_api_mutex);
2065
2066         LASSERT(the_lnet.ln_refcount > 0);
2067
2068         if (the_lnet.ln_refcount != 1) {
2069                 the_lnet.ln_refcount--;
2070         } else {
2071                 LASSERT(!the_lnet.ln_niinit_self);
2072
2073                 lnet_fault_fini();
2074
2075                 lnet_proc_fini();
2076                 lnet_peer_discovery_stop();
2077                 lnet_router_checker_stop();
2078                 lnet_ping_target_fini();
2079
2080                 /* Teardown fns that use my own API functions BEFORE here */
2081                 the_lnet.ln_refcount = 0;
2082
2083                 lnet_acceptor_stop();
2084                 lnet_destroy_routes();
2085                 lnet_shutdown_lndnets();
2086                 lnet_unprepare();
2087         }
2088
2089         mutex_unlock(&the_lnet.ln_api_mutex);
2090         return 0;
2091 }
2092 EXPORT_SYMBOL(LNetNIFini);
2093
2094 /**
2095  * Grabs the ni data from the ni structure and fills the out
2096  * parameters
2097  *
2098  * \param[in] ni network        interface structure
2099  * \param[out] cfg_ni           NI config information
2100  * \param[out] tun              network and LND tunables
2101  */
2102 static void
2103 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
2104                    struct lnet_ioctl_config_lnd_tunables *tun,
2105                    struct lnet_ioctl_element_stats *stats,
2106                    __u32 tun_size)
2107 {
2108         size_t min_size = 0;
2109         int i;
2110
2111         if (!ni || !cfg_ni || !tun)
2112                 return;
2113
2114         if (ni->ni_interfaces[0] != NULL) {
2115                 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2116                         if (ni->ni_interfaces[i] != NULL) {
2117                                 strncpy(cfg_ni->lic_ni_intf[i],
2118                                         ni->ni_interfaces[i],
2119                                         sizeof(cfg_ni->lic_ni_intf[i]));
2120                         }
2121                 }
2122         }
2123
2124         cfg_ni->lic_nid = ni->ni_nid;
2125         if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2126                 cfg_ni->lic_status = LNET_NI_STATUS_UP;
2127         else
2128                 cfg_ni->lic_status = ni->ni_status->ns_status;
2129         cfg_ni->lic_tcp_bonding = use_tcp_bonding;
2130         cfg_ni->lic_dev_cpt = ni->ni_dev_cpt;
2131
2132         memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
2133
2134         if (stats) {
2135                 stats->iel_send_count = atomic_read(&ni->ni_stats.send_count);
2136                 stats->iel_recv_count = atomic_read(&ni->ni_stats.recv_count);
2137         }
2138
2139         /*
2140          * tun->lt_tun will always be present, but in order to be
2141          * backwards compatible, we need to deal with the cases when
2142          * tun->lt_tun is smaller than what the kernel has, because it
2143          * comes from an older version of a userspace program, then we'll
2144          * need to copy as much information as we have available space.
2145          */
2146         min_size = tun_size - sizeof(tun->lt_cmn);
2147         memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
2148
2149         /* copy over the cpts */
2150         if (ni->ni_ncpts == LNET_CPT_NUMBER &&
2151             ni->ni_cpts == NULL)  {
2152                 for (i = 0; i < ni->ni_ncpts; i++)
2153                         cfg_ni->lic_cpts[i] = i;
2154         } else {
2155                 for (i = 0;
2156                      ni->ni_cpts != NULL && i < ni->ni_ncpts &&
2157                      i < LNET_MAX_SHOW_NUM_CPT;
2158                      i++)
2159                         cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
2160         }
2161         cfg_ni->lic_ncpts = ni->ni_ncpts;
2162 }
2163
2164 /**
2165  * NOTE: This is a legacy function left in the code to be backwards
2166  * compatible with older userspace programs. It should eventually be
2167  * removed.
2168  *
2169  * Grabs the ni data from the ni structure and fills the out
2170  * parameters
2171  *
2172  * \param[in] ni network        interface structure
2173  * \param[out] config           config information
2174  */
2175 static void
2176 lnet_fill_ni_info_legacy(struct lnet_ni *ni,
2177                          struct lnet_ioctl_config_data *config)
2178 {
2179         struct lnet_ioctl_net_config *net_config;
2180         struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
2181         size_t min_size, tunable_size = 0;
2182         int i;
2183
2184         if (!ni || !config)
2185                 return;
2186
2187         net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
2188         if (!net_config)
2189                 return;
2190
2191         BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
2192                      ARRAY_SIZE(net_config->ni_interfaces));
2193
2194         for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
2195                 if (!ni->ni_interfaces[i])
2196                         break;
2197
2198                 strncpy(net_config->ni_interfaces[i],
2199                         ni->ni_interfaces[i],
2200                         sizeof(net_config->ni_interfaces[i]));
2201         }
2202
2203         config->cfg_nid = ni->ni_nid;
2204         config->cfg_config_u.cfg_net.net_peer_timeout =
2205                 ni->ni_net->net_tunables.lct_peer_timeout;
2206         config->cfg_config_u.cfg_net.net_max_tx_credits =
2207                 ni->ni_net->net_tunables.lct_max_tx_credits;
2208         config->cfg_config_u.cfg_net.net_peer_tx_credits =
2209                 ni->ni_net->net_tunables.lct_peer_tx_credits;
2210         config->cfg_config_u.cfg_net.net_peer_rtr_credits =
2211                 ni->ni_net->net_tunables.lct_peer_rtr_credits;
2212
2213         if (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND)
2214                 net_config->ni_status = LNET_NI_STATUS_UP;
2215         else
2216                 net_config->ni_status = ni->ni_status->ns_status;
2217
2218         if (ni->ni_cpts) {
2219                 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
2220
2221                 for (i = 0; i < num_cpts; i++)
2222                         net_config->ni_cpts[i] = ni->ni_cpts[i];
2223
2224                 config->cfg_ncpts = num_cpts;
2225         }
2226
2227         /*
2228          * See if user land tools sent in a newer and larger version
2229          * of struct lnet_tunables than what the kernel uses.
2230          */
2231         min_size = sizeof(*config) + sizeof(*net_config);
2232
2233         if (config->cfg_hdr.ioc_len > min_size)
2234                 tunable_size = config->cfg_hdr.ioc_len - min_size;
2235
2236         /* Don't copy too much data to user space */
2237         min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
2238         lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
2239
2240         if (lnd_cfg && min_size) {
2241                 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
2242                 config->cfg_config_u.cfg_net.net_interface_count = 1;
2243
2244                 /* Tell user land that kernel side has less data */
2245                 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
2246                         min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
2247                         config->cfg_hdr.ioc_len -= min_size;
2248                 }
2249         }
2250 }
2251
2252 struct lnet_ni *
2253 lnet_get_ni_idx_locked(int idx)
2254 {
2255         struct lnet_ni          *ni;
2256         struct lnet_net         *net;
2257
2258         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2259                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2260                         if (idx-- == 0)
2261                                 return ni;
2262                 }
2263         }
2264
2265         return NULL;
2266 }
2267
2268 struct lnet_ni *
2269 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2270 {
2271         struct lnet_ni          *ni;
2272         struct lnet_net         *net = mynet;
2273
2274         if (prev == NULL) {
2275                 if (net == NULL)
2276                         net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2277                                         net_list);
2278                 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2279                                 ni_netlist);
2280
2281                 return ni;
2282         }
2283
2284         if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2285                 /* if you reached the end of the ni list and the net is
2286                  * specified, then there are no more nis in that net */
2287                 if (net != NULL)
2288                         return NULL;
2289
2290                 /* we reached the end of this net ni list. move to the
2291                  * next net */
2292                 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2293                         /* no more nets and no more NIs. */
2294                         return NULL;
2295
2296                 /* get the next net */
2297                 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2298                                  net_list);
2299                 /* get the ni on it */
2300                 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2301                                 ni_netlist);
2302
2303                 return ni;
2304         }
2305
2306         /* there are more nis left */
2307         ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2308
2309         return ni;
2310 }
2311
2312 int
2313 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2314 {
2315         struct lnet_ni *ni;
2316         int cpt;
2317         int rc = -ENOENT;
2318         int idx = config->cfg_count;
2319
2320         cpt = lnet_net_lock_current();
2321
2322         ni = lnet_get_ni_idx_locked(idx);
2323
2324         if (ni != NULL) {
2325                 rc = 0;
2326                 lnet_ni_lock(ni);
2327                 lnet_fill_ni_info_legacy(ni, config);
2328                 lnet_ni_unlock(ni);
2329         }
2330
2331         lnet_net_unlock(cpt);
2332         return rc;
2333 }
2334
2335 int
2336 lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
2337                    struct lnet_ioctl_config_lnd_tunables *tun,
2338                    struct lnet_ioctl_element_stats *stats,
2339                    __u32 tun_size)
2340 {
2341         struct lnet_ni          *ni;
2342         int                     cpt;
2343         int                     rc = -ENOENT;
2344
2345         if (!cfg_ni || !tun || !stats)
2346                 return -EINVAL;
2347
2348         cpt = lnet_net_lock_current();
2349
2350         ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
2351
2352         if (ni) {
2353                 rc = 0;
2354                 lnet_ni_lock(ni);
2355                 lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
2356                 lnet_ni_unlock(ni);
2357         }
2358
2359         lnet_net_unlock(cpt);
2360         return rc;
2361 }
2362
2363 static int lnet_add_net_common(struct lnet_net *net,
2364                                struct lnet_ioctl_config_lnd_tunables *tun)
2365 {
2366         __u32                   net_id;
2367         struct lnet_ping_buffer *pbuf;
2368         struct lnet_handle_md   ping_mdh;
2369         int                     rc;
2370         struct lnet_remotenet *rnet;
2371         int                     net_ni_count;
2372         int                     num_acceptor_nets;
2373
2374         lnet_net_lock(LNET_LOCK_EX);
2375         rnet = lnet_find_rnet_locked(net->net_id);
2376         lnet_net_unlock(LNET_LOCK_EX);
2377         /*
2378          * make sure that the net added doesn't invalidate the current
2379          * configuration LNet is keeping
2380          */
2381         if (rnet) {
2382                 CERROR("Adding net %s will invalidate routing configuration\n",
2383                        libcfs_net2str(net->net_id));
2384                 lnet_net_free(net);
2385                 return -EUSERS;
2386         }
2387
2388         /*
2389          * make sure you calculate the correct number of slots in the ping
2390          * buffer. Since the ping info is a flattened list of all the NIs,
2391          * we should allocate enough slots to accomodate the number of NIs
2392          * which will be added.
2393          *
2394          * since ni hasn't been configured yet, use
2395          * lnet_get_net_ni_count_pre() which checks the net_ni_added list
2396          */
2397         net_ni_count = lnet_get_net_ni_count_pre(net);
2398
2399         rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2400                                     net_ni_count + lnet_get_ni_count(),
2401                                     false);
2402         if (rc < 0) {
2403                 lnet_net_free(net);
2404                 return rc;
2405         }
2406
2407         if (tun)
2408                 memcpy(&net->net_tunables,
2409                        &tun->lt_cmn, sizeof(net->net_tunables));
2410         else
2411                 memset(&net->net_tunables, -1, sizeof(net->net_tunables));
2412
2413         /*
2414          * before starting this network get a count of the current TCP
2415          * networks which require the acceptor thread running. If that
2416          * count is == 0 before we start up this network, then we'd want to
2417          * start up the acceptor thread after starting up this network
2418          */
2419         num_acceptor_nets = lnet_count_acceptor_nets();
2420
2421         net_id = net->net_id;
2422
2423         rc = lnet_startup_lndnet(net,
2424                                  (tun) ? &tun->lt_tun : NULL);
2425         if (rc < 0)
2426                 goto failed;
2427
2428         lnet_net_lock(LNET_LOCK_EX);
2429         net = lnet_get_net_locked(net_id);
2430         lnet_net_unlock(LNET_LOCK_EX);
2431
2432         LASSERT(net);
2433
2434         /*
2435          * Start the acceptor thread if this is the first network
2436          * being added that requires the thread.
2437          */
2438         if (net->net_lnd->lnd_accept && num_acceptor_nets == 0) {
2439                 rc = lnet_acceptor_start();
2440                 if (rc < 0) {
2441                         /* shutdown the net that we just started */
2442                         CERROR("Failed to start up acceptor thread\n");
2443                         lnet_shutdown_lndnet(net);
2444                         goto failed;
2445                 }
2446         }
2447
2448         lnet_net_lock(LNET_LOCK_EX);
2449         lnet_peer_net_added(net);
2450         lnet_net_unlock(LNET_LOCK_EX);
2451
2452         lnet_ping_target_update(pbuf, ping_mdh);
2453
2454         return 0;
2455
2456 failed:
2457         lnet_ping_md_unlink(pbuf, &ping_mdh);
2458         lnet_ping_buffer_decref(pbuf);
2459         return rc;
2460 }
2461
2462 static int lnet_handle_legacy_ip2nets(char *ip2nets,
2463                                       struct lnet_ioctl_config_lnd_tunables *tun)
2464 {
2465         struct lnet_net *net;
2466         char *nets;
2467         int rc;
2468         struct list_head net_head;
2469
2470         INIT_LIST_HEAD(&net_head);
2471
2472         rc = lnet_parse_ip2nets(&nets, ip2nets);
2473         if (rc < 0)
2474                 return rc;
2475
2476         rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2477         if (rc < 0)
2478                 return rc;
2479
2480         mutex_lock(&the_lnet.ln_api_mutex);
2481         while (!list_empty(&net_head)) {
2482                 net = list_entry(net_head.next, struct lnet_net, net_list);
2483                 list_del_init(&net->net_list);
2484                 rc = lnet_add_net_common(net, tun);
2485                 if (rc < 0)
2486                         goto out;
2487         }
2488
2489 out:
2490         mutex_unlock(&the_lnet.ln_api_mutex);
2491
2492         while (!list_empty(&net_head)) {
2493                 net = list_entry(net_head.next, struct lnet_net, net_list);
2494                 list_del_init(&net->net_list);
2495                 lnet_net_free(net);
2496         }
2497         return rc;
2498 }
2499
2500 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
2501 {
2502         struct lnet_net *net;
2503         struct lnet_ni *ni;
2504         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2505         int rc, i;
2506         __u32 net_id;
2507
2508         /* get the tunables if they are available */
2509         if (conf->lic_cfg_hdr.ioc_len >=
2510             sizeof(*conf) + sizeof(*tun))
2511                 tun = (struct lnet_ioctl_config_lnd_tunables *)
2512                         conf->lic_bulk;
2513
2514         /* handle legacy ip2nets from DLC */
2515         if (conf->lic_legacy_ip2nets[0] != '\0')
2516                 return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
2517                                                   tun);
2518
2519         net_id = LNET_NIDNET(conf->lic_nid);
2520
2521         net = lnet_net_alloc(net_id, NULL);
2522         if (!net)
2523                 return -ENOMEM;
2524
2525         for (i = 0; i < conf->lic_ncpts; i++) {
2526                 if (conf->lic_cpts[i] >= LNET_CPT_NUMBER)
2527                         return -EINVAL;
2528         }
2529
2530         ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
2531                                        conf->lic_ni_intf[0]);
2532         if (!ni)
2533                 return -ENOMEM;
2534
2535         mutex_lock(&the_lnet.ln_api_mutex);
2536
2537         rc = lnet_add_net_common(net, tun);
2538
2539         mutex_unlock(&the_lnet.ln_api_mutex);
2540
2541         return rc;
2542 }
2543
2544 int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
2545 {
2546         struct lnet_net  *net;
2547         struct lnet_ni *ni;
2548         __u32 net_id = LNET_NIDNET(conf->lic_nid);
2549         struct lnet_ping_buffer *pbuf;
2550         struct lnet_handle_md  ping_mdh;
2551         int               rc;
2552         int               net_count;
2553         __u32             addr;
2554
2555         /* don't allow userspace to shutdown the LOLND */
2556         if (LNET_NETTYP(net_id) == LOLND)
2557                 return -EINVAL;
2558
2559         mutex_lock(&the_lnet.ln_api_mutex);
2560
2561         lnet_net_lock(0);
2562
2563         net = lnet_get_net_locked(net_id);
2564         if (!net) {
2565                 CERROR("net %s not found\n",
2566                        libcfs_net2str(net_id));
2567                 rc = -ENOENT;
2568                 goto unlock_net;
2569         }
2570
2571         addr = LNET_NIDADDR(conf->lic_nid);
2572         if (addr == 0) {
2573                 /* remove the entire net */
2574                 net_count = lnet_get_net_ni_count_locked(net);
2575
2576                 lnet_net_unlock(0);
2577
2578                 /* create and link a new ping info, before removing the old one */
2579                 rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2580                                         lnet_get_ni_count() - net_count,
2581                                         false);
2582                 if (rc != 0)
2583                         goto unlock_api_mutex;
2584
2585                 lnet_shutdown_lndnet(net);
2586
2587                 if (lnet_count_acceptor_nets() == 0)
2588                         lnet_acceptor_stop();
2589
2590                 lnet_ping_target_update(pbuf, ping_mdh);
2591
2592                 goto unlock_api_mutex;
2593         }
2594
2595         ni = lnet_nid2ni_locked(conf->lic_nid, 0);
2596         if (!ni) {
2597                 CERROR("nid %s not found\n",
2598                        libcfs_nid2str(conf->lic_nid));
2599                 rc = -ENOENT;
2600                 goto unlock_net;
2601         }
2602
2603         net_count = lnet_get_net_ni_count_locked(net);
2604
2605         lnet_net_unlock(0);
2606
2607         /* create and link a new ping info, before removing the old one */
2608         rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2609                                   lnet_get_ni_count() - 1, false);
2610         if (rc != 0)
2611                 goto unlock_api_mutex;
2612
2613         lnet_shutdown_lndni(ni);
2614
2615         if (lnet_count_acceptor_nets() == 0)
2616                 lnet_acceptor_stop();
2617
2618         lnet_ping_target_update(pbuf, ping_mdh);
2619
2620         /* check if the net is empty and remove it if it is */
2621         if (net_count == 1)
2622                 lnet_shutdown_lndnet(net);
2623
2624         goto unlock_api_mutex;
2625
2626 unlock_net:
2627         lnet_net_unlock(0);
2628 unlock_api_mutex:
2629         mutex_unlock(&the_lnet.ln_api_mutex);
2630
2631         return rc;
2632 }
2633
2634 /*
2635  * lnet_dyn_add_net and lnet_dyn_del_net are now deprecated.
2636  * They are only expected to be called for unique networks.
2637  * That can be as a result of older DLC library
2638  * calls. Multi-Rail DLC and beyond no longer uses these APIs.
2639  */
2640 int
2641 lnet_dyn_add_net(struct lnet_ioctl_config_data *conf)
2642 {
2643         struct lnet_net         *net;
2644         struct list_head        net_head;
2645         int                     rc;
2646         struct lnet_ioctl_config_lnd_tunables tun;
2647         char *nets = conf->cfg_config_u.cfg_net.net_intf;
2648
2649         INIT_LIST_HEAD(&net_head);
2650
2651         /* Create a net/ni structures for the network string */
2652         rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2653         if (rc <= 0)
2654                 return rc == 0 ? -EINVAL : rc;
2655
2656         mutex_lock(&the_lnet.ln_api_mutex);
2657
2658         if (rc > 1) {
2659                 rc = -EINVAL; /* only add one network per call */
2660                 goto out_unlock_clean;
2661         }
2662
2663         net = list_entry(net_head.next, struct lnet_net, net_list);
2664         list_del_init(&net->net_list);
2665
2666         LASSERT(lnet_net_unique(net->net_id, &the_lnet.ln_nets, NULL));
2667
2668         memset(&tun, 0, sizeof(tun));
2669
2670         tun.lt_cmn.lct_peer_timeout =
2671           conf->cfg_config_u.cfg_net.net_peer_timeout;
2672         tun.lt_cmn.lct_peer_tx_credits =
2673           conf->cfg_config_u.cfg_net.net_peer_tx_credits;
2674         tun.lt_cmn.lct_peer_rtr_credits =
2675           conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
2676         tun.lt_cmn.lct_max_tx_credits =
2677           conf->cfg_config_u.cfg_net.net_max_tx_credits;
2678
2679         rc = lnet_add_net_common(net, &tun);
2680
2681 out_unlock_clean:
2682         mutex_unlock(&the_lnet.ln_api_mutex);
2683         while (!list_empty(&net_head)) {
2684                 /* net_head list is empty in success case */
2685                 net = list_entry(net_head.next, struct lnet_net, net_list);
2686                 list_del_init(&net->net_list);
2687                 lnet_net_free(net);
2688         }
2689         return rc;
2690 }
2691
2692 int
2693 lnet_dyn_del_net(__u32 net_id)
2694 {
2695         struct lnet_net  *net;
2696         struct lnet_ping_buffer *pbuf;
2697         struct lnet_handle_md ping_mdh;
2698         int               rc;
2699         int               net_ni_count;
2700
2701         /* don't allow userspace to shutdown the LOLND */
2702         if (LNET_NETTYP(net_id) == LOLND)
2703                 return -EINVAL;
2704
2705         mutex_lock(&the_lnet.ln_api_mutex);
2706
2707         lnet_net_lock(0);
2708
2709         net = lnet_get_net_locked(net_id);
2710         if (net == NULL) {
2711                 lnet_net_unlock(0);
2712                 rc = -EINVAL;
2713                 goto out;
2714         }
2715
2716         net_ni_count = lnet_get_net_ni_count_locked(net);
2717
2718         lnet_net_unlock(0);
2719
2720         /* create and link a new ping info, before removing the old one */
2721         rc = lnet_ping_target_setup(&pbuf, &ping_mdh,
2722                                     lnet_get_ni_count() - net_ni_count, false);
2723         if (rc != 0)
2724                 goto out;
2725
2726         lnet_shutdown_lndnet(net);
2727
2728         if (lnet_count_acceptor_nets() == 0)
2729                 lnet_acceptor_stop();
2730
2731         lnet_ping_target_update(pbuf, ping_mdh);
2732
2733 out:
2734         mutex_unlock(&the_lnet.ln_api_mutex);
2735
2736         return rc;
2737 }
2738
2739 void lnet_incr_dlc_seq(void)
2740 {
2741         atomic_inc(&lnet_dlc_seq_no);
2742 }
2743
2744 __u32 lnet_get_dlc_seq_locked(void)
2745 {
2746         return atomic_read(&lnet_dlc_seq_no);
2747 }
2748
2749 /**
2750  * LNet ioctl handler.
2751  *
2752  */
2753 int
2754 LNetCtl(unsigned int cmd, void *arg)
2755 {
2756         struct libcfs_ioctl_data *data = arg;
2757         struct lnet_ioctl_config_data *config;
2758         struct lnet_process_id    id = {0};
2759         struct lnet_ni           *ni;
2760         int                       rc;
2761
2762         BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
2763                      sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
2764
2765         switch (cmd) {
2766         case IOC_LIBCFS_GET_NI:
2767                 rc = LNetGetId(data->ioc_count, &id);
2768                 data->ioc_nid = id.nid;
2769                 return rc;
2770
2771         case IOC_LIBCFS_FAIL_NID:
2772                 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
2773
2774         case IOC_LIBCFS_ADD_ROUTE:
2775                 config = arg;
2776
2777                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2778                         return -EINVAL;
2779
2780                 mutex_lock(&the_lnet.ln_api_mutex);
2781                 rc = lnet_add_route(config->cfg_net,
2782                                     config->cfg_config_u.cfg_route.rtr_hop,
2783                                     config->cfg_nid,
2784                                     config->cfg_config_u.cfg_route.
2785                                         rtr_priority);
2786                 if (rc == 0) {
2787                         rc = lnet_check_routes();
2788                         if (rc != 0)
2789                                 lnet_del_route(config->cfg_net,
2790                                                config->cfg_nid);
2791                 }
2792                 mutex_unlock(&the_lnet.ln_api_mutex);
2793                 return rc;
2794
2795         case IOC_LIBCFS_DEL_ROUTE:
2796                 config = arg;
2797
2798                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2799                         return -EINVAL;
2800
2801                 mutex_lock(&the_lnet.ln_api_mutex);
2802                 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
2803                 mutex_unlock(&the_lnet.ln_api_mutex);
2804                 return rc;
2805
2806         case IOC_LIBCFS_GET_ROUTE:
2807                 config = arg;
2808
2809                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2810                         return -EINVAL;
2811
2812                 mutex_lock(&the_lnet.ln_api_mutex);
2813                 rc = lnet_get_route(config->cfg_count,
2814                                     &config->cfg_net,
2815                                     &config->cfg_config_u.cfg_route.rtr_hop,
2816                                     &config->cfg_nid,
2817                                     &config->cfg_config_u.cfg_route.rtr_flags,
2818                                     &config->cfg_config_u.cfg_route.
2819                                         rtr_priority);
2820                 mutex_unlock(&the_lnet.ln_api_mutex);
2821                 return rc;
2822
2823         case IOC_LIBCFS_GET_LOCAL_NI: {
2824                 struct lnet_ioctl_config_ni *cfg_ni;
2825                 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
2826                 struct lnet_ioctl_element_stats *stats;
2827                 __u32 tun_size;
2828
2829                 cfg_ni = arg;
2830                 /* get the tunables if they are available */
2831                 if (cfg_ni->lic_cfg_hdr.ioc_len <
2832                     sizeof(*cfg_ni) + sizeof(*stats)+ sizeof(*tun))
2833                         return -EINVAL;
2834
2835                 stats = (struct lnet_ioctl_element_stats *)
2836                         cfg_ni->lic_bulk;
2837                 tun = (struct lnet_ioctl_config_lnd_tunables *)
2838                                 (cfg_ni->lic_bulk + sizeof(*stats));
2839
2840                 tun_size = cfg_ni->lic_cfg_hdr.ioc_len - sizeof(*cfg_ni) -
2841                         sizeof(*stats);
2842
2843                 mutex_lock(&the_lnet.ln_api_mutex);
2844                 rc = lnet_get_ni_config(cfg_ni, tun, stats, tun_size);
2845                 mutex_unlock(&the_lnet.ln_api_mutex);
2846                 return rc;
2847         }
2848
2849         case IOC_LIBCFS_GET_NET: {
2850                 size_t total = sizeof(*config) +
2851                                sizeof(struct lnet_ioctl_net_config);
2852                 config = arg;
2853
2854                 if (config->cfg_hdr.ioc_len < total)
2855                         return -EINVAL;
2856
2857                 mutex_lock(&the_lnet.ln_api_mutex);
2858                 rc = lnet_get_net_config(config);
2859                 mutex_unlock(&the_lnet.ln_api_mutex);
2860                 return rc;
2861         }
2862
2863         case IOC_LIBCFS_GET_LNET_STATS:
2864         {
2865                 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
2866
2867                 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
2868                         return -EINVAL;
2869
2870                 mutex_lock(&the_lnet.ln_api_mutex);
2871                 lnet_counters_get(&lnet_stats->st_cntrs);
2872                 mutex_unlock(&the_lnet.ln_api_mutex);
2873                 return 0;
2874         }
2875
2876         case IOC_LIBCFS_CONFIG_RTR:
2877                 config = arg;
2878
2879                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2880                         return -EINVAL;
2881
2882                 mutex_lock(&the_lnet.ln_api_mutex);
2883                 if (config->cfg_config_u.cfg_buffers.buf_enable) {
2884                         rc = lnet_rtrpools_enable();
2885                         mutex_unlock(&the_lnet.ln_api_mutex);
2886                         return rc;
2887                 }
2888                 lnet_rtrpools_disable();
2889                 mutex_unlock(&the_lnet.ln_api_mutex);
2890                 return 0;
2891
2892         case IOC_LIBCFS_ADD_BUF:
2893                 config = arg;
2894
2895                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2896                         return -EINVAL;
2897
2898                 mutex_lock(&the_lnet.ln_api_mutex);
2899                 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
2900                                                 buf_tiny,
2901                                           config->cfg_config_u.cfg_buffers.
2902                                                 buf_small,
2903                                           config->cfg_config_u.cfg_buffers.
2904                                                 buf_large);
2905                 mutex_unlock(&the_lnet.ln_api_mutex);
2906                 return rc;
2907
2908         case IOC_LIBCFS_SET_NUMA_RANGE: {
2909                 struct lnet_ioctl_set_value *numa;
2910                 numa = arg;
2911                 if (numa->sv_hdr.ioc_len != sizeof(*numa))
2912                         return -EINVAL;
2913                 lnet_net_lock(LNET_LOCK_EX);
2914                 lnet_numa_range = numa->sv_value;
2915                 lnet_net_unlock(LNET_LOCK_EX);
2916                 return 0;
2917         }
2918
2919         case IOC_LIBCFS_GET_NUMA_RANGE: {
2920                 struct lnet_ioctl_set_value *numa;
2921                 numa = arg;
2922                 if (numa->sv_hdr.ioc_len != sizeof(*numa))
2923                         return -EINVAL;
2924                 numa->sv_value = lnet_numa_range;
2925                 return 0;
2926         }
2927
2928         case IOC_LIBCFS_GET_BUF: {
2929                 struct lnet_ioctl_pool_cfg *pool_cfg;
2930                 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2931
2932                 config = arg;
2933
2934                 if (config->cfg_hdr.ioc_len < total)
2935                         return -EINVAL;
2936
2937                 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2938
2939                 mutex_lock(&the_lnet.ln_api_mutex);
2940                 rc = lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2941                 mutex_unlock(&the_lnet.ln_api_mutex);
2942                 return rc;
2943         }
2944
2945         case IOC_LIBCFS_ADD_PEER_NI: {
2946                 struct lnet_ioctl_peer_cfg *cfg = arg;
2947
2948                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
2949                         return -EINVAL;
2950
2951                 mutex_lock(&the_lnet.ln_api_mutex);
2952                 rc = lnet_add_peer_ni(cfg->prcfg_prim_nid,
2953                                       cfg->prcfg_cfg_nid,
2954                                       cfg->prcfg_mr);
2955                 mutex_unlock(&the_lnet.ln_api_mutex);
2956                 return rc;
2957         }
2958
2959         case IOC_LIBCFS_DEL_PEER_NI: {
2960                 struct lnet_ioctl_peer_cfg *cfg = arg;
2961
2962                 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
2963                         return -EINVAL;
2964
2965                 mutex_lock(&the_lnet.ln_api_mutex);
2966                 rc = lnet_del_peer_ni(cfg->prcfg_prim_nid,
2967                                       cfg->prcfg_cfg_nid);
2968                 mutex_unlock(&the_lnet.ln_api_mutex);
2969                 return rc;
2970         }
2971
2972         case IOC_LIBCFS_GET_PEER_INFO: {
2973                 struct lnet_ioctl_peer *peer_info = arg;
2974
2975                 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2976                         return -EINVAL;
2977
2978                 mutex_lock(&the_lnet.ln_api_mutex);
2979                 rc = lnet_get_peer_ni_info(
2980                    peer_info->pr_count,
2981                    &peer_info->pr_nid,
2982                    peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2983                    &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2984                    &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2985                    &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2986                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2987                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2988                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_tx_credits,
2989                    &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2990                 mutex_unlock(&the_lnet.ln_api_mutex);
2991                 return rc;
2992         }
2993
2994         case IOC_LIBCFS_GET_PEER_NI: {
2995                 struct lnet_ioctl_peer_cfg *cfg = arg;
2996                 struct lnet_peer_ni_credit_info __user *lpni_cri;
2997                 struct lnet_ioctl_element_stats __user *lpni_stats;
2998                 size_t usr_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2999
3000                 if ((cfg->prcfg_hdr.ioc_len != sizeof(*cfg)) ||
3001                     (cfg->prcfg_size != usr_size))
3002                         return -EINVAL;
3003
3004                 lpni_cri = cfg->prcfg_bulk;
3005                 lpni_stats = cfg->prcfg_bulk + sizeof(*lpni_cri);
3006
3007                 mutex_lock(&the_lnet.ln_api_mutex);
3008                 rc = lnet_get_peer_info(cfg->prcfg_count, &cfg->prcfg_prim_nid,
3009                                         &cfg->prcfg_cfg_nid, &cfg->prcfg_mr,
3010                                         lpni_cri, lpni_stats);
3011                 mutex_unlock(&the_lnet.ln_api_mutex);
3012                 return rc;
3013         }
3014
3015         case IOC_LIBCFS_NOTIFY_ROUTER: {
3016                 unsigned long jiffies_passed;
3017
3018                 jiffies_passed = ktime_get_real_seconds() - data->ioc_u64[0];
3019                 jiffies_passed = cfs_time_seconds(jiffies_passed);
3020
3021                 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
3022                                    jiffies - jiffies_passed);
3023         }
3024
3025         case IOC_LIBCFS_LNET_DIST:
3026                 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
3027                 if (rc < 0 && rc != -EHOSTUNREACH)
3028                         return rc;
3029
3030                 data->ioc_u32[0] = rc;
3031                 return 0;
3032
3033         case IOC_LIBCFS_TESTPROTOCOMPAT:
3034                 lnet_net_lock(LNET_LOCK_EX);
3035                 the_lnet.ln_testprotocompat = data->ioc_flags;
3036                 lnet_net_unlock(LNET_LOCK_EX);
3037                 return 0;
3038
3039         case IOC_LIBCFS_LNET_FAULT:
3040                 return lnet_fault_ctl(data->ioc_flags, data);
3041
3042         case IOC_LIBCFS_PING: {
3043                 signed long timeout;
3044
3045                 id.nid = data->ioc_nid;
3046                 id.pid = data->ioc_u32[0];
3047
3048                 /* Don't block longer than 2 minutes */
3049                 if (data->ioc_u32[1] > 120 * MSEC_PER_SEC)
3050                         return -EINVAL;
3051
3052                 /* If timestamp is negative then disable timeout */
3053                 if ((s32)data->ioc_u32[1] < 0)
3054                         timeout = MAX_SCHEDULE_TIMEOUT;
3055                 else
3056                         timeout = msecs_to_jiffies(data->ioc_u32[1]);
3057
3058                 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
3059                                data->ioc_plen1 / sizeof(struct lnet_process_id));
3060                 if (rc < 0)
3061                         return rc;
3062                 data->ioc_count = rc;
3063                 return 0;
3064         }
3065
3066         default:
3067                 ni = lnet_net2ni_addref(data->ioc_net);
3068                 if (ni == NULL)
3069                         return -EINVAL;
3070
3071                 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
3072                         rc = -EINVAL;
3073                 else
3074                         rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
3075
3076                 lnet_ni_decref(ni);
3077                 return rc;
3078         }
3079         /* not reached */
3080 }
3081 EXPORT_SYMBOL(LNetCtl);
3082
3083 void LNetDebugPeer(struct lnet_process_id id)
3084 {
3085         lnet_debug_peer(id.nid);
3086 }
3087 EXPORT_SYMBOL(LNetDebugPeer);
3088
3089 /**
3090  * Determine if the specified peer \a nid is on the local node.
3091  *
3092  * \param nid   peer nid to check
3093  *
3094  * \retval true         If peer NID is on the local node.
3095  * \retval false        If peer NID is not on the local node.
3096  */
3097 bool LNetIsPeerLocal(lnet_nid_t nid)
3098 {
3099         struct lnet_net *net;
3100         struct lnet_ni *ni;
3101         int cpt;
3102
3103         cpt = lnet_net_lock_current();
3104         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3105                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3106                         if (ni->ni_nid == nid) {
3107                                 lnet_net_unlock(cpt);
3108                                 return true;
3109                         }
3110                 }
3111         }
3112         lnet_net_unlock(cpt);
3113
3114         return false;
3115 }
3116 EXPORT_SYMBOL(LNetIsPeerLocal);
3117
3118 /**
3119  * Retrieve the struct lnet_process_id ID of LNet interface at \a index.
3120  * Note that all interfaces share a same PID, as requested by LNetNIInit().
3121  *
3122  * \param index Index of the interface to look up.
3123  * \param id On successful return, this location will hold the
3124  * struct lnet_process_id ID of the interface.
3125  *
3126  * \retval 0 If an interface exists at \a index.
3127  * \retval -ENOENT If no interface has been found.
3128  */
3129 int
3130 LNetGetId(unsigned int index, struct lnet_process_id *id)
3131 {
3132         struct lnet_ni   *ni;
3133         struct lnet_net  *net;
3134         int               cpt;
3135         int               rc = -ENOENT;
3136
3137         LASSERT(the_lnet.ln_refcount > 0);
3138
3139         cpt = lnet_net_lock_current();
3140
3141         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
3142                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
3143                         if (index-- != 0)
3144                                 continue;
3145
3146                         id->nid = ni->ni_nid;
3147                         id->pid = the_lnet.ln_pid;
3148                         rc = 0;
3149                         break;
3150                 }
3151         }
3152
3153         lnet_net_unlock(cpt);
3154         return rc;
3155 }
3156 EXPORT_SYMBOL(LNetGetId);
3157
3158 static int lnet_ping(struct lnet_process_id id, signed long timeout,
3159                      struct lnet_process_id __user *ids, int n_ids)
3160 {
3161         struct lnet_handle_eq eqh;
3162         struct lnet_handle_md mdh;
3163         struct lnet_event event;
3164         struct lnet_md md = { NULL };
3165         int which;
3166         int unlinked = 0;
3167         int replied = 0;
3168         const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
3169         struct lnet_ping_buffer *pbuf;
3170         struct lnet_process_id tmpid;
3171         int i;
3172         int nob;
3173         int rc;
3174         int rc2;
3175         sigset_t blocked;
3176
3177         /* n_ids limit is arbitrary */
3178         if (n_ids <= 0 || n_ids > lnet_interfaces_max || id.nid == LNET_NID_ANY)
3179                 return -EINVAL;
3180
3181         if (id.pid == LNET_PID_ANY)
3182                 id.pid = LNET_PID_LUSTRE;
3183
3184         pbuf = lnet_ping_buffer_alloc(n_ids, GFP_NOFS);
3185         if (!pbuf)
3186                 return -ENOMEM;
3187
3188         /* NB 2 events max (including any unlink event) */
3189         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
3190         if (rc != 0) {
3191                 CERROR("Can't allocate EQ: %d\n", rc);
3192                 goto fail_ping_buffer_decref;
3193         }
3194
3195         /* initialize md content */
3196         md.start     = &pbuf->pb_info;
3197         md.length    = LNET_PING_INFO_SIZE(n_ids);
3198         md.threshold = 2; /*GET/REPLY*/
3199         md.max_size  = 0;
3200         md.options   = LNET_MD_TRUNCATE;
3201         md.user_ptr  = NULL;
3202         md.eq_handle = eqh;
3203
3204         rc = LNetMDBind(md, LNET_UNLINK, &mdh);
3205         if (rc != 0) {
3206                 CERROR("Can't bind MD: %d\n", rc);
3207                 goto fail_free_eq;
3208         }
3209
3210         rc = LNetGet(LNET_NID_ANY, mdh, id,
3211                      LNET_RESERVED_PORTAL,
3212                      LNET_PROTO_PING_MATCHBITS, 0);
3213
3214         if (rc != 0) {
3215                 /* Don't CERROR; this could be deliberate! */
3216
3217                 rc2 = LNetMDUnlink(mdh);
3218                 LASSERT(rc2 == 0);
3219
3220                 /* NB must wait for the UNLINK event below... */
3221                 unlinked = 1;
3222                 timeout = a_long_time;
3223         }
3224
3225         do {
3226                 /* MUST block for unlink to complete */
3227                 if (unlinked)
3228                         blocked = cfs_block_allsigs();
3229
3230                 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
3231
3232                 if (unlinked)
3233                         cfs_restore_sigs(blocked);
3234
3235                 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
3236                        (rc2 <= 0) ? -1 : event.type,
3237                        (rc2 <= 0) ? -1 : event.status,
3238                        (rc2 > 0 && event.unlinked) ? " unlinked" : "");
3239
3240                 LASSERT(rc2 != -EOVERFLOW);     /* can't miss anything */
3241
3242                 if (rc2 <= 0 || event.status != 0) {
3243                         /* timeout or error */
3244                         if (!replied && rc == 0)
3245                                 rc = (rc2 < 0) ? rc2 :
3246                                      (rc2 == 0) ? -ETIMEDOUT :
3247                                      event.status;
3248
3249                         if (!unlinked) {
3250                                 /* Ensure completion in finite time... */
3251                                 LNetMDUnlink(mdh);
3252                                 /* No assertion (racing with network) */
3253                                 unlinked = 1;
3254                                 timeout = a_long_time;
3255                         } else if (rc2 == 0) {
3256                                 /* timed out waiting for unlink */
3257                                 CWARN("ping %s: late network completion\n",
3258                                       libcfs_id2str(id));
3259                         }
3260                 } else if (event.type == LNET_EVENT_REPLY) {
3261                         replied = 1;
3262                         rc = event.mlength;
3263                 }
3264
3265         } while (rc2 <= 0 || !event.unlinked);
3266
3267         if (!replied) {
3268                 if (rc >= 0)
3269                         CWARN("%s: Unexpected rc >= 0 but no reply!\n",
3270                               libcfs_id2str(id));
3271                 rc = -EIO;
3272                 goto fail_free_eq;
3273         }
3274
3275         nob = rc;
3276         LASSERT(nob >= 0 && nob <= LNET_PING_INFO_SIZE(n_ids));
3277
3278         rc = -EPROTO;                           /* if I can't parse... */
3279
3280         if (nob < 8) {
3281                 /* can't check magic/version */
3282                 CERROR("%s: ping info too short %d\n",
3283                        libcfs_id2str(id), nob);
3284                 goto fail_free_eq;
3285         }
3286
3287         if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
3288                 lnet_swap_pinginfo(pbuf);
3289         } else if (pbuf->pb_info.pi_magic != LNET_PROTO_PING_MAGIC) {
3290                 CERROR("%s: Unexpected magic %08x\n",
3291                        libcfs_id2str(id), pbuf->pb_info.pi_magic);
3292                 goto fail_free_eq;
3293         }
3294
3295         if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
3296                 CERROR("%s: ping w/o NI status: 0x%x\n",
3297                        libcfs_id2str(id), pbuf->pb_info.pi_features);
3298                 goto fail_free_eq;
3299         }
3300
3301         if (nob < LNET_PING_INFO_SIZE(0)) {
3302                 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
3303                        nob, (int)LNET_PING_INFO_SIZE(0));
3304                 goto fail_free_eq;
3305         }
3306
3307         if (pbuf->pb_info.pi_nnis < n_ids)
3308                 n_ids = pbuf->pb_info.pi_nnis;
3309
3310         if (nob < LNET_PING_INFO_SIZE(n_ids)) {
3311                 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
3312                        nob, (int)LNET_PING_INFO_SIZE(n_ids));
3313                 goto fail_free_eq;
3314         }
3315
3316         rc = -EFAULT;                           /* If I SEGV... */
3317
3318         memset(&tmpid, 0, sizeof(tmpid));
3319         for (i = 0; i < n_ids; i++) {
3320                 tmpid.pid = pbuf->pb_info.pi_pid;
3321                 tmpid.nid = pbuf->pb_info.pi_ni[i].ns_nid;
3322                 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
3323                         goto fail_free_eq;
3324         }
3325         rc = pbuf->pb_info.pi_nnis;
3326
3327  fail_free_eq:
3328         rc2 = LNetEQFree(eqh);
3329         if (rc2 != 0)
3330                 CERROR("rc2 %d\n", rc2);
3331         LASSERT(rc2 == 0);
3332
3333  fail_ping_buffer_decref:
3334         lnet_ping_buffer_decref(pbuf);
3335         return rc;
3336 }