• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/log2.h>
39 #include <linux/ktime.h>
40 
41 #include "../../include/linux/lnet/lib-lnet.h"
42 
43 #define D_LNI D_CONSOLE
44 
45 lnet_t the_lnet;			   /* THE state of the network */
46 EXPORT_SYMBOL(the_lnet);
47 
48 static char *ip2nets = "";
49 module_param(ip2nets, charp, 0444);
50 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
51 
52 static char *networks = "";
53 module_param(networks, charp, 0444);
54 MODULE_PARM_DESC(networks, "local networks");
55 
56 static char *routes = "";
57 module_param(routes, charp, 0444);
58 MODULE_PARM_DESC(routes, "routes to non-local networks");
59 
60 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
61 module_param(rnet_htable_size, int, 0444);
62 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
63 
64 static char *
lnet_get_routes(void)65 lnet_get_routes(void)
66 {
67 	return routes;
68 }
69 
70 static char *
lnet_get_networks(void)71 lnet_get_networks(void)
72 {
73 	char *nets;
74 	int rc;
75 
76 	if (*networks != 0 && *ip2nets != 0) {
77 		LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or 'ip2nets' but not both at once\n");
78 		return NULL;
79 	}
80 
81 	if (*ip2nets != 0) {
82 		rc = lnet_parse_ip2nets(&nets, ip2nets);
83 		return (rc == 0) ? nets : NULL;
84 	}
85 
86 	if (*networks != 0)
87 		return networks;
88 
89 	return "tcp";
90 }
91 
92 static void
lnet_init_locks(void)93 lnet_init_locks(void)
94 {
95 	spin_lock_init(&the_lnet.ln_eq_wait_lock);
96 	init_waitqueue_head(&the_lnet.ln_eq_waitq);
97 	mutex_init(&the_lnet.ln_lnd_mutex);
98 	mutex_init(&the_lnet.ln_api_mutex);
99 }
100 
101 static int
lnet_create_remote_nets_table(void)102 lnet_create_remote_nets_table(void)
103 {
104 	int i;
105 	struct list_head *hash;
106 
107 	LASSERT(the_lnet.ln_remote_nets_hash == NULL);
108 	LASSERT(the_lnet.ln_remote_nets_hbits > 0);
109 	LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
110 	if (hash == NULL) {
111 		CERROR("Failed to create remote nets hash table\n");
112 		return -ENOMEM;
113 	}
114 
115 	for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
116 		INIT_LIST_HEAD(&hash[i]);
117 	the_lnet.ln_remote_nets_hash = hash;
118 	return 0;
119 }
120 
121 static void
lnet_destroy_remote_nets_table(void)122 lnet_destroy_remote_nets_table(void)
123 {
124 	int i;
125 
126 	if (the_lnet.ln_remote_nets_hash == NULL)
127 		return;
128 
129 	for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
130 		LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
131 
132 	LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
133 		    LNET_REMOTE_NETS_HASH_SIZE *
134 		    sizeof(the_lnet.ln_remote_nets_hash[0]));
135 	the_lnet.ln_remote_nets_hash = NULL;
136 }
137 
138 static void
lnet_destroy_locks(void)139 lnet_destroy_locks(void)
140 {
141 	if (the_lnet.ln_res_lock != NULL) {
142 		cfs_percpt_lock_free(the_lnet.ln_res_lock);
143 		the_lnet.ln_res_lock = NULL;
144 	}
145 
146 	if (the_lnet.ln_net_lock != NULL) {
147 		cfs_percpt_lock_free(the_lnet.ln_net_lock);
148 		the_lnet.ln_net_lock = NULL;
149 	}
150 }
151 
152 static int
lnet_create_locks(void)153 lnet_create_locks(void)
154 {
155 	lnet_init_locks();
156 
157 	the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
158 	if (the_lnet.ln_res_lock == NULL)
159 		goto failed;
160 
161 	the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
162 	if (the_lnet.ln_net_lock == NULL)
163 		goto failed;
164 
165 	return 0;
166 
167  failed:
168 	lnet_destroy_locks();
169 	return -ENOMEM;
170 }
171 
lnet_assert_wire_constants(void)172 static void lnet_assert_wire_constants(void)
173 {
174 	/* Wire protocol assertions generated by 'wirecheck'
175 	 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
176 	 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
177 	 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
178 
179 	/* Constants... */
180 	CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
181 	CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
182 	CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
183 	CLASSERT(LNET_MSG_ACK == 0);
184 	CLASSERT(LNET_MSG_PUT == 1);
185 	CLASSERT(LNET_MSG_GET == 2);
186 	CLASSERT(LNET_MSG_REPLY == 3);
187 	CLASSERT(LNET_MSG_HELLO == 4);
188 
189 	/* Checks for struct ptl_handle_wire_t */
190 	CLASSERT((int)sizeof(lnet_handle_wire_t) == 16);
191 	CLASSERT((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
192 	CLASSERT((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
193 	CLASSERT((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
194 	CLASSERT((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
195 
196 	/* Checks for struct lnet_magicversion_t */
197 	CLASSERT((int)sizeof(lnet_magicversion_t) == 8);
198 	CLASSERT((int)offsetof(lnet_magicversion_t, magic) == 0);
199 	CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
200 	CLASSERT((int)offsetof(lnet_magicversion_t, version_major) == 4);
201 	CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
202 	CLASSERT((int)offsetof(lnet_magicversion_t, version_minor) == 6);
203 	CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
204 
205 	/* Checks for struct lnet_hdr_t */
206 	CLASSERT((int)sizeof(lnet_hdr_t) == 72);
207 	CLASSERT((int)offsetof(lnet_hdr_t, dest_nid) == 0);
208 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
209 	CLASSERT((int)offsetof(lnet_hdr_t, src_nid) == 8);
210 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
211 	CLASSERT((int)offsetof(lnet_hdr_t, dest_pid) == 16);
212 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
213 	CLASSERT((int)offsetof(lnet_hdr_t, src_pid) == 20);
214 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
215 	CLASSERT((int)offsetof(lnet_hdr_t, type) == 24);
216 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
217 	CLASSERT((int)offsetof(lnet_hdr_t, payload_length) == 28);
218 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
219 	CLASSERT((int)offsetof(lnet_hdr_t, msg) == 32);
220 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
221 
222 	/* Ack */
223 	CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
224 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
225 	CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
226 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
227 	CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
228 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
229 
230 	/* Put */
231 	CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
232 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
233 	CLASSERT((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
234 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
235 	CLASSERT((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
236 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
237 	CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
238 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
239 	CLASSERT((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
240 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
241 
242 	/* Get */
243 	CLASSERT((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
244 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
245 	CLASSERT((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
246 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
247 	CLASSERT((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
248 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
249 	CLASSERT((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
250 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
251 	CLASSERT((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
252 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
253 
254 	/* Reply */
255 	CLASSERT((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
256 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
257 
258 	/* Hello */
259 	CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
260 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
261 	CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
262 	CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
263 }
264 
265 static lnd_t *
lnet_find_lnd_by_type(__u32 type)266 lnet_find_lnd_by_type(__u32 type)
267 {
268 	lnd_t *lnd;
269 	struct list_head *tmp;
270 
271 	/* holding lnd mutex */
272 	list_for_each(tmp, &the_lnet.ln_lnds) {
273 		lnd = list_entry(tmp, lnd_t, lnd_list);
274 
275 		if (lnd->lnd_type == type)
276 			return lnd;
277 	}
278 
279 	return NULL;
280 }
281 
282 void
lnet_register_lnd(lnd_t * lnd)283 lnet_register_lnd(lnd_t *lnd)
284 {
285 	mutex_lock(&the_lnet.ln_lnd_mutex);
286 
287 	LASSERT(the_lnet.ln_init);
288 	LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
289 	LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
290 
291 	list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
292 	lnd->lnd_refcount = 0;
293 
294 	CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
295 
296 	mutex_unlock(&the_lnet.ln_lnd_mutex);
297 }
298 EXPORT_SYMBOL(lnet_register_lnd);
299 
300 void
lnet_unregister_lnd(lnd_t * lnd)301 lnet_unregister_lnd(lnd_t *lnd)
302 {
303 	mutex_lock(&the_lnet.ln_lnd_mutex);
304 
305 	LASSERT(the_lnet.ln_init);
306 	LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
307 	LASSERT(lnd->lnd_refcount == 0);
308 
309 	list_del(&lnd->lnd_list);
310 	CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
311 
312 	mutex_unlock(&the_lnet.ln_lnd_mutex);
313 }
314 EXPORT_SYMBOL(lnet_unregister_lnd);
315 
316 void
lnet_counters_get(lnet_counters_t * counters)317 lnet_counters_get(lnet_counters_t *counters)
318 {
319 	lnet_counters_t *ctr;
320 	int i;
321 
322 	memset(counters, 0, sizeof(*counters));
323 
324 	lnet_net_lock(LNET_LOCK_EX);
325 
326 	cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
327 		counters->msgs_max     += ctr->msgs_max;
328 		counters->msgs_alloc   += ctr->msgs_alloc;
329 		counters->errors       += ctr->errors;
330 		counters->send_count   += ctr->send_count;
331 		counters->recv_count   += ctr->recv_count;
332 		counters->route_count  += ctr->route_count;
333 		counters->drop_count   += ctr->drop_count;
334 		counters->send_length  += ctr->send_length;
335 		counters->recv_length  += ctr->recv_length;
336 		counters->route_length += ctr->route_length;
337 		counters->drop_length  += ctr->drop_length;
338 
339 	}
340 	lnet_net_unlock(LNET_LOCK_EX);
341 }
342 EXPORT_SYMBOL(lnet_counters_get);
343 
344 void
lnet_counters_reset(void)345 lnet_counters_reset(void)
346 {
347 	lnet_counters_t *counters;
348 	int i;
349 
350 	lnet_net_lock(LNET_LOCK_EX);
351 
352 	cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
353 		memset(counters, 0, sizeof(lnet_counters_t));
354 
355 	lnet_net_unlock(LNET_LOCK_EX);
356 }
357 EXPORT_SYMBOL(lnet_counters_reset);
358 
359 static __u64
lnet_create_interface_cookie(void)360 lnet_create_interface_cookie(void)
361 {
362 	/* NB the interface cookie in wire handles guards against delayed
363 	 * replies and ACKs appearing valid after reboot.
364 	 */
365 	return ktime_get_ns();
366 }
367 
368 static char *
lnet_res_type2str(int type)369 lnet_res_type2str(int type)
370 {
371 	switch (type) {
372 	default:
373 		LBUG();
374 	case LNET_COOKIE_TYPE_MD:
375 		return "MD";
376 	case LNET_COOKIE_TYPE_ME:
377 		return "ME";
378 	case LNET_COOKIE_TYPE_EQ:
379 		return "EQ";
380 	}
381 }
382 
383 static void
lnet_res_container_cleanup(struct lnet_res_container * rec)384 lnet_res_container_cleanup(struct lnet_res_container *rec)
385 {
386 	int count = 0;
387 
388 	if (rec->rec_type == 0) /* not set yet, it's uninitialized */
389 		return;
390 
391 	while (!list_empty(&rec->rec_active)) {
392 		struct list_head *e = rec->rec_active.next;
393 
394 		list_del_init(e);
395 		if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
396 			lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
397 
398 		} else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
399 			lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
400 
401 		} else { /* NB: Active MEs should be attached on portals */
402 			LBUG();
403 		}
404 		count++;
405 	}
406 
407 	if (count > 0) {
408 		/* Found alive MD/ME/EQ, user really should unlink/free
409 		 * all of them before finalize LNet, but if someone didn't,
410 		 * we have to recycle garbage for him */
411 		CERROR("%d active elements on exit of %s container\n",
412 		       count, lnet_res_type2str(rec->rec_type));
413 	}
414 
415 	if (rec->rec_lh_hash != NULL) {
416 		LIBCFS_FREE(rec->rec_lh_hash,
417 			    LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
418 		rec->rec_lh_hash = NULL;
419 	}
420 
421 	rec->rec_type = 0; /* mark it as finalized */
422 }
423 
424 static int
lnet_res_container_setup(struct lnet_res_container * rec,int cpt,int type)425 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
426 {
427 	int rc = 0;
428 	int i;
429 
430 	LASSERT(rec->rec_type == 0);
431 
432 	rec->rec_type = type;
433 	INIT_LIST_HEAD(&rec->rec_active);
434 	rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
435 
436 	/* Arbitrary choice of hash table size */
437 	LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
438 			 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
439 	if (rec->rec_lh_hash == NULL) {
440 		rc = -ENOMEM;
441 		goto out;
442 	}
443 
444 	for (i = 0; i < LNET_LH_HASH_SIZE; i++)
445 		INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
446 
447 	return 0;
448 
449 out:
450 	CERROR("Failed to setup %s resource container\n",
451 	       lnet_res_type2str(type));
452 	lnet_res_container_cleanup(rec);
453 	return rc;
454 }
455 
456 static void
lnet_res_containers_destroy(struct lnet_res_container ** recs)457 lnet_res_containers_destroy(struct lnet_res_container **recs)
458 {
459 	struct lnet_res_container *rec;
460 	int i;
461 
462 	cfs_percpt_for_each(rec, i, recs)
463 		lnet_res_container_cleanup(rec);
464 
465 	cfs_percpt_free(recs);
466 }
467 
468 static struct lnet_res_container **
lnet_res_containers_create(int type)469 lnet_res_containers_create(int type)
470 {
471 	struct lnet_res_container **recs;
472 	struct lnet_res_container *rec;
473 	int rc;
474 	int i;
475 
476 	recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
477 	if (recs == NULL) {
478 		CERROR("Failed to allocate %s resource containers\n",
479 		       lnet_res_type2str(type));
480 		return NULL;
481 	}
482 
483 	cfs_percpt_for_each(rec, i, recs) {
484 		rc = lnet_res_container_setup(rec, i, type);
485 		if (rc != 0) {
486 			lnet_res_containers_destroy(recs);
487 			return NULL;
488 		}
489 	}
490 
491 	return recs;
492 }
493 
494 lnet_libhandle_t *
lnet_res_lh_lookup(struct lnet_res_container * rec,__u64 cookie)495 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
496 {
497 	/* ALWAYS called with lnet_res_lock held */
498 	struct list_head *head;
499 	lnet_libhandle_t *lh;
500 	unsigned int hash;
501 
502 	if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
503 		return NULL;
504 
505 	hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
506 	head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
507 
508 	list_for_each_entry(lh, head, lh_hash_chain) {
509 		if (lh->lh_cookie == cookie)
510 			return lh;
511 	}
512 
513 	return NULL;
514 }
515 
516 void
lnet_res_lh_initialize(struct lnet_res_container * rec,lnet_libhandle_t * lh)517 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
518 {
519 	/* ALWAYS called with lnet_res_lock held */
520 	unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
521 	unsigned int hash;
522 
523 	lh->lh_cookie = rec->rec_lh_cookie;
524 	rec->rec_lh_cookie += 1 << ibits;
525 
526 	hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
527 
528 	list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
529 }
530 
531 int lnet_unprepare(void);
532 
533 static int
lnet_prepare(lnet_pid_t requested_pid)534 lnet_prepare(lnet_pid_t requested_pid)
535 {
536 	/* Prepare to bring up the network */
537 	struct lnet_res_container **recs;
538 	int rc = 0;
539 
540 	LASSERT(the_lnet.ln_refcount == 0);
541 
542 	the_lnet.ln_routing = 0;
543 
544 	LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
545 	the_lnet.ln_pid = requested_pid;
546 
547 	INIT_LIST_HEAD(&the_lnet.ln_test_peers);
548 	INIT_LIST_HEAD(&the_lnet.ln_nis);
549 	INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
550 	INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
551 	INIT_LIST_HEAD(&the_lnet.ln_routers);
552 
553 	rc = lnet_create_remote_nets_table();
554 	if (rc != 0)
555 		goto failed;
556 
557 	the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
558 
559 	the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
560 						sizeof(lnet_counters_t));
561 	if (the_lnet.ln_counters == NULL) {
562 		CERROR("Failed to allocate counters for LNet\n");
563 		rc = -ENOMEM;
564 		goto failed;
565 	}
566 
567 	rc = lnet_peer_tables_create();
568 	if (rc != 0)
569 		goto failed;
570 
571 	rc = lnet_msg_containers_create();
572 	if (rc != 0)
573 		goto failed;
574 
575 	rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
576 				      LNET_COOKIE_TYPE_EQ);
577 	if (rc != 0)
578 		goto failed;
579 
580 	recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
581 	if (recs == NULL) {
582 		rc = -ENOMEM;
583 		goto failed;
584 	}
585 
586 	the_lnet.ln_me_containers = recs;
587 
588 	recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
589 	if (recs == NULL) {
590 		rc = -ENOMEM;
591 		goto failed;
592 	}
593 
594 	the_lnet.ln_md_containers = recs;
595 
596 	rc = lnet_portals_create();
597 	if (rc != 0) {
598 		CERROR("Failed to create portals for LNet: %d\n", rc);
599 		goto failed;
600 	}
601 
602 	return 0;
603 
604  failed:
605 	lnet_unprepare();
606 	return rc;
607 }
608 
609 int
lnet_unprepare(void)610 lnet_unprepare(void)
611 {
612 	/* NB no LNET_LOCK since this is the last reference.  All LND instances
613 	 * have shut down already, so it is safe to unlink and free all
614 	 * descriptors, even those that appear committed to a network op (eg MD
615 	 * with non-zero pending count) */
616 
617 	lnet_fail_nid(LNET_NID_ANY, 0);
618 
619 	LASSERT(the_lnet.ln_refcount == 0);
620 	LASSERT(list_empty(&the_lnet.ln_test_peers));
621 	LASSERT(list_empty(&the_lnet.ln_nis));
622 	LASSERT(list_empty(&the_lnet.ln_nis_cpt));
623 	LASSERT(list_empty(&the_lnet.ln_nis_zombie));
624 
625 	lnet_portals_destroy();
626 
627 	if (the_lnet.ln_md_containers != NULL) {
628 		lnet_res_containers_destroy(the_lnet.ln_md_containers);
629 		the_lnet.ln_md_containers = NULL;
630 	}
631 
632 	if (the_lnet.ln_me_containers != NULL) {
633 		lnet_res_containers_destroy(the_lnet.ln_me_containers);
634 		the_lnet.ln_me_containers = NULL;
635 	}
636 
637 	lnet_res_container_cleanup(&the_lnet.ln_eq_container);
638 
639 	lnet_msg_containers_destroy();
640 	lnet_peer_tables_destroy();
641 	lnet_rtrpools_free();
642 
643 	if (the_lnet.ln_counters != NULL) {
644 		cfs_percpt_free(the_lnet.ln_counters);
645 		the_lnet.ln_counters = NULL;
646 	}
647 	lnet_destroy_remote_nets_table();
648 
649 	return 0;
650 }
651 
652 lnet_ni_t  *
lnet_net2ni_locked(__u32 net,int cpt)653 lnet_net2ni_locked(__u32 net, int cpt)
654 {
655 	struct list_head *tmp;
656 	lnet_ni_t *ni;
657 
658 	LASSERT(cpt != LNET_LOCK_EX);
659 
660 	list_for_each(tmp, &the_lnet.ln_nis) {
661 		ni = list_entry(tmp, lnet_ni_t, ni_list);
662 
663 		if (LNET_NIDNET(ni->ni_nid) == net) {
664 			lnet_ni_addref_locked(ni, cpt);
665 			return ni;
666 		}
667 	}
668 
669 	return NULL;
670 }
671 
672 lnet_ni_t *
lnet_net2ni(__u32 net)673 lnet_net2ni(__u32 net)
674 {
675 	lnet_ni_t *ni;
676 
677 	lnet_net_lock(0);
678 	ni = lnet_net2ni_locked(net, 0);
679 	lnet_net_unlock(0);
680 
681 	return ni;
682 }
683 EXPORT_SYMBOL(lnet_net2ni);
684 
685 static unsigned int
lnet_nid_cpt_hash(lnet_nid_t nid,unsigned int number)686 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
687 {
688 	__u64 key = nid;
689 	unsigned int val;
690 
691 	LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
692 
693 	if (number == 1)
694 		return 0;
695 
696 	val = hash_long(key, LNET_CPT_BITS);
697 	/* NB: LNET_CP_NUMBER doesn't have to be PO2 */
698 	if (val < number)
699 		return val;
700 
701 	return (unsigned int)(key + val + (val >> 1)) % number;
702 }
703 
704 int
lnet_cpt_of_nid_locked(lnet_nid_t nid)705 lnet_cpt_of_nid_locked(lnet_nid_t nid)
706 {
707 	struct lnet_ni *ni;
708 
709 	/* must called with hold of lnet_net_lock */
710 	if (LNET_CPT_NUMBER == 1)
711 		return 0; /* the only one */
712 
713 	/* take lnet_net_lock(any) would be OK */
714 	if (!list_empty(&the_lnet.ln_nis_cpt)) {
715 		list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
716 			if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
717 				continue;
718 
719 			LASSERT(ni->ni_cpts != NULL);
720 			return ni->ni_cpts[lnet_nid_cpt_hash
721 					   (nid, ni->ni_ncpts)];
722 		}
723 	}
724 
725 	return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
726 }
727 
728 int
lnet_cpt_of_nid(lnet_nid_t nid)729 lnet_cpt_of_nid(lnet_nid_t nid)
730 {
731 	int cpt;
732 	int cpt2;
733 
734 	if (LNET_CPT_NUMBER == 1)
735 		return 0; /* the only one */
736 
737 	if (list_empty(&the_lnet.ln_nis_cpt))
738 		return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
739 
740 	cpt = lnet_net_lock_current();
741 	cpt2 = lnet_cpt_of_nid_locked(nid);
742 	lnet_net_unlock(cpt);
743 
744 	return cpt2;
745 }
746 EXPORT_SYMBOL(lnet_cpt_of_nid);
747 
748 int
lnet_islocalnet(__u32 net)749 lnet_islocalnet(__u32 net)
750 {
751 	struct lnet_ni *ni;
752 	int cpt;
753 
754 	cpt = lnet_net_lock_current();
755 
756 	ni = lnet_net2ni_locked(net, cpt);
757 	if (ni != NULL)
758 		lnet_ni_decref_locked(ni, cpt);
759 
760 	lnet_net_unlock(cpt);
761 
762 	return ni != NULL;
763 }
764 
765 lnet_ni_t  *
lnet_nid2ni_locked(lnet_nid_t nid,int cpt)766 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
767 {
768 	struct lnet_ni *ni;
769 	struct list_head *tmp;
770 
771 	LASSERT(cpt != LNET_LOCK_EX);
772 
773 	list_for_each(tmp, &the_lnet.ln_nis) {
774 		ni = list_entry(tmp, lnet_ni_t, ni_list);
775 
776 		if (ni->ni_nid == nid) {
777 			lnet_ni_addref_locked(ni, cpt);
778 			return ni;
779 		}
780 	}
781 
782 	return NULL;
783 }
784 
785 int
lnet_islocalnid(lnet_nid_t nid)786 lnet_islocalnid(lnet_nid_t nid)
787 {
788 	struct lnet_ni *ni;
789 	int cpt;
790 
791 	cpt = lnet_net_lock_current();
792 	ni = lnet_nid2ni_locked(nid, cpt);
793 	if (ni != NULL)
794 		lnet_ni_decref_locked(ni, cpt);
795 	lnet_net_unlock(cpt);
796 
797 	return ni != NULL;
798 }
799 
800 int
lnet_count_acceptor_nis(void)801 lnet_count_acceptor_nis(void)
802 {
803 	/* Return the # of NIs that need the acceptor. */
804 	int count = 0;
805 	struct list_head *tmp;
806 	struct lnet_ni *ni;
807 	int cpt;
808 
809 	cpt = lnet_net_lock_current();
810 	list_for_each(tmp, &the_lnet.ln_nis) {
811 		ni = list_entry(tmp, lnet_ni_t, ni_list);
812 
813 		if (ni->ni_lnd->lnd_accept != NULL)
814 			count++;
815 	}
816 
817 	lnet_net_unlock(cpt);
818 
819 	return count;
820 }
821 
822 static int
lnet_ni_tq_credits(lnet_ni_t * ni)823 lnet_ni_tq_credits(lnet_ni_t *ni)
824 {
825 	int credits;
826 
827 	LASSERT(ni->ni_ncpts >= 1);
828 
829 	if (ni->ni_ncpts == 1)
830 		return ni->ni_maxtxcredits;
831 
832 	credits = ni->ni_maxtxcredits / ni->ni_ncpts;
833 	credits = max(credits, 8 * ni->ni_peertxcredits);
834 	credits = min(credits, ni->ni_maxtxcredits);
835 
836 	return credits;
837 }
838 
839 static void
lnet_shutdown_lndnis(void)840 lnet_shutdown_lndnis(void)
841 {
842 	int i;
843 	int islo;
844 	lnet_ni_t *ni;
845 
846 	/* NB called holding the global mutex */
847 
848 	/* All quiet on the API front */
849 	LASSERT(!the_lnet.ln_shutdown);
850 	LASSERT(the_lnet.ln_refcount == 0);
851 	LASSERT(list_empty(&the_lnet.ln_nis_zombie));
852 
853 	lnet_net_lock(LNET_LOCK_EX);
854 	the_lnet.ln_shutdown = 1;	/* flag shutdown */
855 
856 	/* Unlink NIs from the global table */
857 	while (!list_empty(&the_lnet.ln_nis)) {
858 		ni = list_entry(the_lnet.ln_nis.next,
859 				    lnet_ni_t, ni_list);
860 		/* move it to zombie list and nobody can find it anymore */
861 		list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
862 		lnet_ni_decref_locked(ni, 0);	/* drop ln_nis' ref */
863 
864 		if (!list_empty(&ni->ni_cptlist)) {
865 			list_del_init(&ni->ni_cptlist);
866 			lnet_ni_decref_locked(ni, 0);
867 		}
868 	}
869 
870 	/* Drop the cached eqwait NI. */
871 	if (the_lnet.ln_eq_waitni != NULL) {
872 		lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
873 		the_lnet.ln_eq_waitni = NULL;
874 	}
875 
876 	/* Drop the cached loopback NI. */
877 	if (the_lnet.ln_loni != NULL) {
878 		lnet_ni_decref_locked(the_lnet.ln_loni, 0);
879 		the_lnet.ln_loni = NULL;
880 	}
881 
882 	lnet_net_unlock(LNET_LOCK_EX);
883 
884 	/* Clear lazy portals and drop delayed messages which hold refs
885 	 * on their lnet_msg_t::msg_rxpeer */
886 	for (i = 0; i < the_lnet.ln_nportals; i++)
887 		LNetClearLazyPortal(i);
888 
889 	/* Clear the peer table and wait for all peers to go (they hold refs on
890 	 * their NIs) */
891 	lnet_peer_tables_cleanup();
892 
893 	lnet_net_lock(LNET_LOCK_EX);
894 	/* Now wait for the NI's I just nuked to show up on ln_zombie_nis
895 	 * and shut them down in guaranteed thread context */
896 	i = 2;
897 	while (!list_empty(&the_lnet.ln_nis_zombie)) {
898 		int *ref;
899 		int j;
900 
901 		ni = list_entry(the_lnet.ln_nis_zombie.next,
902 				    lnet_ni_t, ni_list);
903 		list_del_init(&ni->ni_list);
904 		cfs_percpt_for_each(ref, j, ni->ni_refs) {
905 			if (*ref == 0)
906 				continue;
907 			/* still busy, add it back to zombie list */
908 			list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
909 			break;
910 		}
911 
912 		if (!list_empty(&ni->ni_list)) {
913 			lnet_net_unlock(LNET_LOCK_EX);
914 			++i;
915 			if ((i & (-i)) == i) {
916 				CDEBUG(D_WARNING, "Waiting for zombie LNI %s\n",
917 				       libcfs_nid2str(ni->ni_nid));
918 			}
919 			set_current_state(TASK_UNINTERRUPTIBLE);
920 			schedule_timeout(cfs_time_seconds(1));
921 			lnet_net_lock(LNET_LOCK_EX);
922 			continue;
923 		}
924 
925 		ni->ni_lnd->lnd_refcount--;
926 		lnet_net_unlock(LNET_LOCK_EX);
927 
928 		islo = ni->ni_lnd->lnd_type == LOLND;
929 
930 		LASSERT(!in_interrupt());
931 		(ni->ni_lnd->lnd_shutdown)(ni);
932 
933 		/* can't deref lnd anymore now; it might have unregistered
934 		 * itself...  */
935 
936 		if (!islo)
937 			CDEBUG(D_LNI, "Removed LNI %s\n",
938 			       libcfs_nid2str(ni->ni_nid));
939 
940 		lnet_ni_free(ni);
941 		i = 2;
942 
943 		lnet_net_lock(LNET_LOCK_EX);
944 	}
945 
946 	the_lnet.ln_shutdown = 0;
947 	lnet_net_unlock(LNET_LOCK_EX);
948 
949 	if (the_lnet.ln_network_tokens != NULL) {
950 		LIBCFS_FREE(the_lnet.ln_network_tokens,
951 			    the_lnet.ln_network_tokens_nob);
952 		the_lnet.ln_network_tokens = NULL;
953 	}
954 }
955 
956 static int
lnet_startup_lndnis(void)957 lnet_startup_lndnis(void)
958 {
959 	lnd_t *lnd;
960 	struct lnet_ni *ni;
961 	struct lnet_tx_queue *tq;
962 	struct list_head nilist;
963 	int i;
964 	int rc = 0;
965 	__u32 lnd_type;
966 	int nicount = 0;
967 	char *nets = lnet_get_networks();
968 
969 	INIT_LIST_HEAD(&nilist);
970 
971 	if (nets == NULL)
972 		goto failed;
973 
974 	rc = lnet_parse_networks(&nilist, nets);
975 	if (rc != 0)
976 		goto failed;
977 
978 	while (!list_empty(&nilist)) {
979 		ni = list_entry(nilist.next, lnet_ni_t, ni_list);
980 		lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
981 
982 		LASSERT(libcfs_isknown_lnd(lnd_type));
983 
984 		if (lnd_type == CIBLND    ||
985 		    lnd_type == OPENIBLND ||
986 		    lnd_type == IIBLND    ||
987 		    lnd_type == VIBLND) {
988 			CERROR("LND %s obsoleted\n",
989 			       libcfs_lnd2str(lnd_type));
990 			goto failed;
991 		}
992 
993 		mutex_lock(&the_lnet.ln_lnd_mutex);
994 		lnd = lnet_find_lnd_by_type(lnd_type);
995 
996 		if (lnd == NULL) {
997 			mutex_unlock(&the_lnet.ln_lnd_mutex);
998 			rc = request_module("%s",
999 						libcfs_lnd2modname(lnd_type));
1000 			mutex_lock(&the_lnet.ln_lnd_mutex);
1001 
1002 			lnd = lnet_find_lnd_by_type(lnd_type);
1003 			if (lnd == NULL) {
1004 				mutex_unlock(&the_lnet.ln_lnd_mutex);
1005 				CERROR("Can't load LND %s, module %s, rc=%d\n",
1006 				       libcfs_lnd2str(lnd_type),
1007 				       libcfs_lnd2modname(lnd_type), rc);
1008 				goto failed;
1009 			}
1010 		}
1011 
1012 		lnet_net_lock(LNET_LOCK_EX);
1013 		lnd->lnd_refcount++;
1014 		lnet_net_unlock(LNET_LOCK_EX);
1015 
1016 		ni->ni_lnd = lnd;
1017 
1018 		rc = (lnd->lnd_startup)(ni);
1019 
1020 		mutex_unlock(&the_lnet.ln_lnd_mutex);
1021 
1022 		if (rc != 0) {
1023 			LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1024 					   rc, libcfs_lnd2str(lnd->lnd_type));
1025 			lnet_net_lock(LNET_LOCK_EX);
1026 			lnd->lnd_refcount--;
1027 			lnet_net_unlock(LNET_LOCK_EX);
1028 			goto failed;
1029 		}
1030 
1031 		LASSERT(ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1032 
1033 		list_del(&ni->ni_list);
1034 
1035 		lnet_net_lock(LNET_LOCK_EX);
1036 		/* refcount for ln_nis */
1037 		lnet_ni_addref_locked(ni, 0);
1038 		list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1039 		if (ni->ni_cpts != NULL) {
1040 			list_add_tail(&ni->ni_cptlist,
1041 					  &the_lnet.ln_nis_cpt);
1042 			lnet_ni_addref_locked(ni, 0);
1043 		}
1044 
1045 		lnet_net_unlock(LNET_LOCK_EX);
1046 
1047 		if (lnd->lnd_type == LOLND) {
1048 			lnet_ni_addref(ni);
1049 			LASSERT(the_lnet.ln_loni == NULL);
1050 			the_lnet.ln_loni = ni;
1051 			continue;
1052 		}
1053 
1054 		if (ni->ni_peertxcredits == 0 ||
1055 		    ni->ni_maxtxcredits == 0) {
1056 			LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1057 					   libcfs_lnd2str(lnd->lnd_type),
1058 					   ni->ni_peertxcredits == 0 ?
1059 					   "" : "per-peer ");
1060 			goto failed;
1061 		}
1062 
1063 		cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1064 			tq->tq_credits_min =
1065 			tq->tq_credits_max =
1066 			tq->tq_credits = lnet_ni_tq_credits(ni);
1067 		}
1068 
1069 		CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1070 		       libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1071 		       lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1072 		       ni->ni_peerrtrcredits, ni->ni_peertimeout);
1073 
1074 		nicount++;
1075 	}
1076 
1077 	if (the_lnet.ln_eq_waitni != NULL && nicount > 1) {
1078 		lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1079 		LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network\n",
1080 				   libcfs_lnd2str(lnd_type));
1081 		goto failed;
1082 	}
1083 
1084 	return 0;
1085 
1086  failed:
1087 	lnet_shutdown_lndnis();
1088 
1089 	while (!list_empty(&nilist)) {
1090 		ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1091 		list_del(&ni->ni_list);
1092 		lnet_ni_free(ni);
1093 	}
1094 
1095 	return -ENETDOWN;
1096 }
1097 
1098 /**
1099  * Initialize LNet library.
1100  *
1101  * Only userspace program needs to call this function - it's automatically
1102  * called in the kernel at module loading time. Caller has to call lnet_fini()
1103  * after a call to lnet_init(), if and only if the latter returned 0. It must
1104  * be called exactly once.
1105  *
1106  * \return 0 on success, and -ve on failures.
1107  */
1108 int
lnet_init(void)1109 lnet_init(void)
1110 {
1111 	int rc;
1112 
1113 	lnet_assert_wire_constants();
1114 	LASSERT(!the_lnet.ln_init);
1115 
1116 	memset(&the_lnet, 0, sizeof(the_lnet));
1117 
1118 	/* refer to global cfs_cpt_table for now */
1119 	the_lnet.ln_cpt_table	= cfs_cpt_table;
1120 	the_lnet.ln_cpt_number	= cfs_cpt_number(cfs_cpt_table);
1121 
1122 	LASSERT(the_lnet.ln_cpt_number > 0);
1123 	if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1124 		/* we are under risk of consuming all lh_cookie */
1125 		CERROR("Can't have %d CPTs for LNet (max allowed is %d), please change setting of CPT-table and retry\n",
1126 		       the_lnet.ln_cpt_number, LNET_CPT_MAX);
1127 		return -1;
1128 	}
1129 
1130 	while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1131 		the_lnet.ln_cpt_bits++;
1132 
1133 	rc = lnet_create_locks();
1134 	if (rc != 0) {
1135 		CERROR("Can't create LNet global locks: %d\n", rc);
1136 		return -1;
1137 	}
1138 
1139 	the_lnet.ln_refcount = 0;
1140 	the_lnet.ln_init = 1;
1141 	LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1142 	INIT_LIST_HEAD(&the_lnet.ln_lnds);
1143 	INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1144 	INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1145 
1146 	/* The hash table size is the number of bits it takes to express the set
1147 	 * ln_num_routes, minus 1 (better to under estimate than over so we
1148 	 * don't waste memory). */
1149 	if (rnet_htable_size <= 0)
1150 		rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1151 	else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1152 		rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1153 	the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1154 					   order_base_2(rnet_htable_size) - 1);
1155 
1156 	/* All LNDs apart from the LOLND are in separate modules.  They
1157 	 * register themselves when their module loads, and unregister
1158 	 * themselves when their module is unloaded. */
1159 	lnet_register_lnd(&the_lolnd);
1160 	return 0;
1161 }
1162 EXPORT_SYMBOL(lnet_init);
1163 
1164 /**
1165  * Finalize LNet library.
1166  *
1167  * Only userspace program needs to call this function. It can be called
1168  * at most once.
1169  *
1170  * \pre lnet_init() called with success.
1171  * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1172  */
1173 void
lnet_fini(void)1174 lnet_fini(void)
1175 {
1176 	LASSERT(the_lnet.ln_init);
1177 	LASSERT(the_lnet.ln_refcount == 0);
1178 
1179 	while (!list_empty(&the_lnet.ln_lnds))
1180 		lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1181 						   lnd_t, lnd_list));
1182 	lnet_destroy_locks();
1183 
1184 	the_lnet.ln_init = 0;
1185 }
1186 EXPORT_SYMBOL(lnet_fini);
1187 
1188 /**
1189  * Set LNet PID and start LNet interfaces, routing, and forwarding.
1190  *
1191  * Userspace program should call this after a successful call to lnet_init().
1192  * Users must call this function at least once before any other functions.
1193  * For each successful call there must be a corresponding call to
1194  * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1195  * ignored.
1196  *
1197  * The PID used by LNet may be different from the one requested.
1198  * See LNetGetId().
1199  *
1200  * \param requested_pid PID requested by the caller.
1201  *
1202  * \return >= 0 on success, and < 0 error code on failures.
1203  */
1204 int
LNetNIInit(lnet_pid_t requested_pid)1205 LNetNIInit(lnet_pid_t requested_pid)
1206 {
1207 	int im_a_router = 0;
1208 	int rc;
1209 
1210 	mutex_lock(&the_lnet.ln_api_mutex);
1211 
1212 	LASSERT(the_lnet.ln_init);
1213 	CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1214 
1215 	if (the_lnet.ln_refcount > 0) {
1216 		rc = the_lnet.ln_refcount++;
1217 		goto out;
1218 	}
1219 
1220 	if (requested_pid == LNET_PID_ANY) {
1221 		/* Don't instantiate LNET just for me */
1222 		rc = -ENETDOWN;
1223 		goto failed0;
1224 	}
1225 
1226 	rc = lnet_prepare(requested_pid);
1227 	if (rc != 0)
1228 		goto failed0;
1229 
1230 	rc = lnet_startup_lndnis();
1231 	if (rc != 0)
1232 		goto failed1;
1233 
1234 	rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1235 	if (rc != 0)
1236 		goto failed2;
1237 
1238 	rc = lnet_check_routes();
1239 	if (rc != 0)
1240 		goto failed2;
1241 
1242 	rc = lnet_rtrpools_alloc(im_a_router);
1243 	if (rc != 0)
1244 		goto failed2;
1245 
1246 	rc = lnet_acceptor_start();
1247 	if (rc != 0)
1248 		goto failed2;
1249 
1250 	the_lnet.ln_refcount = 1;
1251 	/* Now I may use my own API functions... */
1252 
1253 	/* NB router checker needs the_lnet.ln_ping_info in
1254 	 * lnet_router_checker -> lnet_update_ni_status_locked */
1255 	rc = lnet_ping_target_init();
1256 	if (rc != 0)
1257 		goto failed3;
1258 
1259 	rc = lnet_router_checker_start();
1260 	if (rc != 0)
1261 		goto failed4;
1262 
1263 	lnet_router_debugfs_init();
1264 	goto out;
1265 
1266  failed4:
1267 	lnet_ping_target_fini();
1268  failed3:
1269 	the_lnet.ln_refcount = 0;
1270 	lnet_acceptor_stop();
1271  failed2:
1272 	lnet_destroy_routes();
1273 	lnet_shutdown_lndnis();
1274  failed1:
1275 	lnet_unprepare();
1276  failed0:
1277 	LASSERT(rc < 0);
1278  out:
1279 	mutex_unlock(&the_lnet.ln_api_mutex);
1280 	return rc;
1281 }
1282 EXPORT_SYMBOL(LNetNIInit);
1283 
1284 /**
1285  * Stop LNet interfaces, routing, and forwarding.
1286  *
1287  * Users must call this function once for each successful call to LNetNIInit().
1288  * Once the LNetNIFini() operation has been started, the results of pending
1289  * API operations are undefined.
1290  *
1291  * \return always 0 for current implementation.
1292  */
1293 int
LNetNIFini(void)1294 LNetNIFini(void)
1295 {
1296 	mutex_lock(&the_lnet.ln_api_mutex);
1297 
1298 	LASSERT(the_lnet.ln_init);
1299 	LASSERT(the_lnet.ln_refcount > 0);
1300 
1301 	if (the_lnet.ln_refcount != 1) {
1302 		the_lnet.ln_refcount--;
1303 	} else {
1304 		LASSERT(!the_lnet.ln_niinit_self);
1305 
1306 		lnet_router_debugfs_fini();
1307 		lnet_router_checker_stop();
1308 		lnet_ping_target_fini();
1309 
1310 		/* Teardown fns that use my own API functions BEFORE here */
1311 		the_lnet.ln_refcount = 0;
1312 
1313 		lnet_acceptor_stop();
1314 		lnet_destroy_routes();
1315 		lnet_shutdown_lndnis();
1316 		lnet_unprepare();
1317 	}
1318 
1319 	mutex_unlock(&the_lnet.ln_api_mutex);
1320 	return 0;
1321 }
1322 EXPORT_SYMBOL(LNetNIFini);
1323 
1324 /**
1325  * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1326  * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1327  * internal ioctl handler.
1328  *
1329  * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1330  *
1331  * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1332  * The data will be printed to system console. Don't use it excessively.
1333  * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1334  *
1335  * \return Always return 0 when called by users directly (i.e., not via ioctl).
1336  */
1337 int
LNetCtl(unsigned int cmd,void * arg)1338 LNetCtl(unsigned int cmd, void *arg)
1339 {
1340 	struct libcfs_ioctl_data *data = arg;
1341 	lnet_process_id_t id = {0};
1342 	lnet_ni_t *ni;
1343 	int rc;
1344 	unsigned long secs_passed;
1345 
1346 	LASSERT(the_lnet.ln_init);
1347 	LASSERT(the_lnet.ln_refcount > 0);
1348 
1349 	switch (cmd) {
1350 	case IOC_LIBCFS_GET_NI:
1351 		rc = LNetGetId(data->ioc_count, &id);
1352 		data->ioc_nid = id.nid;
1353 		return rc;
1354 
1355 	case IOC_LIBCFS_FAIL_NID:
1356 		return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1357 
1358 	case IOC_LIBCFS_ADD_ROUTE:
1359 		rc = lnet_add_route(data->ioc_net, data->ioc_count,
1360 				    data->ioc_nid, data->ioc_priority);
1361 		return (rc != 0) ? rc : lnet_check_routes();
1362 
1363 	case IOC_LIBCFS_DEL_ROUTE:
1364 		return lnet_del_route(data->ioc_net, data->ioc_nid);
1365 
1366 	case IOC_LIBCFS_GET_ROUTE:
1367 		return lnet_get_route(data->ioc_count,
1368 				      &data->ioc_net, &data->ioc_count,
1369 				      &data->ioc_nid, &data->ioc_flags,
1370 				      &data->ioc_priority);
1371 	case IOC_LIBCFS_NOTIFY_ROUTER:
1372 		secs_passed = (ktime_get_real_seconds() - data->ioc_u64[0]);
1373 		return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1374 				   jiffies - secs_passed * HZ);
1375 
1376 	case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1377 		/* This can be removed once lustre stops calling it */
1378 		return 0;
1379 
1380 	case IOC_LIBCFS_LNET_DIST:
1381 		rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1382 		if (rc < 0 && rc != -EHOSTUNREACH)
1383 			return rc;
1384 
1385 		data->ioc_u32[0] = rc;
1386 		return 0;
1387 
1388 	case IOC_LIBCFS_TESTPROTOCOMPAT:
1389 		lnet_net_lock(LNET_LOCK_EX);
1390 		the_lnet.ln_testprotocompat = data->ioc_flags;
1391 		lnet_net_unlock(LNET_LOCK_EX);
1392 		return 0;
1393 
1394 	case IOC_LIBCFS_PING:
1395 		id.nid = data->ioc_nid;
1396 		id.pid = data->ioc_u32[0];
1397 		rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1398 			       (lnet_process_id_t *)data->ioc_pbuf1,
1399 			       data->ioc_plen1/sizeof(lnet_process_id_t));
1400 		if (rc < 0)
1401 			return rc;
1402 		data->ioc_count = rc;
1403 		return 0;
1404 
1405 	case IOC_LIBCFS_DEBUG_PEER: {
1406 		/* CAVEAT EMPTOR: this one designed for calling directly; not
1407 		 * via an ioctl */
1408 		id = *((lnet_process_id_t *) arg);
1409 
1410 		lnet_debug_peer(id.nid);
1411 
1412 		ni = lnet_net2ni(LNET_NIDNET(id.nid));
1413 		if (ni == NULL) {
1414 			CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1415 		} else {
1416 			if (ni->ni_lnd->lnd_ctl == NULL) {
1417 				CDEBUG(D_WARNING, "No ctl for %s\n",
1418 				       libcfs_id2str(id));
1419 			} else {
1420 				(void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1421 			}
1422 
1423 			lnet_ni_decref(ni);
1424 		}
1425 		return 0;
1426 	}
1427 
1428 	default:
1429 		ni = lnet_net2ni(data->ioc_net);
1430 		if (ni == NULL)
1431 			return -EINVAL;
1432 
1433 		if (ni->ni_lnd->lnd_ctl == NULL)
1434 			rc = -EINVAL;
1435 		else
1436 			rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1437 
1438 		lnet_ni_decref(ni);
1439 		return rc;
1440 	}
1441 	/* not reached */
1442 }
1443 EXPORT_SYMBOL(LNetCtl);
1444 
1445 /**
1446  * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
1447  * all interfaces share a same PID, as requested by LNetNIInit().
1448  *
1449  * \param index Index of the interface to look up.
1450  * \param id On successful return, this location will hold the
1451  * lnet_process_id_t ID of the interface.
1452  *
1453  * \retval 0 If an interface exists at \a index.
1454  * \retval -ENOENT If no interface has been found.
1455  */
1456 int
LNetGetId(unsigned int index,lnet_process_id_t * id)1457 LNetGetId(unsigned int index, lnet_process_id_t *id)
1458 {
1459 	struct lnet_ni *ni;
1460 	struct list_head *tmp;
1461 	int cpt;
1462 	int rc = -ENOENT;
1463 
1464 	LASSERT(the_lnet.ln_init);
1465 
1466 	/* LNetNI initilization failed? */
1467 	if (the_lnet.ln_refcount == 0)
1468 		return rc;
1469 
1470 	cpt = lnet_net_lock_current();
1471 
1472 	list_for_each(tmp, &the_lnet.ln_nis) {
1473 		if (index-- != 0)
1474 			continue;
1475 
1476 		ni = list_entry(tmp, lnet_ni_t, ni_list);
1477 
1478 		id->nid = ni->ni_nid;
1479 		id->pid = the_lnet.ln_pid;
1480 		rc = 0;
1481 		break;
1482 	}
1483 
1484 	lnet_net_unlock(cpt);
1485 	return rc;
1486 }
1487 EXPORT_SYMBOL(LNetGetId);
1488 
1489 /**
1490  * Print a string representation of handle \a h into buffer \a str of
1491  * \a len bytes.
1492  */
1493 void
LNetSnprintHandle(char * str,int len,lnet_handle_any_t h)1494 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1495 {
1496 	snprintf(str, len, "%#llx", h.cookie);
1497 }
1498 EXPORT_SYMBOL(LNetSnprintHandle);
1499 
1500 static int
lnet_create_ping_info(void)1501 lnet_create_ping_info(void)
1502 {
1503 	int i;
1504 	int n;
1505 	int rc;
1506 	unsigned int infosz;
1507 	lnet_ni_t *ni;
1508 	lnet_process_id_t id;
1509 	lnet_ping_info_t *pinfo;
1510 
1511 	for (n = 0; ; n++) {
1512 		rc = LNetGetId(n, &id);
1513 		if (rc == -ENOENT)
1514 			break;
1515 
1516 		LASSERT(rc == 0);
1517 	}
1518 
1519 	infosz = offsetof(lnet_ping_info_t, pi_ni[n]);
1520 	LIBCFS_ALLOC(pinfo, infosz);
1521 	if (pinfo == NULL) {
1522 		CERROR("Can't allocate ping info[%d]\n", n);
1523 		return -ENOMEM;
1524 	}
1525 
1526 	pinfo->pi_nnis    = n;
1527 	pinfo->pi_pid     = the_lnet.ln_pid;
1528 	pinfo->pi_magic   = LNET_PROTO_PING_MAGIC;
1529 	pinfo->pi_features = LNET_PING_FEAT_NI_STATUS;
1530 
1531 	for (i = 0; i < n; i++) {
1532 		lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1533 
1534 		rc = LNetGetId(i, &id);
1535 		LASSERT(rc == 0);
1536 
1537 		ns->ns_nid    = id.nid;
1538 		ns->ns_status = LNET_NI_STATUS_UP;
1539 
1540 		lnet_net_lock(0);
1541 
1542 		ni = lnet_nid2ni_locked(id.nid, 0);
1543 		LASSERT(ni != NULL);
1544 
1545 		lnet_ni_lock(ni);
1546 		LASSERT(ni->ni_status == NULL);
1547 		ni->ni_status = ns;
1548 		lnet_ni_unlock(ni);
1549 
1550 		lnet_ni_decref_locked(ni, 0);
1551 		lnet_net_unlock(0);
1552 	}
1553 
1554 	the_lnet.ln_ping_info = pinfo;
1555 	return 0;
1556 }
1557 
1558 static void
lnet_destroy_ping_info(void)1559 lnet_destroy_ping_info(void)
1560 {
1561 	struct lnet_ni *ni;
1562 
1563 	lnet_net_lock(0);
1564 
1565 	list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1566 		lnet_ni_lock(ni);
1567 		ni->ni_status = NULL;
1568 		lnet_ni_unlock(ni);
1569 	}
1570 
1571 	lnet_net_unlock(0);
1572 
1573 	LIBCFS_FREE(the_lnet.ln_ping_info,
1574 		    offsetof(lnet_ping_info_t,
1575 			     pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1576 	the_lnet.ln_ping_info = NULL;
1577 }
1578 
1579 int
lnet_ping_target_init(void)1580 lnet_ping_target_init(void)
1581 {
1582 	lnet_md_t md = { NULL };
1583 	lnet_handle_me_t meh;
1584 	lnet_process_id_t id;
1585 	int rc;
1586 	int rc2;
1587 	int infosz;
1588 
1589 	rc = lnet_create_ping_info();
1590 	if (rc != 0)
1591 		return rc;
1592 
1593 	/* We can have a tiny EQ since we only need to see the unlink event on
1594 	 * teardown, which by definition is the last one! */
1595 	rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1596 	if (rc != 0) {
1597 		CERROR("Can't allocate ping EQ: %d\n", rc);
1598 		goto failed_0;
1599 	}
1600 
1601 	memset(&id, 0, sizeof(lnet_process_id_t));
1602 	id.nid = LNET_NID_ANY;
1603 	id.pid = LNET_PID_ANY;
1604 
1605 	rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1606 			  LNET_PROTO_PING_MATCHBITS, 0,
1607 			  LNET_UNLINK, LNET_INS_AFTER,
1608 			  &meh);
1609 	if (rc != 0) {
1610 		CERROR("Can't create ping ME: %d\n", rc);
1611 		goto failed_1;
1612 	}
1613 
1614 	/* initialize md content */
1615 	infosz = offsetof(lnet_ping_info_t,
1616 			  pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1617 	md.start     = the_lnet.ln_ping_info;
1618 	md.length    = infosz;
1619 	md.threshold = LNET_MD_THRESH_INF;
1620 	md.max_size  = 0;
1621 	md.options   = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1622 		       LNET_MD_MANAGE_REMOTE;
1623 	md.user_ptr  = NULL;
1624 	md.eq_handle = the_lnet.ln_ping_target_eq;
1625 
1626 	rc = LNetMDAttach(meh, md,
1627 			  LNET_RETAIN,
1628 			  &the_lnet.ln_ping_target_md);
1629 	if (rc != 0) {
1630 		CERROR("Can't attach ping MD: %d\n", rc);
1631 		goto failed_2;
1632 	}
1633 
1634 	return 0;
1635 
1636  failed_2:
1637 	rc2 = LNetMEUnlink(meh);
1638 	LASSERT(rc2 == 0);
1639  failed_1:
1640 	rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1641 	LASSERT(rc2 == 0);
1642  failed_0:
1643 	lnet_destroy_ping_info();
1644 	return rc;
1645 }
1646 
1647 void
lnet_ping_target_fini(void)1648 lnet_ping_target_fini(void)
1649 {
1650 	lnet_event_t event;
1651 	int rc;
1652 	int which;
1653 	int timeout_ms = 1000;
1654 	sigset_t blocked = cfs_block_allsigs();
1655 
1656 	LNetMDUnlink(the_lnet.ln_ping_target_md);
1657 	/* NB md could be busy; this just starts the unlink */
1658 
1659 	for (;;) {
1660 		rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1661 				timeout_ms, &event, &which);
1662 
1663 		/* I expect overflow... */
1664 		LASSERT(rc >= 0 || rc == -EOVERFLOW);
1665 
1666 		if (rc == 0) {
1667 			/* timed out: provide a diagnostic */
1668 			CWARN("Still waiting for ping MD to unlink\n");
1669 			timeout_ms *= 2;
1670 			continue;
1671 		}
1672 
1673 		/* Got a valid event */
1674 		if (event.unlinked)
1675 			break;
1676 	}
1677 
1678 	rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1679 	LASSERT(rc == 0);
1680 	lnet_destroy_ping_info();
1681 	cfs_restore_sigs(blocked);
1682 }
1683 
1684 int
lnet_ping(lnet_process_id_t id,int timeout_ms,lnet_process_id_t * ids,int n_ids)1685 lnet_ping(lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1686 {
1687 	lnet_handle_eq_t eqh;
1688 	lnet_handle_md_t mdh;
1689 	lnet_event_t event;
1690 	lnet_md_t md = { NULL };
1691 	int which;
1692 	int unlinked = 0;
1693 	int replied = 0;
1694 	const int a_long_time = 60000; /* mS */
1695 	int infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1696 	lnet_ping_info_t *info;
1697 	lnet_process_id_t tmpid;
1698 	int i;
1699 	int nob;
1700 	int rc;
1701 	int rc2;
1702 	sigset_t blocked;
1703 
1704 	if (n_ids <= 0 ||
1705 	    id.nid == LNET_NID_ANY ||
1706 	    timeout_ms > 500000 ||	      /* arbitrary limit! */
1707 	    n_ids > 20)			 /* arbitrary limit! */
1708 		return -EINVAL;
1709 
1710 	if (id.pid == LNET_PID_ANY)
1711 		id.pid = LUSTRE_SRV_LNET_PID;
1712 
1713 	LIBCFS_ALLOC(info, infosz);
1714 	if (info == NULL)
1715 		return -ENOMEM;
1716 
1717 	/* NB 2 events max (including any unlink event) */
1718 	rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1719 	if (rc != 0) {
1720 		CERROR("Can't allocate EQ: %d\n", rc);
1721 		goto out_0;
1722 	}
1723 
1724 	/* initialize md content */
1725 	md.start     = info;
1726 	md.length    = infosz;
1727 	md.threshold = 2; /*GET/REPLY*/
1728 	md.max_size  = 0;
1729 	md.options   = LNET_MD_TRUNCATE;
1730 	md.user_ptr  = NULL;
1731 	md.eq_handle = eqh;
1732 
1733 	rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1734 	if (rc != 0) {
1735 		CERROR("Can't bind MD: %d\n", rc);
1736 		goto out_1;
1737 	}
1738 
1739 	rc = LNetGet(LNET_NID_ANY, mdh, id,
1740 		     LNET_RESERVED_PORTAL,
1741 		     LNET_PROTO_PING_MATCHBITS, 0);
1742 
1743 	if (rc != 0) {
1744 		/* Don't CERROR; this could be deliberate! */
1745 
1746 		rc2 = LNetMDUnlink(mdh);
1747 		LASSERT(rc2 == 0);
1748 
1749 		/* NB must wait for the UNLINK event below... */
1750 		unlinked = 1;
1751 		timeout_ms = a_long_time;
1752 	}
1753 
1754 	do {
1755 		/* MUST block for unlink to complete */
1756 		if (unlinked)
1757 			blocked = cfs_block_allsigs();
1758 
1759 		rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1760 
1761 		if (unlinked)
1762 			cfs_restore_sigs(blocked);
1763 
1764 		CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1765 		       (rc2 <= 0) ? -1 : event.type,
1766 		       (rc2 <= 0) ? -1 : event.status,
1767 		       (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1768 
1769 		LASSERT(rc2 != -EOVERFLOW);     /* can't miss anything */
1770 
1771 		if (rc2 <= 0 || event.status != 0) {
1772 			/* timeout or error */
1773 			if (!replied && rc == 0)
1774 				rc = (rc2 < 0) ? rc2 :
1775 				     (rc2 == 0) ? -ETIMEDOUT :
1776 				     event.status;
1777 
1778 			if (!unlinked) {
1779 				/* Ensure completion in finite time... */
1780 				LNetMDUnlink(mdh);
1781 				/* No assertion (racing with network) */
1782 				unlinked = 1;
1783 				timeout_ms = a_long_time;
1784 			} else if (rc2 == 0) {
1785 				/* timed out waiting for unlink */
1786 				CWARN("ping %s: late network completion\n",
1787 				      libcfs_id2str(id));
1788 			}
1789 		} else if (event.type == LNET_EVENT_REPLY) {
1790 			replied = 1;
1791 			rc = event.mlength;
1792 		}
1793 
1794 	} while (rc2 <= 0 || !event.unlinked);
1795 
1796 	if (!replied) {
1797 		if (rc >= 0)
1798 			CWARN("%s: Unexpected rc >= 0 but no reply!\n",
1799 			      libcfs_id2str(id));
1800 		rc = -EIO;
1801 		goto out_1;
1802 	}
1803 
1804 	nob = rc;
1805 	LASSERT(nob >= 0 && nob <= infosz);
1806 
1807 	rc = -EPROTO;			   /* if I can't parse... */
1808 
1809 	if (nob < 8) {
1810 		/* can't check magic/version */
1811 		CERROR("%s: ping info too short %d\n",
1812 		       libcfs_id2str(id), nob);
1813 		goto out_1;
1814 	}
1815 
1816 	if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
1817 		lnet_swap_pinginfo(info);
1818 	} else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
1819 		CERROR("%s: Unexpected magic %08x\n",
1820 		       libcfs_id2str(id), info->pi_magic);
1821 		goto out_1;
1822 	}
1823 
1824 	if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
1825 		CERROR("%s: ping w/o NI status: 0x%x\n",
1826 		       libcfs_id2str(id), info->pi_features);
1827 		goto out_1;
1828 	}
1829 
1830 	if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
1831 		CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
1832 		       nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
1833 		goto out_1;
1834 	}
1835 
1836 	if (info->pi_nnis < n_ids)
1837 		n_ids = info->pi_nnis;
1838 
1839 	if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
1840 		CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
1841 		       nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
1842 		goto out_1;
1843 	}
1844 
1845 	rc = -EFAULT;			   /* If I SEGV... */
1846 
1847 	memset(&tmpid, 0, sizeof(tmpid));
1848 	for (i = 0; i < n_ids; i++) {
1849 		tmpid.pid = info->pi_pid;
1850 		tmpid.nid = info->pi_ni[i].ns_nid;
1851 		if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
1852 			goto out_1;
1853 	}
1854 	rc = info->pi_nnis;
1855 
1856  out_1:
1857 	rc2 = LNetEQFree(eqh);
1858 	if (rc2 != 0)
1859 		CERROR("rc2 %d\n", rc2);
1860 	LASSERT(rc2 == 0);
1861 
1862  out_0:
1863 	LIBCFS_FREE(info, infosz);
1864 	return rc;
1865 }
1866