1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#define DEBUG_SUBSYSTEM S_LNET
38#include <linux/lnet/lib-lnet.h>
39#include <linux/log2.h>
40
41#define D_LNI D_CONSOLE
42
43lnet_t the_lnet;
44EXPORT_SYMBOL(the_lnet);
45
46
47static char *ip2nets = "";
48CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
49 "LNET network <- IP table");
50
51static char *networks = "";
52CFS_MODULE_PARM(networks, "s", charp, 0444,
53 "local networks");
54
55static char *routes = "";
56CFS_MODULE_PARM(routes, "s", charp, 0444,
57 "routes to non-local networks");
58
59static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
60CFS_MODULE_PARM(rnet_htable_size, "i", int, 0444,
61 "size of remote network hash table");
62
63char *
64lnet_get_routes(void)
65{
66 return routes;
67}
68
69char *
70lnet_get_networks(void)
71{
72 char *nets;
73 int rc;
74
75 if (*networks != 0 && *ip2nets != 0) {
76 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
77 "'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
92void
93lnet_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
101void
102lnet_fini_locks(void)
103{
104}
105
106
107static int
108lnet_create_remote_nets_table(void)
109{
110 int i;
111 struct list_head *hash;
112
113 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
114 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
115 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
116 if (hash == NULL) {
117 CERROR("Failed to create remote nets hash table\n");
118 return -ENOMEM;
119 }
120
121 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
122 INIT_LIST_HEAD(&hash[i]);
123 the_lnet.ln_remote_nets_hash = hash;
124 return 0;
125}
126
127static void
128lnet_destroy_remote_nets_table(void)
129{
130 int i;
131 struct list_head *hash;
132
133 if (the_lnet.ln_remote_nets_hash == NULL)
134 return;
135
136 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
137 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
138
139 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
140 LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
141 the_lnet.ln_remote_nets_hash = NULL;
142}
143
144static void
145lnet_destroy_locks(void)
146{
147 if (the_lnet.ln_res_lock != NULL) {
148 cfs_percpt_lock_free(the_lnet.ln_res_lock);
149 the_lnet.ln_res_lock = NULL;
150 }
151
152 if (the_lnet.ln_net_lock != NULL) {
153 cfs_percpt_lock_free(the_lnet.ln_net_lock);
154 the_lnet.ln_net_lock = NULL;
155 }
156
157 lnet_fini_locks();
158}
159
160static int
161lnet_create_locks(void)
162{
163 lnet_init_locks();
164
165 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
166 if (the_lnet.ln_res_lock == NULL)
167 goto failed;
168
169 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
170 if (the_lnet.ln_net_lock == NULL)
171 goto failed;
172
173 return 0;
174
175 failed:
176 lnet_destroy_locks();
177 return -ENOMEM;
178}
179
180void lnet_assert_wire_constants (void)
181{
182
183
184
185
186
187
188 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
189 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
190 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
191 CLASSERT (LNET_MSG_ACK == 0);
192 CLASSERT (LNET_MSG_PUT == 1);
193 CLASSERT (LNET_MSG_GET == 2);
194 CLASSERT (LNET_MSG_REPLY == 3);
195 CLASSERT (LNET_MSG_HELLO == 4);
196
197
198 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
199 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
200 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
201 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
202 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
203
204
205 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
206 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
207 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
208 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
209 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
210 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
211 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
212
213
214 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
215 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
216 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
217 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
218 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
219 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
220 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
221 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
222 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
223 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
224 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
225 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
226 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
227 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
228 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
229
230
231 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
232 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
233 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
234 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
235 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
236 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
237
238
239 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
240 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
241 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
242 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
243 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
244 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
245 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
246 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
247 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
248 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
249
250
251 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
252 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
253 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
254 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
255 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
256 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
257 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
258 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
259 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
260 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
261
262
263 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
264 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
265
266
267 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
268 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
269 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
270 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
271}
272
273lnd_t *
274lnet_find_lnd_by_type (int type)
275{
276 lnd_t *lnd;
277 struct list_head *tmp;
278
279
280 list_for_each (tmp, &the_lnet.ln_lnds) {
281 lnd = list_entry(tmp, lnd_t, lnd_list);
282
283 if ((int)lnd->lnd_type == type)
284 return lnd;
285 }
286
287 return NULL;
288}
289
290void
291lnet_register_lnd (lnd_t *lnd)
292{
293 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
294
295 LASSERT (the_lnet.ln_init);
296 LASSERT (libcfs_isknown_lnd(lnd->lnd_type));
297 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
298
299 list_add_tail (&lnd->lnd_list, &the_lnet.ln_lnds);
300 lnd->lnd_refcount = 0;
301
302 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
303
304 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
305}
306EXPORT_SYMBOL(lnet_register_lnd);
307
308void
309lnet_unregister_lnd (lnd_t *lnd)
310{
311 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
312
313 LASSERT (the_lnet.ln_init);
314 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
315 LASSERT (lnd->lnd_refcount == 0);
316
317 list_del (&lnd->lnd_list);
318 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
319
320 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
321}
322EXPORT_SYMBOL(lnet_unregister_lnd);
323
324void
325lnet_counters_get(lnet_counters_t *counters)
326{
327 lnet_counters_t *ctr;
328 int i;
329
330 memset(counters, 0, sizeof(*counters));
331
332 lnet_net_lock(LNET_LOCK_EX);
333
334 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
335 counters->msgs_max += ctr->msgs_max;
336 counters->msgs_alloc += ctr->msgs_alloc;
337 counters->errors += ctr->errors;
338 counters->send_count += ctr->send_count;
339 counters->recv_count += ctr->recv_count;
340 counters->route_count += ctr->route_count;
341 counters->drop_length += ctr->drop_length;
342 counters->send_length += ctr->send_length;
343 counters->recv_length += ctr->recv_length;
344 counters->route_length += ctr->route_length;
345 counters->drop_length += ctr->drop_length;
346
347 }
348 lnet_net_unlock(LNET_LOCK_EX);
349}
350EXPORT_SYMBOL(lnet_counters_get);
351
352void
353lnet_counters_reset(void)
354{
355 lnet_counters_t *counters;
356 int i;
357
358 lnet_net_lock(LNET_LOCK_EX);
359
360 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
361 memset(counters, 0, sizeof(lnet_counters_t));
362
363 lnet_net_unlock(LNET_LOCK_EX);
364}
365EXPORT_SYMBOL(lnet_counters_reset);
366
367#ifdef LNET_USE_LIB_FREELIST
368
369int
370lnet_freelist_init (lnet_freelist_t *fl, int n, int size)
371{
372 char *space;
373
374 LASSERT (n > 0);
375
376 size += offsetof (lnet_freeobj_t, fo_contents);
377
378 LIBCFS_ALLOC(space, n * size);
379 if (space == NULL)
380 return (-ENOMEM);
381
382 INIT_LIST_HEAD (&fl->fl_list);
383 fl->fl_objs = space;
384 fl->fl_nobjs = n;
385 fl->fl_objsize = size;
386
387 do
388 {
389 memset (space, 0, size);
390 list_add ((struct list_head *)space, &fl->fl_list);
391 space += size;
392 } while (--n != 0);
393
394 return (0);
395}
396
397void
398lnet_freelist_fini (lnet_freelist_t *fl)
399{
400 struct list_head *el;
401 int count;
402
403 if (fl->fl_nobjs == 0)
404 return;
405
406 count = 0;
407 for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
408 count++;
409
410 LASSERT (count == fl->fl_nobjs);
411
412 LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
413 memset (fl, 0, sizeof (*fl));
414}
415
416#endif
417
418__u64
419lnet_create_interface_cookie (void)
420{
421
422
423
424
425 struct timeval tv;
426 __u64 cookie;
427 do_gettimeofday(&tv);
428 cookie = tv.tv_sec;
429 cookie *= 1000000;
430 cookie += tv.tv_usec;
431 return cookie;
432}
433
434static char *
435lnet_res_type2str(int type)
436{
437 switch (type) {
438 default:
439 LBUG();
440 case LNET_COOKIE_TYPE_MD:
441 return "MD";
442 case LNET_COOKIE_TYPE_ME:
443 return "ME";
444 case LNET_COOKIE_TYPE_EQ:
445 return "EQ";
446 }
447}
448
449void
450lnet_res_container_cleanup(struct lnet_res_container *rec)
451{
452 int count = 0;
453
454 if (rec->rec_type == 0)
455 return;
456
457 while (!list_empty(&rec->rec_active)) {
458 struct list_head *e = rec->rec_active.next;
459
460 list_del_init(e);
461 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
462 lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
463
464 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
465 lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
466
467 } else {
468 LBUG();
469 }
470 count++;
471 }
472
473 if (count > 0) {
474
475
476
477 CERROR("%d active elements on exit of %s container\n",
478 count, lnet_res_type2str(rec->rec_type));
479 }
480
481#ifdef LNET_USE_LIB_FREELIST
482 lnet_freelist_fini(&rec->rec_freelist);
483#endif
484 if (rec->rec_lh_hash != NULL) {
485 LIBCFS_FREE(rec->rec_lh_hash,
486 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
487 rec->rec_lh_hash = NULL;
488 }
489
490 rec->rec_type = 0;
491}
492
493int
494lnet_res_container_setup(struct lnet_res_container *rec,
495 int cpt, int type, int objnum, int objsz)
496{
497 int rc = 0;
498 int i;
499
500 LASSERT(rec->rec_type == 0);
501
502 rec->rec_type = type;
503 INIT_LIST_HEAD(&rec->rec_active);
504
505#ifdef LNET_USE_LIB_FREELIST
506 memset(&rec->rec_freelist, 0, sizeof(rec->rec_freelist));
507 rc = lnet_freelist_init(&rec->rec_freelist, objnum, objsz);
508 if (rc != 0)
509 goto out;
510#endif
511 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
512
513
514 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
515 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
516 if (rec->rec_lh_hash == NULL) {
517 rc = -ENOMEM;
518 goto out;
519 }
520
521 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
522 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
523
524 return 0;
525
526out:
527 CERROR("Failed to setup %s resource container\n",
528 lnet_res_type2str(type));
529 lnet_res_container_cleanup(rec);
530 return rc;
531}
532
533static void
534lnet_res_containers_destroy(struct lnet_res_container **recs)
535{
536 struct lnet_res_container *rec;
537 int i;
538
539 cfs_percpt_for_each(rec, i, recs)
540 lnet_res_container_cleanup(rec);
541
542 cfs_percpt_free(recs);
543}
544
545static struct lnet_res_container **
546lnet_res_containers_create(int type, int objnum, int objsz)
547{
548 struct lnet_res_container **recs;
549 struct lnet_res_container *rec;
550 int rc;
551 int i;
552
553 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
554 if (recs == NULL) {
555 CERROR("Failed to allocate %s resource containers\n",
556 lnet_res_type2str(type));
557 return NULL;
558 }
559
560 cfs_percpt_for_each(rec, i, recs) {
561 rc = lnet_res_container_setup(rec, i, type, objnum, objsz);
562 if (rc != 0) {
563 lnet_res_containers_destroy(recs);
564 return NULL;
565 }
566 }
567
568 return recs;
569}
570
571lnet_libhandle_t *
572lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
573{
574
575 struct list_head *head;
576 lnet_libhandle_t *lh;
577 unsigned int hash;
578
579 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
580 return NULL;
581
582 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
583 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
584
585 list_for_each_entry(lh, head, lh_hash_chain) {
586 if (lh->lh_cookie == cookie)
587 return lh;
588 }
589
590 return NULL;
591}
592
593void
594lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
595{
596
597 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
598 unsigned int hash;
599
600 lh->lh_cookie = rec->rec_lh_cookie;
601 rec->rec_lh_cookie += 1 << ibits;
602
603 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
604
605 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
606}
607
608
609int lnet_unprepare(void);
610
611int
612lnet_prepare(lnet_pid_t requested_pid)
613{
614
615 struct lnet_res_container **recs;
616 int rc = 0;
617
618 LASSERT (the_lnet.ln_refcount == 0);
619
620 the_lnet.ln_routing = 0;
621
622 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
623 the_lnet.ln_pid = requested_pid;
624
625 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
626 INIT_LIST_HEAD(&the_lnet.ln_nis);
627 INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
628 INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
629 INIT_LIST_HEAD(&the_lnet.ln_routers);
630
631 rc = lnet_create_remote_nets_table();
632 if (rc != 0)
633 goto failed;
634
635 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
636
637 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
638 sizeof(lnet_counters_t));
639 if (the_lnet.ln_counters == NULL) {
640 CERROR("Failed to allocate counters for LNet\n");
641 rc = -ENOMEM;
642 goto failed;
643 }
644
645 rc = lnet_peer_tables_create();
646 if (rc != 0)
647 goto failed;
648
649 rc = lnet_msg_containers_create();
650 if (rc != 0)
651 goto failed;
652
653 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
654 LNET_COOKIE_TYPE_EQ, LNET_FL_MAX_EQS,
655 sizeof(lnet_eq_t));
656 if (rc != 0)
657 goto failed;
658
659 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME, LNET_FL_MAX_MES,
660 sizeof(lnet_me_t));
661 if (recs == NULL)
662 goto failed;
663
664 the_lnet.ln_me_containers = recs;
665
666 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD, LNET_FL_MAX_MDS,
667 sizeof(lnet_libmd_t));
668 if (recs == NULL)
669 goto failed;
670
671 the_lnet.ln_md_containers = recs;
672
673 rc = lnet_portals_create();
674 if (rc != 0) {
675 CERROR("Failed to create portals for LNet: %d\n", rc);
676 goto failed;
677 }
678
679 return 0;
680
681 failed:
682 lnet_unprepare();
683 return rc;
684}
685
686int
687lnet_unprepare (void)
688{
689
690
691
692
693
694 lnet_fail_nid(LNET_NID_ANY, 0);
695
696 LASSERT(the_lnet.ln_refcount == 0);
697 LASSERT(list_empty(&the_lnet.ln_test_peers));
698 LASSERT(list_empty(&the_lnet.ln_nis));
699 LASSERT(list_empty(&the_lnet.ln_nis_cpt));
700 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
701
702 lnet_portals_destroy();
703
704 if (the_lnet.ln_md_containers != NULL) {
705 lnet_res_containers_destroy(the_lnet.ln_md_containers);
706 the_lnet.ln_md_containers = NULL;
707 }
708
709 if (the_lnet.ln_me_containers != NULL) {
710 lnet_res_containers_destroy(the_lnet.ln_me_containers);
711 the_lnet.ln_me_containers = NULL;
712 }
713
714 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
715
716 lnet_msg_containers_destroy();
717 lnet_peer_tables_destroy();
718 lnet_rtrpools_free();
719
720 if (the_lnet.ln_counters != NULL) {
721 cfs_percpt_free(the_lnet.ln_counters);
722 the_lnet.ln_counters = NULL;
723 }
724 lnet_destroy_remote_nets_table();
725
726 return 0;
727}
728
729lnet_ni_t *
730lnet_net2ni_locked(__u32 net, int cpt)
731{
732 struct list_head *tmp;
733 lnet_ni_t *ni;
734
735 LASSERT(cpt != LNET_LOCK_EX);
736
737 list_for_each(tmp, &the_lnet.ln_nis) {
738 ni = list_entry(tmp, lnet_ni_t, ni_list);
739
740 if (LNET_NIDNET(ni->ni_nid) == net) {
741 lnet_ni_addref_locked(ni, cpt);
742 return ni;
743 }
744 }
745
746 return NULL;
747}
748
749lnet_ni_t *
750lnet_net2ni(__u32 net)
751{
752 lnet_ni_t *ni;
753
754 lnet_net_lock(0);
755 ni = lnet_net2ni_locked(net, 0);
756 lnet_net_unlock(0);
757
758 return ni;
759}
760EXPORT_SYMBOL(lnet_net2ni);
761
762static unsigned int
763lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
764{
765 __u64 key = nid;
766 unsigned int val;
767
768 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
769
770 if (number == 1)
771 return 0;
772
773 val = cfs_hash_long(key, LNET_CPT_BITS);
774
775 if (val < number)
776 return val;
777
778 return (unsigned int)(key + val + (val >> 1)) % number;
779}
780
781int
782lnet_cpt_of_nid_locked(lnet_nid_t nid)
783{
784 struct lnet_ni *ni;
785
786
787 if (LNET_CPT_NUMBER == 1)
788 return 0;
789
790
791 if (!list_empty(&the_lnet.ln_nis_cpt)) {
792 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
793 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
794 continue;
795
796 LASSERT(ni->ni_cpts != NULL);
797 return ni->ni_cpts[lnet_nid_cpt_hash
798 (nid, ni->ni_ncpts)];
799 }
800 }
801
802 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
803}
804
805int
806lnet_cpt_of_nid(lnet_nid_t nid)
807{
808 int cpt;
809 int cpt2;
810
811 if (LNET_CPT_NUMBER == 1)
812 return 0;
813
814 if (list_empty(&the_lnet.ln_nis_cpt))
815 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
816
817 cpt = lnet_net_lock_current();
818 cpt2 = lnet_cpt_of_nid_locked(nid);
819 lnet_net_unlock(cpt);
820
821 return cpt2;
822}
823EXPORT_SYMBOL(lnet_cpt_of_nid);
824
825int
826lnet_islocalnet(__u32 net)
827{
828 struct lnet_ni *ni;
829 int cpt;
830
831 cpt = lnet_net_lock_current();
832
833 ni = lnet_net2ni_locked(net, cpt);
834 if (ni != NULL)
835 lnet_ni_decref_locked(ni, cpt);
836
837 lnet_net_unlock(cpt);
838
839 return ni != NULL;
840}
841
842lnet_ni_t *
843lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
844{
845 struct lnet_ni *ni;
846 struct list_head *tmp;
847
848 LASSERT(cpt != LNET_LOCK_EX);
849
850 list_for_each(tmp, &the_lnet.ln_nis) {
851 ni = list_entry(tmp, lnet_ni_t, ni_list);
852
853 if (ni->ni_nid == nid) {
854 lnet_ni_addref_locked(ni, cpt);
855 return ni;
856 }
857 }
858
859 return NULL;
860}
861
862int
863lnet_islocalnid(lnet_nid_t nid)
864{
865 struct lnet_ni *ni;
866 int cpt;
867
868 cpt = lnet_net_lock_current();
869 ni = lnet_nid2ni_locked(nid, cpt);
870 if (ni != NULL)
871 lnet_ni_decref_locked(ni, cpt);
872 lnet_net_unlock(cpt);
873
874 return ni != NULL;
875}
876
877int
878lnet_count_acceptor_nis (void)
879{
880
881 int count = 0;
882 struct list_head *tmp;
883 struct lnet_ni *ni;
884 int cpt;
885
886 cpt = lnet_net_lock_current();
887 list_for_each(tmp, &the_lnet.ln_nis) {
888 ni = list_entry(tmp, lnet_ni_t, ni_list);
889
890 if (ni->ni_lnd->lnd_accept != NULL)
891 count++;
892 }
893
894 lnet_net_unlock(cpt);
895
896 return count;
897}
898
899static int
900lnet_ni_tq_credits(lnet_ni_t *ni)
901{
902 int credits;
903
904 LASSERT(ni->ni_ncpts >= 1);
905
906 if (ni->ni_ncpts == 1)
907 return ni->ni_maxtxcredits;
908
909 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
910 credits = max(credits, 8 * ni->ni_peertxcredits);
911 credits = min(credits, ni->ni_maxtxcredits);
912
913 return credits;
914}
915
916void
917lnet_shutdown_lndnis (void)
918{
919 int i;
920 int islo;
921 lnet_ni_t *ni;
922
923
924
925
926 LASSERT(!the_lnet.ln_shutdown);
927 LASSERT(the_lnet.ln_refcount == 0);
928 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
929
930 lnet_net_lock(LNET_LOCK_EX);
931 the_lnet.ln_shutdown = 1;
932
933
934 while (!list_empty(&the_lnet.ln_nis)) {
935 ni = list_entry(the_lnet.ln_nis.next,
936 lnet_ni_t, ni_list);
937
938 list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
939 lnet_ni_decref_locked(ni, 0);
940
941 if (!list_empty(&ni->ni_cptlist)) {
942 list_del_init(&ni->ni_cptlist);
943 lnet_ni_decref_locked(ni, 0);
944 }
945 }
946
947
948 if (the_lnet.ln_eq_waitni != NULL) {
949 lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
950 the_lnet.ln_eq_waitni = NULL;
951 }
952
953
954 if (the_lnet.ln_loni != NULL) {
955 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
956 the_lnet.ln_loni = NULL;
957 }
958
959 lnet_net_unlock(LNET_LOCK_EX);
960
961
962
963 for (i = 0; i < the_lnet.ln_nportals; i++)
964 LNetClearLazyPortal(i);
965
966
967
968 lnet_peer_tables_cleanup();
969
970 lnet_net_lock(LNET_LOCK_EX);
971
972
973 i = 2;
974 while (!list_empty(&the_lnet.ln_nis_zombie)) {
975 int *ref;
976 int j;
977
978 ni = list_entry(the_lnet.ln_nis_zombie.next,
979 lnet_ni_t, ni_list);
980 list_del_init(&ni->ni_list);
981 cfs_percpt_for_each(ref, j, ni->ni_refs) {
982 if (*ref == 0)
983 continue;
984
985 list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
986 break;
987 }
988
989 while (!list_empty(&ni->ni_list)) {
990 lnet_net_unlock(LNET_LOCK_EX);
991 ++i;
992 if ((i & (-i)) == i) {
993 CDEBUG(D_WARNING,
994 "Waiting for zombie LNI %s\n",
995 libcfs_nid2str(ni->ni_nid));
996 }
997 cfs_pause(cfs_time_seconds(1));
998 lnet_net_lock(LNET_LOCK_EX);
999 continue;
1000 }
1001
1002 ni->ni_lnd->lnd_refcount--;
1003 lnet_net_unlock(LNET_LOCK_EX);
1004
1005 islo = ni->ni_lnd->lnd_type == LOLND;
1006
1007 LASSERT (!in_interrupt ());
1008 (ni->ni_lnd->lnd_shutdown)(ni);
1009
1010
1011
1012
1013 if (!islo)
1014 CDEBUG(D_LNI, "Removed LNI %s\n",
1015 libcfs_nid2str(ni->ni_nid));
1016
1017 lnet_ni_free(ni);
1018 lnet_net_lock(LNET_LOCK_EX);
1019 }
1020
1021 the_lnet.ln_shutdown = 0;
1022 lnet_net_unlock(LNET_LOCK_EX);
1023
1024 if (the_lnet.ln_network_tokens != NULL) {
1025 LIBCFS_FREE(the_lnet.ln_network_tokens,
1026 the_lnet.ln_network_tokens_nob);
1027 the_lnet.ln_network_tokens = NULL;
1028 }
1029}
1030
1031int
1032lnet_startup_lndnis (void)
1033{
1034 lnd_t *lnd;
1035 struct lnet_ni *ni;
1036 struct lnet_tx_queue *tq;
1037 struct list_head nilist;
1038 int i;
1039 int rc = 0;
1040 int lnd_type;
1041 int nicount = 0;
1042 char *nets = lnet_get_networks();
1043
1044 INIT_LIST_HEAD(&nilist);
1045
1046 if (nets == NULL)
1047 goto failed;
1048
1049 rc = lnet_parse_networks(&nilist, nets);
1050 if (rc != 0)
1051 goto failed;
1052
1053 while (!list_empty(&nilist)) {
1054 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1055 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1056
1057 LASSERT (libcfs_isknown_lnd(lnd_type));
1058
1059 if (lnd_type == CIBLND ||
1060 lnd_type == OPENIBLND ||
1061 lnd_type == IIBLND ||
1062 lnd_type == VIBLND) {
1063 CERROR("LND %s obsoleted\n",
1064 libcfs_lnd2str(lnd_type));
1065 goto failed;
1066 }
1067
1068 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1069 lnd = lnet_find_lnd_by_type(lnd_type);
1070
1071 if (lnd == NULL) {
1072 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1073 rc = request_module("%s",
1074 libcfs_lnd2modname(lnd_type));
1075 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1076
1077 lnd = lnet_find_lnd_by_type(lnd_type);
1078 if (lnd == NULL) {
1079 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1080 CERROR("Can't load LND %s, module %s, rc=%d\n",
1081 libcfs_lnd2str(lnd_type),
1082 libcfs_lnd2modname(lnd_type), rc);
1083 goto failed;
1084 }
1085 }
1086
1087 lnet_net_lock(LNET_LOCK_EX);
1088 lnd->lnd_refcount++;
1089 lnet_net_unlock(LNET_LOCK_EX);
1090
1091 ni->ni_lnd = lnd;
1092
1093 rc = (lnd->lnd_startup)(ni);
1094
1095 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1096
1097 if (rc != 0) {
1098 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1099 "\n",
1100 rc, libcfs_lnd2str(lnd->lnd_type));
1101 lnet_net_lock(LNET_LOCK_EX);
1102 lnd->lnd_refcount--;
1103 lnet_net_unlock(LNET_LOCK_EX);
1104 goto failed;
1105 }
1106
1107 LASSERT (ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1108
1109 list_del(&ni->ni_list);
1110
1111 lnet_net_lock(LNET_LOCK_EX);
1112
1113 lnet_ni_addref_locked(ni, 0);
1114 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1115 if (ni->ni_cpts != NULL) {
1116 list_add_tail(&ni->ni_cptlist,
1117 &the_lnet.ln_nis_cpt);
1118 lnet_ni_addref_locked(ni, 0);
1119 }
1120
1121 lnet_net_unlock(LNET_LOCK_EX);
1122
1123 if (lnd->lnd_type == LOLND) {
1124 lnet_ni_addref(ni);
1125 LASSERT (the_lnet.ln_loni == NULL);
1126 the_lnet.ln_loni = ni;
1127 continue;
1128 }
1129
1130 if (ni->ni_peertxcredits == 0 ||
1131 ni->ni_maxtxcredits == 0) {
1132 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1133 libcfs_lnd2str(lnd->lnd_type),
1134 ni->ni_peertxcredits == 0 ?
1135 "" : "per-peer ");
1136 goto failed;
1137 }
1138
1139 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1140 tq->tq_credits_min =
1141 tq->tq_credits_max =
1142 tq->tq_credits = lnet_ni_tq_credits(ni);
1143 }
1144
1145 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1146 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1147 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1148 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1149
1150 nicount++;
1151 }
1152
1153 if (the_lnet.ln_eq_waitni != NULL && nicount > 1) {
1154 lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1155 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1156 "\n",
1157 libcfs_lnd2str(lnd_type));
1158 goto failed;
1159 }
1160
1161 return 0;
1162
1163 failed:
1164 lnet_shutdown_lndnis();
1165
1166 while (!list_empty(&nilist)) {
1167 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1168 list_del(&ni->ni_list);
1169 lnet_ni_free(ni);
1170 }
1171
1172 return -ENETDOWN;
1173}
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185int
1186LNetInit(void)
1187{
1188 int rc;
1189
1190 lnet_assert_wire_constants();
1191 LASSERT(!the_lnet.ln_init);
1192
1193 memset(&the_lnet, 0, sizeof(the_lnet));
1194
1195
1196 the_lnet.ln_cpt_table = cfs_cpt_table;
1197 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1198
1199 LASSERT(the_lnet.ln_cpt_number > 0);
1200 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1201
1202 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1203 "please change setting of CPT-table and retry\n",
1204 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1205 return -1;
1206 }
1207
1208 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1209 the_lnet.ln_cpt_bits++;
1210
1211 rc = lnet_create_locks();
1212 if (rc != 0) {
1213 CERROR("Can't create LNet global locks: %d\n", rc);
1214 return -1;
1215 }
1216
1217 the_lnet.ln_refcount = 0;
1218 the_lnet.ln_init = 1;
1219 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1220 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1221 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1222 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1223
1224
1225
1226
1227 if (rnet_htable_size <= 0)
1228 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1229 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1230 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1231 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1232 order_base_2(rnet_htable_size) - 1);
1233
1234
1235
1236
1237 lnet_register_lnd(&the_lolnd);
1238 return 0;
1239}
1240EXPORT_SYMBOL(LNetInit);
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251void
1252LNetFini(void)
1253{
1254 LASSERT(the_lnet.ln_init);
1255 LASSERT(the_lnet.ln_refcount == 0);
1256
1257 while (!list_empty(&the_lnet.ln_lnds))
1258 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1259 lnd_t, lnd_list));
1260 lnet_destroy_locks();
1261
1262 the_lnet.ln_init = 0;
1263}
1264EXPORT_SYMBOL(LNetFini);
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282int
1283LNetNIInit(lnet_pid_t requested_pid)
1284{
1285 int im_a_router = 0;
1286 int rc;
1287
1288 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1289
1290 LASSERT (the_lnet.ln_init);
1291 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1292
1293 if (the_lnet.ln_refcount > 0) {
1294 rc = the_lnet.ln_refcount++;
1295 goto out;
1296 }
1297
1298 lnet_get_tunables();
1299
1300 if (requested_pid == LNET_PID_ANY) {
1301
1302 rc = -ENETDOWN;
1303 goto failed0;
1304 }
1305
1306 rc = lnet_prepare(requested_pid);
1307 if (rc != 0)
1308 goto failed0;
1309
1310 rc = lnet_startup_lndnis();
1311 if (rc != 0)
1312 goto failed1;
1313
1314 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1315 if (rc != 0)
1316 goto failed2;
1317
1318 rc = lnet_check_routes();
1319 if (rc != 0)
1320 goto failed2;
1321
1322 rc = lnet_rtrpools_alloc(im_a_router);
1323 if (rc != 0)
1324 goto failed2;
1325
1326 rc = lnet_acceptor_start();
1327 if (rc != 0)
1328 goto failed2;
1329
1330 the_lnet.ln_refcount = 1;
1331
1332
1333
1334
1335 rc = lnet_ping_target_init();
1336 if (rc != 0)
1337 goto failed3;
1338
1339 rc = lnet_router_checker_start();
1340 if (rc != 0)
1341 goto failed4;
1342
1343 lnet_proc_init();
1344 goto out;
1345
1346 failed4:
1347 lnet_ping_target_fini();
1348 failed3:
1349 the_lnet.ln_refcount = 0;
1350 lnet_acceptor_stop();
1351 failed2:
1352 lnet_destroy_routes();
1353 lnet_shutdown_lndnis();
1354 failed1:
1355 lnet_unprepare();
1356 failed0:
1357 LASSERT (rc < 0);
1358 out:
1359 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1360 return rc;
1361}
1362EXPORT_SYMBOL(LNetNIInit);
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373int
1374LNetNIFini(void)
1375{
1376 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1377
1378 LASSERT (the_lnet.ln_init);
1379 LASSERT (the_lnet.ln_refcount > 0);
1380
1381 if (the_lnet.ln_refcount != 1) {
1382 the_lnet.ln_refcount--;
1383 } else {
1384 LASSERT (!the_lnet.ln_niinit_self);
1385
1386 lnet_proc_fini();
1387 lnet_router_checker_stop();
1388 lnet_ping_target_fini();
1389
1390
1391 the_lnet.ln_refcount = 0;
1392
1393 lnet_acceptor_stop();
1394 lnet_destroy_routes();
1395 lnet_shutdown_lndnis();
1396 lnet_unprepare();
1397 }
1398
1399 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1400 return 0;
1401}
1402EXPORT_SYMBOL(LNetNIFini);
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417int
1418LNetCtl(unsigned int cmd, void *arg)
1419{
1420 struct libcfs_ioctl_data *data = arg;
1421 lnet_process_id_t id = {0};
1422 lnet_ni_t *ni;
1423 int rc;
1424
1425 LASSERT (the_lnet.ln_init);
1426 LASSERT (the_lnet.ln_refcount > 0);
1427
1428 switch (cmd) {
1429 case IOC_LIBCFS_GET_NI:
1430 rc = LNetGetId(data->ioc_count, &id);
1431 data->ioc_nid = id.nid;
1432 return rc;
1433
1434 case IOC_LIBCFS_FAIL_NID:
1435 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1436
1437 case IOC_LIBCFS_ADD_ROUTE:
1438 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1439 data->ioc_nid);
1440 return (rc != 0) ? rc : lnet_check_routes();
1441
1442 case IOC_LIBCFS_DEL_ROUTE:
1443 return lnet_del_route(data->ioc_net, data->ioc_nid);
1444
1445 case IOC_LIBCFS_GET_ROUTE:
1446 return lnet_get_route(data->ioc_count,
1447 &data->ioc_net, &data->ioc_count,
1448 &data->ioc_nid, &data->ioc_flags);
1449 case IOC_LIBCFS_NOTIFY_ROUTER:
1450 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1451 cfs_time_current() -
1452 cfs_time_seconds(cfs_time_current_sec() -
1453 (time_t)data->ioc_u64[0]));
1454
1455 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1456
1457 return 0;
1458
1459 case IOC_LIBCFS_LNET_DIST:
1460 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1461 if (rc < 0 && rc != -EHOSTUNREACH)
1462 return rc;
1463
1464 data->ioc_u32[0] = rc;
1465 return 0;
1466
1467 case IOC_LIBCFS_TESTPROTOCOMPAT:
1468 lnet_net_lock(LNET_LOCK_EX);
1469 the_lnet.ln_testprotocompat = data->ioc_flags;
1470 lnet_net_unlock(LNET_LOCK_EX);
1471 return 0;
1472
1473 case IOC_LIBCFS_PING:
1474 id.nid = data->ioc_nid;
1475 id.pid = data->ioc_u32[0];
1476 rc = lnet_ping(id, data->ioc_u32[1],
1477 (lnet_process_id_t *)data->ioc_pbuf1,
1478 data->ioc_plen1/sizeof(lnet_process_id_t));
1479 if (rc < 0)
1480 return rc;
1481 data->ioc_count = rc;
1482 return 0;
1483
1484 case IOC_LIBCFS_DEBUG_PEER: {
1485
1486
1487 id = *((lnet_process_id_t *) arg);
1488
1489 lnet_debug_peer(id.nid);
1490
1491 ni = lnet_net2ni(LNET_NIDNET(id.nid));
1492 if (ni == NULL) {
1493 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1494 } else {
1495 if (ni->ni_lnd->lnd_ctl == NULL) {
1496 CDEBUG(D_WARNING, "No ctl for %s\n",
1497 libcfs_id2str(id));
1498 } else {
1499 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1500 }
1501
1502 lnet_ni_decref(ni);
1503 }
1504 return 0;
1505 }
1506
1507 default:
1508 ni = lnet_net2ni(data->ioc_net);
1509 if (ni == NULL)
1510 return -EINVAL;
1511
1512 if (ni->ni_lnd->lnd_ctl == NULL)
1513 rc = -EINVAL;
1514 else
1515 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1516
1517 lnet_ni_decref(ni);
1518 return rc;
1519 }
1520
1521}
1522EXPORT_SYMBOL(LNetCtl);
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535int
1536LNetGetId(unsigned int index, lnet_process_id_t *id)
1537{
1538 struct lnet_ni *ni;
1539 struct list_head *tmp;
1540 int cpt;
1541 int rc = -ENOENT;
1542
1543 LASSERT(the_lnet.ln_init);
1544
1545
1546 if (the_lnet.ln_refcount == 0)
1547 return rc;
1548
1549 cpt = lnet_net_lock_current();
1550
1551 list_for_each(tmp, &the_lnet.ln_nis) {
1552 if (index-- != 0)
1553 continue;
1554
1555 ni = list_entry(tmp, lnet_ni_t, ni_list);
1556
1557 id->nid = ni->ni_nid;
1558 id->pid = the_lnet.ln_pid;
1559 rc = 0;
1560 break;
1561 }
1562
1563 lnet_net_unlock(cpt);
1564 return rc;
1565}
1566EXPORT_SYMBOL(LNetGetId);
1567
1568
1569
1570
1571
1572void
1573LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1574{
1575 snprintf(str, len, LPX64, h.cookie);
1576}
1577EXPORT_SYMBOL(LNetSnprintHandle);
1578
1579static int
1580lnet_create_ping_info(void)
1581{
1582 int i;
1583 int n;
1584 int rc;
1585 unsigned int infosz;
1586 lnet_ni_t *ni;
1587 lnet_process_id_t id;
1588 lnet_ping_info_t *pinfo;
1589
1590 for (n = 0; ; n++) {
1591 rc = LNetGetId(n, &id);
1592 if (rc == -ENOENT)
1593 break;
1594
1595 LASSERT (rc == 0);
1596 }
1597
1598 infosz = offsetof(lnet_ping_info_t, pi_ni[n]);
1599 LIBCFS_ALLOC(pinfo, infosz);
1600 if (pinfo == NULL) {
1601 CERROR("Can't allocate ping info[%d]\n", n);
1602 return -ENOMEM;
1603 }
1604
1605 pinfo->pi_nnis = n;
1606 pinfo->pi_pid = the_lnet.ln_pid;
1607 pinfo->pi_magic = LNET_PROTO_PING_MAGIC;
1608 pinfo->pi_features = LNET_PING_FEAT_NI_STATUS;
1609
1610 for (i = 0; i < n; i++) {
1611 lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1612
1613 rc = LNetGetId(i, &id);
1614 LASSERT (rc == 0);
1615
1616 ns->ns_nid = id.nid;
1617 ns->ns_status = LNET_NI_STATUS_UP;
1618
1619 lnet_net_lock(0);
1620
1621 ni = lnet_nid2ni_locked(id.nid, 0);
1622 LASSERT(ni != NULL);
1623
1624 lnet_ni_lock(ni);
1625 LASSERT(ni->ni_status == NULL);
1626 ni->ni_status = ns;
1627 lnet_ni_unlock(ni);
1628
1629 lnet_ni_decref_locked(ni, 0);
1630 lnet_net_unlock(0);
1631 }
1632
1633 the_lnet.ln_ping_info = pinfo;
1634 return 0;
1635}
1636
1637static void
1638lnet_destroy_ping_info(void)
1639{
1640 struct lnet_ni *ni;
1641
1642 lnet_net_lock(0);
1643
1644 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1645 lnet_ni_lock(ni);
1646 ni->ni_status = NULL;
1647 lnet_ni_unlock(ni);
1648 }
1649
1650 lnet_net_unlock(0);
1651
1652 LIBCFS_FREE(the_lnet.ln_ping_info,
1653 offsetof(lnet_ping_info_t,
1654 pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1655 the_lnet.ln_ping_info = NULL;
1656 return;
1657}
1658
1659int
1660lnet_ping_target_init(void)
1661{
1662 lnet_md_t md = {0};
1663 lnet_handle_me_t meh;
1664 lnet_process_id_t id;
1665 int rc;
1666 int rc2;
1667 int infosz;
1668
1669 rc = lnet_create_ping_info();
1670 if (rc != 0)
1671 return rc;
1672
1673
1674
1675 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1676 if (rc != 0) {
1677 CERROR("Can't allocate ping EQ: %d\n", rc);
1678 goto failed_0;
1679 }
1680
1681 memset(&id, 0, sizeof(lnet_process_id_t));
1682 id.nid = LNET_NID_ANY;
1683 id.pid = LNET_PID_ANY;
1684
1685 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1686 LNET_PROTO_PING_MATCHBITS, 0,
1687 LNET_UNLINK, LNET_INS_AFTER,
1688 &meh);
1689 if (rc != 0) {
1690 CERROR("Can't create ping ME: %d\n", rc);
1691 goto failed_1;
1692 }
1693
1694
1695 infosz = offsetof(lnet_ping_info_t,
1696 pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1697 md.start = the_lnet.ln_ping_info;
1698 md.length = infosz;
1699 md.threshold = LNET_MD_THRESH_INF;
1700 md.max_size = 0;
1701 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1702 LNET_MD_MANAGE_REMOTE;
1703 md.user_ptr = NULL;
1704 md.eq_handle = the_lnet.ln_ping_target_eq;
1705
1706 rc = LNetMDAttach(meh, md,
1707 LNET_RETAIN,
1708 &the_lnet.ln_ping_target_md);
1709 if (rc != 0) {
1710 CERROR("Can't attach ping MD: %d\n", rc);
1711 goto failed_2;
1712 }
1713
1714 return 0;
1715
1716 failed_2:
1717 rc2 = LNetMEUnlink(meh);
1718 LASSERT (rc2 == 0);
1719 failed_1:
1720 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1721 LASSERT (rc2 == 0);
1722 failed_0:
1723 lnet_destroy_ping_info();
1724 return rc;
1725}
1726
1727void
1728lnet_ping_target_fini(void)
1729{
1730 lnet_event_t event;
1731 int rc;
1732 int which;
1733 int timeout_ms = 1000;
1734 sigset_t blocked = cfs_block_allsigs();
1735
1736 LNetMDUnlink(the_lnet.ln_ping_target_md);
1737
1738
1739 for (;;) {
1740 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1741 timeout_ms, &event, &which);
1742
1743
1744 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1745
1746 if (rc == 0) {
1747
1748 CWARN("Still waiting for ping MD to unlink\n");
1749 timeout_ms *= 2;
1750 continue;
1751 }
1752
1753
1754 if (event.unlinked)
1755 break;
1756 }
1757
1758 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1759 LASSERT (rc == 0);
1760 lnet_destroy_ping_info();
1761 cfs_restore_sigs(blocked);
1762}
1763
1764int
1765lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1766{
1767 lnet_handle_eq_t eqh;
1768 lnet_handle_md_t mdh;
1769 lnet_event_t event;
1770 lnet_md_t md = {0};
1771 int which;
1772 int unlinked = 0;
1773 int replied = 0;
1774 const int a_long_time = 60000;
1775 int infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1776 lnet_ping_info_t *info;
1777 lnet_process_id_t tmpid;
1778 int i;
1779 int nob;
1780 int rc;
1781 int rc2;
1782 sigset_t blocked;
1783
1784 if (n_ids <= 0 ||
1785 id.nid == LNET_NID_ANY ||
1786 timeout_ms > 500000 ||
1787 n_ids > 20)
1788 return -EINVAL;
1789
1790 if (id.pid == LNET_PID_ANY)
1791 id.pid = LUSTRE_SRV_LNET_PID;
1792
1793 LIBCFS_ALLOC(info, infosz);
1794 if (info == NULL)
1795 return -ENOMEM;
1796
1797
1798 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1799 if (rc != 0) {
1800 CERROR("Can't allocate EQ: %d\n", rc);
1801 goto out_0;
1802 }
1803
1804
1805 md.start = info;
1806 md.length = infosz;
1807 md.threshold = 2;
1808 md.max_size = 0;
1809 md.options = LNET_MD_TRUNCATE;
1810 md.user_ptr = NULL;
1811 md.eq_handle = eqh;
1812
1813 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1814 if (rc != 0) {
1815 CERROR("Can't bind MD: %d\n", rc);
1816 goto out_1;
1817 }
1818
1819 rc = LNetGet(LNET_NID_ANY, mdh, id,
1820 LNET_RESERVED_PORTAL,
1821 LNET_PROTO_PING_MATCHBITS, 0);
1822
1823 if (rc != 0) {
1824
1825
1826 rc2 = LNetMDUnlink(mdh);
1827 LASSERT (rc2 == 0);
1828
1829
1830 unlinked = 1;
1831 timeout_ms = a_long_time;
1832 }
1833
1834 do {
1835
1836 if (unlinked)
1837 blocked = cfs_block_allsigs();
1838
1839 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1840
1841 if (unlinked)
1842 cfs_restore_sigs(blocked);
1843
1844 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1845 (rc2 <= 0) ? -1 : event.type,
1846 (rc2 <= 0) ? -1 : event.status,
1847 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1848
1849 LASSERT (rc2 != -EOVERFLOW);
1850
1851 if (rc2 <= 0 || event.status != 0) {
1852
1853 if (!replied && rc == 0)
1854 rc = (rc2 < 0) ? rc2 :
1855 (rc2 == 0) ? -ETIMEDOUT :
1856 event.status;
1857
1858 if (!unlinked) {
1859
1860 LNetMDUnlink(mdh);
1861
1862 unlinked = 1;
1863 timeout_ms = a_long_time;
1864 } else if (rc2 == 0) {
1865
1866 CWARN("ping %s: late network completion\n",
1867 libcfs_id2str(id));
1868 }
1869 } else if (event.type == LNET_EVENT_REPLY) {
1870 replied = 1;
1871 rc = event.mlength;
1872 }
1873
1874 } while (rc2 <= 0 || !event.unlinked);
1875
1876 if (!replied) {
1877 if (rc >= 0)
1878 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
1879 libcfs_id2str(id));
1880 rc = -EIO;
1881 goto out_1;
1882 }
1883
1884 nob = rc;
1885 LASSERT (nob >= 0 && nob <= infosz);
1886
1887 rc = -EPROTO;
1888
1889 if (nob < 8) {
1890
1891 CERROR("%s: ping info too short %d\n",
1892 libcfs_id2str(id), nob);
1893 goto out_1;
1894 }
1895
1896 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
1897 lnet_swap_pinginfo(info);
1898 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
1899 CERROR("%s: Unexpected magic %08x\n",
1900 libcfs_id2str(id), info->pi_magic);
1901 goto out_1;
1902 }
1903
1904 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
1905 CERROR("%s: ping w/o NI status: 0x%x\n",
1906 libcfs_id2str(id), info->pi_features);
1907 goto out_1;
1908 }
1909
1910 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
1911 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
1912 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
1913 goto out_1;
1914 }
1915
1916 if (info->pi_nnis < n_ids)
1917 n_ids = info->pi_nnis;
1918
1919 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
1920 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
1921 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
1922 goto out_1;
1923 }
1924
1925 rc = -EFAULT;
1926
1927 for (i = 0; i < n_ids; i++) {
1928 tmpid.pid = info->pi_pid;
1929 tmpid.nid = info->pi_ni[i].ns_nid;
1930 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
1931 goto out_1;
1932 }
1933 rc = info->pi_nnis;
1934
1935 out_1:
1936 rc2 = LNetEQFree(eqh);
1937 if (rc2 != 0)
1938 CERROR("rc2 %d\n", rc2);
1939 LASSERT (rc2 == 0);
1940
1941 out_0:
1942 LIBCFS_FREE(info, infosz);
1943 return rc;
1944}
1945