Whamcloud - gitweb
- remove the PTL_RPC_TYPE_* in favour of PTL_RPC_MSG_*
[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 DEBUG_SUBSYSTEM S_RPC
24
25 #include <linux/obd_support.h>
26 #include <linux/lustre_net.h>
27 #include <linux/lustre_lib.h>
28
29 extern ptl_handle_eq_t request_out_eq, reply_in_eq, reply_out_eq,
30         bulk_source_eq, bulk_sink_eq;
31 static ptl_process_id_t local_id = {PTL_NID_ANY, PTL_PID_ANY};
32
33 static int ptl_send_buf(struct ptlrpc_request *request,
34                         struct ptlrpc_connection *conn, int portal)
35 {
36         int rc;
37         ptl_process_id_t remote_id;
38         ptl_handle_md_t md_h;
39
40         request->rq_req_md.user_ptr = request;
41
42         switch (request->rq_type) {
43         case PTL_RPC_MSG_REQUEST:
44                 request->rq_reqmsg->type = HTON__u32(request->rq_type);
45                 request->rq_req_md.start = request->rq_reqmsg;
46                 request->rq_req_md.length = request->rq_reqlen;
47                 request->rq_req_md.eventq = request_out_eq;
48                 break;
49         case PTL_RPC_MSG_REPLY:
50                 request->rq_repmsg->type = HTON__u32(request->rq_type);
51                 request->rq_req_md.start = request->rq_repmsg;
52                 request->rq_req_md.length = request->rq_replen;
53                 request->rq_req_md.eventq = reply_out_eq;
54                 break;
55         default:
56                 LBUG();
57                 return -1; /* notreached */
58         }
59         request->rq_req_md.threshold = 1;
60         request->rq_req_md.options = PTL_MD_OP_PUT;
61         request->rq_req_md.user_ptr = request;
62
63         rc = PtlMDBind(conn->c_peer.peer_ni, request->rq_req_md, &md_h);
64         if (rc != 0) {
65                 CERROR("PtlMDBind failed: %d\n", rc);
66                 LBUG();
67                 return rc;
68         }
69
70         remote_id.nid = conn->c_peer.peer_nid;
71         remote_id.pid = 0;
72
73         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %Ld\n",
74                request->rq_req_md.length, portal, request->rq_xid);
75
76         rc = PtlPut(md_h, PTL_NOACK_REQ, remote_id, portal, 0, request->rq_xid,
77                     0, 0);
78         if (rc != PTL_OK) {
79                 CERROR("PtlPut(%Lu, %d, %Ld) failed: %d\n", remote_id.nid,
80                        portal, request->rq_xid, rc);
81                 PtlMDUnlink(md_h);
82         }
83
84         return rc;
85 }
86
87 static inline struct iovec *
88 ptlrpc_get_bulk_iov (struct ptlrpc_bulk_desc *desc)
89 {
90         struct iovec *iov;
91
92         if (desc->bd_page_count <= sizeof (desc->bd_iov)/sizeof (struct iovec))
93                 return (desc->bd_iov);
94
95         OBD_ALLOC (iov, desc->bd_page_count * sizeof (struct iovec));
96         if (iov == NULL)
97                 LBUG();
98
99         return (iov);
100 }
101
102 static inline void
103 ptlrpc_put_bulk_iov (struct ptlrpc_bulk_desc *desc, struct iovec *iov)
104 {
105         if (desc->bd_page_count <= sizeof (desc->bd_iov)/sizeof (struct iovec))
106                 return;
107
108         OBD_FREE (iov, desc->bd_page_count * sizeof (struct iovec));
109 }
110
111 int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *desc)
112 {
113         int rc;
114         struct list_head *tmp, *next;
115         ptl_process_id_t remote_id;
116         __u32 xid = 0;
117         struct iovec *iov;
118         ENTRY;
119
120         iov = ptlrpc_get_bulk_iov (desc);
121         if (iov == NULL)
122                 RETURN (-ENOMEM);
123
124         desc->bd_md.start = iov;
125         desc->bd_md.niov = 0;
126         desc->bd_md.length = 0;
127         desc->bd_md.eventq = bulk_source_eq;
128         desc->bd_md.threshold = 2; /* SENT and ACK */
129         desc->bd_md.options = PTL_MD_OP_PUT | PTL_MD_IOV;
130         desc->bd_md.user_ptr = desc;
131
132         list_for_each_safe(tmp, next, &desc->bd_page_list) {
133                 struct ptlrpc_bulk_page *bulk;
134                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
135
136                 LASSERT (desc->bd_md.niov < desc->bd_page_count);
137
138                 if (desc->bd_md.niov == 0)
139                         xid = bulk->bp_xid;
140                 LASSERT (xid == bulk->bp_xid);   /* should all be the same */
141
142                 iov[desc->bd_md.niov].iov_base = bulk->bp_buf;
143                 iov[desc->bd_md.niov].iov_len = bulk->bp_buflen;
144                 desc->bd_md.niov++;
145                 desc->bd_md.length += bulk->bp_buflen;
146         }
147
148         LASSERT (desc->bd_md.niov == desc->bd_page_count);
149         LASSERT (desc->bd_md.niov != 0);
150
151         rc = PtlMDBind(desc->bd_connection->c_peer.peer_ni, desc->bd_md,
152                        &desc->bd_md_h);
153
154         ptlrpc_put_bulk_iov (desc, iov);        /* move down to reduce latency to send */
155
156         if (rc != PTL_OK) {
157                 CERROR("PtlMDBind failed: %d\n", rc);
158                 LBUG();
159                 RETURN(rc);
160         }
161
162         remote_id.nid = desc->bd_connection->c_peer.peer_nid;
163         remote_id.pid = 0;
164
165         CDEBUG(D_NET, "Sending %u pages %u bytes to portal %d nid %Lx pid %d xid %d\n",
166                desc->bd_md.niov, desc->bd_md.length,
167                desc->bd_portal, remote_id.nid, remote_id.pid, xid);
168
169         rc = PtlPut(desc->bd_md_h, PTL_ACK_REQ, remote_id,
170                     desc->bd_portal, 0, xid, 0, 0);
171         if (rc != PTL_OK) {
172                 CERROR("PtlPut(%Lu, %d, %d) failed: %d\n",
173                        remote_id.nid, desc->bd_portal, xid, rc);
174                 PtlMDUnlink(desc->bd_md_h);
175                 LBUG();
176                 RETURN(rc);
177         }
178
179         RETURN(0);
180 }
181
182 int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *desc)
183 {
184         struct list_head *tmp, *next;
185         int rc;
186         __u32 xid = 0;
187         struct iovec *iov;
188         ENTRY;
189
190         iov = ptlrpc_get_bulk_iov (desc);
191         if (iov == NULL)
192                 return (-ENOMEM);
193
194         desc->bd_md.start = iov;
195         desc->bd_md.niov = 0;
196         desc->bd_md.length = 0;
197         desc->bd_md.threshold = 1;
198         desc->bd_md.options = PTL_MD_OP_PUT | PTL_MD_IOV;
199         desc->bd_md.user_ptr = desc;
200         desc->bd_md.eventq = bulk_sink_eq;
201
202         list_for_each_safe(tmp, next, &desc->bd_page_list) {
203                 struct ptlrpc_bulk_page *bulk;
204                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
205
206                 LASSERT (desc->bd_md.niov < desc->bd_page_count);
207
208                 if (desc->bd_md.niov == 0)
209                         xid = bulk->bp_xid;
210                 LASSERT (xid == bulk->bp_xid);   /* should all be the same */
211
212                 iov[desc->bd_md.niov].iov_base = bulk->bp_buf;
213                 iov[desc->bd_md.niov].iov_len = bulk->bp_buflen;
214                 desc->bd_md.niov++;
215                 desc->bd_md.length += bulk->bp_buflen;
216         }
217
218         LASSERT (desc->bd_md.niov == desc->bd_page_count);
219         LASSERT (desc->bd_md.niov != 0);
220
221         rc = PtlMEAttach(desc->bd_connection->c_peer.peer_ni,
222                          desc->bd_portal, local_id, xid, 0,
223                          PTL_UNLINK, PTL_INS_AFTER, &desc->bd_me_h);
224
225         ptlrpc_put_bulk_iov (desc, iov);
226
227         if (rc != PTL_OK) {
228                 CERROR("PtlMEAttach failed: %d\n", rc);
229                 LBUG();
230                 GOTO(cleanup, rc);
231         }
232
233         rc = PtlMDAttach(desc->bd_me_h, desc->bd_md, PTL_UNLINK,
234                          &desc->bd_md_h);
235         if (rc != PTL_OK) {
236                 CERROR("PtlMDAttach failed: %d\n", rc);
237                 LBUG();
238                 GOTO(cleanup, rc);
239         }
240
241         CDEBUG(D_NET, "Setup bulk sink buffers: %u pages %u bytes, xid %u, "
242                "portal %u\n", desc->bd_md.niov, desc->bd_md.length,
243                xid, desc->bd_portal);
244
245         RETURN(0);
246
247  cleanup:
248         ptlrpc_abort_bulk(desc);
249
250         return rc;
251 }
252
253 int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc)
254 {
255         /* This should be safe: these handles are initialized to be
256          * invalid in ptlrpc_prep_bulk() */
257         PtlMDUnlink(desc->bd_md_h);
258         PtlMEUnlink(desc->bd_me_h);
259
260         return 0;
261 }
262
263 int ptlrpc_reply(struct ptlrpc_service *svc, struct ptlrpc_request *req)
264 {
265         if (req->rq_repmsg == NULL) {
266                 CERROR("bad: someone called ptlrpc_reply when they meant "
267                        "ptlrpc_error\n");
268                 return -EINVAL;
269         }
270
271         /* FIXME: we need to increment the count of handled events */
272         if (req->rq_type != PTL_RPC_MSG_ERR)
273                 req->rq_type = PTL_RPC_MSG_REPLY;
274         //req->rq_repmsg->conn = req->rq_connection->c_remote_conn;
275         //req->rq_repmsg->token = req->rq_connection->c_remote_token;
276         req->rq_repmsg->status = HTON__u32(req->rq_status);
277         return ptl_send_buf(req, req->rq_connection, svc->srv_rep_portal);
278 }
279
280 int ptlrpc_error(struct ptlrpc_service *svc, struct ptlrpc_request *req)
281 {
282         int rc;
283         ENTRY;
284
285         if (req->rq_repmsg) {
286                 CERROR("req already has repmsg\n");
287                 LBUG();
288         }
289
290         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
291         if (rc)
292                 RETURN(rc);
293
294         req->rq_repmsg->type = HTON__u32(PTL_RPC_MSG_ERR);
295
296         rc = ptlrpc_reply(svc, req);
297         RETURN(rc);
298 }
299
300 int ptl_send_rpc(struct ptlrpc_request *request)
301 {
302         int rc;
303         char *repbuf;
304
305         ENTRY;
306
307         if (request->rq_type != PTL_RPC_MSG_REQUEST) {
308                 CERROR("wrong packet type sent %d\n",
309                        NTOH__u32(request->rq_reqmsg->type));
310                 LBUG();
311                 RETURN(EINVAL);
312         }
313         if (request->rq_replen == 0) {
314                 CERROR("request->rq_replen is 0!\n");
315                 RETURN(EINVAL);
316         }
317
318         /* request->rq_repmsg is set only when the reply comes in, in
319          * client_packet_callback() */
320         if (request->rq_reply_md.start)
321                 OBD_FREE(request->rq_reply_md.start, request->rq_replen);
322
323         OBD_ALLOC(repbuf, request->rq_replen);
324         if (!repbuf) {
325                 LBUG();
326                 RETURN(ENOMEM);
327         }
328
329         // down(&request->rq_client->cli_rpc_sem);
330
331         rc = PtlMEAttach(request->rq_connection->c_peer.peer_ni,
332                          request->rq_import->imp_client->cli_reply_portal,
333                          local_id, request->rq_xid, 0, PTL_UNLINK,
334                          PTL_INS_AFTER, &request->rq_reply_me_h);
335         if (rc != PTL_OK) {
336                 CERROR("PtlMEAttach failed: %d\n", rc);
337                 LBUG();
338                 GOTO(cleanup, rc);
339         }
340
341         request->rq_reply_md.start = repbuf;
342         request->rq_reply_md.length = request->rq_replen;
343         request->rq_reply_md.threshold = 1;
344         request->rq_reply_md.options = PTL_MD_OP_PUT;
345         request->rq_reply_md.user_ptr = request;
346         request->rq_reply_md.eventq = reply_in_eq;
347
348         rc = PtlMDAttach(request->rq_reply_me_h, request->rq_reply_md,
349                          PTL_UNLINK, &request->rq_reply_md_h);
350         if (rc != PTL_OK) {
351                 CERROR("PtlMDAttach failed: %d\n", rc);
352                 LBUG();
353                 GOTO(cleanup2, rc);
354         }
355
356         CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid %Lu, portal %u\n",
357                request->rq_replen, request->rq_xid,
358                request->rq_import->imp_client->cli_reply_portal);
359
360         rc = ptl_send_buf(request, request->rq_connection,
361                           request->rq_import->imp_client->cli_request_portal);
362         RETURN(rc);
363
364  cleanup2:
365         PtlMEUnlink(request->rq_reply_me_h);
366  cleanup:
367         OBD_FREE(repbuf, request->rq_replen);
368         // up(&request->rq_client->cli_rpc_sem);
369
370         return rc;
371 }
372
373 void ptlrpc_link_svc_me(struct ptlrpc_service *service, int i)
374 {
375         int rc;
376         ptl_md_t dummy;
377         ptl_handle_md_t md_h;
378
379         /* Attach the leading ME on which we build the ring */
380         rc = PtlMEAttach(service->srv_self.peer_ni, service->srv_req_portal,
381                          local_id, 0, ~0, PTL_RETAIN, PTL_INS_BEFORE,
382                          &(service->srv_me_h[i]));
383         if (rc != PTL_OK) {
384                 CERROR("PtlMEAttach failed: %d\n", rc);
385                 LBUG();
386         }
387
388         if (service->srv_ref_count[i])
389                 LBUG();
390
391         dummy.start         = service->srv_buf[i];
392         dummy.length        = service->srv_buf_size;
393         dummy.max_offset    = service->srv_buf_size;
394         dummy.threshold     = PTL_MD_THRESH_INF;
395         dummy.options       = PTL_MD_OP_PUT | PTL_MD_AUTO_UNLINK;
396         dummy.user_ptr      = service;
397         dummy.eventq        = service->srv_eq_h;
398         dummy.max_offset    = service->srv_buf_size;
399
400         rc = PtlMDAttach(service->srv_me_h[i], dummy, PTL_UNLINK, &md_h);
401         if (rc != PTL_OK) {
402                 /* cleanup */
403                 CERROR("PtlMDAttach failed: %d\n", rc);
404                 LBUG();
405         }
406 }
407
408 /* ptl_handled_rpc() should be called by the sleeping process once
409  * it finishes processing an event.  This ensures the ref count is
410  * decremented and that the rpc ring buffer cycles properly.
411  */
412 int ptl_handled_rpc(struct ptlrpc_service *service, void *start)
413 {
414         int index;
415
416         spin_lock(&service->srv_lock);
417         for (index = 0; index < service->srv_ring_length; index++)
418                 if (service->srv_buf[index] == start)
419                         break;
420
421         if (index == service->srv_ring_length)
422                 LBUG();
423
424         CDEBUG(D_INFO, "MD index=%d Ref Count=%d\n", index,
425                service->srv_ref_count[index]);
426         service->srv_ref_count[index]--;
427
428         if (service->srv_ref_count[index] < 0)
429                 LBUG();
430
431         if (service->srv_ref_count[index] == 0 &&
432             !ptl_is_valid_handle(&(service->srv_me_h[index]))) {
433                 CDEBUG(D_NET, "relinking %d\n", index);
434                 ptlrpc_link_svc_me(service, index);
435         }
436
437         spin_unlock(&service->srv_lock);
438         return 0;
439 }