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.
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
29 #define DEBUG_SUBSYSTEM S_RPC
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/lustre_net.h>
35 extern ptl_handle_eq_t bulk_source_eq, sent_pkt_eq, rcvd_rep_eq, bulk_sink_eq;
36 static ptl_process_id_t local_id = {PTL_ID_ANY, PTL_ID_ANY};
39 int ptlrpc_check_bulk_sent(struct ptlrpc_bulk_desc *bulk)
41 if (bulk->b_flags == PTL_BULK_SENT) {
46 if (sigismember(&(current->pending.signal), SIGKILL) ||
47 sigismember(&(current->pending.signal), SIGINT)) {
48 bulk->b_flags = PTL_RPC_INTR;
53 CDEBUG(D_NET, "no event yet\n");
57 int ptl_send_buf(struct ptlrpc_request *request, struct lustre_peer *peer,
61 ptl_process_id_t remote_id;
65 switch (request->rq_type) {
67 request->rq_req_md.start = request->rq_bulkbuf;
68 request->rq_req_md.length = request->rq_bulklen;
69 request->rq_req_md.eventq = bulk_source_eq;
70 request->rq_req_md.threshold = 2; /* SENT and ACK events */
74 request->rq_req_md.start = request->rq_reqbuf;
75 request->rq_req_md.length = request->rq_reqlen;
76 request->rq_req_md.eventq = sent_pkt_eq;
77 request->rq_req_md.threshold = 1;
81 request->rq_req_md.start = request->rq_repbuf;
82 request->rq_req_md.length = request->rq_replen;
83 request->rq_req_md.eventq = sent_pkt_eq;
84 request->rq_req_md.threshold = 1;
89 return -1; /* notreached */
91 request->rq_req_md.options = PTL_MD_OP_PUT;
92 request->rq_req_md.user_ptr = request;
94 rc = PtlMDBind(peer->peer_ni, request->rq_req_md, &md_h);
95 //CERROR("MDBind (outgoing req/rep/bulk): %Lu\n", (__u64)md_h);
97 CERROR("PtlMDBind failed: %d\n", rc);
102 remote_id.nid = peer->peer_nid;
105 CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
106 request->rq_req_md.length, portal, request->rq_xid);
108 rc = PtlPut(md_h, ack, remote_id, portal, 0, request->rq_xid, 0, 0);
110 CERROR("PtlPut(%d, %d, %d) failed: %d\n", remote_id.nid,
111 portal, request->rq_xid, rc);
118 int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *bulk, int portal)
121 ptl_process_id_t remote_id;
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;
130 rc = PtlMDBind(bulk->b_peer.peer_ni, bulk->b_md, &bulk->b_md_h);
132 CERROR("PtlMDBind failed: %d\n", rc);
137 remote_id.nid = bulk->b_peer.peer_nid;
140 CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
141 bulk->b_md.length, portal, bulk->b_xid);
143 rc = PtlPut(bulk->b_md_h, PTL_ACK_REQ, remote_id, portal, 0,
146 CERROR("PtlPut(%d, %d, %d) failed: %d\n", remote_id.nid,
147 portal, bulk->b_xid, rc);
148 PtlMDUnlink(bulk->b_md_h);
155 int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *bulk)
161 rc = PtlMEAttach(bulk->b_peer.peer_ni, bulk->b_portal, local_id,
162 bulk->b_xid, 0, PTL_UNLINK, PTL_INS_AFTER,
165 CERROR("PtlMEAttach failed: %d\n", rc);
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;
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);
180 CERROR("PtlMDAttach failed: %d\n", rc);
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);
189 // XXX Confirm that this is safe!
191 PtlMEUnlink(bulk->b_me_h);
195 int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *bulk)
199 rc = PtlMEUnlink(bulk->b_me_h);
201 CERROR("PtlMEUnlink failed: %d\n", rc);
206 int ptlrpc_reply(struct obd_device *obddev, struct ptlrpc_service *svc,
207 struct ptlrpc_request *req)
209 struct ptlrpc_request *clnt_req = req->rq_reply_handle;
212 if (req->rq_reply_handle == NULL) {
213 /* This is a request that came from the network via portals. */
215 /* FIXME: we need to increment the count of handled events */
216 req->rq_type = PTL_RPC_REPLY;
217 req->rq_reqhdr->xid = req->rq_reqhdr->xid;
218 ptl_send_buf(req, &req->rq_peer, svc->srv_rep_portal);
220 /* This is a local request that came from another thread. */
222 /* move the reply to the client */
223 clnt_req->rq_replen = req->rq_replen;
224 clnt_req->rq_repbuf = req->rq_repbuf;
225 req->rq_repbuf = NULL;
228 /* free the request buffer */
229 OBD_FREE(req->rq_reqbuf, req->rq_reqlen);
230 req->rq_reqbuf = NULL;
232 /* wake up the client */
233 wake_up_interruptible(&clnt_req->rq_wait_for_rep);
240 int ptlrpc_error(struct obd_device *obddev, struct ptlrpc_service *svc,
241 struct ptlrpc_request *req)
243 struct ptlrep_hdr *hdr;
247 OBD_ALLOC(hdr, sizeof(*hdr));
253 memset(hdr, 0, sizeof(*hdr));
255 hdr->xid = req->rq_reqhdr->xid;
256 hdr->status = req->rq_status;
257 hdr->type = PTL_RPC_ERR;
259 if (req->rq_repbuf) {
260 CERROR("req has repbuf\n");
264 req->rq_repbuf = (char *)hdr;
265 req->rq_replen = sizeof(*hdr);
268 return ptlrpc_reply(obddev, svc, req);
271 int ptl_send_rpc(struct ptlrpc_request *request, struct ptlrpc_client *cl)
273 ptl_process_id_t local_id;
274 struct ptlreq_hdr *hdr;
280 hdr = (struct ptlreq_hdr *)request->rq_reqbuf;
281 if (NTOH__u32(hdr->type) != PTL_RPC_REQUEST) {
282 CERROR("wrong packet type sent %d\n", NTOH__u32(hdr->type));
286 if (request->rq_replen == 0) {
287 CERROR("request->rq_replen is 0!\n");
291 /* request->rq_repbuf is set only when the reply comes in, in
292 * client_packet_callback() */
293 OBD_ALLOC(repbuf, request->rq_replen);
297 local_id.nid = PTL_ID_ANY;
298 local_id.pid = PTL_ID_ANY;
300 down(&cl->cli_rpc_sem);
302 rc = PtlMEAttach(cl->cli_server.peer_ni, request->rq_reply_portal,
303 local_id, request->rq_xid, 0, PTL_UNLINK,
304 PTL_INS_AFTER, &request->rq_reply_me_h);
306 CERROR("PtlMEAttach failed: %d\n", rc);
311 request->rq_type = PTL_RPC_REQUEST;
312 request->rq_reply_md.start = repbuf;
313 request->rq_reply_md.length = request->rq_replen;
314 request->rq_reply_md.threshold = 1;
315 request->rq_reply_md.options = PTL_MD_OP_PUT;
316 request->rq_reply_md.user_ptr = request;
317 request->rq_reply_md.eventq = rcvd_rep_eq;
319 rc = PtlMDAttach(request->rq_reply_me_h, request->rq_reply_md,
320 PTL_UNLINK, &request->rq_reply_md_h);
322 CERROR("PtlMDAttach failed: %d\n", rc);
327 CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid %u, portal %u\n",
328 request->rq_replen, request->rq_xid, request->rq_reply_portal);
330 rc = ptl_send_buf(request, &cl->cli_server, request->rq_req_portal);
334 PtlMEUnlink(request->rq_reply_me_h);
336 OBD_FREE(repbuf, request->rq_replen);
337 up(&cl->cli_rpc_sem);
342 void ptlrpc_link_svc_me(struct ptlrpc_service *service, int i)
346 ptl_handle_md_t md_h;
348 /* Attach the leading ME on which we build the ring */
349 rc = PtlMEAttach(service->srv_self.peer_ni, service->srv_req_portal,
350 service->srv_id, 0, ~0, PTL_RETAIN, PTL_INS_BEFORE,
351 &(service->srv_me_h[i]));
353 CERROR("PtlMEAttach failed: %d\n", rc);
357 if (service->srv_ref_count[i])
360 dummy.start = service->srv_buf[i];
361 dummy.length = service->srv_buf_size;
362 dummy.max_offset = service->srv_buf_size;
363 dummy.threshold = PTL_MD_THRESH_INF;
364 dummy.options = PTL_MD_OP_PUT | PTL_MD_AUTO_UNLINK;
365 dummy.user_ptr = service;
366 dummy.eventq = service->srv_eq_h;
367 dummy.max_offset = service->srv_buf_size;
369 rc = PtlMDAttach(service->srv_me_h[i], dummy, PTL_UNLINK, &md_h);
372 CERROR("PtlMDAttach failed: %d\n", rc);
377 /* ptl_handled_rpc() should be called by the sleeping process once
378 * it finishes processing an event. This ensures the ref count is
379 * decremented and that the rpc ring buffer cycles properly.
381 int ptl_handled_rpc(struct ptlrpc_service *service, void *start)
385 spin_lock(&service->srv_lock);
387 for (index = 0; index < service->srv_ring_length; index++)
388 if (service->srv_buf[index] == start)
391 if (index == service->srv_ring_length)
394 CDEBUG(D_INFO, "MD index=%d Ref Count=%d\n", index,
395 service->srv_ref_count[index]);
396 service->srv_ref_count[index]--;
398 if (service->srv_ref_count[index] < 0)
401 if (service->srv_ref_count[index] == 0 &&
402 service->srv_me_h[index] == 0) {
403 CDEBUG(D_NET, "relinking %d\n", index);
404 ptlrpc_link_svc_me(service, index);
407 spin_unlock(&service->srv_lock);