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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Isaac Huang <isaac@clusterfs.com>
40 * 2012-05-13: Liang Zhen <liang@whamcloud.com>
41 * - percpt data for service to improve smp performance
45 #define DEBUG_SUBSYSTEM S_LNET
57 static struct smoketest_rpc {
58 spinlock_t rpc_glock; /* global lock */
59 srpc_service_t *rpc_services[SRPC_SERVICE_MAX_ID + 1];
60 lnet_handle_eq_t rpc_lnet_eq; /* _the_ LNet event queue */
61 srpc_state_t rpc_state;
62 srpc_counters_t rpc_counters;
63 __u64 rpc_matchbits; /* matchbits counter */
67 srpc_serv_portal(int svc_id)
69 return svc_id < SRPC_FRAMEWORK_SERVICE_MAX_ID ?
70 SRPC_FRAMEWORK_REQUEST_PORTAL : SRPC_REQUEST_PORTAL;
74 int srpc_handle_rpc (swi_workitem_t *wi);
76 void srpc_get_counters (srpc_counters_t *cnt)
78 spin_lock(&srpc_data.rpc_glock);
79 *cnt = srpc_data.rpc_counters;
80 spin_unlock(&srpc_data.rpc_glock);
83 void srpc_set_counters (const srpc_counters_t *cnt)
85 spin_lock(&srpc_data.rpc_glock);
86 srpc_data.rpc_counters = *cnt;
87 spin_unlock(&srpc_data.rpc_glock);
91 srpc_add_bulk_page(srpc_bulk_t *bk, struct page *pg, int i, int nob)
93 nob = min(nob, (int)PAGE_CACHE_SIZE);
96 LASSERT(i >= 0 && i < bk->bk_niov);
99 bk->bk_iovs[i].kiov_offset = 0;
100 bk->bk_iovs[i].kiov_page = pg;
101 bk->bk_iovs[i].kiov_len = nob;
103 LASSERT(bk->bk_pages != NULL);
105 bk->bk_pages[i] = pg;
106 bk->bk_iovs[i].iov_len = nob;
107 bk->bk_iovs[i].iov_base = page_address(pg);
113 srpc_free_bulk (srpc_bulk_t *bk)
118 LASSERT (bk != NULL);
120 LASSERT (bk->bk_pages != NULL);
123 for (i = 0; i < bk->bk_niov; i++) {
125 pg = bk->bk_iovs[i].kiov_page;
127 pg = bk->bk_pages[i];
129 if (pg == NULL) break;
135 LIBCFS_FREE(bk->bk_pages, sizeof(struct page *) * bk->bk_niov);
137 LIBCFS_FREE(bk, offsetof(srpc_bulk_t, bk_iovs[bk->bk_niov]));
142 srpc_alloc_bulk(int cpt, unsigned bulk_npg, unsigned bulk_len, int sink)
147 LASSERT(bulk_npg > 0 && bulk_npg <= LNET_MAX_IOV);
149 LIBCFS_CPT_ALLOC(bk, lnet_cpt_table(), cpt,
150 offsetof(srpc_bulk_t, bk_iovs[bulk_npg]));
152 CERROR("Can't allocate descriptor for %d pages\n", bulk_npg);
156 memset(bk, 0, offsetof(srpc_bulk_t, bk_iovs[bulk_npg]));
158 bk->bk_len = bulk_len;
159 bk->bk_niov = bulk_npg;
164 LIBCFS_CPT_ALLOC(pages, lnet_cpt_table(), cpt,
165 sizeof(struct page *) * bulk_npg);
167 LIBCFS_FREE(bk, offsetof(srpc_bulk_t,
169 CERROR("Can't allocate page array for %d pages\n",
174 memset(pages, 0, sizeof(struct page *) * bulk_npg);
175 bk->bk_pages = pages;
179 for (i = 0; i < bulk_npg; i++) {
183 pg = cfs_page_cpt_alloc(lnet_cpt_table(), cpt, GFP_IOFS);
185 CERROR("Can't allocate page %d of %d\n", i, bulk_npg);
190 nob = srpc_add_bulk_page(bk, pg, i, bulk_len);
202 spin_lock(&srpc_data.rpc_glock);
203 id = srpc_data.rpc_matchbits++;
204 spin_unlock(&srpc_data.rpc_glock);
209 srpc_init_server_rpc(struct srpc_server_rpc *rpc,
210 struct srpc_service_cd *scd,
211 struct srpc_buffer *buffer)
213 memset(rpc, 0, sizeof(*rpc));
214 swi_init_workitem(&rpc->srpc_wi, rpc, srpc_handle_rpc,
215 srpc_serv_is_framework(scd->scd_svc) ?
216 lst_sched_serial : lst_sched_test[scd->scd_cpt]);
218 rpc->srpc_ev.ev_fired = 1; /* no event expected now */
221 rpc->srpc_reqstbuf = buffer;
222 rpc->srpc_peer = buffer->buf_peer;
223 rpc->srpc_self = buffer->buf_self;
224 LNetInvalidateHandle(&rpc->srpc_replymdh);
228 srpc_service_fini(struct srpc_service *svc)
230 struct srpc_service_cd *scd;
231 struct srpc_server_rpc *rpc;
232 struct srpc_buffer *buf;
236 if (svc->sv_cpt_data == NULL)
239 cfs_percpt_for_each(scd, i, svc->sv_cpt_data) {
241 if (!list_empty(&scd->scd_buf_posted))
242 q = &scd->scd_buf_posted;
243 else if (!list_empty(&scd->scd_buf_blocked))
244 q = &scd->scd_buf_blocked;
248 while (!list_empty(q)) {
249 buf = list_entry(q->next,
252 list_del(&buf->buf_list);
253 LIBCFS_FREE(buf, sizeof(*buf));
257 LASSERT(list_empty(&scd->scd_rpc_active));
259 while (!list_empty(&scd->scd_rpc_free)) {
260 rpc = list_entry(scd->scd_rpc_free.next,
261 struct srpc_server_rpc,
263 list_del(&rpc->srpc_list);
264 LIBCFS_FREE(rpc, sizeof(*rpc));
268 cfs_percpt_free(svc->sv_cpt_data);
269 svc->sv_cpt_data = NULL;
273 srpc_service_nrpcs(struct srpc_service *svc)
275 int nrpcs = svc->sv_wi_total / svc->sv_ncpts;
277 return srpc_serv_is_framework(svc) ?
278 max(nrpcs, SFW_FRWK_WI_MIN) : max(nrpcs, SFW_TEST_WI_MIN);
281 int srpc_add_buffer(struct swi_workitem *wi);
284 srpc_service_init(struct srpc_service *svc)
286 struct srpc_service_cd *scd;
287 struct srpc_server_rpc *rpc;
292 svc->sv_shuttingdown = 0;
294 svc->sv_cpt_data = cfs_percpt_alloc(lnet_cpt_table(),
295 sizeof(struct srpc_service_cd));
296 if (svc->sv_cpt_data == NULL)
299 svc->sv_ncpts = srpc_serv_is_framework(svc) ?
300 1 : cfs_cpt_number(lnet_cpt_table());
301 nrpcs = srpc_service_nrpcs(svc);
303 cfs_percpt_for_each(scd, i, svc->sv_cpt_data) {
306 spin_lock_init(&scd->scd_lock);
307 INIT_LIST_HEAD(&scd->scd_rpc_free);
308 INIT_LIST_HEAD(&scd->scd_rpc_active);
309 INIT_LIST_HEAD(&scd->scd_buf_posted);
310 INIT_LIST_HEAD(&scd->scd_buf_blocked);
312 scd->scd_ev.ev_data = scd;
313 scd->scd_ev.ev_type = SRPC_REQUEST_RCVD;
315 /* NB: don't use lst_sched_serial for adding buffer,
316 * see details in srpc_service_add_buffers() */
317 swi_init_workitem(&scd->scd_buf_wi, scd,
318 srpc_add_buffer, lst_sched_test[i]);
320 if (i != 0 && srpc_serv_is_framework(svc)) {
321 /* NB: framework service only needs srpc_service_cd for
322 * one partition, but we allocate for all to make
323 * it easier to implement, it will waste a little
324 * memory but nobody should care about this */
328 for (j = 0; j < nrpcs; j++) {
329 LIBCFS_CPT_ALLOC(rpc, lnet_cpt_table(),
332 srpc_service_fini(svc);
335 list_add(&rpc->srpc_list, &scd->scd_rpc_free);
343 srpc_add_service(struct srpc_service *sv)
347 LASSERT(0 <= id && id <= SRPC_SERVICE_MAX_ID);
349 if (srpc_service_init(sv) != 0)
352 spin_lock(&srpc_data.rpc_glock);
354 LASSERT(srpc_data.rpc_state == SRPC_STATE_RUNNING);
356 if (srpc_data.rpc_services[id] != NULL) {
357 spin_unlock(&srpc_data.rpc_glock);
361 srpc_data.rpc_services[id] = sv;
362 spin_unlock(&srpc_data.rpc_glock);
364 CDEBUG(D_NET, "Adding service: id %d, name %s\n", id, sv->sv_name);
368 srpc_service_fini(sv);
373 srpc_remove_service (srpc_service_t *sv)
377 spin_lock(&srpc_data.rpc_glock);
379 if (srpc_data.rpc_services[id] != sv) {
380 spin_unlock(&srpc_data.rpc_glock);
384 srpc_data.rpc_services[id] = NULL;
385 spin_unlock(&srpc_data.rpc_glock);
390 srpc_post_passive_rdma(int portal, int local, __u64 matchbits, void *buf,
391 int len, int options, lnet_process_id_t peer,
392 lnet_handle_md_t *mdh, srpc_event_t *ev)
396 lnet_handle_me_t meh;
398 rc = LNetMEAttach(portal, peer, matchbits, 0, LNET_UNLINK,
399 local ? LNET_INS_LOCAL : LNET_INS_AFTER, &meh);
401 CERROR ("LNetMEAttach failed: %d\n", rc);
402 LASSERT (rc == -ENOMEM);
410 md.options = options;
411 md.eq_handle = srpc_data.rpc_lnet_eq;
413 rc = LNetMDAttach(meh, md, LNET_UNLINK, mdh);
415 CERROR ("LNetMDAttach failed: %d\n", rc);
416 LASSERT (rc == -ENOMEM);
418 rc = LNetMEUnlink(meh);
424 "Posted passive RDMA: peer %s, portal %d, matchbits "LPX64"\n",
425 libcfs_id2str(peer), portal, matchbits);
430 srpc_post_active_rdma(int portal, __u64 matchbits, void *buf, int len,
431 int options, lnet_process_id_t peer, lnet_nid_t self,
432 lnet_handle_md_t *mdh, srpc_event_t *ev)
440 md.eq_handle = srpc_data.rpc_lnet_eq;
441 md.threshold = ((options & LNET_MD_OP_GET) != 0) ? 2 : 1;
442 md.options = options & ~(LNET_MD_OP_PUT | LNET_MD_OP_GET);
444 rc = LNetMDBind(md, LNET_UNLINK, mdh);
446 CERROR ("LNetMDBind failed: %d\n", rc);
447 LASSERT (rc == -ENOMEM);
451 /* this is kind of an abuse of the LNET_MD_OP_{PUT,GET} options.
452 * they're only meaningful for MDs attached to an ME (i.e. passive
454 if ((options & LNET_MD_OP_PUT) != 0) {
455 rc = LNetPut(self, *mdh, LNET_NOACK_REQ, peer,
456 portal, matchbits, 0, 0);
458 LASSERT ((options & LNET_MD_OP_GET) != 0);
460 rc = LNetGet(self, *mdh, peer, portal, matchbits, 0);
464 CERROR ("LNet%s(%s, %d, "LPD64") failed: %d\n",
465 ((options & LNET_MD_OP_PUT) != 0) ? "Put" : "Get",
466 libcfs_id2str(peer), portal, matchbits, rc);
468 /* The forthcoming unlink event will complete this operation
469 * with failure, so fall through and return success here.
471 rc = LNetMDUnlink(*mdh);
475 "Posted active RDMA: peer %s, portal %u, matchbits "LPX64"\n",
476 libcfs_id2str(peer), portal, matchbits);
482 srpc_post_active_rqtbuf(lnet_process_id_t peer, int service, void *buf,
483 int len, lnet_handle_md_t *mdh, srpc_event_t *ev)
485 return srpc_post_active_rdma(srpc_serv_portal(service), service,
486 buf, len, LNET_MD_OP_PUT, peer,
487 LNET_NID_ANY, mdh, ev);
491 srpc_post_passive_rqtbuf(int service, int local, void *buf, int len,
492 lnet_handle_md_t *mdh, srpc_event_t *ev)
494 lnet_process_id_t any = {0};
496 any.nid = LNET_NID_ANY;
497 any.pid = LNET_PID_ANY;
499 return srpc_post_passive_rdma(srpc_serv_portal(service),
500 local, service, buf, len,
501 LNET_MD_OP_PUT, any, mdh, ev);
505 srpc_service_post_buffer(struct srpc_service_cd *scd, struct srpc_buffer *buf)
506 __must_hold(&scd->scd_lock)
508 struct srpc_service *sv = scd->scd_svc;
509 struct srpc_msg *msg = &buf->buf_msg;
512 LNetInvalidateHandle(&buf->buf_mdh);
513 list_add(&buf->buf_list, &scd->scd_buf_posted);
514 scd->scd_buf_nposted++;
515 spin_unlock(&scd->scd_lock);
517 rc = srpc_post_passive_rqtbuf(sv->sv_id,
518 !srpc_serv_is_framework(sv),
519 msg, sizeof(*msg), &buf->buf_mdh,
522 /* At this point, a RPC (new or delayed) may have arrived in
523 * msg and its event handler has been called. So we must add
524 * buf to scd_buf_posted _before_ dropping scd_lock */
526 spin_lock(&scd->scd_lock);
529 if (!sv->sv_shuttingdown)
532 spin_unlock(&scd->scd_lock);
533 /* srpc_shutdown_service might have tried to unlink me
534 * when my buf_mdh was still invalid */
535 LNetMDUnlink(buf->buf_mdh);
536 spin_lock(&scd->scd_lock);
540 scd->scd_buf_nposted--;
541 if (sv->sv_shuttingdown)
542 return rc; /* don't allow to change scd_buf_posted */
544 list_del(&buf->buf_list);
545 spin_unlock(&scd->scd_lock);
547 LIBCFS_FREE(buf, sizeof(*buf));
549 spin_lock(&scd->scd_lock);
554 srpc_add_buffer(struct swi_workitem *wi)
556 struct srpc_service_cd *scd = wi->swi_workitem.wi_data;
557 struct srpc_buffer *buf;
560 /* it's called by workitem scheduler threads, these threads
561 * should have been set CPT affinity, so buffers will be posted
562 * on CPT local list of Portal */
563 spin_lock(&scd->scd_lock);
565 while (scd->scd_buf_adjust > 0 &&
566 !scd->scd_svc->sv_shuttingdown) {
567 scd->scd_buf_adjust--; /* consume it */
568 scd->scd_buf_posting++;
570 spin_unlock(&scd->scd_lock);
572 LIBCFS_ALLOC(buf, sizeof(*buf));
574 CERROR("Failed to add new buf to service: %s\n",
575 scd->scd_svc->sv_name);
576 spin_lock(&scd->scd_lock);
581 spin_lock(&scd->scd_lock);
582 if (scd->scd_svc->sv_shuttingdown) {
583 spin_unlock(&scd->scd_lock);
584 LIBCFS_FREE(buf, sizeof(*buf));
586 spin_lock(&scd->scd_lock);
591 rc = srpc_service_post_buffer(scd, buf);
593 break; /* buf has been freed inside */
595 LASSERT(scd->scd_buf_posting > 0);
596 scd->scd_buf_posting--;
597 scd->scd_buf_total++;
598 scd->scd_buf_low = MAX(2, scd->scd_buf_total / 4);
602 scd->scd_buf_err_stamp = cfs_time_current_sec();
603 scd->scd_buf_err = rc;
605 LASSERT(scd->scd_buf_posting > 0);
606 scd->scd_buf_posting--;
609 spin_unlock(&scd->scd_lock);
614 srpc_service_add_buffers(struct srpc_service *sv, int nbuffer)
616 struct srpc_service_cd *scd;
620 LASSERTF(nbuffer > 0, "nbuffer must be positive: %d\n", nbuffer);
622 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
623 spin_lock(&scd->scd_lock);
625 scd->scd_buf_err = 0;
626 scd->scd_buf_err_stamp = 0;
627 scd->scd_buf_posting = 0;
628 scd->scd_buf_adjust = nbuffer;
629 /* start to post buffers */
630 swi_schedule_workitem(&scd->scd_buf_wi);
631 spin_unlock(&scd->scd_lock);
633 /* framework service only post buffer for one partition */
634 if (srpc_serv_is_framework(sv))
638 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
639 spin_lock(&scd->scd_lock);
641 * NB: srpc_service_add_buffers() can be called inside
642 * thread context of lst_sched_serial, and we don't normally
643 * allow to sleep inside thread context of WI scheduler
644 * because it will block current scheduler thread from doing
645 * anything else, even worse, it could deadlock if it's
646 * waiting on result from another WI of the same scheduler.
647 * However, it's safe at here because scd_buf_wi is scheduled
648 * by thread in a different WI scheduler (lst_sched_test),
649 * so we don't have any risk of deadlock, though this could
650 * block all WIs pending on lst_sched_serial for a moment
651 * which is not good but not fatal.
653 lst_wait_until(scd->scd_buf_err != 0 ||
654 (scd->scd_buf_adjust == 0 &&
655 scd->scd_buf_posting == 0),
656 scd->scd_lock, "waiting for adding buffer\n");
658 if (scd->scd_buf_err != 0 && rc == 0)
659 rc = scd->scd_buf_err;
661 spin_unlock(&scd->scd_lock);
668 srpc_service_remove_buffers(struct srpc_service *sv, int nbuffer)
670 struct srpc_service_cd *scd;
674 LASSERT(!sv->sv_shuttingdown);
676 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
677 spin_lock(&scd->scd_lock);
679 num = scd->scd_buf_total + scd->scd_buf_posting;
680 scd->scd_buf_adjust -= min(nbuffer, num);
682 spin_unlock(&scd->scd_lock);
686 /* returns 1 if sv has finished, otherwise 0 */
688 srpc_finish_service(struct srpc_service *sv)
690 struct srpc_service_cd *scd;
691 struct srpc_server_rpc *rpc;
694 LASSERT(sv->sv_shuttingdown); /* srpc_shutdown_service called */
696 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
697 spin_lock(&scd->scd_lock);
698 if (!swi_deschedule_workitem(&scd->scd_buf_wi)) {
699 spin_unlock(&scd->scd_lock);
703 if (scd->scd_buf_nposted > 0) {
704 CDEBUG(D_NET, "waiting for %d posted buffers to "
705 "unlink\n", scd->scd_buf_nposted);
706 spin_unlock(&scd->scd_lock);
710 if (list_empty(&scd->scd_rpc_active)) {
711 spin_unlock(&scd->scd_lock);
715 rpc = list_entry(scd->scd_rpc_active.next,
716 struct srpc_server_rpc, srpc_list);
717 CNETERR("Active RPC %p on shutdown: sv %s, peer %s, "
718 "wi %s scheduled %d running %d, "
719 "ev fired %d type %d status %d lnet %d\n",
720 rpc, sv->sv_name, libcfs_id2str(rpc->srpc_peer),
721 swi_state2str(rpc->srpc_wi.swi_state),
722 rpc->srpc_wi.swi_workitem.wi_scheduled,
723 rpc->srpc_wi.swi_workitem.wi_running,
724 rpc->srpc_ev.ev_fired, rpc->srpc_ev.ev_type,
725 rpc->srpc_ev.ev_status, rpc->srpc_ev.ev_lnet);
726 spin_unlock(&scd->scd_lock);
730 /* no lock needed from now on */
731 srpc_service_fini(sv);
735 /* called with sv->sv_lock held */
737 srpc_service_recycle_buffer(struct srpc_service_cd *scd, srpc_buffer_t *buf)
738 __must_hold(&scd->scd_lock)
740 if (!scd->scd_svc->sv_shuttingdown && scd->scd_buf_adjust >= 0) {
741 if (srpc_service_post_buffer(scd, buf) != 0) {
742 CWARN("Failed to post %s buffer\n",
743 scd->scd_svc->sv_name);
748 /* service is shutting down, or we want to recycle some buffers */
749 scd->scd_buf_total--;
751 if (scd->scd_buf_adjust < 0) {
752 scd->scd_buf_adjust++;
753 if (scd->scd_buf_adjust < 0 &&
754 scd->scd_buf_total == 0 && scd->scd_buf_posting == 0) {
756 "Try to recyle %d buffers but nothing left\n",
757 scd->scd_buf_adjust);
758 scd->scd_buf_adjust = 0;
762 spin_unlock(&scd->scd_lock);
763 LIBCFS_FREE(buf, sizeof(*buf));
764 spin_lock(&scd->scd_lock);
768 srpc_abort_service(struct srpc_service *sv)
770 struct srpc_service_cd *scd;
771 struct srpc_server_rpc *rpc;
774 CDEBUG(D_NET, "Aborting service: id %d, name %s\n",
775 sv->sv_id, sv->sv_name);
777 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
778 spin_lock(&scd->scd_lock);
780 /* schedule in-flight RPCs to notice the abort, NB:
781 * racing with incoming RPCs; complete fix should make test
782 * RPCs carry session ID in its headers */
783 list_for_each_entry(rpc, &scd->scd_rpc_active, srpc_list) {
784 rpc->srpc_aborted = 1;
785 swi_schedule_workitem(&rpc->srpc_wi);
788 spin_unlock(&scd->scd_lock);
793 srpc_shutdown_service(srpc_service_t *sv)
795 struct srpc_service_cd *scd;
796 struct srpc_server_rpc *rpc;
800 CDEBUG(D_NET, "Shutting down service: id %d, name %s\n",
801 sv->sv_id, sv->sv_name);
803 cfs_percpt_for_each(scd, i, sv->sv_cpt_data)
804 spin_lock(&scd->scd_lock);
806 sv->sv_shuttingdown = 1; /* i.e. no new active RPC */
808 cfs_percpt_for_each(scd, i, sv->sv_cpt_data)
809 spin_unlock(&scd->scd_lock);
811 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
812 spin_lock(&scd->scd_lock);
814 /* schedule in-flight RPCs to notice the shutdown */
815 list_for_each_entry(rpc, &scd->scd_rpc_active, srpc_list)
816 swi_schedule_workitem(&rpc->srpc_wi);
818 spin_unlock(&scd->scd_lock);
820 /* OK to traverse scd_buf_posted without lock, since no one
821 * touches scd_buf_posted now */
822 list_for_each_entry(buf, &scd->scd_buf_posted, buf_list)
823 LNetMDUnlink(buf->buf_mdh);
828 srpc_send_request (srpc_client_rpc_t *rpc)
830 srpc_event_t *ev = &rpc->crpc_reqstev;
835 ev->ev_type = SRPC_REQUEST_SENT;
837 rc = srpc_post_active_rqtbuf(rpc->crpc_dest, rpc->crpc_service,
838 &rpc->crpc_reqstmsg, sizeof(srpc_msg_t),
839 &rpc->crpc_reqstmdh, ev);
841 LASSERT (rc == -ENOMEM);
842 ev->ev_fired = 1; /* no more event expected */
848 srpc_prepare_reply (srpc_client_rpc_t *rpc)
850 srpc_event_t *ev = &rpc->crpc_replyev;
851 __u64 *id = &rpc->crpc_reqstmsg.msg_body.reqst.rpyid;
856 ev->ev_type = SRPC_REPLY_RCVD;
858 *id = srpc_next_id();
860 rc = srpc_post_passive_rdma(SRPC_RDMA_PORTAL, 0, *id,
861 &rpc->crpc_replymsg, sizeof(srpc_msg_t),
862 LNET_MD_OP_PUT, rpc->crpc_dest,
863 &rpc->crpc_replymdh, ev);
865 LASSERT (rc == -ENOMEM);
866 ev->ev_fired = 1; /* no more event expected */
872 srpc_prepare_bulk (srpc_client_rpc_t *rpc)
874 srpc_bulk_t *bk = &rpc->crpc_bulk;
875 srpc_event_t *ev = &rpc->crpc_bulkev;
876 __u64 *id = &rpc->crpc_reqstmsg.msg_body.reqst.bulkid;
880 LASSERT (bk->bk_niov <= LNET_MAX_IOV);
882 if (bk->bk_niov == 0) return 0; /* nothing to do */
884 opt = bk->bk_sink ? LNET_MD_OP_PUT : LNET_MD_OP_GET;
888 opt |= LNET_MD_IOVEC;
893 ev->ev_type = SRPC_BULK_REQ_RCVD;
895 *id = srpc_next_id();
897 rc = srpc_post_passive_rdma(SRPC_RDMA_PORTAL, 0, *id,
898 &bk->bk_iovs[0], bk->bk_niov, opt,
899 rpc->crpc_dest, &bk->bk_mdh, ev);
901 LASSERT (rc == -ENOMEM);
902 ev->ev_fired = 1; /* no more event expected */
908 srpc_do_bulk (srpc_server_rpc_t *rpc)
910 srpc_event_t *ev = &rpc->srpc_ev;
911 srpc_bulk_t *bk = rpc->srpc_bulk;
912 __u64 id = rpc->srpc_reqstbuf->buf_msg.msg_body.reqst.bulkid;
916 LASSERT (bk != NULL);
918 opt = bk->bk_sink ? LNET_MD_OP_GET : LNET_MD_OP_PUT;
922 opt |= LNET_MD_IOVEC;
927 ev->ev_type = bk->bk_sink ? SRPC_BULK_GET_RPLD : SRPC_BULK_PUT_SENT;
929 rc = srpc_post_active_rdma(SRPC_RDMA_PORTAL, id,
930 &bk->bk_iovs[0], bk->bk_niov, opt,
931 rpc->srpc_peer, rpc->srpc_self,
934 ev->ev_fired = 1; /* no more event expected */
938 /* only called from srpc_handle_rpc */
940 srpc_server_rpc_done(srpc_server_rpc_t *rpc, int status)
942 struct srpc_service_cd *scd = rpc->srpc_scd;
943 struct srpc_service *sv = scd->scd_svc;
944 srpc_buffer_t *buffer;
946 LASSERT (status != 0 || rpc->srpc_wi.swi_state == SWI_STATE_DONE);
948 rpc->srpc_status = status;
950 CDEBUG_LIMIT (status == 0 ? D_NET : D_NETERROR,
951 "Server RPC %p done: service %s, peer %s, status %s:%d\n",
952 rpc, sv->sv_name, libcfs_id2str(rpc->srpc_peer),
953 swi_state2str(rpc->srpc_wi.swi_state), status);
956 spin_lock(&srpc_data.rpc_glock);
957 srpc_data.rpc_counters.rpcs_dropped++;
958 spin_unlock(&srpc_data.rpc_glock);
961 if (rpc->srpc_done != NULL)
962 (*rpc->srpc_done) (rpc);
963 LASSERT(rpc->srpc_bulk == NULL);
965 spin_lock(&scd->scd_lock);
967 if (rpc->srpc_reqstbuf != NULL) {
968 /* NB might drop sv_lock in srpc_service_recycle_buffer, but
969 * sv won't go away for scd_rpc_active must not be empty */
970 srpc_service_recycle_buffer(scd, rpc->srpc_reqstbuf);
971 rpc->srpc_reqstbuf = NULL;
974 list_del(&rpc->srpc_list); /* from scd->scd_rpc_active */
977 * No one can schedule me now since:
978 * - I'm not on scd_rpc_active.
979 * - all LNet events have been fired.
980 * Cancel pending schedules and prevent future schedule attempts:
982 LASSERT(rpc->srpc_ev.ev_fired);
983 swi_exit_workitem(&rpc->srpc_wi);
985 if (!sv->sv_shuttingdown && !list_empty(&scd->scd_buf_blocked)) {
986 buffer = list_entry(scd->scd_buf_blocked.next,
987 srpc_buffer_t, buf_list);
988 list_del(&buffer->buf_list);
990 srpc_init_server_rpc(rpc, scd, buffer);
991 list_add_tail(&rpc->srpc_list, &scd->scd_rpc_active);
992 swi_schedule_workitem(&rpc->srpc_wi);
994 list_add(&rpc->srpc_list, &scd->scd_rpc_free);
997 spin_unlock(&scd->scd_lock);
1001 /* handles an incoming RPC */
1003 srpc_handle_rpc(swi_workitem_t *wi)
1005 struct srpc_server_rpc *rpc = wi->swi_workitem.wi_data;
1006 struct srpc_service_cd *scd = rpc->srpc_scd;
1007 struct srpc_service *sv = scd->scd_svc;
1008 srpc_event_t *ev = &rpc->srpc_ev;
1011 LASSERT(wi == &rpc->srpc_wi);
1013 spin_lock(&scd->scd_lock);
1015 if (sv->sv_shuttingdown || rpc->srpc_aborted) {
1016 spin_unlock(&scd->scd_lock);
1018 if (rpc->srpc_bulk != NULL)
1019 LNetMDUnlink(rpc->srpc_bulk->bk_mdh);
1020 LNetMDUnlink(rpc->srpc_replymdh);
1022 if (ev->ev_fired) { /* no more event, OK to finish */
1023 srpc_server_rpc_done(rpc, -ESHUTDOWN);
1029 spin_unlock(&scd->scd_lock);
1031 switch (wi->swi_state) {
1034 case SWI_STATE_NEWBORN: {
1036 srpc_generic_reply_t *reply;
1038 msg = &rpc->srpc_reqstbuf->buf_msg;
1039 reply = &rpc->srpc_replymsg.msg_body.reply;
1041 if (msg->msg_magic == 0) {
1042 /* moaned already in srpc_lnet_ev_handler */
1043 srpc_server_rpc_done(rpc, EBADMSG);
1047 srpc_unpack_msg_hdr(msg);
1048 if (msg->msg_version != SRPC_MSG_VERSION) {
1049 CWARN("Version mismatch: %u, %u expected, from %s\n",
1050 msg->msg_version, SRPC_MSG_VERSION,
1051 libcfs_id2str(rpc->srpc_peer));
1052 reply->status = EPROTO;
1053 /* drop through and send reply */
1056 rc = (*sv->sv_handler)(rpc);
1057 LASSERT(reply->status == 0 || !rpc->srpc_bulk);
1059 srpc_server_rpc_done(rpc, rc);
1064 wi->swi_state = SWI_STATE_BULK_STARTED;
1066 if (rpc->srpc_bulk != NULL) {
1067 rc = srpc_do_bulk(rpc);
1069 return 0; /* wait for bulk */
1071 LASSERT (ev->ev_fired);
1075 case SWI_STATE_BULK_STARTED:
1076 LASSERT (rpc->srpc_bulk == NULL || ev->ev_fired);
1078 if (rpc->srpc_bulk != NULL) {
1081 if (sv->sv_bulk_ready != NULL)
1082 rc = (*sv->sv_bulk_ready) (rpc, rc);
1085 srpc_server_rpc_done(rpc, rc);
1090 wi->swi_state = SWI_STATE_REPLY_SUBMITTED;
1091 rc = srpc_send_reply(rpc);
1093 return 0; /* wait for reply */
1094 srpc_server_rpc_done(rpc, rc);
1097 case SWI_STATE_REPLY_SUBMITTED:
1098 if (!ev->ev_fired) {
1099 CERROR("RPC %p: bulk %p, service %d\n",
1100 rpc, rpc->srpc_bulk, sv->sv_id);
1101 CERROR("Event: status %d, type %d, lnet %d\n",
1102 ev->ev_status, ev->ev_type, ev->ev_lnet);
1103 LASSERT (ev->ev_fired);
1106 wi->swi_state = SWI_STATE_DONE;
1107 srpc_server_rpc_done(rpc, ev->ev_status);
1115 srpc_client_rpc_expired (void *data)
1117 srpc_client_rpc_t *rpc = data;
1119 CWARN ("Client RPC expired: service %d, peer %s, timeout %d.\n",
1120 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1123 spin_lock(&rpc->crpc_lock);
1125 rpc->crpc_timeout = 0;
1126 srpc_abort_rpc(rpc, -ETIMEDOUT);
1128 spin_unlock(&rpc->crpc_lock);
1130 spin_lock(&srpc_data.rpc_glock);
1131 srpc_data.rpc_counters.rpcs_expired++;
1132 spin_unlock(&srpc_data.rpc_glock);
1136 srpc_add_client_rpc_timer(srpc_client_rpc_t *rpc)
1138 stt_timer_t *timer = &rpc->crpc_timer;
1140 if (rpc->crpc_timeout == 0)
1143 INIT_LIST_HEAD(&timer->stt_list);
1144 timer->stt_data = rpc;
1145 timer->stt_func = srpc_client_rpc_expired;
1146 timer->stt_expires = cfs_time_add(rpc->crpc_timeout,
1147 cfs_time_current_sec());
1148 stt_add_timer(timer);
1153 * Called with rpc->crpc_lock held.
1155 * Upon exit the RPC expiry timer is not queued and the handler is not
1156 * running on any CPU. */
1158 srpc_del_client_rpc_timer (srpc_client_rpc_t *rpc)
1160 /* timer not planted or already exploded */
1161 if (rpc->crpc_timeout == 0)
1164 /* timer sucessfully defused */
1165 if (stt_del_timer(&rpc->crpc_timer))
1169 /* timer detonated, wait for it to explode */
1170 while (rpc->crpc_timeout != 0) {
1171 spin_unlock(&rpc->crpc_lock);
1175 spin_lock(&rpc->crpc_lock);
1178 LBUG(); /* impossible in single-threaded runtime */
1183 srpc_client_rpc_done (srpc_client_rpc_t *rpc, int status)
1185 swi_workitem_t *wi = &rpc->crpc_wi;
1187 LASSERT(status != 0 || wi->swi_state == SWI_STATE_DONE);
1189 spin_lock(&rpc->crpc_lock);
1191 rpc->crpc_closed = 1;
1192 if (rpc->crpc_status == 0)
1193 rpc->crpc_status = status;
1195 srpc_del_client_rpc_timer(rpc);
1197 CDEBUG_LIMIT ((status == 0) ? D_NET : D_NETERROR,
1198 "Client RPC done: service %d, peer %s, status %s:%d:%d\n",
1199 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1200 swi_state2str(wi->swi_state), rpc->crpc_aborted, status);
1203 * No one can schedule me now since:
1204 * - RPC timer has been defused.
1205 * - all LNet events have been fired.
1206 * - crpc_closed has been set, preventing srpc_abort_rpc from
1208 * Cancel pending schedules and prevent future schedule attempts:
1210 LASSERT (!srpc_event_pending(rpc));
1211 swi_exit_workitem(wi);
1213 spin_unlock(&rpc->crpc_lock);
1215 (*rpc->crpc_done)(rpc);
1219 /* sends an outgoing RPC */
1221 srpc_send_rpc (swi_workitem_t *wi)
1224 srpc_client_rpc_t *rpc;
1228 LASSERT(wi != NULL);
1230 rpc = wi->swi_workitem.wi_data;
1232 LASSERT (rpc != NULL);
1233 LASSERT (wi == &rpc->crpc_wi);
1235 reply = &rpc->crpc_replymsg;
1236 do_bulk = rpc->crpc_bulk.bk_niov > 0;
1238 spin_lock(&rpc->crpc_lock);
1240 if (rpc->crpc_aborted) {
1241 spin_unlock(&rpc->crpc_lock);
1245 spin_unlock(&rpc->crpc_lock);
1247 switch (wi->swi_state) {
1250 case SWI_STATE_NEWBORN:
1251 LASSERT (!srpc_event_pending(rpc));
1253 rc = srpc_prepare_reply(rpc);
1255 srpc_client_rpc_done(rpc, rc);
1259 rc = srpc_prepare_bulk(rpc);
1262 wi->swi_state = SWI_STATE_REQUEST_SUBMITTED;
1263 rc = srpc_send_request(rpc);
1266 case SWI_STATE_REQUEST_SUBMITTED:
1267 /* CAVEAT EMPTOR: rqtev, rpyev, and bulkev may come in any
1268 * order; however, they're processed in a strict order:
1269 * rqt, rpy, and bulk. */
1270 if (!rpc->crpc_reqstev.ev_fired) break;
1272 rc = rpc->crpc_reqstev.ev_status;
1275 wi->swi_state = SWI_STATE_REQUEST_SENT;
1276 /* perhaps more events, fall thru */
1277 case SWI_STATE_REQUEST_SENT: {
1278 srpc_msg_type_t type = srpc_service2reply(rpc->crpc_service);
1280 if (!rpc->crpc_replyev.ev_fired) break;
1282 rc = rpc->crpc_replyev.ev_status;
1285 srpc_unpack_msg_hdr(reply);
1286 if (reply->msg_type != type ||
1287 (reply->msg_magic != SRPC_MSG_MAGIC &&
1288 reply->msg_magic != __swab32(SRPC_MSG_MAGIC))) {
1289 CWARN ("Bad message from %s: type %u (%d expected),"
1290 " magic %u (%d expected).\n",
1291 libcfs_id2str(rpc->crpc_dest),
1292 reply->msg_type, type,
1293 reply->msg_magic, SRPC_MSG_MAGIC);
1298 if (do_bulk && reply->msg_body.reply.status != 0) {
1299 CWARN ("Remote error %d at %s, unlink bulk buffer in "
1300 "case peer didn't initiate bulk transfer\n",
1301 reply->msg_body.reply.status,
1302 libcfs_id2str(rpc->crpc_dest));
1303 LNetMDUnlink(rpc->crpc_bulk.bk_mdh);
1306 wi->swi_state = SWI_STATE_REPLY_RECEIVED;
1308 case SWI_STATE_REPLY_RECEIVED:
1309 if (do_bulk && !rpc->crpc_bulkev.ev_fired) break;
1311 rc = do_bulk ? rpc->crpc_bulkev.ev_status : 0;
1313 /* Bulk buffer was unlinked due to remote error. Clear error
1314 * since reply buffer still contains valid data.
1315 * NB rpc->crpc_done shouldn't look into bulk data in case of
1317 if (do_bulk && rpc->crpc_bulkev.ev_lnet == LNET_EVENT_UNLINK &&
1318 rpc->crpc_status == 0 && reply->msg_body.reply.status != 0)
1321 wi->swi_state = SWI_STATE_DONE;
1322 srpc_client_rpc_done(rpc, rc);
1327 spin_lock(&rpc->crpc_lock);
1328 srpc_abort_rpc(rpc, rc);
1329 spin_unlock(&rpc->crpc_lock);
1333 if (rpc->crpc_aborted) {
1334 LNetMDUnlink(rpc->crpc_reqstmdh);
1335 LNetMDUnlink(rpc->crpc_replymdh);
1336 LNetMDUnlink(rpc->crpc_bulk.bk_mdh);
1338 if (!srpc_event_pending(rpc)) {
1339 srpc_client_rpc_done(rpc, -EINTR);
1347 srpc_create_client_rpc (lnet_process_id_t peer, int service,
1348 int nbulkiov, int bulklen,
1349 void (*rpc_done)(srpc_client_rpc_t *),
1350 void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
1352 srpc_client_rpc_t *rpc;
1354 LIBCFS_ALLOC(rpc, offsetof(srpc_client_rpc_t,
1355 crpc_bulk.bk_iovs[nbulkiov]));
1359 srpc_init_client_rpc(rpc, peer, service, nbulkiov,
1360 bulklen, rpc_done, rpc_fini, priv);
1364 /* called with rpc->crpc_lock held */
1366 srpc_abort_rpc (srpc_client_rpc_t *rpc, int why)
1370 if (rpc->crpc_aborted || /* already aborted */
1371 rpc->crpc_closed) /* callback imminent */
1375 "Aborting RPC: service %d, peer %s, state %s, why %d\n",
1376 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1377 swi_state2str(rpc->crpc_wi.swi_state), why);
1379 rpc->crpc_aborted = 1;
1380 rpc->crpc_status = why;
1381 swi_schedule_workitem(&rpc->crpc_wi);
1385 /* called with rpc->crpc_lock held */
1387 srpc_post_rpc (srpc_client_rpc_t *rpc)
1389 LASSERT (!rpc->crpc_aborted);
1390 LASSERT (srpc_data.rpc_state == SRPC_STATE_RUNNING);
1392 CDEBUG (D_NET, "Posting RPC: peer %s, service %d, timeout %d\n",
1393 libcfs_id2str(rpc->crpc_dest), rpc->crpc_service,
1396 srpc_add_client_rpc_timer(rpc);
1397 swi_schedule_workitem(&rpc->crpc_wi);
1403 srpc_send_reply(struct srpc_server_rpc *rpc)
1405 srpc_event_t *ev = &rpc->srpc_ev;
1406 struct srpc_msg *msg = &rpc->srpc_replymsg;
1407 struct srpc_buffer *buffer = rpc->srpc_reqstbuf;
1408 struct srpc_service_cd *scd = rpc->srpc_scd;
1409 struct srpc_service *sv = scd->scd_svc;
1413 LASSERT(buffer != NULL);
1414 rpyid = buffer->buf_msg.msg_body.reqst.rpyid;
1416 spin_lock(&scd->scd_lock);
1418 if (!sv->sv_shuttingdown && !srpc_serv_is_framework(sv)) {
1419 /* Repost buffer before replying since test client
1420 * might send me another RPC once it gets the reply */
1421 if (srpc_service_post_buffer(scd, buffer) != 0)
1422 CWARN("Failed to repost %s buffer\n", sv->sv_name);
1423 rpc->srpc_reqstbuf = NULL;
1426 spin_unlock(&scd->scd_lock);
1430 ev->ev_type = SRPC_REPLY_SENT;
1432 msg->msg_magic = SRPC_MSG_MAGIC;
1433 msg->msg_version = SRPC_MSG_VERSION;
1434 msg->msg_type = srpc_service2reply(sv->sv_id);
1436 rc = srpc_post_active_rdma(SRPC_RDMA_PORTAL, rpyid, msg,
1437 sizeof(*msg), LNET_MD_OP_PUT,
1438 rpc->srpc_peer, rpc->srpc_self,
1439 &rpc->srpc_replymdh, ev);
1441 ev->ev_fired = 1; /* no more event expected */
1445 /* when in kernel always called with LNET_LOCK() held, and in thread context */
1447 srpc_lnet_ev_handler(lnet_event_t *ev)
1449 struct srpc_service_cd *scd;
1450 srpc_event_t *rpcev = ev->md.user_ptr;
1451 srpc_client_rpc_t *crpc;
1452 srpc_server_rpc_t *srpc;
1453 srpc_buffer_t *buffer;
1456 srpc_msg_type_t type;
1458 LASSERT (!in_interrupt());
1460 if (ev->status != 0) {
1463 spin_lock(&srpc_data.rpc_glock);
1464 if (ev->status != -ECANCELED) /* cancellation is not error */
1465 srpc_data.rpc_counters.errors++;
1466 errors = srpc_data.rpc_counters.errors;
1467 spin_unlock(&srpc_data.rpc_glock);
1469 CNETERR("LNet event status %d type %d, RPC errors %u\n",
1470 ev->status, ev->type, errors);
1473 rpcev->ev_lnet = ev->type;
1475 switch (rpcev->ev_type) {
1477 CERROR("Unknown event: status %d, type %d, lnet %d\n",
1478 rpcev->ev_status, rpcev->ev_type, rpcev->ev_lnet);
1480 case SRPC_REQUEST_SENT:
1481 if (ev->status == 0 && ev->type != LNET_EVENT_UNLINK) {
1482 spin_lock(&srpc_data.rpc_glock);
1483 srpc_data.rpc_counters.rpcs_sent++;
1484 spin_unlock(&srpc_data.rpc_glock);
1486 case SRPC_REPLY_RCVD:
1487 case SRPC_BULK_REQ_RCVD:
1488 crpc = rpcev->ev_data;
1490 if (rpcev != &crpc->crpc_reqstev &&
1491 rpcev != &crpc->crpc_replyev &&
1492 rpcev != &crpc->crpc_bulkev) {
1493 CERROR("rpcev %p, crpc %p, reqstev %p, replyev %p, bulkev %p\n",
1494 rpcev, crpc, &crpc->crpc_reqstev,
1495 &crpc->crpc_replyev, &crpc->crpc_bulkev);
1496 CERROR("Bad event: status %d, type %d, lnet %d\n",
1497 rpcev->ev_status, rpcev->ev_type, rpcev->ev_lnet);
1501 spin_lock(&crpc->crpc_lock);
1503 LASSERT(rpcev->ev_fired == 0);
1504 rpcev->ev_fired = 1;
1505 rpcev->ev_status = (ev->type == LNET_EVENT_UNLINK) ?
1506 -EINTR : ev->status;
1507 swi_schedule_workitem(&crpc->crpc_wi);
1509 spin_unlock(&crpc->crpc_lock);
1512 case SRPC_REQUEST_RCVD:
1513 scd = rpcev->ev_data;
1516 LASSERT(rpcev == &scd->scd_ev);
1518 spin_lock(&scd->scd_lock);
1520 LASSERT (ev->unlinked);
1521 LASSERT (ev->type == LNET_EVENT_PUT ||
1522 ev->type == LNET_EVENT_UNLINK);
1523 LASSERT (ev->type != LNET_EVENT_UNLINK ||
1524 sv->sv_shuttingdown);
1526 buffer = container_of(ev->md.start, srpc_buffer_t, buf_msg);
1527 buffer->buf_peer = ev->initiator;
1528 buffer->buf_self = ev->target.nid;
1530 LASSERT(scd->scd_buf_nposted > 0);
1531 scd->scd_buf_nposted--;
1533 if (sv->sv_shuttingdown) {
1534 /* Leave buffer on scd->scd_buf_nposted since
1535 * srpc_finish_service needs to traverse it. */
1536 spin_unlock(&scd->scd_lock);
1540 if (scd->scd_buf_err_stamp != 0 &&
1541 scd->scd_buf_err_stamp < cfs_time_current_sec()) {
1542 /* re-enable adding buffer */
1543 scd->scd_buf_err_stamp = 0;
1544 scd->scd_buf_err = 0;
1547 if (scd->scd_buf_err == 0 && /* adding buffer is enabled */
1548 scd->scd_buf_adjust == 0 &&
1549 scd->scd_buf_nposted < scd->scd_buf_low) {
1550 scd->scd_buf_adjust = MAX(scd->scd_buf_total / 2,
1552 swi_schedule_workitem(&scd->scd_buf_wi);
1555 list_del(&buffer->buf_list); /* from scd->scd_buf_posted */
1556 msg = &buffer->buf_msg;
1557 type = srpc_service2request(sv->sv_id);
1559 if (ev->status != 0 || ev->mlength != sizeof(*msg) ||
1560 (msg->msg_type != type &&
1561 msg->msg_type != __swab32(type)) ||
1562 (msg->msg_magic != SRPC_MSG_MAGIC &&
1563 msg->msg_magic != __swab32(SRPC_MSG_MAGIC))) {
1564 CERROR ("Dropping RPC (%s) from %s: "
1565 "status %d mlength %d type %u magic %u.\n",
1566 sv->sv_name, libcfs_id2str(ev->initiator),
1567 ev->status, ev->mlength,
1568 msg->msg_type, msg->msg_magic);
1570 /* NB can't call srpc_service_recycle_buffer here since
1571 * it may call LNetM[DE]Attach. The invalid magic tells
1572 * srpc_handle_rpc to drop this RPC */
1576 if (!list_empty(&scd->scd_rpc_free)) {
1577 srpc = list_entry(scd->scd_rpc_free.next,
1578 struct srpc_server_rpc,
1580 list_del(&srpc->srpc_list);
1582 srpc_init_server_rpc(srpc, scd, buffer);
1583 list_add_tail(&srpc->srpc_list,
1584 &scd->scd_rpc_active);
1585 swi_schedule_workitem(&srpc->srpc_wi);
1587 list_add_tail(&buffer->buf_list,
1588 &scd->scd_buf_blocked);
1591 spin_unlock(&scd->scd_lock);
1593 spin_lock(&srpc_data.rpc_glock);
1594 srpc_data.rpc_counters.rpcs_rcvd++;
1595 spin_unlock(&srpc_data.rpc_glock);
1598 case SRPC_BULK_GET_RPLD:
1599 LASSERT (ev->type == LNET_EVENT_SEND ||
1600 ev->type == LNET_EVENT_REPLY ||
1601 ev->type == LNET_EVENT_UNLINK);
1604 break; /* wait for final event */
1606 case SRPC_BULK_PUT_SENT:
1607 if (ev->status == 0 && ev->type != LNET_EVENT_UNLINK) {
1608 spin_lock(&srpc_data.rpc_glock);
1610 if (rpcev->ev_type == SRPC_BULK_GET_RPLD)
1611 srpc_data.rpc_counters.bulk_get += ev->mlength;
1613 srpc_data.rpc_counters.bulk_put += ev->mlength;
1615 spin_unlock(&srpc_data.rpc_glock);
1617 case SRPC_REPLY_SENT:
1618 srpc = rpcev->ev_data;
1619 scd = srpc->srpc_scd;
1621 LASSERT(rpcev == &srpc->srpc_ev);
1623 spin_lock(&scd->scd_lock);
1625 rpcev->ev_fired = 1;
1626 rpcev->ev_status = (ev->type == LNET_EVENT_UNLINK) ?
1627 -EINTR : ev->status;
1628 swi_schedule_workitem(&srpc->srpc_wi);
1630 spin_unlock(&scd->scd_lock);
1638 srpc_check_event (int timeout)
1644 rc = LNetEQPoll(&srpc_data.rpc_lnet_eq, 1,
1645 timeout * 1000, &ev, &i);
1646 if (rc == 0) return 0;
1648 LASSERT (rc == -EOVERFLOW || rc == 1);
1650 /* We can't affort to miss any events... */
1651 if (rc == -EOVERFLOW) {
1652 CERROR ("Dropped an event!!!\n");
1656 srpc_lnet_ev_handler(&ev);
1667 memset(&srpc_data, 0, sizeof(struct smoketest_rpc));
1668 spin_lock_init(&srpc_data.rpc_glock);
1670 /* 1 second pause to avoid timestamp reuse */
1671 cfs_pause(cfs_time_seconds(1));
1672 srpc_data.rpc_matchbits = ((__u64) cfs_time_current_sec()) << 48;
1674 srpc_data.rpc_state = SRPC_STATE_NONE;
1677 rc = LNetNIInit(LNET_PID_LUSTRE);
1679 if (the_lnet.ln_server_mode_flag)
1680 rc = LNetNIInit(LNET_PID_LUSTRE);
1682 rc = LNetNIInit(getpid() | LNET_PID_USERFLAG);
1685 CERROR ("LNetNIInit() has failed: %d\n", rc);
1689 srpc_data.rpc_state = SRPC_STATE_NI_INIT;
1691 LNetInvalidateHandle(&srpc_data.rpc_lnet_eq);
1693 rc = LNetEQAlloc(0, srpc_lnet_ev_handler, &srpc_data.rpc_lnet_eq);
1695 rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &srpc_data.rpc_lnet_eq);
1698 CERROR("LNetEQAlloc() has failed: %d\n", rc);
1702 rc = LNetSetLazyPortal(SRPC_FRAMEWORK_REQUEST_PORTAL);
1704 rc = LNetSetLazyPortal(SRPC_REQUEST_PORTAL);
1707 srpc_data.rpc_state = SRPC_STATE_EQ_INIT;
1715 srpc_data.rpc_state = SRPC_STATE_RUNNING;
1721 srpc_shutdown (void)
1727 state = srpc_data.rpc_state;
1728 srpc_data.rpc_state = SRPC_STATE_STOPPING;
1733 case SRPC_STATE_RUNNING:
1734 spin_lock(&srpc_data.rpc_glock);
1736 for (i = 0; i <= SRPC_SERVICE_MAX_ID; i++) {
1737 srpc_service_t *sv = srpc_data.rpc_services[i];
1739 LASSERTF (sv == NULL,
1740 "service not empty: id %d, name %s\n",
1744 spin_unlock(&srpc_data.rpc_glock);
1748 case SRPC_STATE_EQ_INIT:
1749 rc = LNetClearLazyPortal(SRPC_FRAMEWORK_REQUEST_PORTAL);
1750 rc = LNetClearLazyPortal(SRPC_REQUEST_PORTAL);
1752 rc = LNetEQFree(srpc_data.rpc_lnet_eq);
1753 LASSERT (rc == 0); /* the EQ should have no user by now */
1755 case SRPC_STATE_NI_INIT: