Whamcloud - gitweb
b=21500 2.6.31-fc12 patchless client support
[fs/lustre-release.git] / lnet / lnet / api-ni.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 <lnet/lib-lnet.h>
39
40 #ifdef __KERNEL__
41 #define D_LNI D_CONSOLE
42 #else
43 #define D_LNI D_CONFIG
44 #endif
45
46 lnet_t      the_lnet;                           /* THE state of the network */
47
48 #ifdef __KERNEL__
49
50 static char *ip2nets = "";
51 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
52                 "LNET network <- IP table");
53
54 static char *networks = "";
55 CFS_MODULE_PARM(networks, "s", charp, 0444,
56                 "local networks");
57
58 static char *routes = "";
59 CFS_MODULE_PARM(routes, "s", charp, 0444,
60                 "routes to non-local networks");
61
62 char *
63 lnet_get_routes(void)
64 {
65         return routes;
66 }
67
68 char *
69 lnet_get_networks(void)
70 {
71         char   *nets;
72         int     rc;
73
74         if (*networks != 0 && *ip2nets != 0) {
75                 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
76                                    "'ip2nets' but not both at once\n");
77                 return NULL;
78         }
79
80         if (*ip2nets != 0) {
81                 rc = lnet_parse_ip2nets(&nets, ip2nets);
82                 return (rc == 0) ? nets : NULL;
83         }
84
85         if (*networks != 0)
86                 return networks;
87
88         return "tcp";
89 }
90
91 void
92 lnet_init_locks(void)
93 {
94         cfs_spin_lock_init (&the_lnet.ln_lock);
95         cfs_waitq_init (&the_lnet.ln_waitq);
96         cfs_init_mutex(&the_lnet.ln_lnd_mutex);
97         cfs_init_mutex(&the_lnet.ln_api_mutex);
98 }
99
100 void
101 lnet_fini_locks(void)
102 {
103 }
104
105 #else
106
107 char *
108 lnet_get_routes(void)
109 {
110         char *str = getenv("LNET_ROUTES");
111
112         return (str == NULL) ? "" : str;
113 }
114
115 char *
116 lnet_get_networks (void)
117 {
118         static char       default_networks[256];
119         char             *networks = getenv ("LNET_NETWORKS");
120         char             *ip2nets  = getenv ("LNET_IP2NETS");
121         char             *str;
122         char             *sep;
123         int               len;
124         int               nob;
125         int               rc;
126         cfs_list_t       *tmp;
127
128 #ifdef NOT_YET
129         if (networks != NULL && ip2nets != NULL) {
130                 LCONSOLE_ERROR_MSG(0x103, "Please set EITHER 'LNET_NETWORKS' or"
131                                    " 'LNET_IP2NETS' but not both at once\n");
132                 return NULL;
133         }
134
135         if (ip2nets != NULL) {
136                 rc = lnet_parse_ip2nets(&networks, ip2nets);
137                 return (rc == 0) ? networks : NULL;
138         }
139 #else
140         ip2nets = NULL;
141         rc = 0;
142 #endif
143         if (networks != NULL)
144                 return networks;
145
146         /* In userland, the default 'networks=' is the list of known net types */
147
148         len = sizeof(default_networks);
149         str = default_networks;
150         *str = 0;
151         sep = "";
152
153         cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
154                 lnd_t *lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
155
156                 nob = snprintf(str, len, "%s%s", sep,
157                                libcfs_lnd2str(lnd->lnd_type));
158                 len -= nob;
159                 if (len < 0) {
160                         /* overflowed the string; leave it where it was */
161                         *str = 0;
162                         break;
163                 }
164
165                 str += nob;
166                 sep = ",";
167         }
168
169         return default_networks;
170 }
171
172 # ifndef HAVE_LIBPTHREAD
173
174 void lnet_init_locks(void)
175 {
176         the_lnet.ln_lock = 0;
177         the_lnet.ln_lnd_mutex = 0;
178         the_lnet.ln_api_mutex = 0;
179 }
180
181 void lnet_fini_locks(void)
182 {
183         LASSERT (the_lnet.ln_api_mutex == 0);
184         LASSERT (the_lnet.ln_lnd_mutex == 0);
185         LASSERT (the_lnet.ln_lock == 0);
186 }
187
188 # else
189
190 void lnet_init_locks(void)
191 {
192         pthread_cond_init(&the_lnet.ln_cond, NULL);
193         pthread_mutex_init(&the_lnet.ln_lock, NULL);
194         pthread_mutex_init(&the_lnet.ln_lnd_mutex, NULL);
195         pthread_mutex_init(&the_lnet.ln_api_mutex, NULL);
196 }
197
198 void lnet_fini_locks(void)
199 {
200         pthread_mutex_destroy(&the_lnet.ln_api_mutex);
201         pthread_mutex_destroy(&the_lnet.ln_lnd_mutex);
202         pthread_mutex_destroy(&the_lnet.ln_lock);
203         pthread_cond_destroy(&the_lnet.ln_cond);
204 }
205
206 # endif
207 #endif
208
209 void lnet_assert_wire_constants (void)
210 {
211         /* Wire protocol assertions generated by 'wirecheck'
212          * running on Linux robert.bartonsoftware.com 2.6.8-1.521
213          * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
214          * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
215
216         /* Constants... */
217         CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
218         CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
219         CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
220         CLASSERT (LNET_MSG_ACK == 0);
221         CLASSERT (LNET_MSG_PUT == 1);
222         CLASSERT (LNET_MSG_GET == 2);
223         CLASSERT (LNET_MSG_REPLY == 3);
224         CLASSERT (LNET_MSG_HELLO == 4);
225
226         /* Checks for struct ptl_handle_wire_t */
227         CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
228         CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
229         CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
230         CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
231         CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
232
233         /* Checks for struct lnet_magicversion_t */
234         CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
235         CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
236         CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
237         CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
238         CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
239         CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
240         CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
241
242         /* Checks for struct lnet_hdr_t */
243         CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
244         CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
245         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
246         CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
247         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
248         CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
249         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
250         CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
251         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
252         CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
253         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
254         CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
255         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
256         CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
257         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
258
259         /* Ack */
260         CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
261         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
262         CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
263         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
264         CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
265         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
266
267         /* Put */
268         CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
269         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
270         CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
271         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
272         CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
273         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
274         CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
275         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
276         CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
277         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
278
279         /* Get */
280         CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
281         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
282         CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
283         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
284         CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
285         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
286         CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
287         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
288         CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
289         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
290
291         /* Reply */
292         CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
293         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
294
295         /* Hello */
296         CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
297         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
298         CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
299         CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
300 }
301
302 lnd_t *
303 lnet_find_lnd_by_type (int type)
304 {
305         lnd_t              *lnd;
306         cfs_list_t         *tmp;
307
308         /* holding lnd mutex */
309         cfs_list_for_each (tmp, &the_lnet.ln_lnds) {
310                 lnd = cfs_list_entry(tmp, lnd_t, lnd_list);
311
312                 if ((int)lnd->lnd_type == type)
313                         return lnd;
314         }
315
316         return NULL;
317 }
318
319 void
320 lnet_register_lnd (lnd_t *lnd)
321 {
322         LNET_MUTEX_DOWN(&the_lnet.ln_lnd_mutex);
323
324         LASSERT (the_lnet.ln_init);
325         LASSERT (libcfs_isknown_lnd(lnd->lnd_type));
326         LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
327
328         cfs_list_add_tail (&lnd->lnd_list, &the_lnet.ln_lnds);
329         lnd->lnd_refcount = 0;
330
331         CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
332
333         LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
334 }
335
336 void
337 lnet_unregister_lnd (lnd_t *lnd)
338 {
339         LNET_MUTEX_DOWN(&the_lnet.ln_lnd_mutex);
340
341         LASSERT (the_lnet.ln_init);
342         LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
343         LASSERT (lnd->lnd_refcount == 0);
344
345         cfs_list_del (&lnd->lnd_list);
346         CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
347
348         LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
349 }
350
351 #ifndef LNET_USE_LIB_FREELIST
352
353 int
354 lnet_descriptor_setup (void)
355 {
356         return 0;
357 }
358
359 void
360 lnet_descriptor_cleanup (void)
361 {
362 }
363
364 #else
365
366 int
367 lnet_freelist_init (lnet_freelist_t *fl, int n, int size)
368 {
369         char *space;
370
371         LASSERT (n > 0);
372
373         size += offsetof (lnet_freeobj_t, fo_contents);
374
375         LIBCFS_ALLOC(space, n * size);
376         if (space == NULL)
377                 return (-ENOMEM);
378
379         CFS_INIT_LIST_HEAD (&fl->fl_list);
380         fl->fl_objs = space;
381         fl->fl_nobjs = n;
382         fl->fl_objsize = size;
383
384         do
385         {
386                 memset (space, 0, size);
387                 cfs_list_add ((cfs_list_t *)space, &fl->fl_list);
388                 space += size;
389         } while (--n != 0);
390
391         return (0);
392 }
393
394 void
395 lnet_freelist_fini (lnet_freelist_t *fl)
396 {
397         cfs_list_t       *el;
398         int               count;
399
400         if (fl->fl_nobjs == 0)
401                 return;
402
403         count = 0;
404         for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
405                 count++;
406
407         LASSERT (count == fl->fl_nobjs);
408
409         LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
410         memset (fl, 0, sizeof (*fl));
411 }
412
413 int
414 lnet_descriptor_setup (void)
415 {
416         /* NB on failure caller must still call lnet_descriptor_cleanup */
417         /*               ******                                         */
418         int        rc;
419
420         memset (&the_lnet.ln_free_mes,  0, sizeof (the_lnet.ln_free_mes));
421         memset (&the_lnet.ln_free_msgs, 0, sizeof (the_lnet.ln_free_msgs));
422         memset (&the_lnet.ln_free_mds,  0, sizeof (the_lnet.ln_free_mds));
423         memset (&the_lnet.ln_free_eqs,  0, sizeof (the_lnet.ln_free_eqs));
424
425         rc = lnet_freelist_init(&the_lnet.ln_free_mes,
426                                 MAX_MES, sizeof (lnet_me_t));
427         if (rc != 0)
428                 return (rc);
429
430         rc = lnet_freelist_init(&the_lnet.ln_free_msgs,
431                                 MAX_MSGS, sizeof (lnet_msg_t));
432         if (rc != 0)
433                 return (rc);
434
435         rc = lnet_freelist_init(&the_lnet.ln_free_mds,
436                                 MAX_MDS, sizeof (lnet_libmd_t));
437         if (rc != 0)
438                 return (rc);
439
440         rc = lnet_freelist_init(&the_lnet.ln_free_eqs,
441                                 MAX_EQS, sizeof (lnet_eq_t));
442         return (rc);
443 }
444
445 void
446 lnet_descriptor_cleanup (void)
447 {
448         lnet_freelist_fini (&the_lnet.ln_free_mes);
449         lnet_freelist_fini (&the_lnet.ln_free_msgs);
450         lnet_freelist_fini (&the_lnet.ln_free_mds);
451         lnet_freelist_fini (&the_lnet.ln_free_eqs);
452 }
453
454 #endif
455
456 __u64
457 lnet_create_interface_cookie (void)
458 {
459         /* NB the interface cookie in wire handles guards against delayed
460          * replies and ACKs appearing valid after reboot. Initialisation time,
461          * even if it's only implemented to millisecond resolution is probably
462          * easily good enough. */
463         struct timeval tv;
464         __u64          cookie;
465 #ifndef __KERNEL__
466         int            rc = gettimeofday (&tv, NULL);
467         LASSERT (rc == 0);
468 #else
469         cfs_gettimeofday(&tv);
470 #endif
471         cookie = tv.tv_sec;
472         cookie *= 1000000;
473         cookie += tv.tv_usec;
474         return cookie;
475 }
476
477 int
478 lnet_setup_handle_hash (void)
479 {
480         int       i;
481
482         /* Arbitrary choice of hash table size */
483 #ifdef __KERNEL__
484         the_lnet.ln_lh_hash_size =
485                 CFS_PAGE_SIZE / sizeof (cfs_list_t);
486 #else
487         the_lnet.ln_lh_hash_size = (MAX_MES + MAX_MDS + MAX_EQS)/4;
488 #endif
489         LIBCFS_ALLOC(the_lnet.ln_lh_hash_table,
490                      the_lnet.ln_lh_hash_size * sizeof (cfs_list_t));
491         if (the_lnet.ln_lh_hash_table == NULL)
492                 return (-ENOMEM);
493
494         for (i = 0; i < the_lnet.ln_lh_hash_size; i++)
495                 CFS_INIT_LIST_HEAD (&the_lnet.ln_lh_hash_table[i]);
496
497         the_lnet.ln_next_object_cookie = LNET_COOKIE_TYPES;
498
499         return (0);
500 }
501
502 void
503 lnet_cleanup_handle_hash (void)
504 {
505         if (the_lnet.ln_lh_hash_table == NULL)
506                 return;
507
508         LIBCFS_FREE(the_lnet.ln_lh_hash_table,
509                     the_lnet.ln_lh_hash_size * sizeof (cfs_list_t));
510 }
511
512 lnet_libhandle_t *
513 lnet_lookup_cookie (__u64 cookie, int type)
514 {
515         /* ALWAYS called with LNET_LOCK held */
516         cfs_list_t          *list;
517         cfs_list_t          *el;
518         unsigned int         hash;
519
520         if ((cookie & (LNET_COOKIE_TYPES - 1)) != type)
521                 return (NULL);
522
523         hash = ((unsigned int)cookie) % the_lnet.ln_lh_hash_size;
524         list = &the_lnet.ln_lh_hash_table[hash];
525
526         cfs_list_for_each (el, list) {
527                 lnet_libhandle_t *lh = cfs_list_entry (el, lnet_libhandle_t,
528                                                       lh_hash_chain);
529
530                 if (lh->lh_cookie == cookie)
531                         return (lh);
532         }
533
534         return (NULL);
535 }
536
537 void
538 lnet_initialise_handle (lnet_libhandle_t *lh, int type)
539 {
540         /* ALWAYS called with LNET_LOCK held */
541         unsigned int    hash;
542
543         LASSERT (type >= 0 && type < LNET_COOKIE_TYPES);
544         lh->lh_cookie = the_lnet.ln_next_object_cookie | type;
545         the_lnet.ln_next_object_cookie += LNET_COOKIE_TYPES;
546
547         hash = ((unsigned int)lh->lh_cookie) % the_lnet.ln_lh_hash_size;
548         cfs_list_add (&lh->lh_hash_chain, &the_lnet.ln_lh_hash_table[hash]);
549 }
550
551 void
552 lnet_invalidate_handle (lnet_libhandle_t *lh)
553 {
554         /* ALWAYS called with LNET_LOCK held */
555         cfs_list_del (&lh->lh_hash_chain);
556 }
557
558 int
559 lnet_init_finalizers(void)
560 {
561 #ifdef __KERNEL__
562         int    i;
563
564         the_lnet.ln_nfinalizers = (int) cfs_num_online_cpus();
565
566         LIBCFS_ALLOC(the_lnet.ln_finalizers,
567                      the_lnet.ln_nfinalizers *
568                      sizeof(*the_lnet.ln_finalizers));
569         if (the_lnet.ln_finalizers == NULL) {
570                 CERROR("Can't allocate ln_finalizers\n");
571                 return -ENOMEM;
572         }
573
574         for (i = 0; i < the_lnet.ln_nfinalizers; i++)
575                 the_lnet.ln_finalizers[i] = NULL;
576 #else
577         the_lnet.ln_finalizing = 0;
578 #endif
579
580         CFS_INIT_LIST_HEAD(&the_lnet.ln_finalizeq);
581         return 0;
582 }
583
584 void
585 lnet_fini_finalizers(void)
586 {
587 #ifdef __KERNEL__
588         int    i;
589
590         for (i = 0; i < the_lnet.ln_nfinalizers; i++)
591                 LASSERT (the_lnet.ln_finalizers[i] == NULL);
592
593         LIBCFS_FREE(the_lnet.ln_finalizers,
594                     the_lnet.ln_nfinalizers *
595                     sizeof(*the_lnet.ln_finalizers));
596 #else
597         LASSERT (!the_lnet.ln_finalizing);
598 #endif
599         LASSERT (cfs_list_empty(&the_lnet.ln_finalizeq));
600 }
601
602 #ifndef __KERNEL__
603 /* Temporary workaround to allow uOSS and test programs force server
604  * mode in userspace. See comments near ln_server_mode_flag in
605  * lnet/lib-types.h */
606
607 void
608 lnet_server_mode() {
609         the_lnet.ln_server_mode_flag = 1;
610 }
611 #endif
612
613 int
614 lnet_prepare(lnet_pid_t requested_pid)
615 {
616         /* Prepare to bring up the network */
617         int               rc = 0;
618         int               i;
619
620         LASSERT (the_lnet.ln_refcount == 0);
621
622         the_lnet.ln_routing = 0;
623
624 #ifdef __KERNEL__
625         LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
626         the_lnet.ln_pid = requested_pid;
627 #else
628         if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
629                 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
630
631                 if (cfs_curproc_uid())/* Only root can run user-space server */
632                         return -EPERM;
633                 the_lnet.ln_pid = requested_pid;
634
635         } else {/* client case (liblustre) */
636
637                 /* My PID must be unique on this node and flag I'm userspace */
638                 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
639         }
640 #endif
641
642         rc = lnet_descriptor_setup();
643         if (rc != 0)
644                 goto failed0;
645
646         memset(&the_lnet.ln_counters, 0,
647                sizeof(the_lnet.ln_counters));
648
649         CFS_INIT_LIST_HEAD (&the_lnet.ln_active_msgs);
650         CFS_INIT_LIST_HEAD (&the_lnet.ln_active_mds);
651         CFS_INIT_LIST_HEAD (&the_lnet.ln_active_eqs);
652         CFS_INIT_LIST_HEAD (&the_lnet.ln_test_peers);
653         CFS_INIT_LIST_HEAD (&the_lnet.ln_nis);
654         CFS_INIT_LIST_HEAD (&the_lnet.ln_zombie_nis);
655         CFS_INIT_LIST_HEAD (&the_lnet.ln_remote_nets);
656         CFS_INIT_LIST_HEAD (&the_lnet.ln_routers);
657
658         the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
659
660         lnet_init_rtrpools();
661
662         rc = lnet_setup_handle_hash ();
663         if (rc != 0)
664                 goto failed0;
665
666         rc = lnet_create_peer_table();
667         if (rc != 0)
668                 goto failed1;
669
670         rc = lnet_init_finalizers();
671         if (rc != 0)
672                 goto failed2;
673
674         the_lnet.ln_nportals = MAX_PORTALS;
675         LIBCFS_ALLOC(the_lnet.ln_portals,
676                      the_lnet.ln_nportals *
677                      sizeof(*the_lnet.ln_portals));
678         if (the_lnet.ln_portals == NULL) {
679                 rc = -ENOMEM;
680                 goto failed3;
681         }
682
683         for (i = 0; i < the_lnet.ln_nportals; i++) {
684                 CFS_INIT_LIST_HEAD(&(the_lnet.ln_portals[i].ptl_ml));
685                 CFS_INIT_LIST_HEAD(&(the_lnet.ln_portals[i].ptl_msgq));
686                 the_lnet.ln_portals[i].ptl_options = 0;
687         }
688
689         return 0;
690
691  failed3:
692         lnet_fini_finalizers();
693  failed2:
694         lnet_destroy_peer_table();
695  failed1:
696         lnet_cleanup_handle_hash();
697  failed0:
698         lnet_descriptor_cleanup();
699         return rc;
700 }
701
702 int
703 lnet_unprepare (void)
704 {
705         int       idx;
706
707         /* NB no LNET_LOCK since this is the last reference.  All LND instances
708          * have shut down already, so it is safe to unlink and free all
709          * descriptors, even those that appear committed to a network op (eg MD
710          * with non-zero pending count) */
711
712         lnet_fail_nid(LNET_NID_ANY, 0);
713
714         LASSERT (cfs_list_empty(&the_lnet.ln_test_peers));
715         LASSERT (the_lnet.ln_refcount == 0);
716         LASSERT (cfs_list_empty(&the_lnet.ln_nis));
717         LASSERT (cfs_list_empty(&the_lnet.ln_zombie_nis));
718         LASSERT (the_lnet.ln_nzombie_nis == 0);
719
720         for (idx = 0; idx < the_lnet.ln_nportals; idx++) {
721                 LASSERT (cfs_list_empty(&the_lnet.ln_portals[idx].ptl_msgq));
722
723                 while (!cfs_list_empty (&the_lnet.ln_portals[idx].ptl_ml)) {
724                         lnet_me_t *me = cfs_list_entry (the_lnet.ln_portals[idx].ptl_ml.next,
725                                                         lnet_me_t, me_list);
726
727                         CERROR ("Active me %p on exit\n", me);
728                         cfs_list_del (&me->me_list);
729                         lnet_me_free (me);
730                 }
731         }
732
733         while (!cfs_list_empty (&the_lnet.ln_active_mds)) {
734                 lnet_libmd_t *md = cfs_list_entry (the_lnet.ln_active_mds.next,
735                                                    lnet_libmd_t, md_list);
736
737                 CERROR ("Active md %p on exit\n", md);
738                 cfs_list_del_init (&md->md_list);
739                 lnet_md_free (md);
740         }
741
742         while (!cfs_list_empty (&the_lnet.ln_active_eqs)) {
743                 lnet_eq_t *eq = cfs_list_entry (the_lnet.ln_active_eqs.next,
744                                                 lnet_eq_t, eq_list);
745
746                 CERROR ("Active eq %p on exit\n", eq);
747                 cfs_list_del (&eq->eq_list);
748                 lnet_eq_free (eq);
749         }
750
751         while (!cfs_list_empty (&the_lnet.ln_active_msgs)) {
752                 lnet_msg_t *msg = cfs_list_entry (the_lnet.ln_active_msgs.next,
753                                                   lnet_msg_t, msg_activelist);
754
755                 CERROR ("Active msg %p on exit\n", msg);
756                 LASSERT (msg->msg_onactivelist);
757                 msg->msg_onactivelist = 0;
758                 cfs_list_del (&msg->msg_activelist);
759                 lnet_msg_free (msg);
760         }
761
762         LIBCFS_FREE(the_lnet.ln_portals,  
763                     the_lnet.ln_nportals * sizeof(*the_lnet.ln_portals));
764
765         lnet_free_rtrpools();
766         lnet_fini_finalizers();
767         lnet_destroy_peer_table();
768         lnet_cleanup_handle_hash();
769         lnet_descriptor_cleanup();
770
771         return (0);
772 }
773
774 lnet_ni_t  *
775 lnet_net2ni_locked (__u32 net)
776 {
777         cfs_list_t       *tmp;
778         lnet_ni_t        *ni;
779
780         cfs_list_for_each (tmp, &the_lnet.ln_nis) {
781                 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
782
783                 if (LNET_NIDNET(ni->ni_nid) == net) {
784                         lnet_ni_addref_locked(ni);
785                         return ni;
786                 }
787         }
788
789         return NULL;
790 }
791
792 int
793 lnet_islocalnet (__u32 net)
794 {
795         lnet_ni_t        *ni;
796
797         LNET_LOCK();
798         ni = lnet_net2ni_locked(net);
799         if (ni != NULL)
800                 lnet_ni_decref_locked(ni);
801         LNET_UNLOCK();
802
803         return ni != NULL;
804 }
805
806 lnet_ni_t  *
807 lnet_nid2ni_locked (lnet_nid_t nid)
808 {
809         cfs_list_t       *tmp;
810         lnet_ni_t        *ni;
811
812         cfs_list_for_each (tmp, &the_lnet.ln_nis) {
813                 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
814
815                 if (ni->ni_nid == nid) {
816                         lnet_ni_addref_locked(ni);
817                         return ni;
818                 }
819         }
820
821         return NULL;
822 }
823
824 int
825 lnet_islocalnid (lnet_nid_t nid)
826 {
827         lnet_ni_t     *ni;
828
829         LNET_LOCK();
830         ni = lnet_nid2ni_locked(nid);
831         if (ni != NULL)
832                 lnet_ni_decref_locked(ni);
833         LNET_UNLOCK();
834
835         return ni != NULL;
836 }
837
838 int
839 lnet_count_acceptor_nis (void)
840 {
841         /* Return the # of NIs that need the acceptor. */
842         int            count = 0;
843 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
844         cfs_list_t    *tmp;
845         lnet_ni_t     *ni;
846
847         LNET_LOCK();
848         cfs_list_for_each (tmp, &the_lnet.ln_nis) {
849                 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
850
851                 if (ni->ni_lnd->lnd_accept != NULL)
852                         count++;
853         }
854
855         LNET_UNLOCK();
856
857 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
858         return count;
859 }
860
861 void
862 lnet_shutdown_lndnis (void)
863 {
864         int                i;
865         int                islo;
866         lnet_ni_t         *ni;
867
868         /* NB called holding the global mutex */
869
870         /* All quiet on the API front */
871         LASSERT (!the_lnet.ln_shutdown);
872         LASSERT (the_lnet.ln_refcount == 0);
873         LASSERT (cfs_list_empty(&the_lnet.ln_zombie_nis));
874         LASSERT (the_lnet.ln_nzombie_nis == 0);
875         LASSERT (cfs_list_empty(&the_lnet.ln_remote_nets));
876
877         LNET_LOCK();
878         the_lnet.ln_shutdown = 1;               /* flag shutdown */
879
880         /* Unlink NIs from the global table */
881         while (!cfs_list_empty(&the_lnet.ln_nis)) {
882                 ni = cfs_list_entry(the_lnet.ln_nis.next,
883                                     lnet_ni_t, ni_list);
884                 cfs_list_del (&ni->ni_list);
885
886                 the_lnet.ln_nzombie_nis++;
887                 lnet_ni_decref_locked(ni); /* drop apini's ref */
888         }
889
890         /* Drop the cached eqwait NI. */
891         if (the_lnet.ln_eqwaitni != NULL) {
892                 lnet_ni_decref_locked(the_lnet.ln_eqwaitni);
893                 the_lnet.ln_eqwaitni = NULL;
894         }
895
896         /* Drop the cached loopback NI. */
897         if (the_lnet.ln_loni != NULL) {
898                 lnet_ni_decref_locked(the_lnet.ln_loni);
899                 the_lnet.ln_loni = NULL;
900         }
901
902         LNET_UNLOCK();
903
904         /* Clear lazy portals and drop delayed messages which hold refs
905          * on their lnet_msg_t::msg_rxpeer */
906         for (i = 0; i < the_lnet.ln_nportals; i++)
907                 LNetClearLazyPortal(i);
908
909         /* Clear the peer table and wait for all peers to go (they hold refs on
910          * their NIs) */
911         lnet_clear_peer_table();
912
913         LNET_LOCK();
914         /* Now wait for the NI's I just nuked to show up on apini_zombie_nis
915          * and shut them down in guaranteed thread context */
916         i = 2;
917         while (the_lnet.ln_nzombie_nis != 0) {
918
919                 while (cfs_list_empty(&the_lnet.ln_zombie_nis)) {
920                         LNET_UNLOCK();
921                         ++i;
922                         if ((i & (-i)) == i)
923                                 CDEBUG(D_WARNING,"Waiting for %d zombie NIs\n",
924                                        the_lnet.ln_nzombie_nis);
925                         cfs_pause(cfs_time_seconds(1));
926                         LNET_LOCK();
927                 }
928
929                 ni = cfs_list_entry(the_lnet.ln_zombie_nis.next,
930                                     lnet_ni_t, ni_list);
931                 cfs_list_del(&ni->ni_list);
932                 ni->ni_lnd->lnd_refcount--;
933
934                 LNET_UNLOCK();
935
936                 islo = ni->ni_lnd->lnd_type == LOLND;
937
938                 LASSERT (!cfs_in_interrupt ());
939                 (ni->ni_lnd->lnd_shutdown)(ni);
940
941                 /* can't deref lnd anymore now; it might have unregistered
942                  * itself...  */
943
944                 if (!islo)
945                         CDEBUG(D_LNI, "Removed LNI %s\n",
946                                libcfs_nid2str(ni->ni_nid));
947
948                 LIBCFS_FREE(ni, sizeof(*ni));
949
950                 LNET_LOCK();
951                 the_lnet.ln_nzombie_nis--;
952         }
953
954         the_lnet.ln_shutdown = 0;
955         LNET_UNLOCK();
956
957         if (the_lnet.ln_network_tokens != NULL) {
958                 LIBCFS_FREE(the_lnet.ln_network_tokens,
959                             the_lnet.ln_network_tokens_nob);
960                 the_lnet.ln_network_tokens = NULL;
961         }
962 }
963
964 int
965 lnet_startup_lndnis (void)
966 {
967         lnd_t             *lnd;
968         lnet_ni_t         *ni;
969         cfs_list_t         nilist;
970         int                rc = 0;
971         int                lnd_type;
972         int                nicount = 0;
973         char              *nets = lnet_get_networks();
974
975         CFS_INIT_LIST_HEAD(&nilist);
976
977         if (nets == NULL)
978                 goto failed;
979
980         rc = lnet_parse_networks(&nilist, nets);
981         if (rc != 0)
982                 goto failed;
983
984         while (!cfs_list_empty(&nilist)) {
985                 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
986                 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
987
988                 LASSERT (libcfs_isknown_lnd(lnd_type));
989
990                 LNET_MUTEX_DOWN(&the_lnet.ln_lnd_mutex);
991                 lnd = lnet_find_lnd_by_type(lnd_type);
992
993 #ifdef __KERNEL__
994                 if (lnd == NULL) {
995                         LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
996                         rc = cfs_request_module("%s",
997                                                 libcfs_lnd2modname(lnd_type));
998                         LNET_MUTEX_DOWN(&the_lnet.ln_lnd_mutex);
999
1000                         lnd = lnet_find_lnd_by_type(lnd_type);
1001                         if (lnd == NULL) {
1002                                 LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
1003                                 CERROR("Can't load LND %s, module %s, rc=%d\n",
1004                                        libcfs_lnd2str(lnd_type),
1005                                        libcfs_lnd2modname(lnd_type), rc);
1006 #ifndef HAVE_MODULE_LOADING_SUPPORT
1007                                 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1008                                          "compiled with kernel module "
1009                                          "loading support.");
1010 #endif
1011                                 goto failed;
1012                         }
1013                 }
1014 #else
1015                 if (lnd == NULL) {
1016                         LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
1017                         CERROR("LND %s not supported\n",
1018                                libcfs_lnd2str(lnd_type));
1019                         goto failed;
1020                 }
1021 #endif
1022
1023                 ni->ni_refcount = 1;
1024
1025                 LNET_LOCK();
1026                 lnd->lnd_refcount++;
1027                 LNET_UNLOCK();
1028
1029                 ni->ni_lnd = lnd;
1030
1031                 rc = (lnd->lnd_startup)(ni);
1032
1033                 LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
1034
1035                 if (rc != 0) {
1036                         LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1037                                            "\n",
1038                                            rc, libcfs_lnd2str(lnd->lnd_type));
1039                         LNET_LOCK();
1040                         lnd->lnd_refcount--;
1041                         LNET_UNLOCK();
1042                         goto failed;
1043                 }
1044
1045                 LASSERT (ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1046
1047                 cfs_list_del(&ni->ni_list);
1048
1049                 LNET_LOCK();
1050                 cfs_list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1051                 LNET_UNLOCK();
1052
1053                 if (lnd->lnd_type == LOLND) {
1054                         lnet_ni_addref(ni);
1055                         LASSERT (the_lnet.ln_loni == NULL);
1056                         the_lnet.ln_loni = ni;
1057                         continue;
1058                 }
1059
1060 #ifndef __KERNEL__
1061                 if (lnd->lnd_wait != NULL) {
1062                         if (the_lnet.ln_eqwaitni == NULL) {
1063                                 lnet_ni_addref(ni);
1064                                 the_lnet.ln_eqwaitni = ni;
1065                         }
1066                 } else {
1067 # ifndef HAVE_LIBPTHREAD
1068                         LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1069                                            "single-threaded runtime\n",
1070                                            libcfs_lnd2str(lnd_type));
1071                         goto failed;
1072 # endif
1073                 }
1074 #endif
1075                 if (ni->ni_peertxcredits == 0 ||
1076                     ni->ni_maxtxcredits == 0) {
1077                         LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1078                                            libcfs_lnd2str(lnd->lnd_type),
1079                                            ni->ni_peertxcredits == 0 ?
1080                                            "" : "per-peer ");
1081                         goto failed;
1082                 }
1083
1084                 ni->ni_txcredits = ni->ni_mintxcredits = ni->ni_maxtxcredits;
1085
1086                 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1087                        libcfs_nid2str(ni->ni_nid),
1088                        ni->ni_peertxcredits, ni->ni_txcredits,
1089                        ni->ni_peerrtrcredits, ni->ni_peertimeout);
1090
1091                 nicount++;
1092         }
1093
1094         if (the_lnet.ln_eqwaitni != NULL && nicount > 1) {
1095                 lnd_type = the_lnet.ln_eqwaitni->ni_lnd->lnd_type;
1096                 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1097                                    "\n",
1098                                    libcfs_lnd2str(lnd_type));
1099                 goto failed;
1100         }
1101
1102         return 0;
1103
1104  failed:
1105         lnet_shutdown_lndnis();
1106
1107         while (!cfs_list_empty(&nilist)) {
1108                 ni = cfs_list_entry(nilist.next, lnet_ni_t, ni_list);
1109                 cfs_list_del(&ni->ni_list);
1110                 LIBCFS_FREE(ni, sizeof(*ni));
1111         }
1112
1113         return -ENETDOWN;
1114 }
1115
1116 int
1117 LNetInit(void)
1118 {
1119         lnet_assert_wire_constants ();
1120         LASSERT (!the_lnet.ln_init);
1121
1122         memset(&the_lnet, 0, sizeof(the_lnet));
1123
1124         lnet_init_locks();
1125         the_lnet.ln_refcount = 0;
1126         the_lnet.ln_init = 1;
1127         LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1128         CFS_INIT_LIST_HEAD(&the_lnet.ln_lnds);
1129         CFS_INIT_LIST_HEAD(&the_lnet.ln_zombie_rcd);
1130
1131 #ifdef __KERNEL__
1132         /* All LNDs apart from the LOLND are in separate modules.  They
1133          * register themselves when their module loads, and unregister
1134          * themselves when their module is unloaded. */
1135 #else
1136         /* Register LNDs
1137          * NB the order here determines default 'networks=' order */
1138 # ifdef CRAY_XT3
1139         LNET_REGISTER_ULND(the_ptllnd);
1140 # endif
1141 # ifdef HAVE_LIBPTHREAD
1142         LNET_REGISTER_ULND(the_tcplnd);
1143 # endif
1144 #endif
1145         lnet_register_lnd(&the_lolnd);
1146         return 0;
1147 }
1148
1149 void
1150 LNetFini(void)
1151 {
1152         LASSERT (the_lnet.ln_init);
1153         LASSERT (the_lnet.ln_refcount == 0);
1154
1155         while (!cfs_list_empty(&the_lnet.ln_lnds))
1156                 lnet_unregister_lnd(cfs_list_entry(the_lnet.ln_lnds.next,
1157                                                    lnd_t, lnd_list));
1158         lnet_fini_locks();
1159
1160         the_lnet.ln_init = 0;
1161 }
1162
1163 int
1164 LNetNIInit(lnet_pid_t requested_pid)
1165 {
1166         int         im_a_router = 0;
1167         int         rc;
1168
1169         LNET_MUTEX_DOWN(&the_lnet.ln_api_mutex);
1170
1171         LASSERT (the_lnet.ln_init);
1172         CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1173
1174         if (the_lnet.ln_refcount > 0) {
1175                 rc = the_lnet.ln_refcount++;
1176                 goto out;
1177         }
1178
1179         lnet_get_tunables();
1180
1181         if (requested_pid == LNET_PID_ANY) {
1182                 /* Don't instantiate LNET just for me */
1183                 rc = -ENETDOWN;
1184                 goto failed0;
1185         }
1186
1187         rc = lnet_prepare(requested_pid);
1188         if (rc != 0)
1189                 goto failed0;
1190
1191         rc = lnet_startup_lndnis();
1192         if (rc != 0)
1193                 goto failed1;
1194
1195         rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1196         if (rc != 0)
1197                 goto failed2;
1198
1199         rc = lnet_check_routes();
1200         if (rc != 0)
1201                 goto failed2;
1202
1203         rc = lnet_alloc_rtrpools(im_a_router);
1204         if (rc != 0)
1205                 goto failed2;
1206
1207         rc = lnet_acceptor_start();
1208         if (rc != 0)
1209                 goto failed2;
1210
1211         the_lnet.ln_refcount = 1;
1212         /* Now I may use my own API functions... */
1213
1214         /* NB router checker needs the_lnet.ln_ping_info in
1215          * lnet_router_checker -> lnet_update_ni_status */
1216         rc = lnet_ping_target_init();
1217         if (rc != 0)
1218                 goto failed3;
1219
1220         rc = lnet_router_checker_start();
1221         if (rc != 0)
1222                 goto failed4;
1223
1224         lnet_proc_init();
1225         goto out;
1226
1227  failed4:
1228         lnet_ping_target_fini();
1229  failed3:
1230         the_lnet.ln_refcount = 0;
1231         lnet_acceptor_stop();
1232  failed2:
1233         lnet_destroy_routes();
1234         lnet_shutdown_lndnis();
1235  failed1:
1236         lnet_unprepare();
1237  failed0:
1238         LASSERT (rc < 0);
1239  out:
1240         LNET_MUTEX_UP(&the_lnet.ln_api_mutex);
1241         return rc;
1242 }
1243
1244 int
1245 LNetNIFini()
1246 {
1247         LNET_MUTEX_DOWN(&the_lnet.ln_api_mutex);
1248
1249         LASSERT (the_lnet.ln_init);
1250         LASSERT (the_lnet.ln_refcount > 0);
1251
1252         if (the_lnet.ln_refcount != 1) {
1253                 the_lnet.ln_refcount--;
1254         } else {
1255                 LASSERT (!the_lnet.ln_niinit_self);
1256
1257                 lnet_proc_fini();
1258                 lnet_router_checker_stop();
1259                 lnet_ping_target_fini();
1260
1261                 /* Teardown fns that use my own API functions BEFORE here */
1262                 the_lnet.ln_refcount = 0;
1263
1264                 lnet_acceptor_stop();
1265                 lnet_destroy_routes();
1266                 lnet_shutdown_lndnis();
1267                 lnet_unprepare();
1268         }
1269
1270         LNET_MUTEX_UP(&the_lnet.ln_api_mutex);
1271         return 0;
1272 }
1273
1274 int
1275 LNetCtl(unsigned int cmd, void *arg)
1276 {
1277         struct libcfs_ioctl_data *data = arg;
1278         lnet_process_id_t         id = {0};
1279         lnet_ni_t                *ni;
1280         int                       rc;
1281
1282         LASSERT (the_lnet.ln_init);
1283         LASSERT (the_lnet.ln_refcount > 0);
1284
1285         switch (cmd) {
1286         case IOC_LIBCFS_GET_NI:
1287                 rc = LNetGetId(data->ioc_count, &id);
1288                 data->ioc_nid = id.nid;
1289                 return rc;
1290
1291         case IOC_LIBCFS_FAIL_NID:
1292                 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1293
1294         case IOC_LIBCFS_ADD_ROUTE:
1295                 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1296                                     data->ioc_nid);
1297                 return (rc != 0) ? rc : lnet_check_routes();
1298
1299         case IOC_LIBCFS_DEL_ROUTE:
1300                 return lnet_del_route(data->ioc_net, data->ioc_nid);
1301
1302         case IOC_LIBCFS_GET_ROUTE:
1303                 return lnet_get_route(data->ioc_count,
1304                                       &data->ioc_net, &data->ioc_count,
1305                                       &data->ioc_nid, &data->ioc_flags);
1306         case IOC_LIBCFS_NOTIFY_ROUTER:
1307                 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1308                                    cfs_time_current() -
1309                                    cfs_time_seconds(cfs_time_current_sec() -
1310                                                     (time_t)data->ioc_u64[0]));
1311
1312         case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1313                 /* This can be removed once lustre stops calling it */
1314                 return 0;
1315
1316         case IOC_LIBCFS_LNET_DIST:
1317                 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1318                 if (rc < 0 && rc != -EHOSTUNREACH)
1319                         return rc;
1320
1321                 data->ioc_u32[0] = rc;
1322                 return 0;
1323
1324         case IOC_LIBCFS_TESTPROTOCOMPAT:
1325                 LNET_LOCK();
1326                 the_lnet.ln_testprotocompat = data->ioc_flags;
1327                 LNET_UNLOCK();
1328                 return 0;
1329
1330         case IOC_LIBCFS_PING:
1331                 id.nid = data->ioc_nid;
1332                 id.pid = data->ioc_u32[0];
1333                 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1334                                (lnet_process_id_t *)data->ioc_pbuf1,
1335                                data->ioc_plen1/sizeof(lnet_process_id_t));
1336                 if (rc < 0)
1337                         return rc;
1338                 data->ioc_count = rc;
1339                 return 0;
1340
1341         case IOC_LIBCFS_DEBUG_PEER: {
1342                 /* CAVEAT EMPTOR: this one designed for calling directly; not
1343                  * via an ioctl */
1344                 id = *((lnet_process_id_t *) arg);
1345
1346                 lnet_debug_peer(id.nid);
1347
1348                 ni = lnet_net2ni(LNET_NIDNET(id.nid));
1349                 if (ni == NULL) {
1350                         CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1351                 } else {
1352                         if (ni->ni_lnd->lnd_ctl == NULL) {
1353                                 CDEBUG(D_WARNING, "No ctl for %s\n",
1354                                        libcfs_id2str(id));
1355                         } else {
1356                                 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1357                         }
1358
1359                         lnet_ni_decref(ni);
1360                 }
1361                 return 0;
1362         }
1363
1364         default:
1365                 ni = lnet_net2ni(data->ioc_net);
1366                 if (ni == NULL)
1367                         return -EINVAL;
1368
1369                 if (ni->ni_lnd->lnd_ctl == NULL)
1370                         rc = -EINVAL;
1371                 else
1372                         rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1373
1374                 lnet_ni_decref(ni);
1375                 return rc;
1376         }
1377         /* not reached */
1378 }
1379
1380 int
1381 LNetGetId(unsigned int index, lnet_process_id_t *id)
1382 {
1383         lnet_ni_t        *ni;
1384         cfs_list_t       *tmp;
1385         int               rc = -ENOENT;
1386
1387         LASSERT (the_lnet.ln_init);
1388         LASSERT (the_lnet.ln_refcount > 0);
1389
1390         LNET_LOCK();
1391
1392         cfs_list_for_each(tmp, &the_lnet.ln_nis) {
1393                 if (index-- != 0)
1394                         continue;
1395
1396                 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
1397
1398                 id->nid = ni->ni_nid;
1399                 id->pid = the_lnet.ln_pid;
1400                 rc = 0;
1401                 break;
1402         }
1403
1404         LNET_UNLOCK();
1405
1406         return rc;
1407 }
1408
1409 void
1410 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1411 {
1412         snprintf(str, len, LPX64, h.cookie);
1413 }
1414
1415 static int
1416 lnet_create_ping_info(void)
1417 {
1418         int               i;
1419         int               n;
1420         int               rc;
1421         unsigned int      infosz;
1422         lnet_ni_t        *ni;
1423         lnet_process_id_t id;
1424         lnet_ping_info_t *pinfo;
1425
1426         for (n = 0; ; n++) {
1427                 rc = LNetGetId(n, &id);
1428                 if (rc == -ENOENT)
1429                         break;
1430
1431                 LASSERT (rc == 0);
1432         }
1433
1434         infosz = offsetof(lnet_ping_info_t, pi_ni[n]);
1435         LIBCFS_ALLOC(pinfo, infosz);
1436         if (pinfo == NULL) {
1437                 CERROR("Can't allocate ping info[%d]\n", n);
1438                 return -ENOMEM;
1439         }
1440
1441         pinfo->pi_nnis    = n;
1442         pinfo->pi_pid     = the_lnet.ln_pid;
1443         pinfo->pi_magic   = LNET_PROTO_PING_MAGIC;
1444         pinfo->pi_version = LNET_PROTO_PING_VERSION;
1445
1446         for (i = 0; i < n; i++) {
1447                 lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1448
1449                 rc = LNetGetId(i, &id);
1450                 LASSERT (rc == 0);
1451
1452                 ns->ns_nid    = id.nid;
1453                 ns->ns_status = LNET_NI_STATUS_UP;
1454
1455                 LNET_LOCK();
1456
1457                 ni = lnet_nid2ni_locked(id.nid);
1458                 LASSERT (ni != NULL);
1459                 LASSERT (ni->ni_status == NULL);
1460                 ni->ni_status = ns;
1461                 lnet_ni_decref_locked(ni);
1462
1463                 LNET_UNLOCK();
1464         }
1465
1466         the_lnet.ln_ping_info = pinfo;
1467         return 0;
1468 }
1469
1470 static void
1471 lnet_destroy_ping_info(void)
1472 {
1473         lnet_ni_t *ni;
1474
1475         LNET_LOCK();
1476
1477         cfs_list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
1478                 ni->ni_status = NULL;
1479         }
1480
1481         LNET_UNLOCK();
1482
1483         LIBCFS_FREE(the_lnet.ln_ping_info,
1484                     offsetof(lnet_ping_info_t,
1485                              pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1486         the_lnet.ln_ping_info = NULL;
1487         return;
1488 }
1489
1490 int
1491 lnet_ping_target_init(void)
1492 {
1493         lnet_md_t         md = {0};
1494         lnet_handle_me_t  meh;
1495         lnet_process_id_t id;
1496         int               rc;
1497         int               rc2;
1498         int               infosz;
1499
1500         rc = lnet_create_ping_info();
1501         if (rc != 0)
1502                 return rc;
1503
1504         /* We can have a tiny EQ since we only need to see the unlink event on
1505          * teardown, which by definition is the last one! */
1506         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1507         if (rc != 0) {
1508                 CERROR("Can't allocate ping EQ: %d\n", rc);
1509                 goto failed_0;
1510         }
1511
1512         memset(&id, 0, sizeof(lnet_process_id_t));
1513         id.nid = LNET_NID_ANY;
1514         id.pid = LNET_PID_ANY;
1515
1516         rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1517                           LNET_PROTO_PING_MATCHBITS, 0,
1518                           LNET_UNLINK, LNET_INS_AFTER,
1519                           &meh);
1520         if (rc != 0) {
1521                 CERROR("Can't create ping ME: %d\n", rc);
1522                 goto failed_1;
1523         }
1524
1525         /* initialize md content */
1526         infosz = offsetof(lnet_ping_info_t,
1527                           pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1528         md.start     = the_lnet.ln_ping_info;
1529         md.length    = infosz;
1530         md.threshold = LNET_MD_THRESH_INF;
1531         md.max_size  = 0;
1532         md.options   = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1533                        LNET_MD_MANAGE_REMOTE;
1534         md.user_ptr  = NULL;
1535         md.eq_handle = the_lnet.ln_ping_target_eq;
1536
1537         rc = LNetMDAttach(meh, md,
1538                           LNET_RETAIN,
1539                           &the_lnet.ln_ping_target_md);
1540         if (rc != 0) {
1541                 CERROR("Can't attach ping MD: %d\n", rc);
1542                 goto failed_2;
1543         }
1544
1545         return 0;
1546
1547  failed_2:
1548         rc2 = LNetMEUnlink(meh);
1549         LASSERT (rc2 == 0);
1550  failed_1:
1551         rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1552         LASSERT (rc2 == 0);
1553  failed_0:
1554         lnet_destroy_ping_info();
1555         return rc;
1556 }
1557
1558 void
1559 lnet_ping_target_fini(void)
1560 {
1561         lnet_event_t    event;
1562         int             rc;
1563         int             which;
1564         int             timeout_ms = 1000;
1565         cfs_sigset_t    blocked = cfs_block_allsigs();
1566
1567         LNetMDUnlink(the_lnet.ln_ping_target_md);
1568         /* NB md could be busy; this just starts the unlink */
1569
1570         for (;;) {
1571                 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1572                                 timeout_ms, &event, &which);
1573
1574                 /* I expect overflow... */
1575                 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1576
1577                 if (rc == 0) {
1578                         /* timed out: provide a diagnostic */
1579                         CWARN("Still waiting for ping MD to unlink\n");
1580                         timeout_ms *= 2;
1581                         continue;
1582                 }
1583
1584                 /* Got a valid event */
1585                 if (event.unlinked)
1586                         break;
1587         }
1588
1589         rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1590         LASSERT (rc == 0);
1591         lnet_destroy_ping_info();
1592         cfs_restore_sigs(blocked);
1593 }
1594
1595 int
1596 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1597 {
1598         lnet_handle_eq_t     eqh;
1599         lnet_handle_md_t     mdh;
1600         lnet_event_t         event;
1601         lnet_md_t            md = {0};
1602         int                  which;
1603         int                  unlinked = 0;
1604         int                  replied = 0;
1605         const int            a_long_time = 60000; /* mS */
1606         int                  infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1607         lnet_ping_info_t    *info;
1608         lnet_process_id_t    tmpid;
1609         int                  i;
1610         int                  nob;
1611         int                  rc;
1612         int                  rc2;
1613         cfs_sigset_t         blocked;
1614
1615         if (n_ids <= 0 ||
1616             id.nid == LNET_NID_ANY ||
1617             timeout_ms > 500000 ||              /* arbitrary limit! */
1618             n_ids > 20)                         /* arbitrary limit! */
1619                 return -EINVAL;
1620
1621         if (id.pid == LNET_PID_ANY)
1622                 id.pid = LUSTRE_SRV_LNET_PID;
1623
1624         LIBCFS_ALLOC(info, infosz);
1625         if (info == NULL)
1626                 return -ENOMEM;
1627
1628         /* NB 2 events max (including any unlink event) */
1629         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1630         if (rc != 0) {
1631                 CERROR("Can't allocate EQ: %d\n", rc);
1632                 goto out_0;
1633         }
1634
1635         /* initialize md content */
1636         md.start     = info;
1637         md.length    = infosz;
1638         md.threshold = 2; /*GET/REPLY*/
1639         md.max_size  = 0;
1640         md.options   = LNET_MD_TRUNCATE;
1641         md.user_ptr  = NULL;
1642         md.eq_handle = eqh;
1643
1644         rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1645         if (rc != 0) {
1646                 CERROR("Can't bind MD: %d\n", rc);
1647                 goto out_1;
1648         }
1649
1650         rc = LNetGet(LNET_NID_ANY, mdh, id,
1651                      LNET_RESERVED_PORTAL,
1652                      LNET_PROTO_PING_MATCHBITS, 0);
1653
1654         if (rc != 0) {
1655                 /* Don't CERROR; this could be deliberate! */
1656
1657                 rc2 = LNetMDUnlink(mdh);
1658                 LASSERT (rc2 == 0);
1659
1660                 /* NB must wait for the UNLINK event below... */
1661                 unlinked = 1;
1662                 timeout_ms = a_long_time;
1663         }
1664
1665         do {
1666                 /* MUST block for unlink to complete */
1667                 if (unlinked)
1668                         blocked = cfs_block_allsigs();
1669
1670                 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1671
1672                 if (unlinked)
1673                         cfs_restore_sigs(blocked);
1674
1675                 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1676                        (rc2 <= 0) ? -1 : event.type,
1677                        (rc2 <= 0) ? -1 : event.status,
1678                        (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1679
1680                 LASSERT (rc2 != -EOVERFLOW);     /* can't miss anything */
1681
1682                 if (rc2 <= 0 || event.status != 0) {
1683                         /* timeout or error */
1684                         if (!replied && rc == 0)
1685                                 rc = (rc2 < 0) ? rc2 :
1686                                      (rc2 == 0) ? -ETIMEDOUT :
1687                                      event.status;
1688
1689                         if (!unlinked) {
1690                                 /* Ensure completion in finite time... */
1691                                 LNetMDUnlink(mdh);
1692                                 /* No assertion (racing with network) */
1693                                 unlinked = 1;
1694                                 timeout_ms = a_long_time;
1695                         } else if (rc2 == 0) {
1696                                 /* timed out waiting for unlink */
1697                                 CWARN("ping %s: late network completion\n",
1698                                       libcfs_id2str(id));
1699                         }
1700                 } else if (event.type == LNET_EVENT_REPLY) {
1701                         replied = 1;
1702                         rc = event.mlength;
1703                 }
1704
1705         } while (rc2 <= 0 || !event.unlinked);
1706
1707         if (!replied) {
1708                 if (rc >= 0)
1709                         CWARN("%s: Unexpected rc >= 0 but no reply!\n",
1710                               libcfs_id2str(id));
1711                 rc = -EIO;
1712                 goto out_1;
1713         }
1714
1715         nob = rc;
1716         LASSERT (nob >= 0 && nob <= infosz);
1717
1718         rc = -EPROTO;                           /* if I can't parse... */
1719
1720         if (nob < 8) {
1721                 /* can't check magic/version */
1722                 CERROR("%s: ping info too short %d\n",
1723                        libcfs_id2str(id), nob);
1724                 goto out_1;
1725         }
1726
1727         if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
1728                 lnet_swap_pinginfo(info);
1729         } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
1730                 CERROR("%s: Unexpected magic %08x\n", 
1731                        libcfs_id2str(id), info->pi_magic);
1732                 goto out_1;
1733         }
1734
1735         if (info->pi_version != LNET_PROTO_PING_VERSION) {
1736                 CERROR("%s: Unexpected version 0x%x\n",
1737                        libcfs_id2str(id), info->pi_version);
1738                 goto out_1;
1739         }
1740
1741         if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
1742                 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
1743                        nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
1744                 goto out_1;
1745         }
1746
1747         if (info->pi_nnis < n_ids)
1748                 n_ids = info->pi_nnis;
1749
1750         if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
1751                 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
1752                        nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
1753                 goto out_1;
1754         }
1755
1756         rc = -EFAULT;                           /* If I SEGV... */
1757
1758         for (i = 0; i < n_ids; i++) {
1759                 tmpid.pid = info->pi_pid;
1760                 tmpid.nid = info->pi_ni[i].ns_nid;
1761 #ifdef __KERNEL__
1762                 if (cfs_copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
1763                         goto out_1;
1764 #else
1765                 ids[i] = tmpid;
1766 #endif
1767         }
1768         rc = info->pi_nnis;
1769
1770  out_1:
1771         rc2 = LNetEQFree(eqh);
1772         if (rc2 != 0)
1773                 CERROR("rc2 %d\n", rc2);
1774         LASSERT (rc2 == 0);
1775
1776  out_0:
1777         LIBCFS_FREE(info, infosz);
1778         return rc;
1779 }