4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
35 #define DEBUG_SUBSYSTEM S_RPC
38 # include <liblustre.h>
40 # include <libcfs/libcfs.h>
42 # include <linux/kernel.h>
46 #include <obd_class.h>
47 #include <lustre_net.h>
48 #include <lustre_sec.h>
49 #include "ptlrpc_internal.h"
51 lnet_handle_eq_t ptlrpc_eq_h;
54 * Client's outgoing request callback
56 void request_out_callback(lnet_event_t *ev)
58 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
59 struct ptlrpc_request *req = cbid->cbid_arg;
62 LASSERT (ev->type == LNET_EVENT_SEND ||
63 ev->type == LNET_EVENT_UNLINK);
64 LASSERT (ev->unlinked);
66 DEBUG_REQ(D_NET, req, "type %d, status %d", ev->type, ev->status);
68 sptlrpc_request_out_callback(req);
69 req->rq_real_sent = cfs_time_current_sec();
71 if (ev->type == LNET_EVENT_UNLINK || ev->status != 0) {
73 /* Failed send: make it seem like the reply timed out, just
74 * like failing sends in client.c does currently... */
76 cfs_spin_lock(&req->rq_lock);
78 cfs_spin_unlock(&req->rq_lock);
80 ptlrpc_client_wake_req(req);
83 ptlrpc_req_finished(req);
89 * Client's incoming reply callback
91 void reply_in_callback(lnet_event_t *ev)
93 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
94 struct ptlrpc_request *req = cbid->cbid_arg;
97 DEBUG_REQ(D_NET, req, "type %d, status %d", ev->type, ev->status);
99 LASSERT (ev->type == LNET_EVENT_PUT || ev->type == LNET_EVENT_UNLINK);
100 LASSERT (ev->md.start == req->rq_repbuf);
101 LASSERT (ev->offset + ev->mlength <= req->rq_repbuf_len);
102 /* We've set LNET_MD_MANAGE_REMOTE for all outgoing requests
103 for adaptive timeouts' early reply. */
104 LASSERT((ev->md.options & LNET_MD_MANAGE_REMOTE) != 0);
106 cfs_spin_lock(&req->rq_lock);
108 req->rq_receiving_reply = 0;
111 req->rq_must_unlink = 0;
116 if (ev->type == LNET_EVENT_UNLINK) {
117 LASSERT(ev->unlinked);
118 DEBUG_REQ(D_NET, req, "unlink");
122 if (ev->mlength < ev->rlength ) {
123 CDEBUG(D_RPCTRACE, "truncate req %p rpc %d - %d+%d\n", req,
124 req->rq_replen, ev->rlength, ev->offset);
125 req->rq_reply_truncate = 1;
127 req->rq_status = -EOVERFLOW;
128 req->rq_nob_received = ev->rlength + ev->offset;
132 if ((ev->offset == 0) &&
133 ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT))) {
135 DEBUG_REQ(D_ADAPTTO, req,
136 "Early reply received: mlen=%u offset=%d replen=%d "
137 "replied=%d unlinked=%d", ev->mlength, ev->offset,
138 req->rq_replen, req->rq_replied, ev->unlinked);
140 req->rq_early_count++; /* number received, client side */
142 if (req->rq_replied) /* already got the real reply */
146 req->rq_reply_off = ev->offset;
147 req->rq_nob_received = ev->mlength;
148 /* And we're still receiving */
149 req->rq_receiving_reply = 1;
152 req->rq_rep_swab_mask = 0;
154 req->rq_reply_off = ev->offset;
155 req->rq_nob_received = ev->mlength;
156 /* LNetMDUnlink can't be called under the LNET_LOCK,
157 so we must unlink in ptlrpc_unregister_reply */
158 DEBUG_REQ(D_INFO, req,
159 "reply in flags=%x mlen=%u offset=%d replen=%d",
160 lustre_msg_get_flags(req->rq_reqmsg),
161 ev->mlength, ev->offset, req->rq_replen);
164 req->rq_import->imp_last_reply_time = cfs_time_current_sec();
167 /* NB don't unlock till after wakeup; req can disappear under us
168 * since we don't have our own ref */
169 ptlrpc_client_wake_req(req);
170 cfs_spin_unlock(&req->rq_lock);
175 * Client's bulk has been written/read
177 void client_bulk_callback (lnet_event_t *ev)
179 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
180 struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
183 LASSERT ((desc->bd_type == BULK_PUT_SINK &&
184 ev->type == LNET_EVENT_PUT) ||
185 (desc->bd_type == BULK_GET_SOURCE &&
186 ev->type == LNET_EVENT_GET) ||
187 ev->type == LNET_EVENT_UNLINK);
188 LASSERT (ev->unlinked);
190 if (CFS_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_CLIENT_BULK_CB, CFS_FAIL_ONCE))
193 if (CFS_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_CLIENT_BULK_CB2,CFS_FAIL_ONCE))
196 CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
197 "event type %d, status %d, desc %p\n",
198 ev->type, ev->status, desc);
200 cfs_spin_lock(&desc->bd_lock);
202 LASSERT(desc->bd_network_rw);
203 desc->bd_network_rw = 0;
205 if (ev->type != LNET_EVENT_UNLINK && ev->status == 0) {
206 desc->bd_success = 1;
207 desc->bd_nob_transferred = ev->mlength;
208 desc->bd_sender = ev->sender;
211 /* release the encrypted pages for write */
212 if (desc->bd_req->rq_bulk_write)
213 sptlrpc_enc_pool_put_pages(desc);
215 /* NB don't unlock till after wakeup; desc can disappear under us
217 ptlrpc_client_wake_req(desc->bd_req);
219 cfs_spin_unlock(&desc->bd_lock);
224 * Server's incoming request callback
226 void request_in_callback(lnet_event_t *ev)
228 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
229 struct ptlrpc_request_buffer_desc *rqbd = cbid->cbid_arg;
230 struct ptlrpc_service *service = rqbd->rqbd_service;
231 struct ptlrpc_request *req;
234 LASSERT (ev->type == LNET_EVENT_PUT ||
235 ev->type == LNET_EVENT_UNLINK);
236 LASSERT ((char *)ev->md.start >= rqbd->rqbd_buffer);
237 LASSERT ((char *)ev->md.start + ev->offset + ev->mlength <=
238 rqbd->rqbd_buffer + service->srv_buf_size);
240 CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
241 "event type %d, status %d, service %s\n",
242 ev->type, ev->status, service->srv_name);
245 /* If this is the last request message to fit in the
246 * request buffer we can use the request object embedded in
247 * rqbd. Note that if we failed to allocate a request,
248 * we'd have to re-post the rqbd, which we can't do in this
250 req = &rqbd->rqbd_req;
251 memset(req, 0, sizeof (*req));
253 LASSERT (ev->type == LNET_EVENT_PUT);
254 if (ev->status != 0) {
255 /* We moaned above already... */
258 OBD_ALLOC_GFP(req, sizeof(*req), CFS_ALLOC_ATOMIC_TRY);
260 CERROR("Can't allocate incoming request descriptor: "
261 "Dropping %s RPC from %s\n",
263 libcfs_id2str(ev->initiator));
268 /* NB we ABSOLUTELY RELY on req being zeroed, so pointers are NULL,
269 * flags are reset and scalars are zero. We only set the message
270 * size to non-zero if this was a successful receive. */
271 req->rq_xid = ev->match_bits;
272 req->rq_reqbuf = ev->md.start + ev->offset;
273 if (ev->type == LNET_EVENT_PUT && ev->status == 0)
274 req->rq_reqdata_len = ev->mlength;
275 cfs_gettimeofday(&req->rq_arrival_time);
276 req->rq_peer = ev->initiator;
277 req->rq_self = ev->target.nid;
279 req->rq_phase = RQ_PHASE_NEW;
281 req->rq_uid = ev->uid;
283 cfs_spin_lock_init(&req->rq_lock);
284 CFS_INIT_LIST_HEAD(&req->rq_timed_list);
285 cfs_atomic_set(&req->rq_refcount, 1);
286 if (ev->type == LNET_EVENT_PUT)
287 CDEBUG(D_INFO, "incoming req@%p x"LPU64" msgsize %u\n",
288 req, req->rq_xid, ev->mlength);
290 CDEBUG(D_RPCTRACE, "peer: %s\n", libcfs_id2str(req->rq_peer));
292 cfs_spin_lock(&service->srv_lock);
294 req->rq_history_seq = service->srv_request_seq++;
295 cfs_list_add_tail(&req->rq_history_list, &service->srv_request_history);
298 service->srv_nrqbd_receiving--;
299 CDEBUG(D_INFO, "Buffer complete: %d buffers still posted\n",
300 service->srv_nrqbd_receiving);
302 /* Normally, don't complain about 0 buffers posted; LNET won't
303 * drop incoming reqs since we set the portal lazy */
304 if (test_req_buffer_pressure &&
305 ev->type != LNET_EVENT_UNLINK &&
306 service->srv_nrqbd_receiving == 0)
307 CWARN("All %s request buffers busy\n",
310 /* req takes over the network's ref on rqbd */
312 /* req takes a ref on rqbd */
313 rqbd->rqbd_refcount++;
316 cfs_list_add_tail(&req->rq_list, &service->srv_req_in_queue);
317 service->srv_n_queued_reqs++;
319 /* NB everything can disappear under us once the request
320 * has been queued and we unlock, so do the wake now... */
321 cfs_waitq_signal(&service->srv_waitq);
323 cfs_spin_unlock(&service->srv_lock);
328 * Server's outgoing reply callback
330 void reply_out_callback(lnet_event_t *ev)
332 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
333 struct ptlrpc_reply_state *rs = cbid->cbid_arg;
334 struct ptlrpc_service *svc = rs->rs_service;
337 LASSERT (ev->type == LNET_EVENT_SEND ||
338 ev->type == LNET_EVENT_ACK ||
339 ev->type == LNET_EVENT_UNLINK);
341 if (!rs->rs_difficult) {
342 /* 'Easy' replies have no further processing so I drop the
343 * net's ref on 'rs' */
344 LASSERT (ev->unlinked);
345 ptlrpc_rs_decref(rs);
350 LASSERT (rs->rs_on_net);
353 /* Last network callback. The net's ref on 'rs' stays put
354 * until ptlrpc_handle_rs() is done with it */
355 cfs_spin_lock(&svc->srv_rs_lock);
356 cfs_spin_lock(&rs->rs_lock);
358 if (!rs->rs_no_ack ||
359 rs->rs_transno <= rs->rs_export->exp_obd->obd_last_committed)
360 ptlrpc_schedule_difficult_reply (rs);
361 cfs_spin_unlock(&rs->rs_lock);
362 cfs_spin_unlock(&svc->srv_rs_lock);
368 #ifdef HAVE_SERVER_SUPPORT
370 * Server's bulk completion callback
372 void server_bulk_callback (lnet_event_t *ev)
374 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
375 struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
378 LASSERT (ev->type == LNET_EVENT_SEND ||
379 ev->type == LNET_EVENT_UNLINK ||
380 (desc->bd_type == BULK_PUT_SOURCE &&
381 ev->type == LNET_EVENT_ACK) ||
382 (desc->bd_type == BULK_GET_SINK &&
383 ev->type == LNET_EVENT_REPLY));
385 CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
386 "event type %d, status %d, desc %p\n",
387 ev->type, ev->status, desc);
389 cfs_spin_lock(&desc->bd_lock);
391 if ((ev->type == LNET_EVENT_ACK ||
392 ev->type == LNET_EVENT_REPLY) &&
394 /* We heard back from the peer, so even if we get this
395 * before the SENT event (oh yes we can), we know we
396 * read/wrote the peer buffer and how much... */
397 desc->bd_success = 1;
398 desc->bd_nob_transferred = ev->mlength;
399 desc->bd_sender = ev->sender;
403 /* This is the last callback no matter what... */
404 desc->bd_network_rw = 0;
405 cfs_waitq_signal(&desc->bd_waitq);
408 cfs_spin_unlock(&desc->bd_lock);
413 static void ptlrpc_master_callback(lnet_event_t *ev)
415 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
416 void (*callback)(lnet_event_t *ev) = cbid->cbid_fn;
418 /* Honestly, it's best to find out early. */
419 LASSERT (cbid->cbid_arg != LP_POISON);
420 LASSERT (callback == request_out_callback ||
421 callback == reply_in_callback ||
422 callback == client_bulk_callback ||
423 callback == request_in_callback ||
424 callback == reply_out_callback
425 #ifdef HAVE_SERVER_SUPPORT
426 || callback == server_bulk_callback
433 int ptlrpc_uuid_to_peer (struct obd_uuid *uuid,
434 lnet_process_id_t *peer, lnet_nid_t *self)
437 __u32 best_order = 0;
440 int portals_compatibility;
446 portals_compatibility = LNetCtl(IOC_LIBCFS_PORTALS_COMPATIBILITY, NULL);
448 peer->pid = LUSTRE_SRV_LNET_PID;
450 /* Choose the matching UUID that's closest */
451 while (lustre_uuid_to_peer(uuid->uuid, &dst_nid, count++) == 0) {
452 dist = LNetDist(dst_nid, &src_nid, &order);
456 if (dist == 0) { /* local! use loopback LND */
457 peer->nid = *self = LNET_MKNID(LNET_MKNET(LOLND, 0), 0);
464 (dist == best_dist && order < best_order)) {
468 if (portals_compatibility > 1) {
469 /* Strong portals compatibility: Zero the nid's
470 * NET, so if I'm reading new config logs, or
471 * getting configured by (new) lconf I can
472 * still talk to old servers. */
473 dst_nid = LNET_MKNID(0, LNET_NIDADDR(dst_nid));
474 src_nid = LNET_MKNID(0, LNET_NIDADDR(src_nid));
482 CDEBUG(D_NET,"%s->%s\n", uuid->uuid, libcfs_id2str(*peer));
486 void ptlrpc_ni_fini(void)
489 struct l_wait_info lwi;
493 /* Wait for the event queue to become idle since there may still be
494 * messages in flight with pending events (i.e. the fire-and-forget
495 * messages == client requests and "non-difficult" server
498 for (retries = 0;; retries++) {
499 rc = LNetEQFree(ptlrpc_eq_h);
510 CWARN("Event queue still busy\n");
513 cfs_waitq_init(&waitq);
514 lwi = LWI_TIMEOUT(cfs_time_seconds(2), NULL, NULL);
515 l_wait_event(waitq, 0, &lwi);
522 lnet_pid_t ptl_get_pid(void)
529 pid = LUSTRE_SRV_LNET_PID;
534 int ptlrpc_ni_init(void)
540 CDEBUG(D_NET, "My pid is: %x\n", pid);
542 /* We're not passing any limits yet... */
543 rc = LNetNIInit(pid);
545 CDEBUG (D_NET, "Can't init network interface: %d\n", rc);
549 /* CAVEAT EMPTOR: how we process portals events is _radically_
550 * different depending on... */
552 /* kernel portals calls our master callback when events are added to
553 * the event queue. In fact lustre never pulls events off this queue,
554 * so it's only sized for some debug history. */
555 rc = LNetEQAlloc(1024, ptlrpc_master_callback, &ptlrpc_eq_h);
557 /* liblustre calls the master callback when it removes events from the
558 * event queue. The event queue has to be big enough not to drop
560 rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &ptlrpc_eq_h);
565 CERROR ("Failed to allocate event queue: %d\n", rc);
572 CFS_LIST_HEAD(liblustre_wait_callbacks);
573 CFS_LIST_HEAD(liblustre_idle_callbacks);
574 void *liblustre_services_callback;
577 liblustre_register_waitidle_callback (cfs_list_t *callback_list,
579 int (*fn)(void *arg), void *arg)
581 struct liblustre_wait_callback *llwc;
583 OBD_ALLOC(llwc, sizeof(*llwc));
584 LASSERT (llwc != NULL);
586 llwc->llwc_name = name;
588 llwc->llwc_arg = arg;
589 cfs_list_add_tail(&llwc->llwc_list, callback_list);
595 liblustre_deregister_waitidle_callback (void *opaque)
597 struct liblustre_wait_callback *llwc = opaque;
599 cfs_list_del(&llwc->llwc_list);
600 OBD_FREE(llwc, sizeof(*llwc));
604 liblustre_register_wait_callback (const char *name,
605 int (*fn)(void *arg), void *arg)
607 return liblustre_register_waitidle_callback(&liblustre_wait_callbacks,
612 liblustre_deregister_wait_callback (void *opaque)
614 liblustre_deregister_waitidle_callback(opaque);
618 liblustre_register_idle_callback (const char *name,
619 int (*fn)(void *arg), void *arg)
621 return liblustre_register_waitidle_callback(&liblustre_idle_callbacks,
626 liblustre_deregister_idle_callback (void *opaque)
628 liblustre_deregister_waitidle_callback(opaque);
632 liblustre_check_events (int timeout)
639 rc = LNetEQPoll(&ptlrpc_eq_h, 1, timeout * 1000, &ev, &i);
643 LASSERT (rc == -EOVERFLOW || rc == 1);
645 /* liblustre: no asynch callback so we can't afford to miss any
647 if (rc == -EOVERFLOW) {
648 CERROR ("Dropped an event!!!\n");
652 ptlrpc_master_callback (&ev);
656 int liblustre_waiting = 0;
659 liblustre_wait_event (int timeout)
662 struct liblustre_wait_callback *llwc;
663 int found_something = 0;
665 /* single threaded recursion check... */
666 liblustre_waiting = 1;
669 /* Deal with all pending events */
670 while (liblustre_check_events(0))
673 /* Give all registered callbacks a bite at the cherry */
674 cfs_list_for_each(tmp, &liblustre_wait_callbacks) {
675 llwc = cfs_list_entry(tmp,
676 struct liblustre_wait_callback,
679 if (llwc->llwc_fn(llwc->llwc_arg))
683 if (found_something || timeout == 0)
686 /* Nothing so far, but I'm allowed to block... */
687 found_something = liblustre_check_events(timeout);
688 if (!found_something) /* still nothing */
689 break; /* I timed out */
692 liblustre_waiting = 0;
694 return found_something;
698 liblustre_wait_idle(void)
700 static int recursed = 0;
703 struct liblustre_wait_callback *llwc;
710 liblustre_wait_event(0);
714 cfs_list_for_each(tmp, &liblustre_idle_callbacks) {
715 llwc = cfs_list_entry(tmp,
716 struct liblustre_wait_callback,
719 if (!llwc->llwc_fn(llwc->llwc_arg)) {
730 #endif /* __KERNEL__ */
732 int ptlrpc_init_portals(void)
734 int rc = ptlrpc_ni_init();
737 CERROR("network initialisation failed\n");
741 liblustre_services_callback =
742 liblustre_register_wait_callback("liblustre_check_services",
743 &liblustre_check_services,
745 cfs_init_completion_module(liblustre_wait_event);
747 rc = ptlrpcd_addref();
751 CERROR("rpcd initialisation failed\n");
753 liblustre_deregister_wait_callback(liblustre_services_callback);
759 void ptlrpc_exit_portals(void)
762 liblustre_deregister_wait_callback(liblustre_services_callback);