1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002 Cray Inc.
5 * Copyright (c) 2003 Cluster File Systems, Inc.
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * This file provides the 'api' side for the process-based nals.
25 * it is responsible for creating the 'library' side thread,
26 * and passing wrapped portals transactions to it.
28 * Along with initialization, shutdown, and transport to the library
29 * side, this file contains some stubs to satisfy the nal definition.
38 #include <sys/socket.h>
39 #include <procbridge.h>
45 /* XXX CFS workaround, to give a chance to let nal thread wake up
46 * from waiting in select
48 static int procbridge_notifier_handler(void *arg)
51 procbridge p = (procbridge) arg;
53 syscall(SYS_read, p->notifier[1], buf, sizeof(buf));
57 void procbridge_wakeup_nal(procbridge p)
60 syscall(SYS_write, p->notifier[0], buf, sizeof(buf));
64 * Arguments: nal_t *nal: pointer to my top-side nal structure
65 * id: the command to pass to the lower layer
66 * args, args_len:pointer to and length of the request
67 * ret, ret_len: pointer to and size of the result
68 * Returns: a portals status code
70 * forwards a packaged api call from the 'api' side to the 'library'
71 * side, and collects the result
73 static int procbridge_forward(nal_t *n, int id, void *args, size_t args_len,
74 void *ret, size_t ret_len)
76 bridge b = (bridge) n->nal_data;
85 lib_dispatch(b->nal_cb, NULL, id, args, ret);
92 * Arguments: nal: a pointer to my top side nal structure
93 * ni: my network interface index
95 * cleanup nal state, reclaim the lower side thread and
96 * its state using PTL_FINI codepoint
98 static void procbridge_shutdown(nal_t *n)
100 bridge b=(bridge)n->nal_data;
101 procbridge p=(procbridge)b->local;
103 p->nal_flags |= NAL_FLAG_STOPPING;
104 procbridge_wakeup_nal(p);
107 pthread_mutex_lock(&p->mutex);
108 if (p->nal_flags & NAL_FLAG_STOPPED) {
109 pthread_mutex_unlock(&p->mutex);
112 pthread_cond_wait(&p->cond, &p->mutex);
113 pthread_mutex_unlock(&p->mutex);
120 static void procbridge_lock(nal_t * n, unsigned long *flags)
122 bridge b=(bridge)n->nal_data;
123 procbridge p=(procbridge)b->local;
125 pthread_mutex_lock(&p->mutex);
128 static void procbridge_unlock(nal_t * n, unsigned long *flags)
130 bridge b=(bridge)n->nal_data;
131 procbridge p=(procbridge)b->local;
133 pthread_mutex_unlock(&p->mutex);
139 * this function was originally intended to allow the
140 * lower half thread to be scheduled to allow progress. we
141 * overload it to explicitly block until signalled by the
144 static int procbridge_yield(nal_t *n, unsigned long *flags, int milliseconds)
146 bridge b=(bridge)n->nal_data;
147 procbridge p=(procbridge)b->local;
149 if (milliseconds == 0)
152 if (milliseconds < 0) {
153 pthread_cond_wait(&p->cond,&p->mutex);
157 struct timespec timeout;
159 gettimeofday(&then, NULL);
160 timeout.tv_sec = then.tv_sec + milliseconds/1000;
161 timeout.tv_nsec = then.tv_usec * 1000 + milliseconds % 1000 * 1000000;
162 if (timeout.tv_nsec >= 1000000000) {
164 timeout.tv_nsec -= 1000000000;
167 pthread_cond_timedwait(&p->cond, &p->mutex, &timeout);
169 gettimeofday(&now, NULL);
170 milliseconds -= (now.tv_sec - then.tv_sec) * 1000 +
171 (now.tv_usec - then.tv_usec) / 1000;
173 if (milliseconds < 0)
177 return (milliseconds);
181 extern int procbridge_startup (nal_t *, ptl_pid_t,
182 ptl_ni_limits_t *, ptl_ni_limits_t *);
185 * the interface vector to allow the generic code to access
186 * this nal. this is seperate from the library side nal_cb.
187 * TODO: should be dyanmically allocated
189 nal_t procapi_nal = {
191 startup: procbridge_startup,
192 shutdown: procbridge_shutdown,
193 forward: procbridge_forward,
194 yield: procbridge_yield,
195 lock: procbridge_lock,
196 unlock: procbridge_unlock
199 ptl_nid_t tcpnal_mynid;
201 /* Function: procbridge_startup
203 * Arguments: pid: requested process id (port offset)
204 * PTL_ID_ANY not supported.
205 * desired: limits passed from the application
206 * and effectively ignored
207 * actual: limits actually allocated and returned
209 * Returns: portals rc
211 * initializes the tcp nal. we define unix_failure as an
212 * error wrapper to cut down clutter.
214 int procbridge_startup (nal_t *nal, ptl_pid_t requested_pid,
215 ptl_ni_limits_t *requested_limits,
216 ptl_ni_limits_t *actual_limits)
218 nal_init_args_t args;
222 /* XXX nal_type is purely private to tcpnal here */
223 int nal_type = PTL_IFACE_TCP;/* PTL_IFACE_DEFAULT FIXME hack */
225 LASSERT(nal == &procapi_nal);
229 b=(bridge)malloc(sizeof(struct bridge));
230 p=(procbridge)malloc(sizeof(struct procbridge));
234 args.nia_requested_pid = requested_pid;
235 args.nia_requested_limits = requested_limits;
236 args.nia_actual_limits = actual_limits;
237 args.nia_nal_type = nal_type;
240 /* init procbridge */
241 pthread_mutex_init(&p->mutex,0);
242 pthread_cond_init(&p->cond, 0);
245 /* initialize notifier */
246 if (socketpair(AF_UNIX, SOCK_STREAM, 0, p->notifier)) {
247 perror("socketpair failed");
251 if (!register_io_handler(p->notifier[1], READ_HANDLER,
252 procbridge_notifier_handler, p)) {
253 perror("fail to register notifier handler");
257 /* create nal thread */
258 if (pthread_create(&p->t, NULL, nal_thread, &args)) {
259 perror("nal_init: pthread_create");
264 pthread_mutex_lock(&p->mutex);
265 if (p->nal_flags & (NAL_FLAG_RUNNING | NAL_FLAG_STOPPED)) {
266 pthread_mutex_unlock(&p->mutex);
269 pthread_cond_wait(&p->cond, &p->mutex);
270 pthread_mutex_unlock(&p->mutex);
273 if (p->nal_flags & NAL_FLAG_STOPPED)
276 b->nal_cb->ni.nid = tcpnal_mynid;