1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Network Interface code
7 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * Copyright (c) 2001-2002 Sandia National Laboratories
10 * This file is part of Lustre, http://www.sf.net/projects/lustre/
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DEBUG_SUBSYSTEM S_PORTALS
27 #include <portals/api-support.h>
31 /* Put some magic in the NI handle so uninitialised/zeroed handles are easy
33 #define NI_HANDLE_MAGIC 0xebc0de00
34 #define NI_HANDLE_MASK 0x000000ff
36 static struct nal_t *ptl_nal_table[NAL_MAX_NR];
39 DECLARE_MUTEX(ptl_mutex);
41 static void ptl_mutex_enter (void)
46 static void ptl_mutex_exit (void)
51 static void ptl_mutex_enter (void)
55 static void ptl_mutex_exit (void)
60 nal_t *ptl_hndl2nal(ptl_handle_any_t *handle)
62 unsigned int idx = handle->nal_idx;
64 /* XXX we really rely on the caller NOT racing with interface
65 * setup/teardown. That ensures her NI handle can't get
66 * invalidated out from under her (or worse, swapped for a
67 * completely different interface!) */
69 if (((idx ^ NI_HANDLE_MAGIC) & ~NI_HANDLE_MASK) != 0)
72 idx &= NI_HANDLE_MASK;
74 if (idx >= NAL_MAX_NR ||
75 ptl_nal_table[idx] == NULL ||
76 ptl_nal_table[idx]->nal_refct == 0)
79 return ptl_nal_table[idx];
82 int ptl_register_nal (ptl_interface_t interface, nal_t *nal)
88 if (interface < 0 || interface >= NAL_MAX_NR)
89 rc = PTL_IFACE_INVALID;
90 else if (ptl_nal_table[interface] != NULL)
94 ptl_nal_table[interface] = nal;
95 LASSERT(nal->nal_refct == 0);
102 void ptl_unregister_nal (ptl_interface_t interface)
104 LASSERT(interface >= 0 && interface < NAL_MAX_NR);
105 LASSERT(ptl_nal_table[interface] != NULL);
106 LASSERT(ptl_nal_table[interface]->nal_refct == 0);
110 ptl_nal_table[interface] = NULL;
115 int ptl_ni_init(void)
117 /* If this assertion fails, we need more bits in NI_HANDLE_MASK and
118 * to shift NI_HANDLE_MAGIC left appropriately */
119 LASSERT (NAL_MAX_NR <= (NI_HANDLE_MASK + 1));
124 /* NULL pointers, clear flags */
125 memset(ptl_nal_table, 0, sizeof(ptl_nal_table));
127 /* Kernel NALs register themselves when their module loads,
128 * and unregister themselves when their module is unloaded.
129 * Userspace NALs, are plugged in explicitly here... */
131 extern nal_t procapi_nal;
133 /* XXX pretend it's socknal to keep liblustre happy... */
134 ptl_nal_table[SOCKNAL] = &procapi_nal;
135 LASSERT (procapi_nal.nal_refct == 0);
146 void ptl_ni_fini(void)
154 for (i = 0; i < NAL_MAX_NR; i++) {
156 nal = ptl_nal_table[i];
160 if (nal->nal_refct != 0) {
161 CWARN("NAL %d has outstanding refcount %d\n",
166 ptl_nal_table[i] = NULL;
175 int PtlNIInit(ptl_interface_t interface, ptl_pid_t requested_pid,
176 ptl_ni_limits_t *desired_limits, ptl_ni_limits_t *actual_limits,
177 ptl_handle_ni_t *handle)
188 if (interface == PTL_IFACE_DEFAULT) {
189 for (i = 0; i < NAL_MAX_NR; i++)
190 if (ptl_nal_table[i] != NULL) {
194 /* NB if no interfaces are registered, 'interface' will
195 * fail the valid test below */
199 interface >= NAL_MAX_NR ||
200 ptl_nal_table[interface] == NULL) {
201 GOTO(out, rc = PTL_IFACE_INVALID);
204 nal = ptl_nal_table[interface];
206 CDEBUG(D_OTHER, "Starting up NAL (%d) refs %d\n", interface, nal->nal_refct);
207 rc = nal->startup(nal, requested_pid, desired_limits, actual_limits);
210 CERROR("Error %d starting up NAL %d, refs %d\n", rc,
211 interface, nal->nal_refct);
215 if (nal->nal_refct != 0) {
216 /* Caller gets to know if this was the first ref or not */
221 handle->nal_idx = (NI_HANDLE_MAGIC & ~NI_HANDLE_MASK) | interface;
228 int PtlNIFini(ptl_handle_ni_t ni)
238 nal = ptl_hndl2nal (&ni);
241 return PTL_HANDLE_INVALID;
244 idx = ni.nal_idx & NI_HANDLE_MASK;
246 LASSERT(nal->nal_refct > 0);
250 /* nal_refct == 0 tells nal->shutdown to really shut down */
257 int PtlNIHandle(ptl_handle_any_t handle_in, ptl_handle_ni_t * ni_out)