Whamcloud - gitweb
- added connection structure which encompasses some (but perhaps not enough yet)
[fs/lustre-release.git] / lustre / ptlrpc / niobuf.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 #define EXPORT_SYMTAB
24
25 #define DEBUG_SUBSYSTEM S_RPC
26
27 #include <linux/lustre_net.h>
28
29 extern ptl_handle_eq_t request_out_eq, 
30         reply_in_eq, 
31         reply_out_eq,
32         bulk_source_eq, 
33         bulk_sink_eq;
34 static ptl_process_id_t local_id = {PTL_ID_ANY, PTL_ID_ANY};
35
36
37 int ptlrpc_check_bulk_sent(struct ptlrpc_bulk_desc *bulk)
38 {
39         ENTRY;
40
41         if (bulk->b_flags == PTL_BULK_SENT)
42                 RETURN(1);
43
44         if (sigismember(&(current->pending.signal), SIGKILL) ||
45             sigismember(&(current->pending.signal), SIGINT)) {
46                 bulk->b_flags = PTL_RPC_INTR;
47                 RETURN(1);
48         }
49
50         CDEBUG(D_NET, "no event yet\n");
51         RETURN(0);
52 }
53
54 int ptl_send_buf(struct ptlrpc_request *request, struct ptlrpc_connection *conn,
55                  int portal)
56 {
57         int rc;
58         ptl_process_id_t remote_id;
59         ptl_handle_md_t md_h;
60         ptl_ack_req_t ack;
61
62         request->rq_req_md.user_ptr = request;
63
64         switch (request->rq_type) {
65         case PTL_RPC_BULK:
66                 request->rq_req_md.start = request->rq_bulkbuf;
67                 request->rq_req_md.length = request->rq_bulklen;
68                 request->rq_req_md.eventq = bulk_source_eq;
69                 request->rq_req_md.threshold = 2; /* SENT and ACK events */
70                 ack = PTL_ACK_REQ;
71                 break;
72         case PTL_RPC_REQUEST:
73                 request->rq_req_md.start = request->rq_reqmsg;
74                 request->rq_req_md.length = request->rq_reqlen;
75                 request->rq_req_md.eventq = request_out_eq;
76                 request->rq_req_md.threshold = 1;
77                 ack = PTL_NOACK_REQ;
78                 break;
79         case PTL_RPC_REPLY:
80                 request->rq_req_md.start = request->rq_repmsg;
81                 request->rq_req_md.length = request->rq_replen;
82                 request->rq_req_md.eventq = reply_out_eq;
83                 request->rq_req_md.threshold = 1;
84                 ack = PTL_NOACK_REQ;
85                 break;
86         default:
87                 LBUG();
88                 return -1; /* notreached */
89         }
90         request->rq_req_md.options = PTL_MD_OP_PUT;
91         request->rq_req_md.user_ptr = request;
92
93         rc = PtlMDBind(conn->c_peer.peer_ni, request->rq_req_md, &md_h);
94         //CERROR("MDBind (outgoing req/rep/bulk): %Lu\n", (__u64)md_h);
95         if (rc != 0) {
96                 CERROR("PtlMDBind failed: %d\n", rc);
97                 LBUG();
98                 return rc;
99         }
100
101         remote_id.nid = conn->c_peer.peer_nid;
102         remote_id.pid = 0;
103
104         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
105                request->rq_req_md.length, portal, request->rq_reqmsg->xid);
106
107         rc = PtlPut(md_h, ack, remote_id, portal, 0, request->rq_reqmsg->xid,
108                     0, 0);
109         if (rc != PTL_OK) {
110                 CERROR("PtlPut(%d, %d, %d) failed: %d\n", remote_id.nid,
111                        portal, request->rq_reqmsg->xid, rc);
112                 PtlMDUnlink(md_h);
113         }
114
115         return rc;
116 }
117
118 int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *bulk, int portal)
119 {
120         int rc;
121         ptl_process_id_t remote_id;
122
123         bulk->b_md.start = bulk->b_buf;
124         bulk->b_md.length = bulk->b_buflen;
125         bulk->b_md.eventq = bulk_source_eq;
126         bulk->b_md.threshold = 2; /* SENT and ACK events */
127         bulk->b_md.options = PTL_MD_OP_PUT;
128         bulk->b_md.user_ptr = bulk;
129
130         rc = PtlMDBind(bulk->b_connection->c_peer.peer_ni, bulk->b_md,
131                        &bulk->b_md_h);
132         if (rc != 0) {
133                 CERROR("PtlMDBind failed: %d\n", rc);
134                 LBUG();
135                 return rc;
136         }
137
138         remote_id.nid = bulk->b_connection->c_peer.peer_nid;
139         remote_id.pid = 0;
140
141         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
142                bulk->b_md.length, portal, bulk->b_xid);
143
144         rc = PtlPut(bulk->b_md_h, PTL_ACK_REQ, remote_id, portal, 0,
145                     bulk->b_xid, 0, 0);
146         if (rc != PTL_OK) {
147                 CERROR("PtlPut(%d, %d, %d) failed: %d\n", remote_id.nid,
148                        portal, bulk->b_xid, rc);
149                 PtlMDUnlink(bulk->b_md_h);
150                 LBUG();
151         }
152
153         return rc;
154 }
155
156 int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *bulk)
157 {
158         int rc;
159         ENTRY;
160
161         rc = PtlMEAttach(bulk->b_connection->c_peer.peer_ni, bulk->b_portal,
162                          local_id, bulk->b_xid, 0, PTL_UNLINK, PTL_INS_AFTER,
163                          &bulk->b_me_h);
164         if (rc != PTL_OK) {
165                 CERROR("PtlMEAttach failed: %d\n", rc);
166                 LBUG();
167                 GOTO(cleanup, rc);
168         }
169
170         bulk->b_md.start = bulk->b_buf;
171         bulk->b_md.length = bulk->b_buflen;
172         bulk->b_md.threshold = 1;
173         bulk->b_md.options = PTL_MD_OP_PUT;
174         bulk->b_md.user_ptr = bulk;
175         bulk->b_md.eventq = bulk_sink_eq;
176
177         rc = PtlMDAttach(bulk->b_me_h, bulk->b_md, PTL_UNLINK, &bulk->b_md_h);
178         //CERROR("MDAttach (bulk sink): %Lu\n", (__u64)bulk->b_md_h);
179         if (rc != PTL_OK) {
180                 CERROR("PtlMDAttach failed: %d\n", rc);
181                 LBUG();
182                 GOTO(cleanup, rc);
183         }
184
185         CDEBUG(D_NET, "Setup bulk sink buffer: %u bytes, xid %u, portal %u\n",
186                bulk->b_buflen, bulk->b_xid, bulk->b_portal);
187         RETURN(0);
188
189         // XXX Confirm that this is safe!
190  cleanup:
191         PtlMEUnlink(bulk->b_me_h);
192         return rc;
193 }
194
195 int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *bulk)
196 {
197         int rc;
198
199         rc = PtlMEUnlink(bulk->b_me_h);
200         if (rc != PTL_OK)
201                 CERROR("PtlMEUnlink failed: %d\n", rc);
202
203         return rc;
204 }
205
206 int ptlrpc_reply(struct ptlrpc_service *svc, struct ptlrpc_request *req)
207 {
208         /* FIXME: we need to increment the count of handled events */
209         req->rq_type = PTL_RPC_REPLY;
210         req->rq_repmsg->conn = req->rq_reqmsg->conn;
211         req->rq_repmsg->token = req->rq_reqmsg->token;
212         req->rq_repmsg->xid = HTON__u32(req->rq_reqmsg->xid);
213         req->rq_repmsg->status = HTON__u32(req->rq_status);
214         req->rq_reqmsg->type = HTON__u32(req->rq_type);
215         return ptl_send_buf(req, req->rq_connection, svc->srv_rep_portal);
216 }
217
218 int ptlrpc_error(struct ptlrpc_service *svc, struct ptlrpc_request *req)
219 {
220         int rc;
221         ENTRY;
222
223         if (req->rq_repmsg) {
224                 CERROR("req already has repmsg\n");
225                 LBUG();
226         }
227
228         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
229         if (rc)
230                 RETURN(rc);
231
232         req->rq_repmsg->type = HTON__u32(PTL_RPC_ERR);
233
234         rc = ptlrpc_reply(svc, req);
235         RETURN(rc);
236 }
237
238 int ptl_send_rpc(struct ptlrpc_request *request)
239 {
240         ptl_process_id_t local_id;
241         int rc;
242         char *repbuf;
243
244         ENTRY;
245
246         if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_REQUEST) {
247                 CERROR("wrong packet type sent %d\n",
248                        NTOH__u32(request->rq_reqmsg->type));
249                 LBUG();
250                 RETURN(-EINVAL);
251         }
252         if (request->rq_replen == 0) {
253                 CERROR("request->rq_replen is 0!\n");
254                 RETURN(-EINVAL);
255         }
256
257         /* request->rq_repmsg is set only when the reply comes in, in
258          * client_packet_callback() */
259         OBD_ALLOC(repbuf, request->rq_replen);
260         if (!repbuf)
261                 RETURN(-ENOMEM);
262
263         local_id.nid = PTL_ID_ANY;
264         local_id.pid = PTL_ID_ANY;
265
266         down(&request->rq_client->cli_rpc_sem);
267
268         rc = PtlMEAttach(request->rq_connection->c_peer.peer_ni,
269                          request->rq_client->cli_reply_portal,
270                          local_id, request->rq_reqmsg->xid, 0, PTL_UNLINK,
271                          PTL_INS_AFTER, &request->rq_reply_me_h);
272         if (rc != PTL_OK) {
273                 CERROR("PtlMEAttach failed: %d\n", rc);
274                 LBUG();
275                 GOTO(cleanup, rc);
276         }
277
278         request->rq_type = PTL_RPC_REQUEST;
279         request->rq_reply_md.start = repbuf;
280         request->rq_reply_md.length = request->rq_replen;
281         request->rq_reply_md.threshold = 1;
282         request->rq_reply_md.options = PTL_MD_OP_PUT;
283         request->rq_reply_md.user_ptr = request;
284         request->rq_reply_md.eventq = reply_in_eq;
285
286         rc = PtlMDAttach(request->rq_reply_me_h, request->rq_reply_md,
287                          PTL_UNLINK, &request->rq_reply_md_h);
288         if (rc != PTL_OK) {
289                 CERROR("PtlMDAttach failed: %d\n", rc);
290                 LBUG();
291                 GOTO(cleanup2, rc);
292         }
293
294         CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid %u, portal %u\n",
295                request->rq_replen, request->rq_reqmsg->xid,
296                request->rq_client->cli_request_portal);
297
298         list_add(&request->rq_list, &request->rq_client->cli_sending_head);
299         rc = ptl_send_buf(request, request->rq_connection,
300                           request->rq_client->cli_request_portal);
301         RETURN(rc);
302
303  cleanup2:
304         PtlMEUnlink(request->rq_reply_me_h);
305  cleanup:
306         OBD_FREE(repbuf, request->rq_replen);
307         up(&request->rq_client->cli_rpc_sem);
308
309         return rc;
310 }
311
312 void ptlrpc_link_svc_me(struct ptlrpc_service *service, int i)
313 {
314         int rc;
315         ptl_md_t dummy;
316         ptl_handle_md_t md_h;
317
318         /* Attach the leading ME on which we build the ring */
319         rc = PtlMEAttach(service->srv_self.peer_ni, service->srv_req_portal,
320                          service->srv_id, 0, ~0, PTL_RETAIN, PTL_INS_BEFORE,
321                          &(service->srv_me_h[i]));
322         if (rc != PTL_OK) {
323                 CERROR("PtlMEAttach failed: %d\n", rc);
324                 LBUG();
325         }
326         
327         if (service->srv_ref_count[i])
328                 LBUG();
329
330         dummy.start         = service->srv_buf[i];
331         dummy.length        = service->srv_buf_size;
332         dummy.max_offset    = service->srv_buf_size;
333         dummy.threshold     = PTL_MD_THRESH_INF;
334         dummy.options       = PTL_MD_OP_PUT | PTL_MD_AUTO_UNLINK;
335         dummy.user_ptr      = service;
336         dummy.eventq        = service->srv_eq_h;
337         dummy.max_offset    = service->srv_buf_size;
338         
339         rc = PtlMDAttach(service->srv_me_h[i], dummy, PTL_UNLINK, &md_h);
340         if (rc != PTL_OK) {
341                 /* cleanup */
342                 CERROR("PtlMDAttach failed: %d\n", rc);
343                 LBUG();
344         }
345 }        
346
347 /* ptl_handled_rpc() should be called by the sleeping process once
348  * it finishes processing an event.  This ensures the ref count is
349  * decremented and that the rpc ring buffer cycles properly.
350  */ 
351 int ptl_handled_rpc(struct ptlrpc_service *service, void *start) 
352 {
353         int index;
354
355         spin_lock(&service->srv_lock);
356
357         for (index = 0; index < service->srv_ring_length; index++)
358                 if (service->srv_buf[index] == start) 
359                         break;
360
361         if (index == service->srv_ring_length)
362                 LBUG();
363
364         CDEBUG(D_INFO, "MD index=%d Ref Count=%d\n", index,
365                service->srv_ref_count[index]);
366         service->srv_ref_count[index]--;
367
368         if (service->srv_ref_count[index] < 0)
369                 LBUG();
370
371         if (service->srv_ref_count[index] == 0 &&
372             !ptl_is_valid_handle(&(service->srv_me_h[index]))) {
373                 CDEBUG(D_NET, "relinking %d\n", index); 
374                 ptlrpc_link_svc_me(service, index); 
375         }
376         
377         spin_unlock(&service->srv_lock);
378         return 0;
379 }