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.
23 #define DEBUG_SUBSYSTEM S_RPC
25 #include <linux/obd_support.h>
26 #include <linux/lustre_net.h>
27 #include <linux/lustre_lib.h>
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};
33 int ptlrpc_check_bulk_sent(struct ptlrpc_bulk_desc *bulk)
37 if (bulk->b_flags & PTL_BULK_FL_SENT)
40 if (l_killable_pending(current)) {
41 bulk->b_flags |= PTL_RPC_FL_INTR;
45 CDEBUG(D_NET, "no event yet\n");
49 int ptlrpc_check_bulk_received(struct ptlrpc_bulk_desc *bulk)
53 if (bulk->b_flags & PTL_BULK_FL_RCVD)
56 if (l_killable_pending(current)) {
57 bulk->b_flags |= PTL_RPC_FL_INTR;
61 CDEBUG(D_NET, "no event yet\n");
65 static int ptl_send_buf(struct ptlrpc_request *request,
66 struct ptlrpc_connection *conn, int portal)
69 ptl_process_id_t remote_id;
73 request->rq_req_md.user_ptr = request;
75 switch (request->rq_type) {
76 case PTL_RPC_TYPE_REQUEST:
77 request->rq_req_md.start = request->rq_reqmsg;
78 request->rq_req_md.length = request->rq_reqlen;
79 request->rq_req_md.eventq = request_out_eq;
80 request->rq_req_md.threshold = 1;
83 case PTL_RPC_TYPE_REPLY:
84 request->rq_req_md.start = request->rq_repmsg;
85 request->rq_req_md.length = request->rq_replen;
86 request->rq_req_md.eventq = reply_out_eq;
87 request->rq_req_md.threshold = 1;
92 return -1; /* notreached */
94 request->rq_req_md.options = PTL_MD_OP_PUT;
95 request->rq_req_md.user_ptr = request;
97 rc = PtlMDBind(conn->c_peer.peer_ni, request->rq_req_md, &md_h);
98 //CERROR("MDBind (outgoing req/rep/bulk): %Lu\n", (__u64)md_h);
100 CERROR("PtlMDBind failed: %d\n", rc);
105 remote_id.nid = conn->c_peer.peer_nid;
108 CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %Ld\n",
109 request->rq_req_md.length, portal, request->rq_xid);
111 rc = PtlPut(md_h, ack, remote_id, portal, 0, request->rq_xid,
114 CERROR("PtlPut(%Lu, %d, %Ld) failed: %d\n", remote_id.nid,
115 portal, request->rq_xid, rc);
122 int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *desc)
125 struct list_head *tmp, *next;
126 ptl_process_id_t remote_id;
129 list_for_each_safe(tmp, next, &desc->b_page_list) {
130 struct ptlrpc_bulk_page *bulk;
131 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
133 bulk->b_md.start = bulk->b_buf;
134 bulk->b_md.length = bulk->b_buflen;
135 bulk->b_md.eventq = bulk_source_eq;
136 bulk->b_md.threshold = 2; /* SENT and ACK */
137 bulk->b_md.options = PTL_MD_OP_PUT;
138 bulk->b_md.user_ptr = bulk;
140 rc = PtlMDBind(desc->b_connection->c_peer.peer_ni, bulk->b_md,
143 CERROR("PtlMDBind failed: %d\n", rc);
148 remote_id.nid = desc->b_connection->c_peer.peer_nid;
151 CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
152 bulk->b_md.length, desc->b_portal, bulk->b_xid);
154 rc = PtlPut(bulk->b_md_h, PTL_ACK_REQ, remote_id,
155 desc->b_portal, 0, bulk->b_xid, 0, 0);
157 CERROR("PtlPut(%Lu, %d, %d) failed: %d\n",
158 remote_id.nid, desc->b_portal, bulk->b_xid, rc);
159 PtlMDUnlink(bulk->b_md_h);
168 int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *desc)
170 struct list_head *tmp, *next;
174 list_for_each_safe(tmp, next, &desc->b_page_list) {
175 struct ptlrpc_bulk_page *bulk;
176 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
178 rc = PtlMEAttach(desc->b_connection->c_peer.peer_ni,
179 desc->b_portal, local_id, bulk->b_xid, 0,
180 PTL_UNLINK, PTL_INS_AFTER, &bulk->b_me_h);
182 CERROR("PtlMEAttach failed: %d\n", rc);
187 bulk->b_md.start = bulk->b_buf;
188 bulk->b_md.length = bulk->b_buflen;
189 bulk->b_md.threshold = 1;
190 bulk->b_md.options = PTL_MD_OP_PUT;
191 bulk->b_md.user_ptr = bulk;
192 bulk->b_md.eventq = bulk_sink_eq;
194 rc = PtlMDAttach(bulk->b_me_h, bulk->b_md, PTL_UNLINK,
197 CERROR("PtlMDAttach failed: %d\n", rc);
202 CDEBUG(D_NET, "Setup bulk sink buffer: %u bytes, xid %u, "
203 "portal %u\n", bulk->b_buflen, bulk->b_xid,
210 ptlrpc_abort_bulk(desc);
215 int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc)
217 struct list_head *tmp, *next;
219 list_for_each_safe(tmp, next, &desc->b_page_list) {
220 struct ptlrpc_bulk_page *bulk;
221 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
223 /* This should be safe: these handles are initialized to be
224 * invalid in ptlrpc_prep_bulk_page() */
225 PtlMDUnlink(bulk->b_md_h);
226 PtlMEUnlink(bulk->b_me_h);
232 int ptlrpc_reply(struct ptlrpc_service *svc, struct ptlrpc_request *req)
234 if (req->rq_repmsg == NULL) {
235 CERROR("bad: someone called ptlrpc_reply when they meant "
240 /* FIXME: we need to increment the count of handled events */
241 req->rq_type = PTL_RPC_TYPE_REPLY;
242 //req->rq_repmsg->conn = req->rq_connection->c_remote_conn;
243 //req->rq_repmsg->token = req->rq_connection->c_remote_token;
244 req->rq_repmsg->status = HTON__u32(req->rq_status);
245 req->rq_reqmsg->type = HTON__u32(req->rq_type);
246 return ptl_send_buf(req, req->rq_connection, svc->srv_rep_portal);
249 int ptlrpc_error(struct ptlrpc_service *svc, struct ptlrpc_request *req)
254 if (req->rq_repmsg) {
255 CERROR("req already has repmsg\n");
259 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
263 req->rq_repmsg->type = HTON__u32(PTL_RPC_MSG_ERR);
265 rc = ptlrpc_reply(svc, req);
270 int ptl_send_rpc(struct ptlrpc_request *request)
277 if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
278 CERROR("wrong packet type sent %d\n",
279 NTOH__u32(request->rq_reqmsg->type));
283 if (request->rq_replen == 0) {
284 CERROR("request->rq_replen is 0!\n");
288 /* request->rq_repmsg is set only when the reply comes in, in
289 * client_packet_callback() */
290 if (request->rq_reply_md.start)
291 OBD_FREE(request->rq_reply_md.start, request->rq_replen);
293 OBD_ALLOC(repbuf, request->rq_replen);
299 down(&request->rq_client->cli_rpc_sem);
301 rc = PtlMEAttach(request->rq_connection->c_peer.peer_ni,
302 request->rq_client->cli_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_TYPE_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 = reply_in_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 %Lu, portal %u\n",
328 request->rq_replen, request->rq_xid,
329 request->rq_client->cli_reply_portal);
331 rc = ptl_send_buf(request, request->rq_connection,
332 request->rq_client->cli_request_portal);
336 PtlMEUnlink(request->rq_reply_me_h);
338 OBD_FREE(repbuf, request->rq_replen);
339 up(&request->rq_client->cli_rpc_sem);
344 void ptlrpc_link_svc_me(struct ptlrpc_service *service, int i)
348 ptl_handle_md_t md_h;
350 /* Attach the leading ME on which we build the ring */
351 rc = PtlMEAttach(service->srv_self.peer_ni, service->srv_req_portal,
352 local_id, 0, ~0, PTL_RETAIN, PTL_INS_BEFORE,
353 &(service->srv_me_h[i]));
355 CERROR("PtlMEAttach failed: %d\n", rc);
359 if (service->srv_ref_count[i])
362 dummy.start = service->srv_buf[i];
363 dummy.length = service->srv_buf_size;
364 dummy.max_offset = service->srv_buf_size;
365 dummy.threshold = PTL_MD_THRESH_INF;
366 dummy.options = PTL_MD_OP_PUT | PTL_MD_AUTO_UNLINK;
367 dummy.user_ptr = service;
368 dummy.eventq = service->srv_eq_h;
369 dummy.max_offset = service->srv_buf_size;
371 rc = PtlMDAttach(service->srv_me_h[i], dummy, PTL_UNLINK, &md_h);
374 CERROR("PtlMDAttach failed: %d\n", rc);
379 /* ptl_handled_rpc() should be called by the sleeping process once
380 * it finishes processing an event. This ensures the ref count is
381 * decremented and that the rpc ring buffer cycles properly.
383 int ptl_handled_rpc(struct ptlrpc_service *service, void *start)
387 spin_lock(&service->srv_lock);
388 for (index = 0; index < service->srv_ring_length; index++)
389 if (service->srv_buf[index] == start)
392 if (index == service->srv_ring_length)
395 CDEBUG(D_INFO, "MD index=%d Ref Count=%d\n", index,
396 service->srv_ref_count[index]);
397 service->srv_ref_count[index]--;
399 if (service->srv_ref_count[index] < 0)
402 if (service->srv_ref_count[index] == 0 &&
403 !ptl_is_valid_handle(&(service->srv_me_h[index]))) {
404 CDEBUG(D_NET, "relinking %d\n", index);
405 ptlrpc_link_svc_me(service, index);
408 spin_unlock(&service->srv_lock);