Whamcloud - gitweb
e2bfe89fc988e6b92c08476c8a60ce506793a350
[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 [sun.com URL with a
20  * copy of GPLv2].
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                 return 1;
104                 LCONSOLE_WARN("Starting in weak portals-compatible mode\n");
105         }
106
107         if (!strcmp(portals_compatibility, "strong")) {
108                 return 2;
109                 LCONSOLE_WARN("Starting in strong portals-compatible mode\n");
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 (&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(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                 list_del(&ni->ni_list);
1084
1085                 LNET_LOCK();
1086                 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1087                 LNET_UNLOCK();
1088
1089                 if (lnd->lnd_type == LOLND) {
1090                         lnet_ni_addref(ni);
1091                         LASSERT (the_lnet.ln_loni == NULL);
1092                         the_lnet.ln_loni = ni;
1093                         continue;
1094                 }
1095
1096 #ifndef __KERNEL__
1097                 if (lnd->lnd_wait != NULL) {
1098                         if (the_lnet.ln_eqwaitni == NULL) {
1099                                 lnet_ni_addref(ni);
1100                                 the_lnet.ln_eqwaitni = ni;
1101                         }
1102                 } else {
1103 # ifndef HAVE_LIBPTHREAD
1104                         LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1105                                            "single-threaded runtime\n",
1106                                            libcfs_lnd2str(lnd_type));
1107                         goto failed;
1108 # endif
1109                 }
1110 #endif
1111                 if (ni->ni_peertxcredits == 0 ||
1112                     ni->ni_maxtxcredits == 0) {
1113                         LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1114                                            libcfs_lnd2str(lnd->lnd_type),
1115                                            ni->ni_peertxcredits == 0 ?
1116                                            "" : "per-peer ");
1117                         goto failed;
1118                 }
1119
1120                 ni->ni_txcredits = ni->ni_mintxcredits = ni->ni_maxtxcredits;
1121
1122                 CDEBUG(D_LNI, "Added LNI %s [%d/%d]\n",
1123                        libcfs_nid2str(ni->ni_nid),
1124                        ni->ni_peertxcredits, ni->ni_txcredits);
1125
1126                 /* Handle nidstrings for network 0 just like this one */
1127                 if (the_lnet.ln_ptlcompat > 0) {
1128                         if (nicount > 0) {
1129                                 LCONSOLE_ERROR_MSG(0x108, "Can't run > 1 "
1130                                        "network when portals_compatibility is "
1131                                        "set\n");
1132                                 goto failed;
1133                         }
1134                         libcfs_setnet0alias(lnd->lnd_type);
1135                 }
1136
1137                 nicount++;
1138         }
1139
1140         if (the_lnet.ln_eqwaitni != NULL && nicount > 1) {
1141                 lnd_type = the_lnet.ln_eqwaitni->ni_lnd->lnd_type;
1142                 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1143                                    "\n",
1144                                    libcfs_lnd2str(lnd_type));
1145                 goto failed;
1146         }
1147
1148         return 0;
1149
1150  failed:
1151         lnet_shutdown_lndnis();
1152
1153         while (!list_empty(&nilist)) {
1154                 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1155                 list_del(&ni->ni_list);
1156                 LIBCFS_FREE(ni, sizeof(*ni));
1157         }
1158
1159         return -ENETDOWN;
1160 }
1161
1162 int
1163 LNetInit(void)
1164 {
1165         int    rc;
1166
1167         lnet_assert_wire_constants ();
1168         LASSERT (!the_lnet.ln_init);
1169
1170         memset(&the_lnet, 0, sizeof(the_lnet));
1171
1172         rc = lnet_get_portals_compatibility();
1173         if (rc < 0)
1174                 return rc;
1175
1176         lnet_init_locks();
1177         CFS_INIT_LIST_HEAD(&the_lnet.ln_lnds);
1178         the_lnet.ln_ptlcompat = rc;
1179         the_lnet.ln_refcount = 0;
1180         the_lnet.ln_init = 1;
1181
1182 #ifdef __KERNEL__
1183         /* All LNDs apart from the LOLND are in separate modules.  They
1184          * register themselves when their module loads, and unregister
1185          * themselves when their module is unloaded. */
1186 #else
1187         /* Register LNDs
1188          * NB the order here determines default 'networks=' order */
1189 # ifdef CRAY_XT3
1190         LNET_REGISTER_ULND(the_ptllnd);
1191 # endif
1192 # ifdef HAVE_LIBPTHREAD
1193         LNET_REGISTER_ULND(the_tcplnd);
1194 # endif
1195 #endif
1196         lnet_register_lnd(&the_lolnd);
1197         return 0;
1198 }
1199
1200 void
1201 LNetFini(void)
1202 {
1203         LASSERT (the_lnet.ln_init);
1204         LASSERT (the_lnet.ln_refcount == 0);
1205
1206         while (!list_empty(&the_lnet.ln_lnds))
1207                 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1208                                                lnd_t, lnd_list));
1209         lnet_fini_locks();
1210
1211         the_lnet.ln_init = 0;
1212 }
1213
1214 int
1215 LNetNIInit(lnet_pid_t requested_pid)
1216 {
1217         int         im_a_router = 0;
1218         int         rc;
1219
1220         LNET_MUTEX_DOWN(&the_lnet.ln_api_mutex);
1221
1222         LASSERT (the_lnet.ln_init);
1223         CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1224
1225         if (the_lnet.ln_refcount > 0) {
1226                 rc = the_lnet.ln_refcount++;
1227                 goto out;
1228         }
1229
1230         if (requested_pid == LNET_PID_ANY) {
1231                 /* Don't instantiate LNET just for me */
1232                 rc = -ENETDOWN;
1233                 goto failed0;
1234         }
1235
1236         rc = lnet_prepare(requested_pid);
1237         if (rc != 0)
1238                 goto failed0;
1239
1240         rc = lnet_startup_lndnis();
1241         if (rc != 0)
1242                 goto failed1;
1243
1244         rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1245         if (rc != 0)
1246                 goto failed2;
1247
1248         rc = lnet_check_routes();
1249         if (rc != 0)
1250                 goto failed2;
1251
1252         rc = lnet_alloc_rtrpools(im_a_router);
1253         if (rc != 0)
1254                 goto failed2;
1255
1256         rc = lnet_acceptor_start();
1257         if (rc != 0)
1258                 goto failed2;
1259
1260         the_lnet.ln_refcount = 1;
1261         /* Now I may use my own API functions... */
1262
1263         rc = lnet_router_checker_start();
1264         if (rc != 0)
1265                 goto failed3;
1266
1267         rc = lnet_ping_target_init();
1268         if (rc != 0)
1269                 goto failed4;
1270
1271         lnet_proc_init();
1272         goto out;
1273
1274  failed4:
1275         lnet_router_checker_stop();
1276  failed3:
1277         the_lnet.ln_refcount = 0;
1278         lnet_acceptor_stop();
1279  failed2:
1280         lnet_destroy_routes();
1281         lnet_shutdown_lndnis();
1282  failed1:
1283         lnet_unprepare();
1284  failed0:
1285         LASSERT (rc < 0);
1286  out:
1287         LNET_MUTEX_UP(&the_lnet.ln_api_mutex);
1288         return rc;
1289 }
1290
1291 int
1292 LNetNIFini()
1293 {
1294         LNET_MUTEX_DOWN(&the_lnet.ln_api_mutex);
1295
1296         LASSERT (the_lnet.ln_init);
1297         LASSERT (the_lnet.ln_refcount > 0);
1298
1299         if (the_lnet.ln_refcount != 1) {
1300                 the_lnet.ln_refcount--;
1301         } else {
1302                 LASSERT (!the_lnet.ln_niinit_self);
1303
1304                 lnet_proc_fini();
1305                 lnet_ping_target_fini();
1306                 lnet_router_checker_stop();
1307
1308                 /* Teardown fns that use my own API functions BEFORE here */
1309                 the_lnet.ln_refcount = 0;
1310
1311                 lnet_acceptor_stop();
1312                 lnet_destroy_routes();
1313                 lnet_shutdown_lndnis();
1314                 lnet_unprepare();
1315         }
1316
1317         LNET_MUTEX_UP(&the_lnet.ln_api_mutex);
1318         return 0;
1319 }
1320
1321 int
1322 LNetCtl(unsigned int cmd, void *arg)
1323 {
1324         struct libcfs_ioctl_data *data = arg;
1325         lnet_process_id_t         id;
1326         lnet_ni_t                *ni;
1327         int                       rc;
1328
1329         LASSERT (the_lnet.ln_init);
1330         LASSERT (the_lnet.ln_refcount > 0);
1331
1332         switch (cmd) {
1333         case IOC_LIBCFS_GET_NI:
1334                 rc = LNetGetId(data->ioc_count, &id);
1335                 data->ioc_nid = id.nid;
1336                 return rc;
1337
1338         case IOC_LIBCFS_FAIL_NID:
1339                 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1340
1341         case IOC_LIBCFS_ADD_ROUTE:
1342                 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1343                                     data->ioc_nid);
1344                 return (rc != 0) ? rc : lnet_check_routes();
1345
1346         case IOC_LIBCFS_DEL_ROUTE:
1347                 return lnet_del_route(data->ioc_net, data->ioc_nid);
1348
1349         case IOC_LIBCFS_GET_ROUTE:
1350                 return lnet_get_route(data->ioc_count,
1351                                       &data->ioc_net, &data->ioc_count,
1352                                       &data->ioc_nid, &data->ioc_flags);
1353         case IOC_LIBCFS_NOTIFY_ROUTER:
1354                 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1355                                    (time_t)data->ioc_u64[0]);
1356
1357         case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1358                 return the_lnet.ln_ptlcompat;
1359
1360         case IOC_LIBCFS_LNET_DIST:
1361                 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1362                 if (rc < 0 && rc != -EHOSTUNREACH)
1363                         return rc;
1364
1365                 data->ioc_u32[0] = rc;
1366                 return 0;
1367
1368         case IOC_LIBCFS_TESTPROTOCOMPAT:
1369                 LNET_LOCK();
1370                 the_lnet.ln_testprotocompat = data->ioc_flags;
1371                 LNET_UNLOCK();
1372                 return 0;
1373
1374         case IOC_LIBCFS_PING:
1375                 rc = lnet_ping((lnet_process_id_t) {.nid = data->ioc_nid,
1376                                                     .pid = data->ioc_u32[0]},
1377                                data->ioc_u32[1], /* timeout */
1378                                (lnet_process_id_t *)data->ioc_pbuf1,
1379                                data->ioc_plen1/sizeof(lnet_process_id_t));
1380                 if (rc < 0)
1381                         return rc;
1382                 data->ioc_count = rc;
1383                 return 0;
1384
1385         case IOC_LIBCFS_DEBUG_PEER: {
1386                 /* CAVEAT EMPTOR: this one designed for calling directly; not
1387                  * via an ioctl */
1388                 lnet_process_id_t *id = arg;
1389
1390                 lnet_debug_peer(id->nid);
1391
1392                 ni = lnet_net2ni(LNET_NIDNET(id->nid));
1393                 if (ni == NULL) {
1394                         CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(*id));
1395                 } else {
1396                         if (ni->ni_lnd->lnd_ctl == NULL) {
1397                                 CDEBUG(D_WARNING, "No ctl for %s\n",
1398                                        libcfs_id2str(*id));
1399                         } else {
1400                                 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1401                         }
1402
1403                         lnet_ni_decref(ni);
1404                 }
1405                 return 0;
1406         }
1407
1408         default:
1409                 ni = lnet_net2ni(data->ioc_net);
1410                 if (ni == NULL)
1411                         return -EINVAL;
1412
1413                 if (ni->ni_lnd->lnd_ctl == NULL)
1414                         rc = -EINVAL;
1415                 else
1416                         rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1417
1418                 lnet_ni_decref(ni);
1419                 return rc;
1420         }
1421         /* not reached */
1422 }
1423
1424 int
1425 LNetGetId(unsigned int index, lnet_process_id_t *id)
1426 {
1427         lnet_ni_t        *ni;
1428         struct list_head *tmp;
1429         int               rc = -ENOENT;
1430
1431         LASSERT (the_lnet.ln_init);
1432         LASSERT (the_lnet.ln_refcount > 0);
1433
1434         LNET_LOCK();
1435
1436         list_for_each(tmp, &the_lnet.ln_nis) {
1437                 if (index-- != 0)
1438                         continue;
1439
1440                 ni = list_entry(tmp, lnet_ni_t, ni_list);
1441
1442                 id->nid = ni->ni_nid;
1443                 id->pid = the_lnet.ln_pid;
1444                 rc = 0;
1445                 break;
1446         }
1447
1448         LNET_UNLOCK();
1449
1450         return rc;
1451 }
1452
1453 void
1454 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1455 {
1456         snprintf(str, len, LPX64, h.cookie);
1457 }
1458
1459
1460 int
1461 lnet_ping_target_init(void)
1462 {
1463         lnet_handle_me_t  meh;
1464         lnet_process_id_t id;
1465         int               rc;
1466         int               rc2;
1467         int               n;
1468         int               infosz;
1469         int               i;
1470
1471         for (n = 0; ; n++) {
1472                 rc = LNetGetId(n, &id);
1473                 if (rc == -ENOENT)
1474                         break;
1475
1476                 LASSERT (rc == 0);
1477         }
1478
1479         infosz = offsetof(lnet_ping_info_t, pi_nid[n]);
1480         LIBCFS_ALLOC(the_lnet.ln_ping_info, infosz);
1481         if (the_lnet.ln_ping_info == NULL) {
1482                 CERROR("Can't allocate ping info[%d]\n", n);
1483                 return -ENOMEM;
1484         }
1485
1486         the_lnet.ln_ping_info->pi_magic   = LNET_PROTO_PING_MAGIC;
1487         the_lnet.ln_ping_info->pi_version = LNET_PROTO_PING_VERSION;
1488         the_lnet.ln_ping_info->pi_pid     = the_lnet.ln_pid;
1489         the_lnet.ln_ping_info->pi_nnids   = n;
1490
1491         for (i = 0; i < n; i++) {
1492                 rc = LNetGetId(i, &id);
1493                 LASSERT (rc == 0);
1494                 the_lnet.ln_ping_info->pi_nid[i] = id.nid;
1495         }
1496
1497         /* We can have a tiny EQ since we only need to see the unlink event on
1498          * teardown, which by definition is the last one! */
1499         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1500         if (rc != 0) {
1501                 CERROR("Can't allocate ping EQ: %d\n", rc);
1502                 goto failed_0;
1503         }
1504
1505         rc = LNetMEAttach(LNET_RESERVED_PORTAL,
1506                           (lnet_process_id_t){.nid = LNET_NID_ANY,
1507                                               .pid = LNET_PID_ANY},
1508                           LNET_PROTO_PING_MATCHBITS, 0LL,
1509                           LNET_UNLINK, LNET_INS_AFTER,
1510                           &meh);
1511         if (rc != 0) {
1512                 CERROR("Can't create ping ME: %d\n", rc);
1513                 goto failed_1;
1514         }
1515
1516         rc = LNetMDAttach(meh,
1517                           (lnet_md_t){.start = the_lnet.ln_ping_info,
1518                                       .length = infosz,
1519                                       .threshold = LNET_MD_THRESH_INF,
1520                                       .options = (LNET_MD_OP_GET |
1521                                                   LNET_MD_TRUNCATE |
1522                                                   LNET_MD_MANAGE_REMOTE),
1523                                       .eq_handle = the_lnet.ln_ping_target_eq},
1524                           LNET_RETAIN,
1525                           &the_lnet.ln_ping_target_md);
1526         if (rc != 0) {
1527                 CERROR("Can't attach ping MD: %d\n", rc);
1528                 goto failed_2;
1529         }
1530
1531         return 0;
1532
1533  failed_2:
1534         rc2 = LNetMEUnlink(meh);
1535         LASSERT (rc2 == 0);
1536  failed_1:
1537         rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1538         LASSERT (rc2 == 0);
1539  failed_0:
1540         LIBCFS_FREE(the_lnet.ln_ping_info, infosz);
1541
1542         return rc;
1543 }
1544
1545 void
1546 lnet_ping_target_fini(void)
1547 {
1548         lnet_event_t    event;
1549         int             rc;
1550         int             which;
1551         int             timeout_ms = 1000;
1552         cfs_sigset_t    blocked = cfs_block_allsigs();
1553
1554         LNetMDUnlink(the_lnet.ln_ping_target_md);
1555         /* NB md could be busy; this just starts the unlink */
1556
1557         for (;;) {
1558                 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1559                                 timeout_ms, &event, &which);
1560
1561                 /* I expect overflow... */
1562                 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1563
1564                 if (rc == 0) {
1565                         /* timed out: provide a diagnostic */
1566                         CWARN("Still waiting for ping MD to unlink\n");
1567                         timeout_ms *= 2;
1568                         continue;
1569                 }
1570
1571                 /* Got a valid event */
1572                 if (event.unlinked)
1573                         break;
1574         }
1575
1576         rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1577         LASSERT (rc == 0);
1578
1579         LIBCFS_FREE(the_lnet.ln_ping_info,
1580                     offsetof(lnet_ping_info_t,
1581                              pi_nid[the_lnet.ln_ping_info->pi_nnids]));
1582
1583         cfs_restore_sigs(blocked);
1584 }
1585
1586 int
1587 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1588 {
1589         lnet_handle_eq_t     eqh;
1590         lnet_handle_md_t     mdh;
1591         lnet_event_t         event;
1592         int                  which;
1593         int                  unlinked = 0;
1594         int                  replied = 0;
1595         const int            a_long_time = 60000; /* mS */
1596         int                  infosz = offsetof(lnet_ping_info_t, pi_nid[n_ids]);
1597         lnet_ping_info_t    *info;
1598         lnet_process_id_t    tmpid;
1599         int                  i;
1600         int                  nob;
1601         int                  rc;
1602         int                  rc2;
1603         cfs_sigset_t         blocked;
1604
1605         if (n_ids <= 0 ||
1606             id.nid == LNET_NID_ANY ||
1607             timeout_ms > 500000 ||              /* arbitrary limit! */
1608             n_ids > 20)                         /* arbitrary limit! */
1609                 return -EINVAL;
1610
1611         if (id.pid == LNET_PID_ANY)
1612                 id.pid = LUSTRE_SRV_LNET_PID;
1613
1614         LIBCFS_ALLOC(info, infosz);
1615         if (info == NULL)
1616                 return -ENOMEM;
1617
1618         /* NB 2 events max (including any unlink event) */
1619         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1620         if (rc != 0) {
1621                 CERROR("Can't allocate EQ: %d\n", rc);
1622                 goto out_0;
1623         }
1624
1625         rc = LNetMDBind((lnet_md_t){.start = info,
1626                                     .length = infosz,
1627                                     .threshold = 2, /* GET/REPLY */
1628                                     .options = LNET_MD_TRUNCATE,
1629                                     .eq_handle = eqh},
1630                         LNET_UNLINK,
1631                         &mdh);
1632         if (rc != 0) {
1633                 CERROR("Can't bind MD: %d\n", rc);
1634                 goto out_1;
1635         }
1636
1637         rc = LNetGet(LNET_NID_ANY, mdh, id,
1638                      LNET_RESERVED_PORTAL,
1639                      LNET_PROTO_PING_MATCHBITS, 0);
1640
1641         if (rc != 0) {
1642                 /* Don't CERROR; this could be deliberate! */
1643
1644                 rc2 = LNetMDUnlink(mdh);
1645                 LASSERT (rc2 == 0);
1646
1647                 /* NB must wait for the UNLINK event below... */
1648                 unlinked = 1;
1649                 timeout_ms = a_long_time;
1650         }
1651
1652         do {
1653                 /* MUST block for unlink to complete */
1654                 if (unlinked)
1655                         blocked = cfs_block_allsigs();
1656
1657                 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1658
1659                 if (unlinked)
1660                         cfs_restore_sigs(blocked);
1661
1662                 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1663                        (rc2 <= 0) ? -1 : event.type,
1664                        (rc2 <= 0) ? -1 : event.status,
1665                        (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1666
1667                 LASSERT (rc2 != -EOVERFLOW);     /* can't miss anything */
1668
1669                 if (rc2 <= 0 || event.status != 0) {
1670                         /* timeout or error */
1671                         if (!replied && rc == 0)
1672                                 rc = (rc2 < 0) ? rc2 :
1673                                      (rc2 == 0) ? -ETIMEDOUT :
1674                                      event.status;
1675
1676                         if (!unlinked) {
1677                                 /* Ensure completion in finite time... */
1678                                 LNetMDUnlink(mdh);
1679                                 /* No assertion (racing with network) */
1680                                 unlinked = 1;
1681                                 timeout_ms = a_long_time;
1682                         } else if (rc2 == 0) {
1683                                 /* timed out waiting for unlink */
1684                                 CWARN("ping %s: late network completion\n",
1685                                       libcfs_id2str(id));
1686                         }
1687
1688                 } else if (event.type == LNET_EVENT_REPLY) {
1689                         replied = 1;
1690                         rc = event.mlength;
1691                 }
1692
1693         } while (rc2 <= 0 || !event.unlinked);
1694
1695         if (!replied) {
1696                 if (rc >= 0)
1697                         CWARN("%s: Unexpected rc >= 0 but no reply!\n",
1698                               libcfs_id2str(id));
1699                 rc = -EIO;
1700                 goto out_1;
1701         }
1702
1703         nob = rc;
1704         LASSERT (nob >= 0 && nob <= infosz);
1705
1706         rc = -EPROTO;                           /* if I can't parse... */
1707
1708         if (nob < 8) {
1709                 /* can't check magic/version */
1710                 CERROR("%s: ping info too short %d\n",
1711                        libcfs_id2str(id), nob);
1712                 goto out_1;
1713         }
1714
1715         if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
1716                 /* NB I might be swabbing garbage until I check below, but it
1717                  * doesn't matter */
1718                 __swab32s(&info->pi_version);
1719                 __swab32s(&info->pi_pid);
1720                 __swab32s(&info->pi_nnids);
1721                 for (i = 0; i < info->pi_nnids && i < n_ids; i++)
1722                         __swab64s(&info->pi_nid[i]);
1723
1724         } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
1725                 CERROR("%s: Unexpected magic %08x\n", 
1726                        libcfs_id2str(id), info->pi_magic);
1727                 goto out_1;
1728         }
1729
1730         if (info->pi_version != LNET_PROTO_PING_VERSION) {
1731                 CERROR("%s: Unexpected version 0x%x\n",
1732                        libcfs_id2str(id), info->pi_version);
1733                 goto out_1;
1734         }
1735
1736         if (nob < offsetof(lnet_ping_info_t, pi_nid[0])) {
1737                 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
1738                        nob, (int)offsetof(lnet_ping_info_t, pi_nid[0]));
1739                 goto out_1;
1740         }
1741
1742         if (info->pi_nnids < n_ids)
1743                 n_ids = info->pi_nnids;
1744
1745         if (nob < offsetof(lnet_ping_info_t, pi_nid[n_ids])) {
1746                 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
1747                        nob, (int)offsetof(lnet_ping_info_t, pi_nid[n_ids]));
1748                 goto out_1;
1749         }
1750
1751         rc = -EFAULT;                           /* If I SEGV... */
1752
1753         for (i = 0; i < n_ids; i++) {
1754                 tmpid.pid = info->pi_pid;
1755                 tmpid.nid = info->pi_nid[i];
1756 #ifdef __KERNEL__
1757                 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
1758                         goto out_1;
1759 #else
1760                 ids[i] = tmpid;
1761 #endif
1762         }
1763         rc = info->pi_nnids;
1764
1765  out_1:
1766         rc2 = LNetEQFree(eqh);
1767         if (rc2 != 0)
1768                 CERROR("rc2 %d\n", rc2);
1769         LASSERT (rc2 == 0);
1770
1771  out_0:
1772         LIBCFS_FREE(info, infosz);
1773         return rc;
1774 }