1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 #define DEBUG_SUBSYSTEM S_RPC
28 #include <liblustre.h>
30 #include <obd_support.h>
31 #include <lustre_net.h>
32 #include <lustre_lib.h>
34 #include "ptlrpc_internal.h"
36 static int ptl_send_buf (lnet_handle_md_t *mdh, void *base, int len,
37 lnet_ack_req_t ack, struct ptlrpc_cb_id *cbid,
38 struct ptlrpc_connection *conn, int portal, __u64 xid)
44 LASSERT (portal != 0);
45 LASSERT (conn != NULL);
46 CDEBUG (D_INFO, "conn=%p id %s\n", conn, libcfs_id2str(conn->c_peer));
49 md.threshold = (ack == LNET_ACK_REQ) ? 2 : 1;
50 md.options = PTLRPC_MD_OPTIONS;
52 md.eq_handle = ptlrpc_eq_h;
54 if (unlikely(ack == LNET_ACK_REQ &&
55 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_ACK, OBD_FAIL_ONCE))){
56 /* don't ask for the ack to simulate failing client */
60 rc = LNetMDBind (md, LNET_UNLINK, mdh);
61 if (unlikely(rc != 0)) {
62 CERROR ("LNetMDBind failed: %d\n", rc);
63 LASSERT (rc == -ENOMEM);
67 CDEBUG(D_NET, "Sending %d bytes to portal %d, xid "LPD64"\n",
70 rc = LNetPut (conn->c_self, *mdh, ack,
71 conn->c_peer, portal, xid, 0, 0);
72 if (unlikely(rc != 0)) {
74 /* We're going to get an UNLINK event when I unlink below,
75 * which will complete just like any other failed send, so
76 * I fall through and return success here! */
77 CERROR("LNetPut(%s, %d, "LPD64") failed: %d\n",
78 libcfs_id2str(conn->c_peer), portal, xid, rc);
79 rc2 = LNetMDUnlink(*mdh);
80 LASSERTF(rc2 == 0, "rc2 = %d\n", rc2);
86 int ptlrpc_start_bulk_transfer (struct ptlrpc_bulk_desc *desc)
88 struct ptlrpc_connection *conn = desc->bd_export->exp_connection;
95 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_PUT_NET))
98 /* NB no locking required until desc is on the network */
99 LASSERT (!desc->bd_network_rw);
100 LASSERT (desc->bd_type == BULK_PUT_SOURCE ||
101 desc->bd_type == BULK_GET_SINK);
102 desc->bd_success = 0;
104 md.user_ptr = &desc->bd_cbid;
105 md.eq_handle = ptlrpc_eq_h;
106 md.threshold = 2; /* SENT and ACK/REPLY */
107 md.options = PTLRPC_MD_OPTIONS;
108 ptlrpc_fill_bulk_md(&md, desc);
110 LASSERT (desc->bd_cbid.cbid_fn == server_bulk_callback);
111 LASSERT (desc->bd_cbid.cbid_arg == desc);
113 /* NB total length may be 0 for a read past EOF, so we send a 0
114 * length bulk, since the client expects a bulk event. */
116 rc = LNetMDBind(md, LNET_UNLINK, &desc->bd_md_h);
118 CERROR("LNetMDBind failed: %d\n", rc);
119 LASSERT (rc == -ENOMEM);
123 /* Client's bulk and reply matchbits are the same */
124 xid = desc->bd_req->rq_xid;
125 CDEBUG(D_NET, "Transferring %u pages %u bytes via portal %d "
126 "id %s xid "LPX64"\n", desc->bd_iov_count,
127 desc->bd_nob, desc->bd_portal,
128 libcfs_id2str(conn->c_peer), xid);
130 /* Network is about to get at the memory */
131 desc->bd_network_rw = 1;
133 if (desc->bd_type == BULK_PUT_SOURCE)
134 rc = LNetPut (conn->c_self, desc->bd_md_h, LNET_ACK_REQ,
135 conn->c_peer, desc->bd_portal, xid, 0, 0);
137 rc = LNetGet (conn->c_self, desc->bd_md_h,
138 conn->c_peer, desc->bd_portal, xid, 0);
141 /* Can't send, so we unlink the MD bound above. The UNLINK
142 * event this creates will signal completion with failure,
143 * so we return SUCCESS here! */
144 CERROR("Transfer(%s, %d, "LPX64") failed: %d\n",
145 libcfs_id2str(conn->c_peer), desc->bd_portal, xid, rc);
146 rc2 = LNetMDUnlink(desc->bd_md_h);
153 void ptlrpc_abort_bulk (struct ptlrpc_bulk_desc *desc)
155 /* Server side bulk abort. Idempotent. Not thread-safe (i.e. only
156 * serialises with completion callback) */
157 struct l_wait_info lwi;
160 LASSERT (!in_interrupt ()); /* might sleep */
162 if (!ptlrpc_bulk_active(desc)) /* completed or */
163 return; /* never started */
165 /* Do not send any meaningful data over the wire for evicted clients */
166 if (desc->bd_export && desc->bd_export->exp_failed)
167 ptl_rpc_wipe_bulk_pages(desc);
169 /* The unlink ensures the callback happens ASAP and is the last
170 * one. If it fails, it must be because completion just happened,
171 * but we must still l_wait_event() in this case, to give liblustre
172 * a chance to run server_bulk_callback()*/
174 LNetMDUnlink (desc->bd_md_h);
177 /* Network access will complete in finite time but the HUGE
178 * timeout lets us CWARN for visibility of sluggish NALs */
179 lwi = LWI_TIMEOUT (cfs_time_seconds(300), NULL, NULL);
180 rc = l_wait_event(desc->bd_waitq,
181 !ptlrpc_bulk_active(desc), &lwi);
185 LASSERT(rc == -ETIMEDOUT);
186 CWARN("Unexpectedly long timeout: desc %p\n", desc);
190 int ptlrpc_register_bulk (struct ptlrpc_request *req)
192 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
193 lnet_process_id_t peer;
196 lnet_handle_me_t me_h;
200 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_GET_NET))
203 /* NB no locking required until desc is on the network */
204 LASSERT (desc->bd_nob > 0);
205 LASSERT (!desc->bd_network_rw);
206 LASSERT (desc->bd_iov_count <= PTLRPC_MAX_BRW_PAGES);
207 LASSERT (desc->bd_req != NULL);
208 LASSERT (desc->bd_type == BULK_PUT_SINK ||
209 desc->bd_type == BULK_GET_SOURCE);
211 desc->bd_success = 0;
213 peer = desc->bd_import->imp_connection->c_peer;
215 md.user_ptr = &desc->bd_cbid;
216 md.eq_handle = ptlrpc_eq_h;
217 md.threshold = 1; /* PUT or GET */
218 md.options = PTLRPC_MD_OPTIONS |
219 ((desc->bd_type == BULK_GET_SOURCE) ?
220 LNET_MD_OP_GET : LNET_MD_OP_PUT);
221 ptlrpc_fill_bulk_md(&md, desc);
223 LASSERT (desc->bd_cbid.cbid_fn == client_bulk_callback);
224 LASSERT (desc->bd_cbid.cbid_arg == desc);
226 /* XXX Registering the same xid on retried bulk makes my head
227 * explode trying to understand how the original request's bulk
228 * might interfere with the retried request -eeb */
229 LASSERTF (!desc->bd_registered || req->rq_xid != desc->bd_last_xid,
230 "registered: %d rq_xid: "LPU64" bd_last_xid: "LPU64"\n",
231 desc->bd_registered, req->rq_xid, desc->bd_last_xid);
232 desc->bd_registered = 1;
233 desc->bd_last_xid = req->rq_xid;
235 rc = LNetMEAttach(desc->bd_portal, peer,
236 req->rq_xid, 0, LNET_UNLINK, LNET_INS_AFTER, &me_h);
238 CERROR("LNetMEAttach failed: %d\n", rc);
239 LASSERT (rc == -ENOMEM);
243 /* About to let the network at it... */
244 desc->bd_network_rw = 1;
245 rc = LNetMDAttach(me_h, md, LNET_UNLINK, &desc->bd_md_h);
247 CERROR("LNetMDAttach failed: %d\n", rc);
248 LASSERT (rc == -ENOMEM);
249 desc->bd_network_rw = 0;
250 rc2 = LNetMEUnlink (me_h);
255 CDEBUG(D_NET, "Setup bulk %s buffers: %u pages %u bytes, xid "LPX64", "
257 desc->bd_type == BULK_GET_SOURCE ? "get-source" : "put-sink",
258 desc->bd_iov_count, desc->bd_nob,
259 req->rq_xid, desc->bd_portal);
263 void ptlrpc_unregister_bulk (struct ptlrpc_request *req)
265 /* Disconnect a bulk desc from the network. Idempotent. Not
266 * thread-safe (i.e. only interlocks with completion callback). */
267 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
269 struct l_wait_info lwi;
272 LASSERT (!in_interrupt ()); /* might sleep */
274 if (!ptlrpc_bulk_active(desc)) /* completed or */
275 return; /* never registered */
277 LASSERT (desc->bd_req == req); /* bd_req NULL until registered */
279 /* the unlink ensures the callback happens ASAP and is the last
280 * one. If it fails, it must be because completion just happened,
281 * but we must still l_wait_event() in this case to give liblustre
282 * a chance to run client_bulk_callback() */
284 LNetMDUnlink (desc->bd_md_h);
286 if (req->rq_set != NULL)
287 wq = &req->rq_set->set_waitq;
289 wq = &req->rq_reply_waitq;
292 /* Network access will complete in finite time but the HUGE
293 * timeout lets us CWARN for visibility of sluggish NALs */
294 lwi = LWI_TIMEOUT (cfs_time_seconds(300), NULL, NULL);
295 rc = l_wait_event(*wq, !ptlrpc_bulk_active(desc), &lwi);
299 LASSERT (rc == -ETIMEDOUT);
300 DEBUG_REQ(D_WARNING,req,"Unexpectedly long timeout: desc %p",
305 int ptlrpc_send_reply (struct ptlrpc_request *req, int may_be_difficult)
307 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
308 struct ptlrpc_reply_state *rs = req->rq_reply_state;
309 struct ptlrpc_connection *conn;
312 /* We must already have a reply buffer (only ptlrpc_error() may be
313 * called without one). The reply generated by sptlrpc layer (e.g.
314 * error notify, etc.) might have NULL rq->reqmsg; Otherwise we must
315 * have a request buffer which is either the actual (swabbed) incoming
316 * request, or a saved copy if this is a req saved in
317 * target_queue_final_reply().
319 LASSERT (req->rq_no_reply == 0);
320 LASSERT (req->rq_reqbuf != NULL);
321 LASSERT (rs != NULL);
322 LASSERT (may_be_difficult || !rs->rs_difficult);
323 LASSERT (req->rq_repmsg != NULL);
324 LASSERT (req->rq_repmsg == rs->rs_msg);
325 LASSERT (rs->rs_cb_id.cbid_fn == reply_out_callback);
326 LASSERT (rs->rs_cb_id.cbid_arg == rs);
328 if (unlikely(req->rq_export && req->rq_export->exp_obd &&
329 req->rq_export->exp_obd->obd_fail)) {
330 /* Failed obd's only send ENODEV */
331 req->rq_type = PTL_RPC_MSG_ERR;
332 req->rq_status = -ENODEV;
333 CDEBUG(D_HA, "sending ENODEV from failed obd %d\n",
334 req->rq_export->exp_obd->obd_minor);
337 if (req->rq_type != PTL_RPC_MSG_ERR)
338 req->rq_type = PTL_RPC_MSG_REPLY;
340 lustre_msg_set_type(req->rq_repmsg, req->rq_type);
341 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
342 lustre_msg_set_opc(req->rq_repmsg,
343 req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : 0);
345 target_pack_pool_reply(req);
347 if (req->rq_export == NULL || req->rq_export->exp_connection == NULL)
348 conn = ptlrpc_get_connection(req->rq_peer, req->rq_self, NULL);
350 conn = ptlrpc_connection_addref(req->rq_export->exp_connection);
352 if (unlikely(conn == NULL)) {
353 CERROR("not replying on NULL connection\n"); /* bug 9635 */
356 atomic_inc (&svc->srv_outstanding_replies);
357 ptlrpc_rs_addref(rs); /* +1 ref for the network */
359 rc = sptlrpc_svc_wrap_reply(req);
363 rc = ptl_send_buf (&rs->rs_md_h, rs->rs_repbuf, rs->rs_repdata_len,
364 rs->rs_difficult ? LNET_ACK_REQ : LNET_NOACK_REQ,
366 svc->srv_rep_portal, req->rq_xid);
368 if (unlikely(rc != 0)) {
369 atomic_dec (&svc->srv_outstanding_replies);
370 ptlrpc_rs_decref(rs);
372 ptlrpc_put_connection(conn);
376 int ptlrpc_reply (struct ptlrpc_request *req)
378 if (req->rq_no_reply)
381 return (ptlrpc_send_reply(req, 0));
384 int ptlrpc_error(struct ptlrpc_request *req)
389 if (req->rq_no_reply)
392 if (!req->rq_repmsg) {
393 rc = lustre_pack_reply(req, 1, NULL, NULL);
398 req->rq_type = PTL_RPC_MSG_ERR;
400 rc = ptlrpc_send_reply(req, 0);
404 int ptl_send_rpc(struct ptlrpc_request *request, int noreply)
408 struct ptlrpc_connection *connection;
409 lnet_handle_me_t reply_me_h;
411 struct obd_device *obd = request->rq_import->imp_obd;
414 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_RPC))
417 LASSERT (request->rq_type == PTL_RPC_MSG_REQUEST);
419 /* If this is a re-transmit, we're required to have disengaged
420 * cleanly from the previous attempt */
421 LASSERT (!request->rq_receiving_reply);
423 if (request->rq_import->imp_obd &&
424 request->rq_import->imp_obd->obd_fail) {
425 CDEBUG(D_HA, "muting rpc for failed imp obd %s\n",
426 request->rq_import->imp_obd->obd_name);
427 /* this prevents us from waiting in ptlrpc_queue_wait */
432 connection = request->rq_import->imp_connection;
434 lustre_msg_set_handle(request->rq_reqmsg,
435 &request->rq_import->imp_remote_handle);
436 lustre_msg_set_type(request->rq_reqmsg, PTL_RPC_MSG_REQUEST);
437 lustre_msg_set_conn_cnt(request->rq_reqmsg,
438 request->rq_import->imp_conn_cnt);
440 rc = sptlrpc_cli_wrap_request(request);
444 /* bulk register should be done after wrap_request() */
445 if (request->rq_bulk != NULL) {
446 rc = ptlrpc_register_bulk (request);
452 LASSERT (request->rq_replen != 0);
453 if (request->rq_repbuf == NULL) {
454 rc = sptlrpc_cli_alloc_repbuf(request,
457 GOTO(cleanup_bulk, rc);
460 rc = LNetMEAttach(request->rq_reply_portal,/*XXX FIXME bug 249*/
461 connection->c_peer, request->rq_xid, 0,
462 LNET_UNLINK, LNET_INS_AFTER, &reply_me_h);
464 CERROR("LNetMEAttach failed: %d\n", rc);
465 LASSERT (rc == -ENOMEM);
466 GOTO(cleanup_bulk, rc = -ENOMEM);
470 spin_lock(&request->rq_lock);
471 /* If the MD attach succeeds, there _will_ be a reply_in callback */
472 request->rq_receiving_reply = !noreply;
473 /* Clear any flags that may be present from previous sends. */
474 request->rq_replied = 0;
476 request->rq_timedout = 0;
477 request->rq_net_err = 0;
478 request->rq_resend = 0;
479 request->rq_restart = 0;
480 spin_unlock(&request->rq_lock);
483 reply_md.start = request->rq_repbuf;
484 reply_md.length = request->rq_repbuf_len;
485 reply_md.threshold = 1;
486 reply_md.options = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT;
487 reply_md.user_ptr = &request->rq_reply_cbid;
488 reply_md.eq_handle = ptlrpc_eq_h;
490 rc = LNetMDAttach(reply_me_h, reply_md, LNET_UNLINK,
491 &request->rq_reply_md_h);
493 CERROR("LNetMDAttach failed: %d\n", rc);
494 LASSERT (rc == -ENOMEM);
495 spin_lock(&request->rq_lock);
496 /* ...but the MD attach didn't succeed... */
497 request->rq_receiving_reply = 0;
498 spin_unlock(&request->rq_lock);
499 GOTO(cleanup_me, rc -ENOMEM);
502 CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid "LPU64
504 request->rq_repbuf_len, request->rq_xid,
505 request->rq_reply_portal);
508 /* add references on request and import for request_out_callback */
509 ptlrpc_request_addref(request);
510 atomic_inc(&request->rq_import->imp_inflight);
511 if (obd->obd_svc_stats != NULL)
512 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQACTIVE_CNTR,
513 request->rq_import->imp_inflight.counter);
515 OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_SEND, request->rq_timeout + 5);
517 request->rq_sent = CURRENT_SECONDS;
518 do_gettimeofday(&request->rq_arrival_time);
519 ptlrpc_pinger_sending_on_import(request->rq_import);
520 rc = ptl_send_buf(&request->rq_req_md_h,
521 request->rq_reqbuf, request->rq_reqdata_len,
522 LNET_NOACK_REQ, &request->rq_req_cbid,
524 request->rq_request_portal,
527 ptlrpc_lprocfs_rpc_sent(request);
531 /* drop request_out_callback refs, we couldn't start the send */
532 atomic_dec(&request->rq_import->imp_inflight);
533 ptlrpc_req_finished(request);
538 GOTO(cleanup_me, rc);
540 /* MEUnlink is safe; the PUT didn't even get off the ground, and
541 * nobody apart from the PUT's target has the right nid+XID to
542 * access the reply buffer. */
543 rc2 = LNetMEUnlink(reply_me_h);
545 /* UNLINKED callback called synchronously */
546 LASSERT (!request->rq_receiving_reply);
549 if (request->rq_bulk != NULL)
550 ptlrpc_unregister_bulk(request);
555 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
557 struct ptlrpc_service *service = rqbd->rqbd_service;
558 static lnet_process_id_t match_id = {LNET_NID_ANY, LNET_PID_ANY};
561 lnet_handle_me_t me_h;
563 CDEBUG(D_NET, "LNetMEAttach: portal %d\n",
564 service->srv_req_portal);
566 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_RQBD))
569 rc = LNetMEAttach(service->srv_req_portal,
570 match_id, 0, ~0, LNET_UNLINK, LNET_INS_AFTER, &me_h);
572 CERROR("LNetMEAttach failed: %d\n", rc);
576 LASSERT(rqbd->rqbd_refcount == 0);
577 rqbd->rqbd_refcount = 1;
579 md.start = rqbd->rqbd_buffer;
580 md.length = service->srv_buf_size;
581 md.max_size = service->srv_max_req_size;
582 md.threshold = LNET_MD_THRESH_INF;
583 md.options = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT | LNET_MD_MAX_SIZE;
584 md.user_ptr = &rqbd->rqbd_cbid;
585 md.eq_handle = ptlrpc_eq_h;
587 rc = LNetMDAttach(me_h, md, LNET_UNLINK, &rqbd->rqbd_md_h);
591 CERROR("LNetMDAttach failed: %d; \n", rc);
592 LASSERT (rc == -ENOMEM);
593 rc = LNetMEUnlink (me_h);
595 rqbd->rqbd_refcount = 0;