1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
41 #define D_LNI D_CONSOLE
43 #define D_LNI D_CONFIG
46 lnet_t the_lnet; /* THE state of the network */
50 static char *ip2nets = "";
51 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
52 "LNET network <- IP table");
54 static char *networks = "";
55 CFS_MODULE_PARM(networks, "s", charp, 0444,
58 static char *routes = "";
59 CFS_MODULE_PARM(routes, "s", charp, 0444,
60 "routes to non-local networks");
69 lnet_get_networks(void)
74 if (*networks != 0 && *ip2nets != 0) {
75 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
76 "'ip2nets' but not both at once\n");
81 rc = lnet_parse_ip2nets(&nets, ip2nets);
82 return (rc == 0) ? nets : NULL;
94 spin_lock_init (&the_lnet.ln_lock);
95 cfs_waitq_init (&the_lnet.ln_waitq);
96 init_mutex(&the_lnet.ln_lnd_mutex);
97 init_mutex(&the_lnet.ln_api_mutex);
101 lnet_fini_locks(void)
108 lnet_get_routes(void)
110 char *str = getenv("LNET_ROUTES");
112 return (str == NULL) ? "" : str;
116 lnet_get_networks (void)
118 static char default_networks[256];
119 char *networks = getenv ("LNET_NETWORKS");
120 char *ip2nets = getenv ("LNET_IP2NETS");
126 struct list_head *tmp;
129 if (networks != NULL && ip2nets != NULL) {
130 LCONSOLE_ERROR_MSG(0x103, "Please set EITHER 'LNET_NETWORKS' or"
131 " 'LNET_IP2NETS' but not both at once\n");
135 if (ip2nets != NULL) {
136 rc = lnet_parse_ip2nets(&networks, ip2nets);
137 return (rc == 0) ? networks : NULL;
143 if (networks != NULL)
146 /* In userland, the default 'networks=' is the list of known net types */
148 len = sizeof(default_networks);
149 str = default_networks;
153 list_for_each (tmp, &the_lnet.ln_lnds) {
154 lnd_t *lnd = list_entry(tmp, lnd_t, lnd_list);
156 nob = snprintf(str, len, "%s%s", sep,
157 libcfs_lnd2str(lnd->lnd_type));
160 /* overflowed the string; leave it where it was */
169 return default_networks;
172 # ifndef HAVE_LIBPTHREAD
174 void lnet_init_locks(void)
176 the_lnet.ln_lock = 0;
177 the_lnet.ln_lnd_mutex = 0;
178 the_lnet.ln_api_mutex = 0;
181 void lnet_fini_locks(void)
183 LASSERT (the_lnet.ln_api_mutex == 0);
184 LASSERT (the_lnet.ln_lnd_mutex == 0);
185 LASSERT (the_lnet.ln_lock == 0);
190 void lnet_init_locks(void)
192 pthread_cond_init(&the_lnet.ln_cond, NULL);
193 pthread_mutex_init(&the_lnet.ln_lock, NULL);
194 pthread_mutex_init(&the_lnet.ln_lnd_mutex, NULL);
195 pthread_mutex_init(&the_lnet.ln_api_mutex, NULL);
198 void lnet_fini_locks(void)
200 pthread_mutex_destroy(&the_lnet.ln_api_mutex);
201 pthread_mutex_destroy(&the_lnet.ln_lnd_mutex);
202 pthread_mutex_destroy(&the_lnet.ln_lock);
203 pthread_cond_destroy(&the_lnet.ln_cond);
209 void lnet_assert_wire_constants (void)
211 /* Wire protocol assertions generated by 'wirecheck'
212 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
213 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
214 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
217 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
218 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
219 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
220 CLASSERT (LNET_MSG_ACK == 0);
221 CLASSERT (LNET_MSG_PUT == 1);
222 CLASSERT (LNET_MSG_GET == 2);
223 CLASSERT (LNET_MSG_REPLY == 3);
224 CLASSERT (LNET_MSG_HELLO == 4);
226 /* Checks for struct ptl_handle_wire_t */
227 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
228 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
229 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
230 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
231 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
233 /* Checks for struct lnet_magicversion_t */
234 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
235 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
236 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
237 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
238 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
239 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
240 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
242 /* Checks for struct lnet_hdr_t */
243 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
244 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
245 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
246 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
247 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
248 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
249 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
250 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
251 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
252 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
253 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
254 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
255 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
256 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
257 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
260 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
261 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
262 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
263 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
264 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
265 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
268 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
269 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
270 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
271 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
272 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
273 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
274 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
275 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
276 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
277 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
280 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
281 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
282 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
283 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
284 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
285 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
286 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
287 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
288 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
289 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
292 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
293 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
296 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
297 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
298 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
299 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
303 lnet_find_lnd_by_type (int type)
306 struct list_head *tmp;
308 /* holding lnd mutex */
309 list_for_each (tmp, &the_lnet.ln_lnds) {
310 lnd = list_entry(tmp, lnd_t, lnd_list);
312 if ((int)lnd->lnd_type == type)
320 lnet_register_lnd (lnd_t *lnd)
322 LNET_MUTEX_DOWN(&the_lnet.ln_lnd_mutex);
324 LASSERT (the_lnet.ln_init);
325 LASSERT (libcfs_isknown_lnd(lnd->lnd_type));
326 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
328 list_add_tail (&lnd->lnd_list, &the_lnet.ln_lnds);
329 lnd->lnd_refcount = 0;
331 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
333 LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
337 lnet_unregister_lnd (lnd_t *lnd)
339 LNET_MUTEX_DOWN(&the_lnet.ln_lnd_mutex);
341 LASSERT (the_lnet.ln_init);
342 LASSERT (lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
343 LASSERT (lnd->lnd_refcount == 0);
345 list_del (&lnd->lnd_list);
346 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
348 LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
351 #ifndef LNET_USE_LIB_FREELIST
354 lnet_descriptor_setup (void)
360 lnet_descriptor_cleanup (void)
367 lnet_freelist_init (lnet_freelist_t *fl, int n, int size)
373 size += offsetof (lnet_freeobj_t, fo_contents);
375 LIBCFS_ALLOC(space, n * size);
379 CFS_INIT_LIST_HEAD (&fl->fl_list);
382 fl->fl_objsize = size;
386 memset (space, 0, size);
387 list_add ((struct list_head *)space, &fl->fl_list);
395 lnet_freelist_fini (lnet_freelist_t *fl)
397 struct list_head *el;
400 if (fl->fl_nobjs == 0)
404 for (el = fl->fl_list.next; el != &fl->fl_list; el = el->next)
407 LASSERT (count == fl->fl_nobjs);
409 LIBCFS_FREE(fl->fl_objs, fl->fl_nobjs * fl->fl_objsize);
410 memset (fl, 0, sizeof (*fl));
414 lnet_descriptor_setup (void)
416 /* NB on failure caller must still call lnet_descriptor_cleanup */
420 memset (&the_lnet.ln_free_mes, 0, sizeof (the_lnet.ln_free_mes));
421 memset (&the_lnet.ln_free_msgs, 0, sizeof (the_lnet.ln_free_msgs));
422 memset (&the_lnet.ln_free_mds, 0, sizeof (the_lnet.ln_free_mds));
423 memset (&the_lnet.ln_free_eqs, 0, sizeof (the_lnet.ln_free_eqs));
425 rc = lnet_freelist_init(&the_lnet.ln_free_mes,
426 MAX_MES, sizeof (lnet_me_t));
430 rc = lnet_freelist_init(&the_lnet.ln_free_msgs,
431 MAX_MSGS, sizeof (lnet_msg_t));
435 rc = lnet_freelist_init(&the_lnet.ln_free_mds,
436 MAX_MDS, sizeof (lnet_libmd_t));
440 rc = lnet_freelist_init(&the_lnet.ln_free_eqs,
441 MAX_EQS, sizeof (lnet_eq_t));
446 lnet_descriptor_cleanup (void)
448 lnet_freelist_fini (&the_lnet.ln_free_mes);
449 lnet_freelist_fini (&the_lnet.ln_free_msgs);
450 lnet_freelist_fini (&the_lnet.ln_free_mds);
451 lnet_freelist_fini (&the_lnet.ln_free_eqs);
457 lnet_create_interface_cookie (void)
459 /* NB the interface cookie in wire handles guards against delayed
460 * replies and ACKs appearing valid after reboot. Initialisation time,
461 * even if it's only implemented to millisecond resolution is probably
462 * easily good enough. */
466 int rc = gettimeofday (&tv, NULL);
469 do_gettimeofday(&tv);
473 cookie += tv.tv_usec;
478 lnet_setup_handle_hash (void)
482 /* Arbitrary choice of hash table size */
484 the_lnet.ln_lh_hash_size = CFS_PAGE_SIZE / sizeof (struct list_head);
486 the_lnet.ln_lh_hash_size = (MAX_MES + MAX_MDS + MAX_EQS)/4;
488 LIBCFS_ALLOC(the_lnet.ln_lh_hash_table,
489 the_lnet.ln_lh_hash_size * sizeof (struct list_head));
490 if (the_lnet.ln_lh_hash_table == NULL)
493 for (i = 0; i < the_lnet.ln_lh_hash_size; i++)
494 CFS_INIT_LIST_HEAD (&the_lnet.ln_lh_hash_table[i]);
496 the_lnet.ln_next_object_cookie = LNET_COOKIE_TYPES;
502 lnet_cleanup_handle_hash (void)
504 if (the_lnet.ln_lh_hash_table == NULL)
507 LIBCFS_FREE(the_lnet.ln_lh_hash_table,
508 the_lnet.ln_lh_hash_size * sizeof (struct list_head));
512 lnet_lookup_cookie (__u64 cookie, int type)
514 /* ALWAYS called with LNET_LOCK held */
515 struct list_head *list;
516 struct list_head *el;
519 if ((cookie & (LNET_COOKIE_TYPES - 1)) != type)
522 hash = ((unsigned int)cookie) % the_lnet.ln_lh_hash_size;
523 list = &the_lnet.ln_lh_hash_table[hash];
525 list_for_each (el, list) {
526 lnet_libhandle_t *lh = list_entry (el, lnet_libhandle_t,
529 if (lh->lh_cookie == cookie)
537 lnet_initialise_handle (lnet_libhandle_t *lh, int type)
539 /* ALWAYS called with LNET_LOCK held */
542 LASSERT (type >= 0 && type < LNET_COOKIE_TYPES);
543 lh->lh_cookie = the_lnet.ln_next_object_cookie | type;
544 the_lnet.ln_next_object_cookie += LNET_COOKIE_TYPES;
546 hash = ((unsigned int)lh->lh_cookie) % the_lnet.ln_lh_hash_size;
547 list_add (&lh->lh_hash_chain, &the_lnet.ln_lh_hash_table[hash]);
551 lnet_invalidate_handle (lnet_libhandle_t *lh)
553 /* ALWAYS called with LNET_LOCK held */
554 list_del (&lh->lh_hash_chain);
558 lnet_init_finalizers(void)
563 the_lnet.ln_nfinalizers = (int) num_online_cpus();
565 LIBCFS_ALLOC(the_lnet.ln_finalizers,
566 the_lnet.ln_nfinalizers *
567 sizeof(*the_lnet.ln_finalizers));
568 if (the_lnet.ln_finalizers == NULL) {
569 CERROR("Can't allocate ln_finalizers\n");
573 for (i = 0; i < the_lnet.ln_nfinalizers; i++)
574 the_lnet.ln_finalizers[i] = NULL;
576 the_lnet.ln_finalizing = 0;
579 CFS_INIT_LIST_HEAD(&the_lnet.ln_finalizeq);
584 lnet_fini_finalizers(void)
589 for (i = 0; i < the_lnet.ln_nfinalizers; i++)
590 LASSERT (the_lnet.ln_finalizers[i] == NULL);
592 LIBCFS_FREE(the_lnet.ln_finalizers,
593 the_lnet.ln_nfinalizers *
594 sizeof(*the_lnet.ln_finalizers));
596 LASSERT (!the_lnet.ln_finalizing);
598 LASSERT (list_empty(&the_lnet.ln_finalizeq));
602 /* Temporary workaround to allow uOSS and test programs force server
603 * mode in userspace. See comments near ln_server_mode_flag in
604 * lnet/lib-types.h */
608 the_lnet.ln_server_mode_flag = 1;
613 lnet_prepare(lnet_pid_t requested_pid)
615 /* Prepare to bring up the network */
619 LASSERT (the_lnet.ln_refcount == 0);
621 the_lnet.ln_routing = 0;
624 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
625 the_lnet.ln_pid = requested_pid;
627 if (the_lnet.ln_server_mode_flag) {/* server case (uOSS) */
628 LASSERT ((requested_pid & LNET_PID_USERFLAG) == 0);
630 if (cfs_curproc_uid())/* Only root can run user-space server */
632 the_lnet.ln_pid = requested_pid;
634 } else {/* client case (liblustre) */
636 /* My PID must be unique on this node and flag I'm userspace */
637 the_lnet.ln_pid = getpid() | LNET_PID_USERFLAG;
641 rc = lnet_descriptor_setup();
645 memset(&the_lnet.ln_counters, 0,
646 sizeof(the_lnet.ln_counters));
648 CFS_INIT_LIST_HEAD (&the_lnet.ln_active_msgs);
649 CFS_INIT_LIST_HEAD (&the_lnet.ln_active_mds);
650 CFS_INIT_LIST_HEAD (&the_lnet.ln_active_eqs);
651 CFS_INIT_LIST_HEAD (&the_lnet.ln_test_peers);
652 CFS_INIT_LIST_HEAD (&the_lnet.ln_nis);
653 CFS_INIT_LIST_HEAD (&the_lnet.ln_zombie_nis);
654 CFS_INIT_LIST_HEAD (&the_lnet.ln_remote_nets);
655 CFS_INIT_LIST_HEAD (&the_lnet.ln_routers);
657 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
659 lnet_init_rtrpools();
661 rc = lnet_setup_handle_hash ();
665 rc = lnet_create_peer_table();
669 rc = lnet_init_finalizers();
673 the_lnet.ln_nportals = MAX_PORTALS;
674 LIBCFS_ALLOC(the_lnet.ln_portals,
675 the_lnet.ln_nportals *
676 sizeof(*the_lnet.ln_portals));
677 if (the_lnet.ln_portals == NULL) {
682 for (i = 0; i < the_lnet.ln_nportals; i++) {
683 CFS_INIT_LIST_HEAD(&(the_lnet.ln_portals[i].ptl_ml));
684 CFS_INIT_LIST_HEAD(&(the_lnet.ln_portals[i].ptl_msgq));
685 the_lnet.ln_portals[i].ptl_options = 0;
691 lnet_fini_finalizers();
693 lnet_destroy_peer_table();
695 lnet_cleanup_handle_hash();
697 lnet_descriptor_cleanup();
702 lnet_unprepare (void)
706 /* NB no LNET_LOCK since this is the last reference. All LND instances
707 * have shut down already, so it is safe to unlink and free all
708 * descriptors, even those that appear committed to a network op (eg MD
709 * with non-zero pending count) */
711 lnet_fail_nid(LNET_NID_ANY, 0);
713 LASSERT (list_empty(&the_lnet.ln_test_peers));
714 LASSERT (the_lnet.ln_refcount == 0);
715 LASSERT (list_empty(&the_lnet.ln_nis));
716 LASSERT (list_empty(&the_lnet.ln_zombie_nis));
717 LASSERT (the_lnet.ln_nzombie_nis == 0);
719 for (idx = 0; idx < the_lnet.ln_nportals; idx++) {
720 LASSERT (list_empty(&the_lnet.ln_portals[idx].ptl_msgq));
722 while (!list_empty (&the_lnet.ln_portals[idx].ptl_ml)) {
723 lnet_me_t *me = list_entry (the_lnet.ln_portals[idx].ptl_ml.next,
726 CERROR ("Active me %p on exit\n", me);
727 list_del (&me->me_list);
732 while (!list_empty (&the_lnet.ln_active_mds)) {
733 lnet_libmd_t *md = list_entry (the_lnet.ln_active_mds.next,
734 lnet_libmd_t, md_list);
736 CERROR ("Active md %p on exit\n", md);
737 list_del_init (&md->md_list);
741 while (!list_empty (&the_lnet.ln_active_eqs)) {
742 lnet_eq_t *eq = list_entry (the_lnet.ln_active_eqs.next,
745 CERROR ("Active eq %p on exit\n", eq);
746 list_del (&eq->eq_list);
750 while (!list_empty (&the_lnet.ln_active_msgs)) {
751 lnet_msg_t *msg = list_entry (the_lnet.ln_active_msgs.next,
752 lnet_msg_t, msg_activelist);
754 CERROR ("Active msg %p on exit\n", msg);
755 LASSERT (msg->msg_onactivelist);
756 msg->msg_onactivelist = 0;
757 list_del (&msg->msg_activelist);
761 LIBCFS_FREE(the_lnet.ln_portals,
762 the_lnet.ln_nportals * sizeof(*the_lnet.ln_portals));
764 lnet_free_rtrpools();
765 lnet_fini_finalizers();
766 lnet_destroy_peer_table();
767 lnet_cleanup_handle_hash();
768 lnet_descriptor_cleanup();
774 lnet_net2ni_locked (__u32 net)
776 struct list_head *tmp;
779 list_for_each (tmp, &the_lnet.ln_nis) {
780 ni = list_entry(tmp, lnet_ni_t, ni_list);
782 if (LNET_NIDNET(ni->ni_nid) == net) {
783 lnet_ni_addref_locked(ni);
792 lnet_islocalnet (__u32 net)
797 ni = lnet_net2ni_locked(net);
799 lnet_ni_decref_locked(ni);
806 lnet_nid2ni_locked (lnet_nid_t nid)
808 struct list_head *tmp;
811 list_for_each (tmp, &the_lnet.ln_nis) {
812 ni = list_entry(tmp, lnet_ni_t, ni_list);
814 if (ni->ni_nid == nid) {
815 lnet_ni_addref_locked(ni);
824 lnet_islocalnid (lnet_nid_t nid)
829 ni = lnet_nid2ni_locked(nid);
831 lnet_ni_decref_locked(ni);
838 lnet_count_acceptor_nis (void)
840 /* Return the # of NIs that need the acceptor. */
842 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
843 struct list_head *tmp;
847 list_for_each (tmp, &the_lnet.ln_nis) {
848 ni = list_entry(tmp, lnet_ni_t, ni_list);
850 if (ni->ni_lnd->lnd_accept != NULL)
856 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */
861 lnet_shutdown_lndnis (void)
867 /* NB called holding the global mutex */
869 /* All quiet on the API front */
870 LASSERT (!the_lnet.ln_shutdown);
871 LASSERT (the_lnet.ln_refcount == 0);
872 LASSERT (list_empty(&the_lnet.ln_zombie_nis));
873 LASSERT (the_lnet.ln_nzombie_nis == 0);
874 LASSERT (list_empty(&the_lnet.ln_remote_nets));
877 the_lnet.ln_shutdown = 1; /* flag shutdown */
879 /* Unlink NIs from the global table */
880 while (!list_empty(&the_lnet.ln_nis)) {
881 ni = list_entry(the_lnet.ln_nis.next,
883 list_del (&ni->ni_list);
885 the_lnet.ln_nzombie_nis++;
886 lnet_ni_decref_locked(ni); /* drop apini's ref */
889 /* Drop the cached eqwait NI. */
890 if (the_lnet.ln_eqwaitni != NULL) {
891 lnet_ni_decref_locked(the_lnet.ln_eqwaitni);
892 the_lnet.ln_eqwaitni = NULL;
895 /* Drop the cached loopback NI. */
896 if (the_lnet.ln_loni != NULL) {
897 lnet_ni_decref_locked(the_lnet.ln_loni);
898 the_lnet.ln_loni = NULL;
903 /* Clear lazy portals and drop delayed messages which hold refs
904 * on their lnet_msg_t::msg_rxpeer */
905 for (i = 0; i < the_lnet.ln_nportals; i++)
906 LNetClearLazyPortal(i);
908 /* Clear the peer table and wait for all peers to go (they hold refs on
910 lnet_clear_peer_table();
913 /* Now wait for the NI's I just nuked to show up on apini_zombie_nis
914 * and shut them down in guaranteed thread context */
916 while (the_lnet.ln_nzombie_nis != 0) {
918 while (list_empty(&the_lnet.ln_zombie_nis)) {
922 CDEBUG(D_WARNING,"Waiting for %d zombie NIs\n",
923 the_lnet.ln_nzombie_nis);
924 cfs_pause(cfs_time_seconds(1));
928 ni = list_entry(the_lnet.ln_zombie_nis.next,
930 list_del(&ni->ni_list);
931 ni->ni_lnd->lnd_refcount--;
935 islo = ni->ni_lnd->lnd_type == LOLND;
937 LASSERT (!in_interrupt ());
938 (ni->ni_lnd->lnd_shutdown)(ni);
940 /* can't deref lnd anymore now; it might have unregistered
944 CDEBUG(D_LNI, "Removed LNI %s\n",
945 libcfs_nid2str(ni->ni_nid));
947 LIBCFS_FREE(ni, sizeof(*ni));
950 the_lnet.ln_nzombie_nis--;
953 the_lnet.ln_shutdown = 0;
956 if (the_lnet.ln_network_tokens != NULL) {
957 LIBCFS_FREE(the_lnet.ln_network_tokens,
958 the_lnet.ln_network_tokens_nob);
959 the_lnet.ln_network_tokens = NULL;
964 lnet_startup_lndnis (void)
968 struct list_head nilist;
972 char *nets = lnet_get_networks();
974 CFS_INIT_LIST_HEAD(&nilist);
979 rc = lnet_parse_networks(&nilist, nets);
983 while (!list_empty(&nilist)) {
984 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
985 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
987 LASSERT (libcfs_isknown_lnd(lnd_type));
989 LNET_MUTEX_DOWN(&the_lnet.ln_lnd_mutex);
990 lnd = lnet_find_lnd_by_type(lnd_type);
994 LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
995 rc = request_module(libcfs_lnd2modname(lnd_type));
996 LNET_MUTEX_DOWN(&the_lnet.ln_lnd_mutex);
998 lnd = lnet_find_lnd_by_type(lnd_type);
1000 LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
1001 CERROR("Can't load LND %s, module %s, rc=%d\n",
1002 libcfs_lnd2str(lnd_type),
1003 libcfs_lnd2modname(lnd_type), rc);
1005 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1006 "compiled with CONFIG_KMOD set for "
1007 "automatic module loading.");
1014 LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
1015 CERROR("LND %s not supported\n",
1016 libcfs_lnd2str(lnd_type));
1021 ni->ni_refcount = 1;
1024 lnd->lnd_refcount++;
1029 rc = (lnd->lnd_startup)(ni);
1031 LNET_MUTEX_UP(&the_lnet.ln_lnd_mutex);
1034 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s"
1036 rc, libcfs_lnd2str(lnd->lnd_type));
1038 lnd->lnd_refcount--;
1043 list_del(&ni->ni_list);
1046 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1049 if (lnd->lnd_type == LOLND) {
1051 LASSERT (the_lnet.ln_loni == NULL);
1052 the_lnet.ln_loni = ni;
1057 if (lnd->lnd_wait != NULL) {
1058 if (the_lnet.ln_eqwaitni == NULL) {
1060 the_lnet.ln_eqwaitni = ni;
1063 # ifndef HAVE_LIBPTHREAD
1064 LCONSOLE_ERROR_MSG(0x106, "LND %s not supported in a "
1065 "single-threaded runtime\n",
1066 libcfs_lnd2str(lnd_type));
1071 if (ni->ni_peertxcredits == 0 ||
1072 ni->ni_maxtxcredits == 0) {
1073 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1074 libcfs_lnd2str(lnd->lnd_type),
1075 ni->ni_peertxcredits == 0 ?
1080 ni->ni_txcredits = ni->ni_mintxcredits = ni->ni_maxtxcredits;
1082 CDEBUG(D_LNI, "Added LNI %s [%d/%d]\n",
1083 libcfs_nid2str(ni->ni_nid),
1084 ni->ni_peertxcredits, ni->ni_txcredits);
1089 if (the_lnet.ln_eqwaitni != NULL && nicount > 1) {
1090 lnd_type = the_lnet.ln_eqwaitni->ni_lnd->lnd_type;
1091 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network"
1093 libcfs_lnd2str(lnd_type));
1100 lnet_shutdown_lndnis();
1102 while (!list_empty(&nilist)) {
1103 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1104 list_del(&ni->ni_list);
1105 LIBCFS_FREE(ni, sizeof(*ni));
1114 lnet_assert_wire_constants ();
1115 LASSERT (!the_lnet.ln_init);
1117 memset(&the_lnet, 0, sizeof(the_lnet));
1120 CFS_INIT_LIST_HEAD(&the_lnet.ln_lnds);
1121 the_lnet.ln_refcount = 0;
1122 the_lnet.ln_init = 1;
1125 /* All LNDs apart from the LOLND are in separate modules. They
1126 * register themselves when their module loads, and unregister
1127 * themselves when their module is unloaded. */
1130 * NB the order here determines default 'networks=' order */
1132 LNET_REGISTER_ULND(the_ptllnd);
1134 # ifdef HAVE_LIBPTHREAD
1135 LNET_REGISTER_ULND(the_tcplnd);
1138 lnet_register_lnd(&the_lolnd);
1145 LASSERT (the_lnet.ln_init);
1146 LASSERT (the_lnet.ln_refcount == 0);
1148 while (!list_empty(&the_lnet.ln_lnds))
1149 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1153 the_lnet.ln_init = 0;
1157 LNetNIInit(lnet_pid_t requested_pid)
1159 int im_a_router = 0;
1162 LNET_MUTEX_DOWN(&the_lnet.ln_api_mutex);
1164 LASSERT (the_lnet.ln_init);
1165 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1167 if (the_lnet.ln_refcount > 0) {
1168 rc = the_lnet.ln_refcount++;
1172 if (requested_pid == LNET_PID_ANY) {
1173 /* Don't instantiate LNET just for me */
1178 rc = lnet_prepare(requested_pid);
1182 rc = lnet_startup_lndnis();
1186 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1190 rc = lnet_check_routes();
1194 rc = lnet_alloc_rtrpools(im_a_router);
1198 rc = lnet_acceptor_start();
1202 the_lnet.ln_refcount = 1;
1203 /* Now I may use my own API functions... */
1205 rc = lnet_router_checker_start();
1209 rc = lnet_ping_target_init();
1217 lnet_router_checker_stop();
1219 the_lnet.ln_refcount = 0;
1220 lnet_acceptor_stop();
1222 lnet_destroy_routes();
1223 lnet_shutdown_lndnis();
1229 LNET_MUTEX_UP(&the_lnet.ln_api_mutex);
1236 LNET_MUTEX_DOWN(&the_lnet.ln_api_mutex);
1238 LASSERT (the_lnet.ln_init);
1239 LASSERT (the_lnet.ln_refcount > 0);
1241 if (the_lnet.ln_refcount != 1) {
1242 the_lnet.ln_refcount--;
1244 LASSERT (!the_lnet.ln_niinit_self);
1247 lnet_ping_target_fini();
1248 lnet_router_checker_stop();
1250 /* Teardown fns that use my own API functions BEFORE here */
1251 the_lnet.ln_refcount = 0;
1253 lnet_acceptor_stop();
1254 lnet_destroy_routes();
1255 lnet_shutdown_lndnis();
1259 LNET_MUTEX_UP(&the_lnet.ln_api_mutex);
1264 LNetCtl(unsigned int cmd, void *arg)
1266 struct libcfs_ioctl_data *data = arg;
1267 lnet_process_id_t id = {0};
1271 LASSERT (the_lnet.ln_init);
1272 LASSERT (the_lnet.ln_refcount > 0);
1275 case IOC_LIBCFS_GET_NI:
1276 rc = LNetGetId(data->ioc_count, &id);
1277 data->ioc_nid = id.nid;
1280 case IOC_LIBCFS_FAIL_NID:
1281 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1283 case IOC_LIBCFS_ADD_ROUTE:
1284 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1286 return (rc != 0) ? rc : lnet_check_routes();
1288 case IOC_LIBCFS_DEL_ROUTE:
1289 return lnet_del_route(data->ioc_net, data->ioc_nid);
1291 case IOC_LIBCFS_GET_ROUTE:
1292 return lnet_get_route(data->ioc_count,
1293 &data->ioc_net, &data->ioc_count,
1294 &data->ioc_nid, &data->ioc_flags);
1295 case IOC_LIBCFS_NOTIFY_ROUTER:
1296 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1297 (time_t)data->ioc_u64[0]);
1299 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
1300 /* This can be removed once lustre stops calling it */
1303 case IOC_LIBCFS_LNET_DIST:
1304 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1305 if (rc < 0 && rc != -EHOSTUNREACH)
1308 data->ioc_u32[0] = rc;
1311 case IOC_LIBCFS_TESTPROTOCOMPAT:
1313 the_lnet.ln_testprotocompat = data->ioc_flags;
1317 case IOC_LIBCFS_PING:
1318 id.nid = data->ioc_nid;
1319 id.pid = data->ioc_u32[0];
1320 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1321 (lnet_process_id_t *)data->ioc_pbuf1,
1322 data->ioc_plen1/sizeof(lnet_process_id_t));
1325 data->ioc_count = rc;
1328 case IOC_LIBCFS_DEBUG_PEER: {
1329 /* CAVEAT EMPTOR: this one designed for calling directly; not
1331 id = *((lnet_process_id_t *) arg);
1333 lnet_debug_peer(id.nid);
1335 ni = lnet_net2ni(LNET_NIDNET(id.nid));
1337 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
1339 if (ni->ni_lnd->lnd_ctl == NULL) {
1340 CDEBUG(D_WARNING, "No ctl for %s\n",
1343 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1352 ni = lnet_net2ni(data->ioc_net);
1356 if (ni->ni_lnd->lnd_ctl == NULL)
1359 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1368 LNetGetId(unsigned int index, lnet_process_id_t *id)
1371 struct list_head *tmp;
1374 LASSERT (the_lnet.ln_init);
1375 LASSERT (the_lnet.ln_refcount > 0);
1379 list_for_each(tmp, &the_lnet.ln_nis) {
1383 ni = list_entry(tmp, lnet_ni_t, ni_list);
1385 id->nid = ni->ni_nid;
1386 id->pid = the_lnet.ln_pid;
1397 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1399 snprintf(str, len, LPX64, h.cookie);
1404 lnet_ping_target_init(void)
1406 lnet_handle_me_t meh;
1407 lnet_process_id_t id;
1412 unsigned int infosz;
1415 for (n = 0; ; n++) {
1416 rc = LNetGetId(n, &id);
1423 infosz = offsetof(lnet_ping_info_t, pi_nid[n]);
1424 LIBCFS_ALLOC(the_lnet.ln_ping_info, infosz);
1425 if (the_lnet.ln_ping_info == NULL) {
1426 CERROR("Can't allocate ping info[%d]\n", n);
1430 the_lnet.ln_ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
1431 the_lnet.ln_ping_info->pi_version = LNET_PROTO_PING_VERSION;
1432 the_lnet.ln_ping_info->pi_pid = the_lnet.ln_pid;
1433 the_lnet.ln_ping_info->pi_nnids = n;
1435 for (i = 0; i < n; i++) {
1436 rc = LNetGetId(i, &id);
1438 the_lnet.ln_ping_info->pi_nid[i] = id.nid;
1441 /* We can have a tiny EQ since we only need to see the unlink event on
1442 * teardown, which by definition is the last one! */
1443 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1445 CERROR("Can't allocate ping EQ: %d\n", rc);
1449 memset(&id, 0, sizeof(lnet_process_id_t));
1450 id.nid = LNET_NID_ANY;
1451 id.pid = LNET_PID_ANY;
1453 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1454 LNET_PROTO_PING_MATCHBITS, 0,
1455 LNET_UNLINK, LNET_INS_AFTER,
1458 CERROR("Can't create ping ME: %d\n", rc);
1462 /* initialize md content */
1463 md.start = the_lnet.ln_ping_info;
1465 md.threshold = LNET_MD_THRESH_INF;
1467 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1468 LNET_MD_MANAGE_REMOTE;
1470 md.eq_handle = the_lnet.ln_ping_target_eq;
1472 rc = LNetMDAttach(meh, md,
1474 &the_lnet.ln_ping_target_md);
1476 CERROR("Can't attach ping MD: %d\n", rc);
1483 rc2 = LNetMEUnlink(meh);
1486 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1489 LIBCFS_FREE(the_lnet.ln_ping_info, infosz);
1495 lnet_ping_target_fini(void)
1500 int timeout_ms = 1000;
1501 cfs_sigset_t blocked = cfs_block_allsigs();
1503 LNetMDUnlink(the_lnet.ln_ping_target_md);
1504 /* NB md could be busy; this just starts the unlink */
1507 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1508 timeout_ms, &event, &which);
1510 /* I expect overflow... */
1511 LASSERT (rc >= 0 || rc == -EOVERFLOW);
1514 /* timed out: provide a diagnostic */
1515 CWARN("Still waiting for ping MD to unlink\n");
1520 /* Got a valid event */
1525 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1528 LIBCFS_FREE(the_lnet.ln_ping_info,
1529 offsetof(lnet_ping_info_t,
1530 pi_nid[the_lnet.ln_ping_info->pi_nnids]));
1532 cfs_restore_sigs(blocked);
1536 lnet_ping (lnet_process_id_t id, int timeout_ms, lnet_process_id_t *ids, int n_ids)
1538 lnet_handle_eq_t eqh;
1539 lnet_handle_md_t mdh;
1545 const int a_long_time = 60000; /* mS */
1546 int infosz = offsetof(lnet_ping_info_t, pi_nid[n_ids]);
1547 lnet_ping_info_t *info;
1548 lnet_process_id_t tmpid;
1553 cfs_sigset_t blocked;
1556 id.nid == LNET_NID_ANY ||
1557 timeout_ms > 500000 || /* arbitrary limit! */
1558 n_ids > 20) /* arbitrary limit! */
1561 if (id.pid == LNET_PID_ANY)
1562 id.pid = LUSTRE_SRV_LNET_PID;
1564 LIBCFS_ALLOC(info, infosz);
1568 /* NB 2 events max (including any unlink event) */
1569 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1571 CERROR("Can't allocate EQ: %d\n", rc);
1575 /* initialize md content */
1578 md.threshold = 2; /*GET/REPLY*/
1580 md.options = LNET_MD_TRUNCATE;
1584 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1586 CERROR("Can't bind MD: %d\n", rc);
1590 rc = LNetGet(LNET_NID_ANY, mdh, id,
1591 LNET_RESERVED_PORTAL,
1592 LNET_PROTO_PING_MATCHBITS, 0);
1595 /* Don't CERROR; this could be deliberate! */
1597 rc2 = LNetMDUnlink(mdh);
1600 /* NB must wait for the UNLINK event below... */
1602 timeout_ms = a_long_time;
1606 /* MUST block for unlink to complete */
1608 blocked = cfs_block_allsigs();
1610 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1613 cfs_restore_sigs(blocked);
1615 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1616 (rc2 <= 0) ? -1 : event.type,
1617 (rc2 <= 0) ? -1 : event.status,
1618 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1620 LASSERT (rc2 != -EOVERFLOW); /* can't miss anything */
1622 if (rc2 <= 0 || event.status != 0) {
1623 /* timeout or error */
1624 if (!replied && rc == 0)
1625 rc = (rc2 < 0) ? rc2 :
1626 (rc2 == 0) ? -ETIMEDOUT :
1630 /* Ensure completion in finite time... */
1632 /* No assertion (racing with network) */
1634 timeout_ms = a_long_time;
1635 } else if (rc2 == 0) {
1636 /* timed out waiting for unlink */
1637 CWARN("ping %s: late network completion\n",
1641 } else if (event.type == LNET_EVENT_REPLY) {
1646 } while (rc2 <= 0 || !event.unlinked);
1650 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
1657 LASSERT (nob >= 0 && nob <= infosz);
1659 rc = -EPROTO; /* if I can't parse... */
1662 /* can't check magic/version */
1663 CERROR("%s: ping info too short %d\n",
1664 libcfs_id2str(id), nob);
1668 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
1669 /* NB I might be swabbing garbage until I check below, but it
1671 __swab32s(&info->pi_version);
1672 __swab32s(&info->pi_pid);
1673 __swab32s(&info->pi_nnids);
1674 for (i = 0; i < (int)info->pi_nnids && i < (int)n_ids; i++)
1675 __swab64s(&info->pi_nid[i]);
1677 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
1678 CERROR("%s: Unexpected magic %08x\n",
1679 libcfs_id2str(id), info->pi_magic);
1683 if (info->pi_version != LNET_PROTO_PING_VERSION) {
1684 CERROR("%s: Unexpected version 0x%x\n",
1685 libcfs_id2str(id), info->pi_version);
1689 if (nob < (int)offsetof(lnet_ping_info_t, pi_nid[0])) {
1690 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
1691 nob, (int)offsetof(lnet_ping_info_t, pi_nid[0]));
1695 if ((int) info->pi_nnids < n_ids)
1696 n_ids = info->pi_nnids;
1698 if (nob < (int)offsetof(lnet_ping_info_t, pi_nid[n_ids])) {
1699 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
1700 nob, (int)offsetof(lnet_ping_info_t, pi_nid[n_ids]));
1704 rc = -EFAULT; /* If I SEGV... */
1706 for (i = 0; i < n_ids; i++) {
1707 tmpid.pid = info->pi_pid;
1708 tmpid.nid = info->pi_nid[i];
1710 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
1716 rc = info->pi_nnids;
1719 rc2 = LNetEQFree(eqh);
1721 CERROR("rc2 %d\n", rc2);
1725 LIBCFS_FREE(info, infosz);