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