Whamcloud - gitweb
b=21456 Patch to support lnet v1 pings in 'lctl ping'
[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 (c) 2003, 2010, Oracle and/or its affiliates. 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 = (2 * 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 >> LNET_COOKIE_TYPE_BITS)) % 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 >> LNET_COOKIE_TYPE_BITS)) % 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 struct list_head *
590 lnet_portal_mhash_alloc(void)
591 {
592         struct list_head *mhash;
593         int               i;
594
595         LIBCFS_ALLOC(mhash, sizeof(struct list_head) * LNET_PORTAL_HASH_SIZE);
596         if (mhash == NULL)
597                 return NULL;
598
599         for (i = 0; i < LNET_PORTAL_HASH_SIZE; i++)
600                 CFS_INIT_LIST_HEAD(&mhash[i]);
601
602         return mhash;
603 }
604
605 void
606 lnet_portal_mhash_free(struct list_head *mhash)
607 {
608         int     i;
609
610         for (i = 0; i < LNET_PORTAL_HASH_SIZE; i++) {
611                 while (!list_empty(&mhash[i])) {
612                         lnet_me_t *me = list_entry (mhash[i].next,
613                                                     lnet_me_t, me_list);
614                         CERROR ("Active ME %p on exit portal mhash\n", me);
615                         list_del (&me->me_list);
616                         lnet_me_free (me);
617                 }
618         }
619         LIBCFS_FREE(mhash, sizeof(struct list_head) * LNET_PORTAL_HASH_SIZE);
620 }
621
622 int
623 lnet_init_finalizers(void)
624 {
625 #ifdef __KERNEL__
626         int    i;
627
628         the_lnet.ln_nfinalizers = num_online_cpus();
629
630         LIBCFS_ALLOC(the_lnet.ln_finalizers,
631                      the_lnet.ln_nfinalizers *
632                      sizeof(*the_lnet.ln_finalizers));
633         if (the_lnet.ln_finalizers == NULL) {
634                 CERROR("Can't allocate ln_finalizers\n");
635                 return -ENOMEM;
636         }
637
638         for (i = 0; i < the_lnet.ln_nfinalizers; i++)
639                 the_lnet.ln_finalizers[i] = NULL;
640 #else
641         the_lnet.ln_finalizing = 0;
642 #endif
643
644         CFS_INIT_LIST_HEAD(&the_lnet.ln_finalizeq);
645         return 0;
646 }
647
648 void
649 lnet_fini_finalizers(void)
650 {
651 #ifdef __KERNEL__
652         int    i;
653
654         for (i = 0; i < the_lnet.ln_nfinalizers; i++)
655                 LASSERT (the_lnet.ln_finalizers[i] == NULL);
656
657         LIBCFS_FREE(the_lnet.ln_finalizers,
658                     the_lnet.ln_nfinalizers *
659                     sizeof(*the_lnet.ln_finalizers));
660 #else
661         LASSERT (!the_lnet.ln_finalizing);
662 #endif
663         LASSERT (list_empty(&the_lnet.ln_finalizeq));
664 }
665
666 #ifndef __KERNEL__
667 /* Temporary workaround to allow uOSS and test programs force server
668  * mode in userspace. See comments near ln_server_mode_flag in
669  * lnet/lib-types.h */
670
671 void
672 lnet_server_mode() {
673         the_lnet.ln_server_mode_flag = 1;
674 }
675 #endif
676
677 int
678 lnet_prepare(lnet_pid_t requested_pid)
679 {
680         /* Prepare to bring up the network */
681         int               rc = 0;
682         int               i;
683
684         LASSERT (the_lnet.ln_refcount == 0);
685
686         the_lnet.ln_routing = 0;
687
688 #ifdef __KERNEL__
689         LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
690         the_lnet.ln_pid = requested_pid;
691 #else
692         if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
693                 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
694
695                 if (cfs_curproc_uid())/* Only root can run user-space server */
696                         return -EPERM;
697                 the_lnet.ln_pid = requested_pid;
698
699         } else {/* client case (liblustre) */
700
701                 /* My PID must be unique on this node and flag I'm userspace */
702                 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
703         }
704 #endif
705
706         rc = lnet_descriptor_setup();
707         if (rc != 0)
708                 goto failed0;
709
710         memset(&the_lnet.ln_counters, 0,
711                sizeof(the_lnet.ln_counters));
712
713         CFS_INIT_LIST_HEAD (&the_lnet.ln_active_msgs);
714         CFS_INIT_LIST_HEAD (&the_lnet.ln_active_mds);
715         CFS_INIT_LIST_HEAD (&the_lnet.ln_active_eqs);
716         CFS_INIT_LIST_HEAD (&the_lnet.ln_test_peers);
717         CFS_INIT_LIST_HEAD (&the_lnet.ln_nis);
718         CFS_INIT_LIST_HEAD (&the_lnet.ln_zombie_nis);
719         CFS_INIT_LIST_HEAD (&the_lnet.ln_remote_nets);
720         CFS_INIT_LIST_HEAD (&the_lnet.ln_routers);
721
722         the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
723
724         lnet_init_rtrpools();
725
726         rc = lnet_setup_handle_hash ();
727         if (rc != 0)
728                 goto failed0;
729
730         rc = lnet_create_peer_table();
731         if (rc != 0)
732                 goto failed1;
733
734         rc = lnet_init_finalizers();
735         if (rc != 0)
736                 goto failed2;
737
738         the_lnet.ln_nportals = MAX_PORTALS;
739         LIBCFS_ALLOC(the_lnet.ln_portals,
740                      the_lnet.ln_nportals *
741                      sizeof(*the_lnet.ln_portals));
742         if (the_lnet.ln_portals == NULL) {
743                 rc = -ENOMEM;
744                 goto failed3;
745         }
746
747         for (i = 0; i < the_lnet.ln_nportals; i++) {
748                 CFS_INIT_LIST_HEAD(&(the_lnet.ln_portals[i].ptl_mlist));
749                 CFS_INIT_LIST_HEAD(&(the_lnet.ln_portals[i].ptl_msgq));
750                 the_lnet.ln_portals[i].ptl_options = 0;
751         }
752
753         return 0;
754
755  failed3:
756         lnet_fini_finalizers();
757  failed2:
758         lnet_destroy_peer_table();
759  failed1:
760         lnet_cleanup_handle_hash();
761  failed0:
762         lnet_descriptor_cleanup();
763         return rc;
764 }
765
766 int
767 lnet_unprepare (void)
768 {
769         int       idx;
770
771         /* NB no LNET_LOCK since this is the last reference.  All LND instances
772          * have shut down already, so it is safe to unlink and free all
773          * descriptors, even those that appear committed to a network op (eg MD
774          * with non-zero pending count) */
775
776         lnet_fail_nid(LNET_NID_ANY, 0);
777
778         LASSERT (list_empty(&the_lnet.ln_test_peers));
779         LASSERT (the_lnet.ln_refcount == 0);
780         LASSERT (list_empty(&the_lnet.ln_nis));
781         LASSERT (list_empty(&the_lnet.ln_zombie_nis));
782         LASSERT (the_lnet.ln_nzombie_nis == 0);
783
784         for (idx = 0; idx < the_lnet.ln_nportals; idx++) {
785                 LASSERT (list_empty(&the_lnet.ln_portals[idx].ptl_msgq));
786
787                 while (!list_empty (&the_lnet.ln_portals[idx].ptl_mlist)) {
788                         lnet_me_t *me = list_entry (the_lnet.ln_portals[idx].ptl_mlist.next,
789                                                     lnet_me_t, me_list);
790
791                         CERROR ("Active ME %p on exit\n", me);
792                         list_del (&me->me_list);
793                         lnet_me_free (me);
794                 }
795
796                 if (the_lnet.ln_portals[idx].ptl_mhash != NULL) {
797                         LASSERT (lnet_portal_is_unique(&the_lnet.ln_portals[idx]));
798                         lnet_portal_mhash_free(the_lnet.ln_portals[idx].ptl_mhash);
799                 }
800         }
801
802         while (!list_empty (&the_lnet.ln_active_mds)) {
803                 lnet_libmd_t *md = list_entry (the_lnet.ln_active_mds.next,
804                                                lnet_libmd_t, md_list);
805
806                 CERROR ("Active MD %p on exit\n", md);
807                 list_del_init (&md->md_list);
808                 lnet_md_free (md);
809         }
810
811         while (!list_empty (&the_lnet.ln_active_eqs)) {
812                 lnet_eq_t *eq = list_entry (the_lnet.ln_active_eqs.next,
813                                             lnet_eq_t, eq_list);
814
815                 CERROR ("Active EQ %p on exit\n", eq);
816                 list_del (&eq->eq_list);
817                 lnet_eq_free (eq);
818         }
819
820         while (!list_empty (&the_lnet.ln_active_msgs)) {
821                 lnet_msg_t *msg = list_entry (the_lnet.ln_active_msgs.next,
822                                               lnet_msg_t, msg_activelist);
823
824                 CERROR ("Active msg %p on exit\n", msg);
825                 LASSERT (msg->msg_onactivelist);
826                 msg->msg_onactivelist = 0;
827                 list_del (&msg->msg_activelist);
828                 lnet_msg_free (msg);
829         }
830
831         LIBCFS_FREE(the_lnet.ln_portals,  
832                     the_lnet.ln_nportals * sizeof(*the_lnet.ln_portals));
833
834         lnet_free_rtrpools();
835         lnet_fini_finalizers();
836         lnet_destroy_peer_table();
837         lnet_cleanup_handle_hash();
838         lnet_descriptor_cleanup();
839
840         return (0);
841 }
842
843 lnet_ni_t  *
844 lnet_net2ni_locked (__u32 net)
845 {
846         struct list_head *tmp;
847         lnet_ni_t        *ni;
848
849         list_for_each (tmp, &the_lnet.ln_nis) {
850                 ni = list_entry(tmp, lnet_ni_t, ni_list);
851
852                 if (lnet_ptlcompat_matchnet(LNET_NIDNET(ni->ni_nid), net)) {
853                         lnet_ni_addref_locked(ni);
854                         return ni;
855                 }
856         }
857
858         return NULL;
859 }
860
861 int
862 lnet_islocalnet (__u32 net)
863 {
864         lnet_ni_t        *ni;
865
866         LNET_LOCK();
867         ni = lnet_net2ni_locked(net);
868         if (ni != NULL)
869                 lnet_ni_decref_locked(ni);
870         LNET_UNLOCK();
871
872         return ni != NULL;
873 }
874
875 lnet_ni_t  *
876 lnet_nid2ni_locked (lnet_nid_t nid)
877 {
878         struct list_head *tmp;
879         lnet_ni_t        *ni;
880
881         list_for_each (tmp, &the_lnet.ln_nis) {
882                 ni = list_entry(tmp, lnet_ni_t, ni_list);
883
884                 if (lnet_ptlcompat_matchnid(ni->ni_nid, nid)) {
885                         lnet_ni_addref_locked(ni);
886                         return ni;
887                 }
888         }
889
890         return NULL;
891 }
892
893 int
894 lnet_islocalnid (lnet_nid_t nid)
895 {
896         lnet_ni_t     *ni;
897
898         LNET_LOCK();
899         ni = lnet_nid2ni_locked(nid);
900         if (ni != NULL)
901                 lnet_ni_decref_locked(ni);
902         LNET_UNLOCK();
903
904         return ni != NULL;
905 }
906
907 int
908 lnet_count_acceptor_nis (lnet_ni_t **first_ni)
909 {
910         /* Return the # of NIs that need the acceptor.  Return the first one in
911          * *first_ni so the acceptor can pass it connections "blind" to retain
912          * binary compatibility. */
913         int                count = 0;
914 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
915         struct list_head  *tmp;
916         lnet_ni_t         *ni;
917
918         LNET_LOCK();
919         list_for_each (tmp, &the_lnet.ln_nis) {
920                 ni = list_entry(tmp, lnet_ni_t, ni_list);
921
922                 if (ni->ni_lnd->lnd_accept != NULL) {
923                         /* This LND uses the acceptor */
924                         if (count == 0 && first_ni != NULL) {
925                                 lnet_ni_addref_locked(ni);
926                                 *first_ni = ni;
927                         }
928                         count++;
929                 }
930         }
931
932         LNET_UNLOCK();
933
934 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
935         return count;
936 }
937
938 void
939 lnet_shutdown_lndnis (void)
940 {
941         int                i;
942         int                islo;
943         lnet_ni_t         *ni;
944
945         /* NB called holding the global mutex */
946
947         /* All quiet on the API front */
948         LASSERT (!the_lnet.ln_shutdown);
949         LASSERT (the_lnet.ln_refcount == 0);
950         LASSERT (list_empty(&the_lnet.ln_zombie_nis));
951         LASSERT (the_lnet.ln_nzombie_nis == 0);
952         LASSERT (list_empty(&the_lnet.ln_remote_nets));
953
954         LNET_LOCK();
955         the_lnet.ln_shutdown = 1;               /* flag shutdown */
956
957         /* Unlink NIs from the global table */
958         while (!list_empty(&the_lnet.ln_nis)) {
959                 ni = list_entry(the_lnet.ln_nis.next,
960                                 lnet_ni_t, ni_list);
961                 list_del (&ni->ni_list);
962
963                 the_lnet.ln_nzombie_nis++;
964                 lnet_ni_decref_locked(ni); /* drop apini's ref */
965         }
966
967         /* Drop the cached eqwait NI. */
968         if (the_lnet.ln_eqwaitni != NULL) {
969                 lnet_ni_decref_locked(the_lnet.ln_eqwaitni);
970                 the_lnet.ln_eqwaitni = NULL;
971         }
972
973         /* Drop the cached loopback NI. */
974         if (the_lnet.ln_loni != NULL) {
975                 lnet_ni_decref_locked(the_lnet.ln_loni);
976                 the_lnet.ln_loni = NULL;
977         }
978
979         LNET_UNLOCK();
980
981         /* Clear lazy portals and drop delayed messages which hold refs
982          * on their lnet_msg_t::msg_rxpeer */
983         for (i = 0; i < the_lnet.ln_nportals; i++)
984                 LNetClearLazyPortal(i);
985
986         /* Clear the peer table and wait for all peers to go (they hold refs on
987          * their NIs) */
988         lnet_clear_peer_table();
989
990         LNET_LOCK();
991         /* Now wait for the NI's I just nuked to show up on apini_zombie_nis
992          * and shut them down in guaranteed thread context */
993         i = 2;
994         while (the_lnet.ln_nzombie_nis != 0) {
995
996                 while (list_empty(&the_lnet.ln_zombie_nis)) {
997                         LNET_UNLOCK();
998                         ++i;
999                         if ((i & (-i)) == i)
1000                                 CDEBUG(D_WARNING,"Waiting for %d zombie NIs\n",
1001                                        the_lnet.ln_nzombie_nis);
1002                         cfs_pause(cfs_time_seconds(1));
1003                         LNET_LOCK();
1004                 }
1005
1006                 ni = list_entry(the_lnet.ln_zombie_nis.next,
1007                                 lnet_ni_t, ni_list);
1008                 list_del(&ni->ni_list);
1009                 ni->ni_lnd->lnd_refcount--;
1010
1011                 LNET_UNLOCK();
1012
1013                 islo = ni->ni_lnd->lnd_type == LOLND;
1014
1015                 LASSERT (!in_interrupt ());
1016                 (ni->ni_lnd->lnd_shutdown)(ni);
1017
1018                 /* can't deref lnd anymore now; it might have unregistered
1019                  * itself...  */
1020
1021                 if (!islo)
1022                         CDEBUG(D_LNI, "Removed LNI %s\n",
1023                                libcfs_nid2str(ni->ni_nid));
1024
1025                 LIBCFS_FREE(ni, sizeof(*ni));
1026
1027                 LNET_LOCK();
1028                 the_lnet.ln_nzombie_nis--;
1029         }
1030
1031         the_lnet.ln_shutdown = 0;
1032         LNET_UNLOCK();
1033
1034         if (the_lnet.ln_network_tokens != NULL) {
1035                 LIBCFS_FREE(the_lnet.ln_network_tokens,
1036                             the_lnet.ln_network_tokens_nob);
1037                 the_lnet.ln_network_tokens = NULL;
1038         }
1039 }
1040
1041 int
1042 lnet_startup_lndnis (void)
1043 {
1044         lnd_t             *lnd;
1045         lnet_ni_t         *ni;
1046         struct list_head   nilist;
1047         int                rc = 0;
1048         int                lnd_type;
1049         int                nicount = 0;
1050         char              *nets = lnet_get_networks();
1051
1052         CFS_INIT_LIST_HEAD(&nilist);
1053
1054         if (nets == NULL)
1055                 goto failed;
1056
1057         rc = lnet_parse_networks(&nilist, nets);
1058         if (rc != 0)
1059                 goto failed;
1060
1061         while (!list_empty(&nilist)) {
1062                 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1063                 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1064
1065                 LASSERT (libcfs_isknown_lnd(lnd_type));
1066
1067                 LNET_MUTEX_DOWN(&the_lnet.ln_lnd_mutex);
1068                 lnd = lnet_find_lnd_by_type(lnd_type);
1069
1070 #ifdef __KERNEL__
1071                 if (lnd == NULL) {
1072                         LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
1073                         rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1074                         LNET_MUTEX_DOWN(&the_lnet.ln_lnd_mutex);
1075
1076                         lnd = lnet_find_lnd_by_type(lnd_type);
1077                         if (lnd == NULL) {
1078                                 LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
1079                                 CERROR("Can't load LND %s, module %s, rc=%d\n",
1080                                        libcfs_lnd2str(lnd_type),
1081                                        libcfs_lnd2modname(lnd_type), rc);
1082 #ifndef HAVE_MODULE_LOADING_SUPPORT
1083                                 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1084                                          "compiled with kernel module "
1085                                          "loading support.");
1086 #endif
1087                                 goto failed;
1088                         }
1089                 }
1090 #else
1091                 if (lnd == NULL) {
1092                         LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
1093                         CERROR("LND %s not supported\n",
1094                                libcfs_lnd2str(lnd_type));
1095                         goto failed;
1096                 }
1097 #endif
1098
1099                 ni->ni_refcount = 1;
1100
1101                 LNET_LOCK();
1102                 lnd->lnd_refcount++;
1103                 LNET_UNLOCK();
1104
1105                 ni->ni_lnd = lnd;
1106
1107                 rc = (lnd->lnd_startup)(ni);
1108
1109                 LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
1110
1111                 if (rc != 0) {
1112                         LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1113                                            "\n",
1114                                            rc, libcfs_lnd2str(lnd->lnd_type));
1115                         LNET_LOCK();
1116                         lnd->lnd_refcount--;
1117                         LNET_UNLOCK();
1118                         goto failed;
1119                 }
1120
1121                 LASSERT (ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1122
1123                 list_del(&ni->ni_list);
1124
1125                 LNET_LOCK();
1126                 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1127                 LNET_UNLOCK();
1128
1129                 if (lnd->lnd_type == LOLND) {
1130                         lnet_ni_addref(ni);
1131                         LASSERT (the_lnet.ln_loni == NULL);
1132                         the_lnet.ln_loni = ni;
1133                         continue;
1134                 }
1135
1136 #ifndef __KERNEL__
1137                 if (lnd->lnd_wait != NULL) {
1138                         if (the_lnet.ln_eqwaitni == NULL) {
1139                                 lnet_ni_addref(ni);
1140                                 the_lnet.ln_eqwaitni = ni;
1141                         }
1142                 } else {
1143 # ifndef HAVE_LIBPTHREAD
1144                         LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1145                                            "single-threaded runtime\n",
1146                                            libcfs_lnd2str(lnd_type));
1147                         goto failed;
1148 # endif
1149                 }
1150 #endif
1151                 if (ni->ni_peertxcredits == 0 ||
1152                     ni->ni_maxtxcredits == 0) {
1153                         LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1154                                            libcfs_lnd2str(lnd->lnd_type),
1155                                            ni->ni_peertxcredits == 0 ?
1156                                            "" : "per-peer ");
1157                         goto failed;
1158                 }
1159
1160                 ni->ni_txcredits = ni->ni_mintxcredits = ni->ni_maxtxcredits;
1161
1162                 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1163                        libcfs_nid2str(ni->ni_nid),
1164                        ni->ni_peertxcredits, ni->ni_txcredits,
1165                        ni->ni_peerrtrcredits, ni->ni_peertimeout);
1166
1167                 /* Handle nidstrings for network 0 just like this one */
1168                 if (the_lnet.ln_ptlcompat > 0) {
1169                         if (nicount > 0) {
1170                                 LCONSOLE_ERROR_MSG(0x108, "Can't run > 1 "
1171                                        "network when portals_compatibility is "
1172                                        "set\n");
1173                                 goto failed;
1174                         }
1175                         libcfs_setnet0alias(lnd->lnd_type);
1176                 }
1177
1178                 nicount++;
1179         }
1180
1181         if (the_lnet.ln_eqwaitni != NULL && nicount > 1) {
1182                 lnd_type = the_lnet.ln_eqwaitni->ni_lnd->lnd_type;
1183                 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1184                                    "\n",
1185                                    libcfs_lnd2str(lnd_type));
1186                 goto failed;
1187         }
1188
1189         return 0;
1190
1191  failed:
1192         lnet_shutdown_lndnis();
1193
1194         while (!list_empty(&nilist)) {
1195                 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1196                 list_del(&ni->ni_list);
1197                 LIBCFS_FREE(ni, sizeof(*ni));
1198         }
1199
1200         return -ENETDOWN;
1201 }
1202
1203 int
1204 LNetInit(void)
1205 {
1206         int    rc;
1207
1208         lnet_assert_wire_constants ();
1209         LASSERT (!the_lnet.ln_init);
1210
1211         memset(&the_lnet, 0, sizeof(the_lnet));
1212
1213         rc = lnet_get_portals_compatibility();
1214         if (rc < 0)
1215                 return rc;
1216
1217         lnet_init_locks();
1218         the_lnet.ln_ptlcompat = rc;
1219         the_lnet.ln_refcount = 0;
1220         the_lnet.ln_init = 1;
1221         the_lnet.ln_rc_eqh = LNET_EQ_NONE;
1222         CFS_INIT_LIST_HEAD(&the_lnet.ln_lnds);
1223         CFS_INIT_LIST_HEAD(&the_lnet.ln_zombie_rcd);
1224
1225 #ifdef __KERNEL__
1226         /* All LNDs apart from the LOLND are in separate modules.  They
1227          * register themselves when their module loads, and unregister
1228          * themselves when their module is unloaded. */
1229 #else
1230         /* Register LNDs
1231          * NB the order here determines default 'networks=' order */
1232 # ifdef CRAY_XT3
1233         LNET_REGISTER_ULND(the_ptllnd);
1234 # endif
1235 # ifdef HAVE_LIBPTHREAD
1236         LNET_REGISTER_ULND(the_tcplnd);
1237 # endif
1238 #endif
1239         lnet_register_lnd(&the_lolnd);
1240         return 0;
1241 }
1242
1243 void
1244 LNetFini(void)
1245 {
1246         LASSERT (the_lnet.ln_init);
1247         LASSERT (the_lnet.ln_refcount == 0);
1248
1249         while (!list_empty(&the_lnet.ln_lnds))
1250                 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1251                                                lnd_t, lnd_list));
1252         lnet_fini_locks();
1253
1254         the_lnet.ln_init = 0;
1255 }
1256
1257 int
1258 LNetNIInit(lnet_pid_t requested_pid)
1259 {
1260         int         im_a_router = 0;
1261         int         rc;
1262
1263         LNET_MUTEX_DOWN(&the_lnet.ln_api_mutex);
1264
1265         LASSERT (the_lnet.ln_init);
1266         CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1267
1268         if (the_lnet.ln_refcount > 0) {
1269                 rc = the_lnet.ln_refcount++;
1270                 goto out;
1271         }
1272
1273         lnet_get_tunables();
1274
1275         if (requested_pid == LNET_PID_ANY) {
1276                 /* Don't instantiate LNET just for me */
1277                 rc = -ENETDOWN;
1278                 goto failed0;
1279         }
1280
1281         rc = lnet_prepare(requested_pid);
1282         if (rc != 0)
1283                 goto failed0;
1284
1285         rc = lnet_startup_lndnis();
1286         if (rc != 0)
1287                 goto failed1;
1288
1289         rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1290         if (rc != 0)
1291                 goto failed2;
1292
1293         rc = lnet_check_routes();
1294         if (rc != 0)
1295                 goto failed2;
1296
1297         rc = lnet_alloc_rtrpools(im_a_router);
1298         if (rc != 0)
1299                 goto failed2;
1300
1301         rc = lnet_acceptor_start();
1302         if (rc != 0)
1303                 goto failed2;
1304
1305         the_lnet.ln_refcount = 1;
1306         /* Now I may use my own API functions... */
1307
1308         /* NB router checker needs the_lnet.ln_ping_info in
1309          * lnet_router_checker -> lnet_update_ni_status */
1310         rc = lnet_ping_target_init();
1311         if (rc != 0)
1312                 goto failed3;
1313
1314         rc = lnet_router_checker_start();
1315         if (rc != 0)
1316                 goto failed4;
1317
1318         lnet_proc_init();
1319         goto out;
1320
1321  failed4:
1322         lnet_ping_target_fini();
1323  failed3:
1324         the_lnet.ln_refcount = 0;
1325         lnet_acceptor_stop();
1326  failed2:
1327         lnet_destroy_routes();
1328         lnet_shutdown_lndnis();
1329  failed1:
1330         lnet_unprepare();
1331  failed0:
1332         LASSERT (rc < 0);
1333  out:
1334         LNET_MUTEX_UP(&the_lnet.ln_api_mutex);
1335         return rc;
1336 }
1337
1338 int
1339 LNetNIFini()
1340 {
1341         LNET_MUTEX_DOWN(&the_lnet.ln_api_mutex);
1342
1343         LASSERT (the_lnet.ln_init);
1344         LASSERT (the_lnet.ln_refcount > 0);
1345
1346         if (the_lnet.ln_refcount != 1) {
1347                 the_lnet.ln_refcount--;
1348         } else {
1349                 LASSERT (!the_lnet.ln_niinit_self);
1350
1351                 lnet_proc_fini();
1352                 lnet_router_checker_stop();
1353                 lnet_ping_target_fini();
1354
1355                 /* Teardown fns that use my own API functions BEFORE here */
1356                 the_lnet.ln_refcount = 0;
1357
1358                 lnet_acceptor_stop();
1359                 lnet_destroy_routes();
1360                 lnet_shutdown_lndnis();
1361                 lnet_unprepare();
1362         }
1363
1364         LNET_MUTEX_UP(&the_lnet.ln_api_mutex);
1365         return 0;
1366 }
1367
1368 int
1369 LNetCtl(unsigned int cmd, void *arg)
1370 {
1371         struct libcfs_ioctl_data *data = arg;
1372         lnet_process_id_t         id;
1373         lnet_ni_t                *ni;
1374         int                       rc;
1375
1376         LASSERT (the_lnet.ln_init);
1377         LASSERT (the_lnet.ln_refcount > 0);
1378
1379         switch (cmd) {
1380         case IOC_LIBCFS_GET_NI:
1381                 rc = LNetGetId(data->ioc_count, &id);
1382                 data->ioc_nid = id.nid;
1383                 return rc;
1384
1385         case IOC_LIBCFS_FAIL_NID:
1386                 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1387
1388         case IOC_LIBCFS_ADD_ROUTE:
1389                 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1390                                     data->ioc_nid);
1391                 return (rc != 0) ? rc : lnet_check_routes();
1392
1393         case IOC_LIBCFS_DEL_ROUTE:
1394                 return lnet_del_route(data->ioc_net, data->ioc_nid);
1395
1396         case IOC_LIBCFS_GET_ROUTE:
1397                 return lnet_get_route(data->ioc_count,
1398                                       &data->ioc_net, &data->ioc_count,
1399                                       &data->ioc_nid, &data->ioc_flags);
1400         case IOC_LIBCFS_NOTIFY_ROUTER:
1401                 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1402                                    cfs_time_current() -
1403                                    cfs_time_seconds(cfs_time_current_sec() -
1404                                                     (time_t)data->ioc_u64[0]));
1405
1406         case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1407                 return the_lnet.ln_ptlcompat;
1408
1409         case IOC_LIBCFS_LNET_DIST:
1410                 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1411                 if (rc < 0 && rc != -EHOSTUNREACH)
1412                         return rc;
1413
1414                 data->ioc_u32[0] = rc;
1415                 return 0;
1416
1417         case IOC_LIBCFS_TESTPROTOCOMPAT:
1418                 LNET_LOCK();
1419                 the_lnet.ln_testprotocompat = data->ioc_flags;
1420                 LNET_UNLOCK();
1421                 return 0;
1422
1423         case IOC_LIBCFS_PING:
1424                 rc = lnet_ping((lnet_process_id_t) {.nid = data->ioc_nid,
1425                                                     .pid = data->ioc_u32[0]},
1426                                data->ioc_u32[1], /* timeout */
1427                                (lnet_process_id_t *)data->ioc_pbuf1,
1428                                data->ioc_plen1/sizeof(lnet_process_id_t));
1429                 if (rc < 0)
1430                         return rc;
1431                 data->ioc_count = rc;
1432                 return 0;
1433
1434         case IOC_LIBCFS_DEBUG_PEER: {
1435                 /* CAVEAT EMPTOR: this one designed for calling directly; not
1436                  * via an ioctl */
1437                 lnet_process_id_t *id = arg;
1438
1439                 lnet_debug_peer(id->nid);
1440
1441                 ni = lnet_net2ni(LNET_NIDNET(id->nid));
1442                 if (ni == NULL) {
1443                         CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(*id));
1444                 } else {
1445                         if (ni->ni_lnd->lnd_ctl == NULL) {
1446                                 CDEBUG(D_WARNING, "No ctl for %s\n",
1447                                        libcfs_id2str(*id));
1448                         } else {
1449                                 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1450                         }
1451
1452                         lnet_ni_decref(ni);
1453                 }
1454                 return 0;
1455         }
1456
1457         default:
1458                 ni = lnet_net2ni(data->ioc_net);
1459                 if (ni == NULL)
1460                         return -EINVAL;
1461
1462                 if (ni->ni_lnd->lnd_ctl == NULL)
1463                         rc = -EINVAL;
1464                 else
1465                         rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1466
1467                 lnet_ni_decref(ni);
1468                 return rc;
1469         }
1470         /* not reached */
1471 }
1472
1473 int
1474 LNetGetId(unsigned int index, lnet_process_id_t *id)
1475 {
1476         lnet_ni_t        *ni;
1477         struct list_head *tmp;
1478         int               rc = -ENOENT;
1479
1480         LASSERT (the_lnet.ln_init);
1481         LASSERT (the_lnet.ln_refcount > 0);
1482
1483         LNET_LOCK();
1484
1485         list_for_each(tmp, &the_lnet.ln_nis) {
1486                 if (index-- != 0)
1487                         continue;
1488
1489                 ni = list_entry(tmp, lnet_ni_t, ni_list);
1490
1491                 id->nid = ni->ni_nid;
1492                 id->pid = the_lnet.ln_pid;
1493                 rc = 0;
1494                 break;
1495         }
1496
1497         LNET_UNLOCK();
1498
1499         return rc;
1500 }
1501
1502 void
1503 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1504 {
1505         snprintf(str, len, LPX64, h.cookie);
1506 }
1507
1508 static int
1509 lnet_create_ping_info(void)
1510 {
1511         int               i;
1512         int               n;
1513         int               rc;
1514         int               infosz;
1515         lnet_ni_t        *ni;
1516         lnet_process_id_t id;
1517         lnet_ping_info_t *pinfo;
1518
1519         for (n = 0; ; n++) {
1520                 rc = LNetGetId(n, &id);
1521                 if (rc == -ENOENT)
1522                         break;
1523
1524                 LASSERT (rc == 0);
1525         }
1526
1527         infosz = lnet_pinginfo_size(n);
1528         LIBCFS_ALLOC(pinfo, infosz);
1529         if (pinfo == NULL) {
1530                 CERROR("Can't allocate ping info[%d]\n", n);
1531                 return -ENOMEM;
1532         }
1533
1534         pinfo->pi_nnis    = n;
1535         pinfo->pi_pid     = the_lnet.ln_pid;
1536         pinfo->pi_magic   = LNET_PROTO_PING_MAGIC;
1537         pinfo->pi_version = LNET_PROTO_PING_VERSION;
1538
1539         for (i = 0; i < n; i++) {
1540                 lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1541
1542                 rc = LNetGetId(i, &id);
1543                 LASSERT (rc == 0);
1544
1545                 ns->ns_nid    = id.nid;
1546                 ns->ns_status = LNET_NI_STATUS_UP;
1547
1548                 LNET_LOCK();
1549
1550                 ni = lnet_nid2ni_locked(id.nid);
1551                 LASSERT (ni != NULL);
1552                 LASSERT (ni->ni_status == NULL);
1553                 ni->ni_status = ns;
1554                 lnet_ni_decref_locked(ni);
1555
1556                 LNET_UNLOCK();
1557         }
1558
1559         the_lnet.ln_ping_info = pinfo;
1560         return 0;
1561 }
1562
1563 static void
1564 lnet_destroy_ping_info(void)
1565 {
1566         lnet_ni_t *ni;
1567
1568         LNET_LOCK();
1569
1570         list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
1571                 ni->ni_status = NULL;
1572         }
1573
1574         LNET_UNLOCK();
1575
1576         LIBCFS_FREE(the_lnet.ln_ping_info,
1577                     lnet_pinginfo_size(the_lnet.ln_ping_info->pi_nnis));
1578         the_lnet.ln_ping_info = NULL;
1579         return;
1580 }
1581
1582 int
1583 lnet_ping_target_init(void)
1584 {
1585         lnet_handle_me_t  meh;
1586         int               rc;
1587         int               rc2;
1588
1589         rc = lnet_create_ping_info();
1590         if (rc != 0)
1591                 return rc;
1592
1593         /* We can have a tiny EQ since we only need to see the unlink event on
1594          * teardown, which by definition is the last one! */
1595         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1596         if (rc != 0) {
1597                 CERROR("Can't allocate ping EQ: %d\n", rc);
1598                 goto failed_0;
1599         }
1600
1601         rc = LNetMEAttach(LNET_RESERVED_PORTAL,
1602                           (lnet_process_id_t){.nid = LNET_NID_ANY,
1603                                               .pid = LNET_PID_ANY},
1604                           LNET_PROTO_PING_MATCHBITS, 0LL,
1605                           LNET_UNLINK, LNET_INS_AFTER,
1606                           &meh);
1607         if (rc != 0) {
1608                 CERROR("Can't create ping ME: %d\n", rc);
1609                 goto failed_1;
1610         }
1611
1612         rc = LNetMDAttach(meh,
1613                           (lnet_md_t){.start = the_lnet.ln_ping_info,
1614                                       .length = lnet_pinginfo_size(the_lnet.ln_ping_info->pi_nnis),
1615                                       .threshold = LNET_MD_THRESH_INF,
1616                                       .options = (LNET_MD_OP_GET |
1617                                                   LNET_MD_TRUNCATE |
1618                                                   LNET_MD_MANAGE_REMOTE),
1619                                       .eq_handle = the_lnet.ln_ping_target_eq},
1620                           LNET_RETAIN,
1621                           &the_lnet.ln_ping_target_md);
1622         if (rc != 0) {
1623                 CERROR("Can't attach ping MD: %d\n", rc);
1624                 goto failed_2;
1625         }
1626
1627         return 0;
1628
1629  failed_2:
1630         rc2 = LNetMEUnlink(meh);
1631         LASSERT (rc2 == 0);
1632  failed_1:
1633         rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1634         LASSERT (rc2 == 0);
1635  failed_0:
1636         lnet_destroy_ping_info();
1637         return rc;
1638 }
1639
1640 void
1641 lnet_ping_target_fini(void)
1642 {
1643         lnet_event_t    event;
1644         int             rc;
1645         int             which;
1646         int             timeout_ms = 1000;
1647         cfs_sigset_t    blocked = cfs_block_allsigs();
1648
1649         LNetMDUnlink(the_lnet.ln_ping_target_md);
1650         /* NB md could be busy; this just starts the unlink */
1651
1652         for (;;) {
1653                 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1654                                 timeout_ms, &event, &which);
1655
1656                 /* I expect overflow... */
1657                 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1658
1659                 if (rc == 0) {
1660                         /* timed out: provide a diagnostic */
1661                         CWARN("Still waiting for ping MD to unlink\n");
1662                         timeout_ms *= 2;
1663                         continue;
1664                 }
1665
1666                 /* Got a valid event */
1667                 if (event.unlinked)
1668                         break;
1669         }
1670
1671         rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1672         LASSERT (rc == 0);
1673         lnet_destroy_ping_info();
1674         cfs_restore_sigs(blocked);
1675 }
1676
1677 int
1678 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1679 {
1680         lnet_handle_eq_t     eqh;
1681         lnet_handle_md_t     mdh;
1682         lnet_event_t         event;
1683         int                  which;
1684         int                  unlinked = 0;
1685         int                  replied = 0;
1686         const int            a_long_time = 60000; /* mS */
1687         int                  infosz;
1688         lnet_ping_info_t    *info;
1689         lnet_process_id_t    tmpid;
1690         int                  i;
1691         int                  rc;
1692         int                  rc2;
1693         cfs_sigset_t         blocked;
1694
1695         if (n_ids <= 0 ||
1696             id.nid == LNET_NID_ANY ||
1697             timeout_ms > 500000 ||              /* arbitrary limit! */
1698             n_ids > 20)                         /* arbitrary limit! */
1699                 return -EINVAL;
1700
1701         if (id.pid == LNET_PID_ANY)
1702                 id.pid = LUSTRE_SRV_LNET_PID;
1703
1704         infosz = lnet_pinginfo_size(n_ids);
1705         LIBCFS_ALLOC(info, infosz);
1706         if (info == NULL)
1707                 return -ENOMEM;
1708
1709         /* NB 2 events max (including any unlink event) */
1710         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1711         if (rc != 0) {
1712                 CERROR("Can't allocate EQ: %d\n", rc);
1713                 goto out_0;
1714         }
1715
1716         rc = LNetMDBind((lnet_md_t){.start = info,
1717                                     .length = infosz,
1718                                     .threshold = 2, /* GET/REPLY */
1719                                     .options = LNET_MD_TRUNCATE,
1720                                     .eq_handle = eqh},
1721                         LNET_UNLINK,
1722                         &mdh);
1723         if (rc != 0) {
1724                 CERROR("Can't bind MD: %d\n", rc);
1725                 goto out_1;
1726         }
1727
1728         rc = LNetGet(LNET_NID_ANY, mdh, id,
1729                      LNET_RESERVED_PORTAL,
1730                      LNET_PROTO_PING_MATCHBITS, 0);
1731
1732         if (rc != 0) {
1733                 /* Don't CERROR; this could be deliberate! */
1734
1735                 rc2 = LNetMDUnlink(mdh);
1736                 LASSERT (rc2 == 0);
1737
1738                 /* NB must wait for the UNLINK event below... */
1739                 unlinked = 1;
1740                 timeout_ms = a_long_time;
1741         }
1742
1743         do {
1744                 /* MUST block for unlink to complete */
1745                 if (unlinked)
1746                         blocked = cfs_block_allsigs();
1747
1748                 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1749
1750                 if (unlinked)
1751                         cfs_restore_sigs(blocked);
1752
1753                 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1754                        (rc2 <= 0) ? -1 : event.type,
1755                        (rc2 <= 0) ? -1 : event.status,
1756                        (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1757
1758                 LASSERT (rc2 != -EOVERFLOW);     /* can't miss anything */
1759
1760                 if (rc2 <= 0 || event.status != 0) {
1761                         /* timeout or error */
1762                         if (!replied && rc == 0)
1763                                 rc = (rc2 < 0) ? rc2 :
1764                                      (rc2 == 0) ? -ETIMEDOUT :
1765                                      event.status;
1766
1767                         if (!unlinked) {
1768                                 /* Ensure completion in finite time... */
1769                                 LNetMDUnlink(mdh);
1770                                 /* No assertion (racing with network) */
1771                                 unlinked = 1;
1772                                 timeout_ms = a_long_time;
1773                         } else if (rc2 == 0) {
1774                                 /* timed out waiting for unlink */
1775                                 CWARN("ping %s: late network completion\n",
1776                                       libcfs_id2str(id));
1777                         }
1778                 } else if (event.type == LNET_EVENT_REPLY) {
1779                         replied = 1;
1780                         rc = event.mlength;
1781                 }
1782
1783         } while (rc2 <= 0 || !event.unlinked);
1784
1785         if (!replied) {
1786                 if (rc >= 0)
1787                         CWARN("%s: Unexpected rc(%d) >= 0 but no reply!\n",
1788                               libcfs_id2str(id), rc);
1789                 rc = -EIO;
1790                 goto out_1;
1791         }
1792
1793         LASSERT (rc >= 0 && rc <= infosz); /* rc == event.mlength */
1794         rc = lnet_parse_pinginfo(info, rc, n_ids);
1795         if (rc != 0) {
1796                 CERROR("Bad ping reply from %s\n", libcfs_id2str(id));
1797                 goto out_1;
1798         }
1799
1800         rc = -EFAULT;                           /* If I SEGV... */
1801         n_ids = MIN(info->pi_nnis, n_ids);
1802         for (i = 0; i < n_ids; i++) {
1803                 tmpid.pid = info->pi_pid;
1804                 if (info->pi_version == LNET_PROTO_PING_VERSION)
1805                         tmpid.nid = info->pi_ni[i].ns_nid;
1806                 else
1807                         tmpid.nid = info->pi_nid[i];
1808
1809 #ifdef __KERNEL__
1810                 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
1811                         goto out_1;
1812 #else
1813                 ids[i] = tmpid;
1814 #endif
1815         }
1816         rc = info->pi_nnis;
1817
1818  out_1:
1819         rc2 = LNetEQFree(eqh);
1820         if (rc2 != 0)
1821                 CERROR("rc2 %d\n", rc2);
1822         LASSERT (rc2 == 0);
1823
1824  out_0:
1825         LIBCFS_FREE(info, infosz);
1826         return rc;
1827 }