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