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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Author: Isaac Huang <isaac@clusterfs.com>
38 * 2012-05-13: Liang Zhen <liang@whamcloud.com>
39 * - percpt data for service to improve smp performance
43 #define DEBUG_SUBSYSTEM S_LNET
55 struct smoketest_rpc {
56 spinlock_t rpc_glock; /* global lock */
57 srpc_service_t *rpc_services[SRPC_SERVICE_MAX_ID + 1];
58 lnet_handle_eq_t rpc_lnet_eq; /* _the_ LNet event queue */
59 srpc_state_t rpc_state;
60 srpc_counters_t rpc_counters;
61 __u64 rpc_matchbits; /* matchbits counter */
65 srpc_serv_portal(int svc_id)
67 return svc_id < SRPC_FRAMEWORK_SERVICE_MAX_ID ?
68 SRPC_FRAMEWORK_REQUEST_PORTAL : SRPC_REQUEST_PORTAL;
72 int srpc_handle_rpc (swi_workitem_t *wi);
74 void srpc_get_counters (srpc_counters_t *cnt)
76 spin_lock(&srpc_data.rpc_glock);
77 *cnt = srpc_data.rpc_counters;
78 spin_unlock(&srpc_data.rpc_glock);
81 void srpc_set_counters (const srpc_counters_t *cnt)
83 spin_lock(&srpc_data.rpc_glock);
84 srpc_data.rpc_counters = *cnt;
85 spin_unlock(&srpc_data.rpc_glock);
89 srpc_add_bulk_page(srpc_bulk_t *bk, cfs_page_t *pg, int i, int nob)
91 nob = min(nob, (int)CFS_PAGE_SIZE);
94 LASSERT(i >= 0 && i < bk->bk_niov);
97 bk->bk_iovs[i].kiov_offset = 0;
98 bk->bk_iovs[i].kiov_page = pg;
99 bk->bk_iovs[i].kiov_len = nob;
101 LASSERT(bk->bk_pages != NULL);
103 bk->bk_pages[i] = pg;
104 bk->bk_iovs[i].iov_len = nob;
105 bk->bk_iovs[i].iov_base = cfs_page_address(pg);
111 srpc_free_bulk (srpc_bulk_t *bk)
116 LASSERT (bk != NULL);
118 LASSERT (bk->bk_pages != NULL);
121 for (i = 0; i < bk->bk_niov; i++) {
123 pg = bk->bk_iovs[i].kiov_page;
125 pg = bk->bk_pages[i];
127 if (pg == NULL) break;
133 LIBCFS_FREE(bk->bk_pages, sizeof(cfs_page_t *) * bk->bk_niov);
135 LIBCFS_FREE(bk, offsetof(srpc_bulk_t, bk_iovs[bk->bk_niov]));
140 srpc_alloc_bulk(int cpt, unsigned bulk_npg, unsigned bulk_len, int sink)
146 LASSERT(bulk_npg > 0 && bulk_npg <= LNET_MAX_IOV);
148 LIBCFS_CPT_ALLOC(bk, lnet_cpt_table(), cpt,
149 offsetof(srpc_bulk_t, bk_iovs[bulk_npg]));
151 CERROR("Can't allocate descriptor for %d pages\n", bulk_npg);
155 memset(bk, 0, offsetof(srpc_bulk_t, bk_iovs[bulk_npg]));
157 bk->bk_len = bulk_len;
158 bk->bk_niov = bulk_npg;
160 LIBCFS_CPT_ALLOC(pages, lnet_cpt_table(), cpt,
161 sizeof(cfs_page_t *) * bulk_npg);
163 LIBCFS_FREE(bk, offsetof(srpc_bulk_t, bk_iovs[bulk_npg]));
164 CERROR("Can't allocate page array for %d pages\n", bulk_npg);
168 memset(pages, 0, sizeof(cfs_page_t *) * bulk_npg);
169 bk->bk_pages = pages;
174 for (i = 0; i < bulk_npg; i++) {
178 pg = cfs_page_cpt_alloc(lnet_cpt_table(), cpt, CFS_ALLOC_STD);
180 CERROR("Can't allocate page %d of %d\n", i, bulk_npg);
185 nob = srpc_add_bulk_page(bk, pg, i, bulk_len);
197 spin_lock(&srpc_data.rpc_glock);
198 id = srpc_data.rpc_matchbits++;
199 spin_unlock(&srpc_data.rpc_glock);
204 srpc_init_server_rpc(struct srpc_server_rpc *rpc,
205 struct srpc_service_cd *scd,
206 struct srpc_buffer *buffer)
208 memset(rpc, 0, sizeof(*rpc));
209 swi_init_workitem(&rpc->srpc_wi, rpc, srpc_handle_rpc,
210 srpc_serv_is_framework(scd->scd_svc) ?
211 lst_sched_serial : lst_sched_test[scd->scd_cpt]);
213 rpc->srpc_ev.ev_fired = 1; /* no event expected now */
216 rpc->srpc_reqstbuf = buffer;
217 rpc->srpc_peer = buffer->buf_peer;
218 rpc->srpc_self = buffer->buf_self;
219 LNetInvalidateHandle(&rpc->srpc_replymdh);
223 srpc_service_fini(struct srpc_service *svc)
225 struct srpc_service_cd *scd;
226 struct srpc_server_rpc *rpc;
227 struct srpc_buffer *buf;
231 if (svc->sv_cpt_data == NULL)
234 cfs_percpt_for_each(scd, i, svc->sv_cpt_data) {
236 if (!cfs_list_empty(&scd->scd_buf_posted))
237 q = &scd->scd_buf_posted;
238 else if (!cfs_list_empty(&scd->scd_buf_blocked))
239 q = &scd->scd_buf_blocked;
243 while (!cfs_list_empty(q)) {
244 buf = cfs_list_entry(q->next,
247 cfs_list_del(&buf->buf_list);
248 LIBCFS_FREE(buf, sizeof(*buf));
252 LASSERT(cfs_list_empty(&scd->scd_rpc_active));
254 while (!cfs_list_empty(&scd->scd_rpc_free)) {
255 rpc = cfs_list_entry(scd->scd_rpc_free.next,
256 struct srpc_server_rpc,
258 cfs_list_del(&rpc->srpc_list);
259 LIBCFS_FREE(rpc, sizeof(*rpc));
263 cfs_percpt_free(svc->sv_cpt_data);
264 svc->sv_cpt_data = NULL;
268 srpc_service_nrpcs(struct srpc_service *svc)
270 int nrpcs = svc->sv_wi_total / svc->sv_ncpts;
272 return srpc_serv_is_framework(svc) ?
273 max(nrpcs, SFW_FRWK_WI_MIN) : max(nrpcs, SFW_TEST_WI_MIN);
276 int srpc_add_buffer(struct swi_workitem *wi);
279 srpc_service_init(struct srpc_service *svc)
281 struct srpc_service_cd *scd;
282 struct srpc_server_rpc *rpc;
287 svc->sv_shuttingdown = 0;
289 svc->sv_cpt_data = cfs_percpt_alloc(lnet_cpt_table(),
290 sizeof(struct srpc_service_cd));
291 if (svc->sv_cpt_data == NULL)
294 svc->sv_ncpts = srpc_serv_is_framework(svc) ?
295 1 : cfs_cpt_number(lnet_cpt_table());
296 nrpcs = srpc_service_nrpcs(svc);
298 cfs_percpt_for_each(scd, i, svc->sv_cpt_data) {
301 spin_lock_init(&scd->scd_lock);
302 CFS_INIT_LIST_HEAD(&scd->scd_rpc_free);
303 CFS_INIT_LIST_HEAD(&scd->scd_rpc_active);
304 CFS_INIT_LIST_HEAD(&scd->scd_buf_posted);
305 CFS_INIT_LIST_HEAD(&scd->scd_buf_blocked);
307 scd->scd_ev.ev_data = scd;
308 scd->scd_ev.ev_type = SRPC_REQUEST_RCVD;
310 /* NB: don't use lst_sched_serial for adding buffer,
311 * see details in srpc_service_add_buffers() */
312 swi_init_workitem(&scd->scd_buf_wi, scd,
313 srpc_add_buffer, lst_sched_test[i]);
315 if (i != 0 && srpc_serv_is_framework(svc)) {
316 /* NB: framework service only needs srpc_service_cd for
317 * one partition, but we allocate for all to make
318 * it easier to implement, it will waste a little
319 * memory but nobody should care about this */
323 for (j = 0; j < nrpcs; j++) {
324 LIBCFS_CPT_ALLOC(rpc, lnet_cpt_table(),
327 srpc_service_fini(svc);
330 cfs_list_add(&rpc->srpc_list, &scd->scd_rpc_free);
338 srpc_add_service(struct srpc_service *sv)
342 LASSERT(0 <= id && id <= SRPC_SERVICE_MAX_ID);
344 if (srpc_service_init(sv) != 0)
347 spin_lock(&srpc_data.rpc_glock);
349 LASSERT(srpc_data.rpc_state == SRPC_STATE_RUNNING);
351 if (srpc_data.rpc_services[id] != NULL) {
352 spin_unlock(&srpc_data.rpc_glock);
356 srpc_data.rpc_services[id] = sv;
357 spin_unlock(&srpc_data.rpc_glock);
359 CDEBUG(D_NET, "Adding service: id %d, name %s\n", id, sv->sv_name);
363 srpc_service_fini(sv);
368 srpc_remove_service (srpc_service_t *sv)
372 spin_lock(&srpc_data.rpc_glock);
374 if (srpc_data.rpc_services[id] != sv) {
375 spin_unlock(&srpc_data.rpc_glock);
379 srpc_data.rpc_services[id] = NULL;
380 spin_unlock(&srpc_data.rpc_glock);
385 srpc_post_passive_rdma(int portal, int local, __u64 matchbits, void *buf,
386 int len, int options, lnet_process_id_t peer,
387 lnet_handle_md_t *mdh, srpc_event_t *ev)
391 lnet_handle_me_t meh;
393 rc = LNetMEAttach(portal, peer, matchbits, 0, LNET_UNLINK,
394 local ? LNET_INS_LOCAL : LNET_INS_AFTER, &meh);
396 CERROR ("LNetMEAttach failed: %d\n", rc);
397 LASSERT (rc == -ENOMEM);
405 md.options = options;
406 md.eq_handle = srpc_data.rpc_lnet_eq;
408 rc = LNetMDAttach(meh, md, LNET_UNLINK, mdh);
410 CERROR ("LNetMDAttach failed: %d\n", rc);
411 LASSERT (rc == -ENOMEM);
413 rc = LNetMEUnlink(meh);
419 "Posted passive RDMA: peer %s, portal %d, matchbits "LPX64"\n",
420 libcfs_id2str(peer), portal, matchbits);
425 srpc_post_active_rdma(int portal, __u64 matchbits, void *buf, int len,
426 int options, lnet_process_id_t peer, lnet_nid_t self,
427 lnet_handle_md_t *mdh, srpc_event_t *ev)
435 md.eq_handle = srpc_data.rpc_lnet_eq;
436 md.threshold = ((options & LNET_MD_OP_GET) != 0) ? 2 : 1;
437 md.options = options & ~(LNET_MD_OP_PUT | LNET_MD_OP_GET);
439 rc = LNetMDBind(md, LNET_UNLINK, mdh);
441 CERROR ("LNetMDBind failed: %d\n", rc);
442 LASSERT (rc == -ENOMEM);
446 /* this is kind of an abuse of the LNET_MD_OP_{PUT,GET} options.
447 * they're only meaningful for MDs attached to an ME (i.e. passive
449 if ((options & LNET_MD_OP_PUT) != 0) {
450 rc = LNetPut(self, *mdh, LNET_NOACK_REQ, peer,
451 portal, matchbits, 0, 0);
453 LASSERT ((options & LNET_MD_OP_GET) != 0);
455 rc = LNetGet(self, *mdh, peer, portal, matchbits, 0);
459 CERROR ("LNet%s(%s, %d, "LPD64") failed: %d\n",
460 ((options & LNET_MD_OP_PUT) != 0) ? "Put" : "Get",
461 libcfs_id2str(peer), portal, matchbits, rc);
463 /* The forthcoming unlink event will complete this operation
464 * with failure, so fall through and return success here.
466 rc = LNetMDUnlink(*mdh);
470 "Posted active RDMA: peer %s, portal %u, matchbits "LPX64"\n",
471 libcfs_id2str(peer), portal, matchbits);
477 srpc_post_active_rqtbuf(lnet_process_id_t peer, int service, void *buf,
478 int len, lnet_handle_md_t *mdh, srpc_event_t *ev)
480 return srpc_post_active_rdma(srpc_serv_portal(service), service,
481 buf, len, LNET_MD_OP_PUT, peer,
482 LNET_NID_ANY, mdh, ev);
486 srpc_post_passive_rqtbuf(int service, int local, void *buf, int len,
487 lnet_handle_md_t *mdh, srpc_event_t *ev)
489 lnet_process_id_t any = {0};
491 any.nid = LNET_NID_ANY;
492 any.pid = LNET_PID_ANY;
494 return srpc_post_passive_rdma(srpc_serv_portal(service),
495 local, service, buf, len,
496 LNET_MD_OP_PUT, any, mdh, ev);
500 srpc_service_post_buffer(struct srpc_service_cd *scd, struct srpc_buffer *buf)
502 struct srpc_service *sv = scd->scd_svc;
503 struct srpc_msg *msg = &buf->buf_msg;
506 LNetInvalidateHandle(&buf->buf_mdh);
507 cfs_list_add(&buf->buf_list, &scd->scd_buf_posted);
508 scd->scd_buf_nposted++;
509 spin_unlock(&scd->scd_lock);
511 rc = srpc_post_passive_rqtbuf(sv->sv_id,
512 !srpc_serv_is_framework(sv),
513 msg, sizeof(*msg), &buf->buf_mdh,
516 /* At this point, a RPC (new or delayed) may have arrived in
517 * msg and its event handler has been called. So we must add
518 * buf to scd_buf_posted _before_ dropping scd_lock */
520 spin_lock(&scd->scd_lock);
523 if (!sv->sv_shuttingdown)
526 spin_unlock(&scd->scd_lock);
527 /* srpc_shutdown_service might have tried to unlink me
528 * when my buf_mdh was still invalid */
529 LNetMDUnlink(buf->buf_mdh);
530 spin_lock(&scd->scd_lock);
534 scd->scd_buf_nposted--;
535 if (sv->sv_shuttingdown)
536 return rc; /* don't allow to change scd_buf_posted */
538 cfs_list_del(&buf->buf_list);
539 spin_unlock(&scd->scd_lock);
541 LIBCFS_FREE(buf, sizeof(*buf));
543 spin_lock(&scd->scd_lock);
548 srpc_add_buffer(struct swi_workitem *wi)
550 struct srpc_service_cd *scd = wi->swi_workitem.wi_data;
551 struct srpc_buffer *buf;
554 /* it's called by workitem scheduler threads, these threads
555 * should have been set CPT affinity, so buffers will be posted
556 * on CPT local list of Portal */
557 spin_lock(&scd->scd_lock);
559 while (scd->scd_buf_adjust > 0 &&
560 !scd->scd_svc->sv_shuttingdown) {
561 scd->scd_buf_adjust--; /* consume it */
562 scd->scd_buf_posting++;
564 spin_unlock(&scd->scd_lock);
566 LIBCFS_ALLOC(buf, sizeof(*buf));
568 CERROR("Failed to add new buf to service: %s\n",
569 scd->scd_svc->sv_name);
570 spin_lock(&scd->scd_lock);
575 spin_lock(&scd->scd_lock);
576 if (scd->scd_svc->sv_shuttingdown) {
577 spin_unlock(&scd->scd_lock);
578 LIBCFS_FREE(buf, sizeof(*buf));
580 spin_lock(&scd->scd_lock);
585 rc = srpc_service_post_buffer(scd, buf);
587 break; /* buf has been freed inside */
589 LASSERT(scd->scd_buf_posting > 0);
590 scd->scd_buf_posting--;
591 scd->scd_buf_total++;
592 scd->scd_buf_low = MAX(2, scd->scd_buf_total / 4);
596 scd->scd_buf_err_stamp = cfs_time_current_sec();
597 scd->scd_buf_err = rc;
599 LASSERT(scd->scd_buf_posting > 0);
600 scd->scd_buf_posting--;
603 spin_unlock(&scd->scd_lock);
608 srpc_service_add_buffers(struct srpc_service *sv, int nbuffer)
610 struct srpc_service_cd *scd;
614 LASSERTF(nbuffer > 0, "nbuffer must be positive: %d\n", nbuffer);
616 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
617 spin_lock(&scd->scd_lock);
619 scd->scd_buf_err = 0;
620 scd->scd_buf_err_stamp = 0;
621 scd->scd_buf_posting = 0;
622 scd->scd_buf_adjust = nbuffer;
623 /* start to post buffers */
624 swi_schedule_workitem(&scd->scd_buf_wi);
625 spin_unlock(&scd->scd_lock);
627 /* framework service only post buffer for one partition */
628 if (srpc_serv_is_framework(sv))
632 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
633 spin_lock(&scd->scd_lock);
635 * NB: srpc_service_add_buffers() can be called inside
636 * thread context of lst_sched_serial, and we don't normally
637 * allow to sleep inside thread context of WI scheduler
638 * because it will block current scheduler thread from doing
639 * anything else, even worse, it could deadlock if it's
640 * waiting on result from another WI of the same scheduler.
641 * However, it's safe at here because scd_buf_wi is scheduled
642 * by thread in a different WI scheduler (lst_sched_test),
643 * so we don't have any risk of deadlock, though this could
644 * block all WIs pending on lst_sched_serial for a moment
645 * which is not good but not fatal.
647 lst_wait_until(scd->scd_buf_err != 0 ||
648 (scd->scd_buf_adjust == 0 &&
649 scd->scd_buf_posting == 0),
650 scd->scd_lock, "waiting for adding buffer\n");
652 if (scd->scd_buf_err != 0 && rc == 0)
653 rc = scd->scd_buf_err;
655 spin_unlock(&scd->scd_lock);
662 srpc_service_remove_buffers(struct srpc_service *sv, int nbuffer)
664 struct srpc_service_cd *scd;
668 LASSERT(!sv->sv_shuttingdown);
670 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
671 spin_lock(&scd->scd_lock);
673 num = scd->scd_buf_total + scd->scd_buf_posting;
674 scd->scd_buf_adjust -= min(nbuffer, num);
676 spin_unlock(&scd->scd_lock);
680 /* returns 1 if sv has finished, otherwise 0 */
682 srpc_finish_service(struct srpc_service *sv)
684 struct srpc_service_cd *scd;
685 struct srpc_server_rpc *rpc;
688 LASSERT(sv->sv_shuttingdown); /* srpc_shutdown_service called */
690 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
691 spin_lock(&scd->scd_lock);
692 if (!swi_deschedule_workitem(&scd->scd_buf_wi))
695 if (scd->scd_buf_nposted > 0) {
696 CDEBUG(D_NET, "waiting for %d posted buffers to unlink",
697 scd->scd_buf_nposted);
698 spin_unlock(&scd->scd_lock);
702 if (cfs_list_empty(&scd->scd_rpc_active)) {
703 spin_unlock(&scd->scd_lock);
707 rpc = cfs_list_entry(scd->scd_rpc_active.next,
708 struct srpc_server_rpc, srpc_list);
709 CNETERR("Active RPC %p on shutdown: sv %s, peer %s, "
710 "wi %s scheduled %d running %d, "
711 "ev fired %d type %d status %d lnet %d\n",
712 rpc, sv->sv_name, libcfs_id2str(rpc->srpc_peer),
713 swi_state2str(rpc->srpc_wi.swi_state),
714 rpc->srpc_wi.swi_workitem.wi_scheduled,
715 rpc->srpc_wi.swi_workitem.wi_running,
716 rpc->srpc_ev.ev_fired, rpc->srpc_ev.ev_type,
717 rpc->srpc_ev.ev_status, rpc->srpc_ev.ev_lnet);
718 spin_unlock(&scd->scd_lock);
722 /* no lock needed from now on */
723 srpc_service_fini(sv);
727 /* called with sv->sv_lock held */
729 srpc_service_recycle_buffer(struct srpc_service_cd *scd, srpc_buffer_t *buf)
731 if (!scd->scd_svc->sv_shuttingdown && scd->scd_buf_adjust >= 0) {
732 if (srpc_service_post_buffer(scd, buf) != 0) {
733 CWARN("Failed to post %s buffer\n",
734 scd->scd_svc->sv_name);
739 /* service is shutting down, or we want to recycle some buffers */
740 scd->scd_buf_total--;
742 if (scd->scd_buf_adjust < 0) {
743 scd->scd_buf_adjust++;
744 if (scd->scd_buf_adjust < 0 &&
745 scd->scd_buf_total == 0 && scd->scd_buf_posting == 0) {
747 "Try to recyle %d buffers but nothing left\n",
748 scd->scd_buf_adjust);
749 scd->scd_buf_adjust = 0;
753 spin_unlock(&scd->scd_lock);
754 LIBCFS_FREE(buf, sizeof(*buf));
755 spin_lock(&scd->scd_lock);
759 srpc_abort_service(struct srpc_service *sv)
761 struct srpc_service_cd *scd;
762 struct srpc_server_rpc *rpc;
765 CDEBUG(D_NET, "Aborting service: id %d, name %s\n",
766 sv->sv_id, sv->sv_name);
768 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
769 spin_lock(&scd->scd_lock);
771 /* schedule in-flight RPCs to notice the abort, NB:
772 * racing with incoming RPCs; complete fix should make test
773 * RPCs carry session ID in its headers */
774 cfs_list_for_each_entry(rpc, &scd->scd_rpc_active, srpc_list) {
775 rpc->srpc_aborted = 1;
776 swi_schedule_workitem(&rpc->srpc_wi);
779 spin_unlock(&scd->scd_lock);
784 srpc_shutdown_service(srpc_service_t *sv)
786 struct srpc_service_cd *scd;
787 struct srpc_server_rpc *rpc;
791 CDEBUG(D_NET, "Shutting down service: id %d, name %s\n",
792 sv->sv_id, sv->sv_name);
794 cfs_percpt_for_each(scd, i, sv->sv_cpt_data)
795 spin_lock(&scd->scd_lock);
797 sv->sv_shuttingdown = 1; /* i.e. no new active RPC */
799 cfs_percpt_for_each(scd, i, sv->sv_cpt_data)
800 spin_unlock(&scd->scd_lock);
802 cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
803 spin_lock(&scd->scd_lock);
805 /* schedule in-flight RPCs to notice the shutdown */
806 cfs_list_for_each_entry(rpc, &scd->scd_rpc_active, srpc_list)
807 swi_schedule_workitem(&rpc->srpc_wi);
809 spin_unlock(&scd->scd_lock);
811 /* OK to traverse scd_buf_posted without lock, since no one
812 * touches scd_buf_posted now */
813 cfs_list_for_each_entry(buf, &scd->scd_buf_posted, buf_list)
814 LNetMDUnlink(buf->buf_mdh);
819 srpc_send_request (srpc_client_rpc_t *rpc)
821 srpc_event_t *ev = &rpc->crpc_reqstev;
826 ev->ev_type = SRPC_REQUEST_SENT;
828 rc = srpc_post_active_rqtbuf(rpc->crpc_dest, rpc->crpc_service,
829 &rpc->crpc_reqstmsg, sizeof(srpc_msg_t),
830 &rpc->crpc_reqstmdh, ev);
832 LASSERT (rc == -ENOMEM);
833 ev->ev_fired = 1; /* no more event expected */
839 srpc_prepare_reply (srpc_client_rpc_t *rpc)
841 srpc_event_t *ev = &rpc->crpc_replyev;
842 __u64 *id = &rpc->crpc_reqstmsg.msg_body.reqst.rpyid;
847 ev->ev_type = SRPC_REPLY_RCVD;
849 *id = srpc_next_id();
851 rc = srpc_post_passive_rdma(SRPC_RDMA_PORTAL, 0, *id,
852 &rpc->crpc_replymsg, sizeof(srpc_msg_t),
853 LNET_MD_OP_PUT, rpc->crpc_dest,
854 &rpc->crpc_replymdh, ev);
856 LASSERT (rc == -ENOMEM);
857 ev->ev_fired = 1; /* no more event expected */
863 srpc_prepare_bulk (srpc_client_rpc_t *rpc)
865 srpc_bulk_t *bk = &rpc->crpc_bulk;
866 srpc_event_t *ev = &rpc->crpc_bulkev;
867 __u64 *id = &rpc->crpc_reqstmsg.msg_body.reqst.bulkid;
871 LASSERT (bk->bk_niov <= LNET_MAX_IOV);
873 if (bk->bk_niov == 0) return 0; /* nothing to do */
875 opt = bk->bk_sink ? LNET_MD_OP_PUT : LNET_MD_OP_GET;
879 opt |= LNET_MD_IOVEC;
884 ev->ev_type = SRPC_BULK_REQ_RCVD;
886 *id = srpc_next_id();
888 rc = srpc_post_passive_rdma(SRPC_RDMA_PORTAL, 0, *id,
889 &bk->bk_iovs[0], bk->bk_niov, opt,
890 rpc->crpc_dest, &bk->bk_mdh, ev);
892 LASSERT (rc == -ENOMEM);
893 ev->ev_fired = 1; /* no more event expected */
899 srpc_do_bulk (srpc_server_rpc_t *rpc)
901 srpc_event_t *ev = &rpc->srpc_ev;
902 srpc_bulk_t *bk = rpc->srpc_bulk;
903 __u64 id = rpc->srpc_reqstbuf->buf_msg.msg_body.reqst.bulkid;
907 LASSERT (bk != NULL);
909 opt = bk->bk_sink ? LNET_MD_OP_GET : LNET_MD_OP_PUT;
913 opt |= LNET_MD_IOVEC;
918 ev->ev_type = bk->bk_sink ? SRPC_BULK_GET_RPLD : SRPC_BULK_PUT_SENT;
920 rc = srpc_post_active_rdma(SRPC_RDMA_PORTAL, id,
921 &bk->bk_iovs[0], bk->bk_niov, opt,
922 rpc->srpc_peer, rpc->srpc_self,
925 ev->ev_fired = 1; /* no more event expected */
929 /* only called from srpc_handle_rpc */
931 srpc_server_rpc_done(srpc_server_rpc_t *rpc, int status)
933 struct srpc_service_cd *scd = rpc->srpc_scd;
934 struct srpc_service *sv = scd->scd_svc;
935 srpc_buffer_t *buffer;
937 LASSERT (status != 0 || rpc->srpc_wi.swi_state == SWI_STATE_DONE);
939 rpc->srpc_status = status;
941 CDEBUG_LIMIT (status == 0 ? D_NET : D_NETERROR,
942 "Server RPC %p done: service %s, peer %s, status %s:%d\n",
943 rpc, sv->sv_name, libcfs_id2str(rpc->srpc_peer),
944 swi_state2str(rpc->srpc_wi.swi_state), status);
947 spin_lock(&srpc_data.rpc_glock);
948 srpc_data.rpc_counters.rpcs_dropped++;
949 spin_unlock(&srpc_data.rpc_glock);
952 if (rpc->srpc_done != NULL)
953 (*rpc->srpc_done) (rpc);
954 LASSERT(rpc->srpc_bulk == NULL);
956 spin_lock(&scd->scd_lock);
958 if (rpc->srpc_reqstbuf != NULL) {
959 /* NB might drop sv_lock in srpc_service_recycle_buffer, but
960 * sv won't go away for scd_rpc_active must not be empty */
961 srpc_service_recycle_buffer(scd, rpc->srpc_reqstbuf);
962 rpc->srpc_reqstbuf = NULL;
965 cfs_list_del(&rpc->srpc_list); /* from scd->scd_rpc_active */
968 * No one can schedule me now since:
969 * - I'm not on scd_rpc_active.
970 * - all LNet events have been fired.
971 * Cancel pending schedules and prevent future schedule attempts:
973 LASSERT(rpc->srpc_ev.ev_fired);
974 swi_exit_workitem(&rpc->srpc_wi);
976 if (!sv->sv_shuttingdown && !cfs_list_empty(&scd->scd_buf_blocked)) {
977 buffer = cfs_list_entry(scd->scd_buf_blocked.next,
978 srpc_buffer_t, buf_list);
979 cfs_list_del(&buffer->buf_list);
981 srpc_init_server_rpc(rpc, scd, buffer);
982 cfs_list_add_tail(&rpc->srpc_list, &scd->scd_rpc_active);
983 swi_schedule_workitem(&rpc->srpc_wi);
985 cfs_list_add(&rpc->srpc_list, &scd->scd_rpc_free);
988 spin_unlock(&scd->scd_lock);
992 /* handles an incoming RPC */
994 srpc_handle_rpc(swi_workitem_t *wi)
996 struct srpc_server_rpc *rpc = wi->swi_workitem.wi_data;
997 struct srpc_service_cd *scd = rpc->srpc_scd;
998 struct srpc_service *sv = scd->scd_svc;
999 srpc_event_t *ev = &rpc->srpc_ev;
1002 LASSERT(wi == &rpc->srpc_wi);
1004 spin_lock(&scd->scd_lock);
1006 if (sv->sv_shuttingdown || rpc->srpc_aborted) {
1007 spin_unlock(&scd->scd_lock);
1009 if (rpc->srpc_bulk != NULL)
1010 LNetMDUnlink(rpc->srpc_bulk->bk_mdh);
1011 LNetMDUnlink(rpc->srpc_replymdh);
1013 if (ev->ev_fired) { /* no more event, OK to finish */
1014 srpc_server_rpc_done(rpc, -ESHUTDOWN);
1020 spin_unlock(&scd->scd_lock);
1022 switch (wi->swi_state) {
1025 case SWI_STATE_NEWBORN: {
1027 srpc_generic_reply_t *reply;
1029 msg = &rpc->srpc_reqstbuf->buf_msg;
1030 reply = &rpc->srpc_replymsg.msg_body.reply;
1032 if (msg->msg_magic == 0) {
1033 /* moaned already in srpc_lnet_ev_handler */
1034 srpc_server_rpc_done(rpc, EBADMSG);
1038 srpc_unpack_msg_hdr(msg);
1039 if (msg->msg_version != SRPC_MSG_VERSION) {
1040 CWARN("Version mismatch: %u, %u expected, from %s\n",
1041 msg->msg_version, SRPC_MSG_VERSION,
1042 libcfs_id2str(rpc->srpc_peer));
1043 reply->status = EPROTO;
1044 /* drop through and send reply */
1047 rc = (*sv->sv_handler)(rpc);
1048 LASSERT(reply->status == 0 || !rpc->srpc_bulk);
1050 srpc_server_rpc_done(rpc, rc);
1055 wi->swi_state = SWI_STATE_BULK_STARTED;
1057 if (rpc->srpc_bulk != NULL) {
1058 rc = srpc_do_bulk(rpc);
1060 return 0; /* wait for bulk */
1062 LASSERT (ev->ev_fired);
1066 case SWI_STATE_BULK_STARTED:
1067 LASSERT (rpc->srpc_bulk == NULL || ev->ev_fired);
1069 if (rpc->srpc_bulk != NULL) {
1072 if (sv->sv_bulk_ready != NULL)
1073 rc = (*sv->sv_bulk_ready) (rpc, rc);
1076 srpc_server_rpc_done(rpc, rc);
1081 wi->swi_state = SWI_STATE_REPLY_SUBMITTED;
1082 rc = srpc_send_reply(rpc);
1084 return 0; /* wait for reply */
1085 srpc_server_rpc_done(rpc, rc);
1088 case SWI_STATE_REPLY_SUBMITTED:
1089 if (!ev->ev_fired) {
1090 CERROR("RPC %p: bulk %p, service %d\n",
1091 rpc, rpc->srpc_bulk, sv->sv_id);
1092 CERROR("Event: status %d, type %d, lnet %d\n",
1093 ev->ev_status, ev->ev_type, ev->ev_lnet);
1094 LASSERT (ev->ev_fired);
1097 wi->swi_state = SWI_STATE_DONE;
1098 srpc_server_rpc_done(rpc, ev->ev_status);
1106 srpc_client_rpc_expired (void *data)
1108 srpc_client_rpc_t *rpc = data;
1110 CWARN ("Client RPC expired: service %d, peer %s, timeout %d.\n",
1111 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1114 spin_lock(&rpc->crpc_lock);
1116 rpc->crpc_timeout = 0;
1117 srpc_abort_rpc(rpc, -ETIMEDOUT);
1119 spin_unlock(&rpc->crpc_lock);
1121 spin_lock(&srpc_data.rpc_glock);
1122 srpc_data.rpc_counters.rpcs_expired++;
1123 spin_unlock(&srpc_data.rpc_glock);
1127 srpc_add_client_rpc_timer (srpc_client_rpc_t *rpc)
1129 stt_timer_t *timer = &rpc->crpc_timer;
1131 if (rpc->crpc_timeout == 0) return;
1133 CFS_INIT_LIST_HEAD(&timer->stt_list);
1134 timer->stt_data = rpc;
1135 timer->stt_func = srpc_client_rpc_expired;
1136 timer->stt_expires = cfs_time_add(rpc->crpc_timeout,
1137 cfs_time_current_sec());
1138 stt_add_timer(timer);
1143 * Called with rpc->crpc_lock held.
1145 * Upon exit the RPC expiry timer is not queued and the handler is not
1146 * running on any CPU. */
1148 srpc_del_client_rpc_timer (srpc_client_rpc_t *rpc)
1150 /* timer not planted or already exploded */
1151 if (rpc->crpc_timeout == 0)
1154 /* timer sucessfully defused */
1155 if (stt_del_timer(&rpc->crpc_timer))
1159 /* timer detonated, wait for it to explode */
1160 while (rpc->crpc_timeout != 0) {
1161 spin_unlock(&rpc->crpc_lock);
1165 spin_lock(&rpc->crpc_lock);
1168 LBUG(); /* impossible in single-threaded runtime */
1173 srpc_client_rpc_done (srpc_client_rpc_t *rpc, int status)
1175 swi_workitem_t *wi = &rpc->crpc_wi;
1177 LASSERT(status != 0 || wi->swi_state == SWI_STATE_DONE);
1179 spin_lock(&rpc->crpc_lock);
1181 rpc->crpc_closed = 1;
1182 if (rpc->crpc_status == 0)
1183 rpc->crpc_status = status;
1185 srpc_del_client_rpc_timer(rpc);
1187 CDEBUG_LIMIT ((status == 0) ? D_NET : D_NETERROR,
1188 "Client RPC done: service %d, peer %s, status %s:%d:%d\n",
1189 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1190 swi_state2str(wi->swi_state), rpc->crpc_aborted, status);
1193 * No one can schedule me now since:
1194 * - RPC timer has been defused.
1195 * - all LNet events have been fired.
1196 * - crpc_closed has been set, preventing srpc_abort_rpc from
1198 * Cancel pending schedules and prevent future schedule attempts:
1200 LASSERT (!srpc_event_pending(rpc));
1201 swi_exit_workitem(wi);
1203 spin_unlock(&rpc->crpc_lock);
1205 (*rpc->crpc_done)(rpc);
1209 /* sends an outgoing RPC */
1211 srpc_send_rpc (swi_workitem_t *wi)
1214 srpc_client_rpc_t *rpc = wi->swi_workitem.wi_data;
1215 srpc_msg_t *reply = &rpc->crpc_replymsg;
1216 int do_bulk = rpc->crpc_bulk.bk_niov > 0;
1218 LASSERT (rpc != NULL);
1219 LASSERT (wi == &rpc->crpc_wi);
1221 spin_lock(&rpc->crpc_lock);
1223 if (rpc->crpc_aborted) {
1224 spin_unlock(&rpc->crpc_lock);
1228 spin_unlock(&rpc->crpc_lock);
1230 switch (wi->swi_state) {
1233 case SWI_STATE_NEWBORN:
1234 LASSERT (!srpc_event_pending(rpc));
1236 rc = srpc_prepare_reply(rpc);
1238 srpc_client_rpc_done(rpc, rc);
1242 rc = srpc_prepare_bulk(rpc);
1245 wi->swi_state = SWI_STATE_REQUEST_SUBMITTED;
1246 rc = srpc_send_request(rpc);
1249 case SWI_STATE_REQUEST_SUBMITTED:
1250 /* CAVEAT EMPTOR: rqtev, rpyev, and bulkev may come in any
1251 * order; however, they're processed in a strict order:
1252 * rqt, rpy, and bulk. */
1253 if (!rpc->crpc_reqstev.ev_fired) break;
1255 rc = rpc->crpc_reqstev.ev_status;
1258 wi->swi_state = SWI_STATE_REQUEST_SENT;
1259 /* perhaps more events, fall thru */
1260 case SWI_STATE_REQUEST_SENT: {
1261 srpc_msg_type_t type = srpc_service2reply(rpc->crpc_service);
1263 if (!rpc->crpc_replyev.ev_fired) break;
1265 rc = rpc->crpc_replyev.ev_status;
1268 srpc_unpack_msg_hdr(reply);
1269 if (reply->msg_type != type ||
1270 (reply->msg_magic != SRPC_MSG_MAGIC &&
1271 reply->msg_magic != __swab32(SRPC_MSG_MAGIC))) {
1272 CWARN ("Bad message from %s: type %u (%d expected),"
1273 " magic %u (%d expected).\n",
1274 libcfs_id2str(rpc->crpc_dest),
1275 reply->msg_type, type,
1276 reply->msg_magic, SRPC_MSG_MAGIC);
1281 if (do_bulk && reply->msg_body.reply.status != 0) {
1282 CWARN ("Remote error %d at %s, unlink bulk buffer in "
1283 "case peer didn't initiate bulk transfer\n",
1284 reply->msg_body.reply.status,
1285 libcfs_id2str(rpc->crpc_dest));
1286 LNetMDUnlink(rpc->crpc_bulk.bk_mdh);
1289 wi->swi_state = SWI_STATE_REPLY_RECEIVED;
1291 case SWI_STATE_REPLY_RECEIVED:
1292 if (do_bulk && !rpc->crpc_bulkev.ev_fired) break;
1294 rc = do_bulk ? rpc->crpc_bulkev.ev_status : 0;
1296 /* Bulk buffer was unlinked due to remote error. Clear error
1297 * since reply buffer still contains valid data.
1298 * NB rpc->crpc_done shouldn't look into bulk data in case of
1300 if (do_bulk && rpc->crpc_bulkev.ev_lnet == LNET_EVENT_UNLINK &&
1301 rpc->crpc_status == 0 && reply->msg_body.reply.status != 0)
1304 wi->swi_state = SWI_STATE_DONE;
1305 srpc_client_rpc_done(rpc, rc);
1310 spin_lock(&rpc->crpc_lock);
1311 srpc_abort_rpc(rpc, rc);
1312 spin_unlock(&rpc->crpc_lock);
1316 if (rpc->crpc_aborted) {
1317 LNetMDUnlink(rpc->crpc_reqstmdh);
1318 LNetMDUnlink(rpc->crpc_replymdh);
1319 LNetMDUnlink(rpc->crpc_bulk.bk_mdh);
1321 if (!srpc_event_pending(rpc)) {
1322 srpc_client_rpc_done(rpc, -EINTR);
1330 srpc_create_client_rpc (lnet_process_id_t peer, int service,
1331 int nbulkiov, int bulklen,
1332 void (*rpc_done)(srpc_client_rpc_t *),
1333 void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
1335 srpc_client_rpc_t *rpc;
1337 LIBCFS_ALLOC(rpc, offsetof(srpc_client_rpc_t,
1338 crpc_bulk.bk_iovs[nbulkiov]));
1342 srpc_init_client_rpc(rpc, peer, service, nbulkiov,
1343 bulklen, rpc_done, rpc_fini, priv);
1347 /* called with rpc->crpc_lock held */
1349 srpc_abort_rpc (srpc_client_rpc_t *rpc, int why)
1353 if (rpc->crpc_aborted || /* already aborted */
1354 rpc->crpc_closed) /* callback imminent */
1358 "Aborting RPC: service %d, peer %s, state %s, why %d\n",
1359 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1360 swi_state2str(rpc->crpc_wi.swi_state), why);
1362 rpc->crpc_aborted = 1;
1363 rpc->crpc_status = why;
1364 swi_schedule_workitem(&rpc->crpc_wi);
1368 /* called with rpc->crpc_lock held */
1370 srpc_post_rpc (srpc_client_rpc_t *rpc)
1372 LASSERT (!rpc->crpc_aborted);
1373 LASSERT (srpc_data.rpc_state == SRPC_STATE_RUNNING);
1375 CDEBUG (D_NET, "Posting RPC: peer %s, service %d, timeout %d\n",
1376 libcfs_id2str(rpc->crpc_dest), rpc->crpc_service,
1379 srpc_add_client_rpc_timer(rpc);
1380 swi_schedule_workitem(&rpc->crpc_wi);
1386 srpc_send_reply(struct srpc_server_rpc *rpc)
1388 srpc_event_t *ev = &rpc->srpc_ev;
1389 struct srpc_msg *msg = &rpc->srpc_replymsg;
1390 struct srpc_buffer *buffer = rpc->srpc_reqstbuf;
1391 struct srpc_service_cd *scd = rpc->srpc_scd;
1392 struct srpc_service *sv = scd->scd_svc;
1396 LASSERT(buffer != NULL);
1397 rpyid = buffer->buf_msg.msg_body.reqst.rpyid;
1399 spin_lock(&scd->scd_lock);
1401 if (!sv->sv_shuttingdown && !srpc_serv_is_framework(sv)) {
1402 /* Repost buffer before replying since test client
1403 * might send me another RPC once it gets the reply */
1404 if (srpc_service_post_buffer(scd, buffer) != 0)
1405 CWARN("Failed to repost %s buffer\n", sv->sv_name);
1406 rpc->srpc_reqstbuf = NULL;
1409 spin_unlock(&scd->scd_lock);
1413 ev->ev_type = SRPC_REPLY_SENT;
1415 msg->msg_magic = SRPC_MSG_MAGIC;
1416 msg->msg_version = SRPC_MSG_VERSION;
1417 msg->msg_type = srpc_service2reply(sv->sv_id);
1419 rc = srpc_post_active_rdma(SRPC_RDMA_PORTAL, rpyid, msg,
1420 sizeof(*msg), LNET_MD_OP_PUT,
1421 rpc->srpc_peer, rpc->srpc_self,
1422 &rpc->srpc_replymdh, ev);
1424 ev->ev_fired = 1; /* no more event expected */
1428 /* when in kernel always called with LNET_LOCK() held, and in thread context */
1430 srpc_lnet_ev_handler(lnet_event_t *ev)
1432 struct srpc_service_cd *scd;
1433 srpc_event_t *rpcev = ev->md.user_ptr;
1434 srpc_client_rpc_t *crpc;
1435 srpc_server_rpc_t *srpc;
1436 srpc_buffer_t *buffer;
1439 srpc_msg_type_t type;
1441 LASSERT (!cfs_in_interrupt());
1443 if (ev->status != 0) {
1444 spin_lock(&srpc_data.rpc_glock);
1445 srpc_data.rpc_counters.errors++;
1446 spin_unlock(&srpc_data.rpc_glock);
1449 rpcev->ev_lnet = ev->type;
1451 switch (rpcev->ev_type) {
1453 CERROR("Unknown event: status %d, type %d, lnet %d\n",
1454 rpcev->ev_status, rpcev->ev_type, rpcev->ev_lnet);
1456 case SRPC_REQUEST_SENT:
1457 if (ev->status == 0 && ev->type != LNET_EVENT_UNLINK) {
1458 spin_lock(&srpc_data.rpc_glock);
1459 srpc_data.rpc_counters.rpcs_sent++;
1460 spin_unlock(&srpc_data.rpc_glock);
1462 case SRPC_REPLY_RCVD:
1463 case SRPC_BULK_REQ_RCVD:
1464 crpc = rpcev->ev_data;
1466 if (rpcev != &crpc->crpc_reqstev &&
1467 rpcev != &crpc->crpc_replyev &&
1468 rpcev != &crpc->crpc_bulkev) {
1469 CERROR("rpcev %p, crpc %p, reqstev %p, replyev %p, bulkev %p\n",
1470 rpcev, crpc, &crpc->crpc_reqstev,
1471 &crpc->crpc_replyev, &crpc->crpc_bulkev);
1472 CERROR("Bad event: status %d, type %d, lnet %d\n",
1473 rpcev->ev_status, rpcev->ev_type, rpcev->ev_lnet);
1477 spin_lock(&crpc->crpc_lock);
1479 LASSERT(rpcev->ev_fired == 0);
1480 rpcev->ev_fired = 1;
1481 rpcev->ev_status = (ev->type == LNET_EVENT_UNLINK) ?
1482 -EINTR : ev->status;
1483 swi_schedule_workitem(&crpc->crpc_wi);
1485 spin_unlock(&crpc->crpc_lock);
1488 case SRPC_REQUEST_RCVD:
1489 scd = rpcev->ev_data;
1492 LASSERT(rpcev == &scd->scd_ev);
1494 spin_lock(&scd->scd_lock);
1496 LASSERT (ev->unlinked);
1497 LASSERT (ev->type == LNET_EVENT_PUT ||
1498 ev->type == LNET_EVENT_UNLINK);
1499 LASSERT (ev->type != LNET_EVENT_UNLINK ||
1500 sv->sv_shuttingdown);
1502 buffer = container_of(ev->md.start, srpc_buffer_t, buf_msg);
1503 buffer->buf_peer = ev->initiator;
1504 buffer->buf_self = ev->target.nid;
1506 LASSERT(scd->scd_buf_nposted > 0);
1507 scd->scd_buf_nposted--;
1509 if (sv->sv_shuttingdown) {
1510 /* Leave buffer on scd->scd_buf_nposted since
1511 * srpc_finish_service needs to traverse it. */
1512 spin_unlock(&scd->scd_lock);
1516 if (scd->scd_buf_err_stamp != 0 &&
1517 scd->scd_buf_err_stamp < cfs_time_current_sec()) {
1518 /* re-enable adding buffer */
1519 scd->scd_buf_err_stamp = 0;
1520 scd->scd_buf_err = 0;
1523 if (scd->scd_buf_err == 0 && /* adding buffer is enabled */
1524 scd->scd_buf_adjust == 0 &&
1525 scd->scd_buf_nposted < scd->scd_buf_low) {
1526 scd->scd_buf_adjust = MAX(scd->scd_buf_total / 2,
1528 swi_schedule_workitem(&scd->scd_buf_wi);
1531 cfs_list_del(&buffer->buf_list); /* from scd->scd_buf_posted */
1532 msg = &buffer->buf_msg;
1533 type = srpc_service2request(sv->sv_id);
1535 if (ev->status != 0 || ev->mlength != sizeof(*msg) ||
1536 (msg->msg_type != type &&
1537 msg->msg_type != __swab32(type)) ||
1538 (msg->msg_magic != SRPC_MSG_MAGIC &&
1539 msg->msg_magic != __swab32(SRPC_MSG_MAGIC))) {
1540 CERROR ("Dropping RPC (%s) from %s: "
1541 "status %d mlength %d type %u magic %u.\n",
1542 sv->sv_name, libcfs_id2str(ev->initiator),
1543 ev->status, ev->mlength,
1544 msg->msg_type, msg->msg_magic);
1546 /* NB can't call srpc_service_recycle_buffer here since
1547 * it may call LNetM[DE]Attach. The invalid magic tells
1548 * srpc_handle_rpc to drop this RPC */
1552 if (!cfs_list_empty(&scd->scd_rpc_free)) {
1553 srpc = cfs_list_entry(scd->scd_rpc_free.next,
1554 struct srpc_server_rpc,
1556 cfs_list_del(&srpc->srpc_list);
1558 srpc_init_server_rpc(srpc, scd, buffer);
1559 cfs_list_add_tail(&srpc->srpc_list,
1560 &scd->scd_rpc_active);
1561 swi_schedule_workitem(&srpc->srpc_wi);
1563 cfs_list_add_tail(&buffer->buf_list,
1564 &scd->scd_buf_blocked);
1567 spin_unlock(&scd->scd_lock);
1569 spin_lock(&srpc_data.rpc_glock);
1570 srpc_data.rpc_counters.rpcs_rcvd++;
1571 spin_unlock(&srpc_data.rpc_glock);
1574 case SRPC_BULK_GET_RPLD:
1575 LASSERT (ev->type == LNET_EVENT_SEND ||
1576 ev->type == LNET_EVENT_REPLY ||
1577 ev->type == LNET_EVENT_UNLINK);
1580 break; /* wait for final event */
1582 case SRPC_BULK_PUT_SENT:
1583 if (ev->status == 0 && ev->type != LNET_EVENT_UNLINK) {
1584 spin_lock(&srpc_data.rpc_glock);
1586 if (rpcev->ev_type == SRPC_BULK_GET_RPLD)
1587 srpc_data.rpc_counters.bulk_get += ev->mlength;
1589 srpc_data.rpc_counters.bulk_put += ev->mlength;
1591 spin_unlock(&srpc_data.rpc_glock);
1593 case SRPC_REPLY_SENT:
1594 srpc = rpcev->ev_data;
1595 scd = srpc->srpc_scd;
1597 LASSERT(rpcev == &srpc->srpc_ev);
1599 spin_lock(&scd->scd_lock);
1601 rpcev->ev_fired = 1;
1602 rpcev->ev_status = (ev->type == LNET_EVENT_UNLINK) ?
1603 -EINTR : ev->status;
1604 swi_schedule_workitem(&srpc->srpc_wi);
1606 spin_unlock(&scd->scd_lock);
1614 srpc_check_event (int timeout)
1620 rc = LNetEQPoll(&srpc_data.rpc_lnet_eq, 1,
1621 timeout * 1000, &ev, &i);
1622 if (rc == 0) return 0;
1624 LASSERT (rc == -EOVERFLOW || rc == 1);
1626 /* We can't affort to miss any events... */
1627 if (rc == -EOVERFLOW) {
1628 CERROR ("Dropped an event!!!\n");
1632 srpc_lnet_ev_handler(&ev);
1643 memset(&srpc_data, 0, sizeof(struct smoketest_rpc));
1644 spin_lock_init(&srpc_data.rpc_glock);
1646 /* 1 second pause to avoid timestamp reuse */
1647 cfs_pause(cfs_time_seconds(1));
1648 srpc_data.rpc_matchbits = ((__u64) cfs_time_current_sec()) << 48;
1650 srpc_data.rpc_state = SRPC_STATE_NONE;
1653 rc = LNetNIInit(LUSTRE_SRV_LNET_PID);
1655 if (the_lnet.ln_server_mode_flag)
1656 rc = LNetNIInit(LUSTRE_SRV_LNET_PID);
1658 rc = LNetNIInit(getpid() | LNET_PID_USERFLAG);
1661 CERROR ("LNetNIInit() has failed: %d\n", rc);
1665 srpc_data.rpc_state = SRPC_STATE_NI_INIT;
1667 LNetInvalidateHandle(&srpc_data.rpc_lnet_eq);
1669 rc = LNetEQAlloc(0, srpc_lnet_ev_handler, &srpc_data.rpc_lnet_eq);
1671 rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &srpc_data.rpc_lnet_eq);
1674 CERROR("LNetEQAlloc() has failed: %d\n", rc);
1678 rc = LNetSetLazyPortal(SRPC_FRAMEWORK_REQUEST_PORTAL);
1680 rc = LNetSetLazyPortal(SRPC_REQUEST_PORTAL);
1683 srpc_data.rpc_state = SRPC_STATE_EQ_INIT;
1691 srpc_data.rpc_state = SRPC_STATE_RUNNING;
1697 srpc_shutdown (void)
1703 state = srpc_data.rpc_state;
1704 srpc_data.rpc_state = SRPC_STATE_STOPPING;
1709 case SRPC_STATE_RUNNING:
1710 spin_lock(&srpc_data.rpc_glock);
1712 for (i = 0; i <= SRPC_SERVICE_MAX_ID; i++) {
1713 srpc_service_t *sv = srpc_data.rpc_services[i];
1715 LASSERTF (sv == NULL,
1716 "service not empty: id %d, name %s\n",
1720 spin_unlock(&srpc_data.rpc_glock);
1724 case SRPC_STATE_EQ_INIT:
1725 rc = LNetClearLazyPortal(SRPC_FRAMEWORK_REQUEST_PORTAL);
1726 rc = LNetClearLazyPortal(SRPC_REQUEST_PORTAL);
1728 rc = LNetEQFree(srpc_data.rpc_lnet_eq);
1729 LASSERT (rc == 0); /* the EQ should have no user by now */
1731 case SRPC_STATE_NI_INIT: