1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_RPC
26 #include <linux/lustre_net.h>
28 extern int request_in_callback(ptl_event_t *ev, void *data);
29 extern int ptl_handled_rpc(struct ptlrpc_service *service, void *start);
31 static int ptlrpc_check_event(struct ptlrpc_service *svc)
36 spin_lock(&svc->srv_lock);
37 if (sigismember(&(current->pending.signal), SIGKILL) ||
38 sigismember(&(current->pending.signal), SIGTERM) ||
39 sigismember(&(current->pending.signal), SIGINT)) {
40 svc->srv_flags |= SVC_KILLED;
44 if (svc->srv_flags & SVC_STOPPING)
47 if (ptl_is_valid_handle(&svc->srv_eq_h)) {
49 err = PtlEQGet(svc->srv_eq_h, &svc->srv_ev);
52 svc->srv_flags |= SVC_EVENT;
56 if (err != PTL_EQ_EMPTY) {
57 CDEBUG(D_NET, "BUG: PtlEQGet returned %d\n", rc);
66 spin_unlock(&svc->srv_lock);
70 struct ptlrpc_service *
71 ptlrpc_init_svc(__u32 bufsize, int req_portal, int rep_portal, char *uuid,
72 svc_handler_t handler)
76 struct ptlrpc_service *service;
79 OBD_ALLOC(service, sizeof(*service));
85 spin_lock_init(&service->srv_lock);
86 INIT_LIST_HEAD(&service->srv_reqs);
87 init_waitqueue_head(&service->srv_ctl_waitq);
88 init_waitqueue_head(&service->srv_waitq);
90 service->srv_thread = NULL;
91 service->srv_flags = 0;
93 service->srv_buf_size = bufsize;
94 service->srv_rep_portal = rep_portal;
95 service->srv_req_portal = req_portal;
96 service->srv_handler = handler;
98 err = kportal_uuid_to_peer(uuid, &service->srv_self);
100 CERROR("cannot get peer for uuid %s", uuid);
101 GOTO(err_free, NULL);
104 service->srv_ring_length = RPC_RING_LENGTH;
105 service->srv_id.nid = PTL_ID_ANY;
106 service->srv_id.pid = PTL_ID_ANY;
108 rc = PtlEQAlloc(service->srv_self.peer_ni, 128, request_in_callback,
109 service, &(service->srv_eq_h));
112 CERROR("PtlEQAlloc failed: %d\n", rc);
114 GOTO(err_free, NULL);
117 for (i = 0; i < service->srv_ring_length; i++) {
118 OBD_ALLOC(service->srv_buf[i], service->srv_buf_size);
119 if (service->srv_buf[i] == NULL) {
120 CERROR("no memory\n");
122 GOTO(err_ring, NULL);
124 service->srv_ref_count[i] = 0;
125 ptlrpc_link_svc_me(service, i);
128 CDEBUG(D_NET, "Starting service listening on portal %d\n",
129 service->srv_req_portal);
133 service->srv_ring_length = i;
134 rpc_unregister_service(service); // XXX verify this is right
135 PtlEQFree(service->srv_eq_h);
137 OBD_FREE(service, sizeof(*service));
141 static int handle_incoming_request(struct obd_device *obddev,
142 struct ptlrpc_service *svc)
144 struct ptlrpc_request request;
145 struct lustre_peer peer;
149 /* FIXME: If we move to an event-driven model, we should put the request
150 * on the stack of mds_handle instead. */
151 start = svc->srv_ev.mem_desc.start;
152 memset(&request, 0, sizeof(request));
153 request.rq_obd = obddev;
154 request.rq_reqmsg = (svc->srv_ev.mem_desc.start +
156 request.rq_reqlen = svc->srv_ev.mem_desc.length;
158 if (request.rq_reqmsg->xid != svc->srv_ev.match_bits)
161 CDEBUG(D_NET, "got req %d\n", request.rq_reqmsg->xid);
163 peer.peer_nid = svc->srv_ev.initiator.nid;
164 /* FIXME: this NI should be the incoming NI.
165 * We don't know how to find that from here. */
166 peer.peer_ni = svc->srv_self.peer_ni;
168 if (request.rq_reqmsg->conn) {
169 request.rq_connection =
170 (void *)(unsigned long)request.rq_reqmsg->conn;
171 if (request.rq_reqmsg->token !=
172 request.rq_connection->c_token) {
173 struct ptlrpc_connection *tmp;
174 tmp = ptlrpc_get_connection(&peer);
175 CERROR("rq_reqmsg->conn: %p\n", request.rq_connection);
176 CERROR("real connection: %p\n", tmp);
177 CERROR("rq_reqmsg->token: %Lu\n",
178 (unsigned long long)request.rq_reqmsg->token);
179 CERROR("real token : %Lu\n",
180 (unsigned long long)tmp->c_token);
183 ptlrpc_connection_addref(request.rq_connection);
186 PHIL? should we perhaps only do this when
187 we get an incoming connmgr_connect request?
189 request.rq_connection = ptlrpc_get_connection(&peer);
190 if (!request.rq_connection)
194 svc->srv_flags &= ~SVC_EVENT;
196 spin_unlock(&svc->srv_lock);
197 rc = svc->srv_handler(obddev, svc, &request);
198 ptlrpc_put_connection(request.rq_connection);
199 ptl_handled_rpc(svc, start);
203 static int ptlrpc_main(void *arg)
206 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
207 struct obd_device *obddev = data->dev;
208 struct ptlrpc_service *svc = data->svc;
214 spin_lock_irq(¤t->sigmask_lock);
215 sigfillset(¤t->blocked);
216 recalc_sigpending(current);
217 spin_unlock_irq(¤t->sigmask_lock);
219 sprintf(current->comm, data->name);
221 /* Record that the thread is running */
222 svc->srv_thread = current;
223 svc->srv_flags = SVC_RUNNING;
224 wake_up(&svc->srv_ctl_waitq);
226 /* XXX maintain a list of all managed devices: insert here */
228 /* And now, loop forever on requests */
230 wait_event(svc->srv_waitq, ptlrpc_check_event(svc));
232 spin_lock(&svc->srv_lock);
233 if (svc->srv_flags & SVC_SIGNAL) {
234 svc->srv_flags &= ~SVC_SIGNAL;
235 spin_unlock(&svc->srv_lock);
240 if (svc->srv_flags & SVC_STOPPING) {
241 svc->srv_flags &= ~SVC_STOPPING;
242 spin_unlock(&svc->srv_lock);
247 if (svc->srv_flags & SVC_EVENT) {
248 svc->srv_flags &= ~SVC_EVENT;
249 rc = handle_incoming_request(obddev, svc);
253 CERROR("unknown break in service");
254 spin_unlock(&svc->srv_lock);
259 svc->srv_thread = NULL;
260 svc->srv_flags = SVC_STOPPED;
261 wake_up(&svc->srv_ctl_waitq);
262 CDEBUG(D_NET, "svc exiting process %d\n", current->pid);
266 void ptlrpc_stop_thread(struct ptlrpc_service *svc)
268 svc->srv_flags = SVC_STOPPING;
270 wake_up(&svc->srv_waitq);
271 wait_event_interruptible(svc->srv_ctl_waitq,
272 (svc->srv_flags & SVC_STOPPED));
275 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
278 struct ptlrpc_svc_data d;
286 init_waitqueue_head(&svc->srv_waitq);
288 init_waitqueue_head(&svc->srv_ctl_waitq);
289 rc = kernel_thread(ptlrpc_main, (void *) &d,
290 CLONE_VM | CLONE_FS | CLONE_FILES);
292 CERROR("cannot start thread\n");
295 wait_event(svc->srv_ctl_waitq, svc->srv_flags & SVC_RUNNING);
300 int rpc_unregister_service(struct ptlrpc_service *service)
304 for (i = 0; i < service->srv_ring_length; i++) {
305 if (ptl_is_valid_handle(&(service->srv_me_h[i]))) {
306 rc = PtlMEUnlink(service->srv_me_h[i]);
308 CERROR("PtlMEUnlink failed: %d\n", rc);
309 ptl_set_inv_handle(&(service->srv_me_h[i]));
312 if (service->srv_buf[i] != NULL)
313 OBD_FREE(service->srv_buf[i], service->srv_buf_size);
314 service->srv_buf[i] = NULL;
317 rc = PtlEQFree(service->srv_eq_h);
319 CERROR("PtlEQFree failed: %d\n", rc);