Whamcloud - gitweb
LU-3195 tests: Interop 2.3.0<->2.3.63 sanity-quota test_6
[fs/lustre-release.git] / lnet / selftest / rpc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/selftest/rpc.c
37  *
38  * Author: Isaac Huang <isaac@clusterfs.com>
39  *
40  * 2012-05-13: Liang Zhen <liang@whamcloud.com>
41  * - percpt data for service to improve smp performance
42  * - code cleanup
43  */
44
45 #define DEBUG_SUBSYSTEM S_LNET
46
47 #include "selftest.h"
48
49 typedef enum {
50         SRPC_STATE_NONE,
51         SRPC_STATE_NI_INIT,
52         SRPC_STATE_EQ_INIT,
53         SRPC_STATE_RUNNING,
54         SRPC_STATE_STOPPING,
55 } srpc_state_t;
56
57 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 */
64 } srpc_data;
65
66 static inline int
67 srpc_serv_portal(int svc_id)
68 {
69         return svc_id < SRPC_FRAMEWORK_SERVICE_MAX_ID ?
70                SRPC_FRAMEWORK_REQUEST_PORTAL : SRPC_REQUEST_PORTAL;
71 }
72
73 /* forward ref's */
74 int srpc_handle_rpc (swi_workitem_t *wi);
75
76 void srpc_get_counters (srpc_counters_t *cnt)
77 {
78         spin_lock(&srpc_data.rpc_glock);
79         *cnt = srpc_data.rpc_counters;
80         spin_unlock(&srpc_data.rpc_glock);
81 }
82
83 void srpc_set_counters (const srpc_counters_t *cnt)
84 {
85         spin_lock(&srpc_data.rpc_glock);
86         srpc_data.rpc_counters = *cnt;
87         spin_unlock(&srpc_data.rpc_glock);
88 }
89
90 int
91 srpc_add_bulk_page(srpc_bulk_t *bk, cfs_page_t *pg, int i, int nob)
92 {
93         nob = min(nob, (int)CFS_PAGE_SIZE);
94
95         LASSERT(nob > 0);
96         LASSERT(i >= 0 && i < bk->bk_niov);
97
98 #ifdef __KERNEL__
99         bk->bk_iovs[i].kiov_offset = 0;
100         bk->bk_iovs[i].kiov_page   = pg;
101         bk->bk_iovs[i].kiov_len    = nob;
102 #else
103         LASSERT(bk->bk_pages != NULL);
104
105         bk->bk_pages[i] = pg;
106         bk->bk_iovs[i].iov_len  = nob;
107         bk->bk_iovs[i].iov_base = cfs_page_address(pg);
108 #endif
109         return nob;
110 }
111
112 void
113 srpc_free_bulk (srpc_bulk_t *bk)
114 {
115         int         i;
116         cfs_page_t *pg;
117
118         LASSERT (bk != NULL);
119 #ifndef __KERNEL__
120         LASSERT (bk->bk_pages != NULL);
121 #endif
122
123         for (i = 0; i < bk->bk_niov; i++) {
124 #ifdef __KERNEL__
125                 pg = bk->bk_iovs[i].kiov_page;
126 #else
127                 pg = bk->bk_pages[i];
128 #endif
129                 if (pg == NULL) break;
130
131                 cfs_free_page(pg);
132         }
133
134 #ifndef __KERNEL__
135         LIBCFS_FREE(bk->bk_pages, sizeof(cfs_page_t *) * bk->bk_niov);
136 #endif
137         LIBCFS_FREE(bk, offsetof(srpc_bulk_t, bk_iovs[bk->bk_niov]));
138         return;
139 }
140
141 srpc_bulk_t *
142 srpc_alloc_bulk(int cpt, unsigned bulk_npg, unsigned bulk_len, int sink)
143 {
144         srpc_bulk_t  *bk;
145         cfs_page_t  **pages;
146         int           i;
147
148         LASSERT(bulk_npg > 0 && bulk_npg <= LNET_MAX_IOV);
149
150         LIBCFS_CPT_ALLOC(bk, lnet_cpt_table(), cpt,
151                          offsetof(srpc_bulk_t, bk_iovs[bulk_npg]));
152         if (bk == NULL) {
153                 CERROR("Can't allocate descriptor for %d pages\n", bulk_npg);
154                 return NULL;
155         }
156
157         memset(bk, 0, offsetof(srpc_bulk_t, bk_iovs[bulk_npg]));
158         bk->bk_sink   = sink;
159         bk->bk_len    = bulk_len;
160         bk->bk_niov   = bulk_npg;
161 #ifndef __KERNEL__
162         LIBCFS_CPT_ALLOC(pages, lnet_cpt_table(), cpt,
163                          sizeof(cfs_page_t *) * bulk_npg);
164         if (pages == NULL) {
165                 LIBCFS_FREE(bk, offsetof(srpc_bulk_t, bk_iovs[bulk_npg]));
166                 CERROR("Can't allocate page array for %d pages\n", bulk_npg);
167                 return NULL;
168         }
169
170         memset(pages, 0, sizeof(cfs_page_t *) * bulk_npg);
171         bk->bk_pages = pages;
172 #else
173         UNUSED(pages);
174 #endif
175
176         for (i = 0; i < bulk_npg; i++) {
177                 cfs_page_t *pg;
178                 int         nob;
179
180                 pg = cfs_page_cpt_alloc(lnet_cpt_table(), cpt, CFS_ALLOC_STD);
181                 if (pg == NULL) {
182                         CERROR("Can't allocate page %d of %d\n", i, bulk_npg);
183                         srpc_free_bulk(bk);
184                         return NULL;
185                 }
186
187                 nob = srpc_add_bulk_page(bk, pg, i, bulk_len);
188                 bulk_len -= nob;
189         }
190
191         return bk;
192 }
193
194 static inline __u64
195 srpc_next_id (void)
196 {
197         __u64 id;
198
199         spin_lock(&srpc_data.rpc_glock);
200         id = srpc_data.rpc_matchbits++;
201         spin_unlock(&srpc_data.rpc_glock);
202         return id;
203 }
204
205 void
206 srpc_init_server_rpc(struct srpc_server_rpc *rpc,
207                      struct srpc_service_cd *scd,
208                      struct srpc_buffer *buffer)
209 {
210         memset(rpc, 0, sizeof(*rpc));
211         swi_init_workitem(&rpc->srpc_wi, rpc, srpc_handle_rpc,
212                           srpc_serv_is_framework(scd->scd_svc) ?
213                           lst_sched_serial : lst_sched_test[scd->scd_cpt]);
214
215         rpc->srpc_ev.ev_fired = 1; /* no event expected now */
216
217         rpc->srpc_scd      = scd;
218         rpc->srpc_reqstbuf = buffer;
219         rpc->srpc_peer     = buffer->buf_peer;
220         rpc->srpc_self     = buffer->buf_self;
221         LNetInvalidateHandle(&rpc->srpc_replymdh);
222 }
223
224 static void
225 srpc_service_fini(struct srpc_service *svc)
226 {
227         struct srpc_service_cd  *scd;
228         struct srpc_server_rpc  *rpc;
229         struct srpc_buffer      *buf;
230         cfs_list_t              *q;
231         int                     i;
232
233         if (svc->sv_cpt_data == NULL)
234                 return;
235
236         cfs_percpt_for_each(scd, i, svc->sv_cpt_data) {
237                 while (1) {
238                         if (!cfs_list_empty(&scd->scd_buf_posted))
239                                 q = &scd->scd_buf_posted;
240                         else if (!cfs_list_empty(&scd->scd_buf_blocked))
241                                 q = &scd->scd_buf_blocked;
242                         else
243                                 break;
244
245                         while (!cfs_list_empty(q)) {
246                                 buf = cfs_list_entry(q->next,
247                                                      struct srpc_buffer,
248                                                      buf_list);
249                                 cfs_list_del(&buf->buf_list);
250                                 LIBCFS_FREE(buf, sizeof(*buf));
251                         }
252                 }
253
254                 LASSERT(cfs_list_empty(&scd->scd_rpc_active));
255
256                 while (!cfs_list_empty(&scd->scd_rpc_free)) {
257                         rpc = cfs_list_entry(scd->scd_rpc_free.next,
258                                              struct srpc_server_rpc,
259                                              srpc_list);
260                         cfs_list_del(&rpc->srpc_list);
261                         LIBCFS_FREE(rpc, sizeof(*rpc));
262                 }
263         }
264
265         cfs_percpt_free(svc->sv_cpt_data);
266         svc->sv_cpt_data = NULL;
267 }
268
269 static int
270 srpc_service_nrpcs(struct srpc_service *svc)
271 {
272         int nrpcs = svc->sv_wi_total / svc->sv_ncpts;
273
274         return srpc_serv_is_framework(svc) ?
275                max(nrpcs, SFW_FRWK_WI_MIN) : max(nrpcs, SFW_TEST_WI_MIN);
276 }
277
278 int srpc_add_buffer(struct swi_workitem *wi);
279
280 static int
281 srpc_service_init(struct srpc_service *svc)
282 {
283         struct srpc_service_cd  *scd;
284         struct srpc_server_rpc  *rpc;
285         int                     nrpcs;
286         int                     i;
287         int                     j;
288
289         svc->sv_shuttingdown = 0;
290
291         svc->sv_cpt_data = cfs_percpt_alloc(lnet_cpt_table(),
292                                             sizeof(struct srpc_service_cd));
293         if (svc->sv_cpt_data == NULL)
294                 return -ENOMEM;
295
296         svc->sv_ncpts = srpc_serv_is_framework(svc) ?
297                         1 : cfs_cpt_number(lnet_cpt_table());
298         nrpcs = srpc_service_nrpcs(svc);
299
300         cfs_percpt_for_each(scd, i, svc->sv_cpt_data) {
301                 scd->scd_cpt = i;
302                 scd->scd_svc = svc;
303                 spin_lock_init(&scd->scd_lock);
304                 CFS_INIT_LIST_HEAD(&scd->scd_rpc_free);
305                 CFS_INIT_LIST_HEAD(&scd->scd_rpc_active);
306                 CFS_INIT_LIST_HEAD(&scd->scd_buf_posted);
307                 CFS_INIT_LIST_HEAD(&scd->scd_buf_blocked);
308
309                 scd->scd_ev.ev_data = scd;
310                 scd->scd_ev.ev_type = SRPC_REQUEST_RCVD;
311
312                 /* NB: don't use lst_sched_serial for adding buffer,
313                  * see details in srpc_service_add_buffers() */
314                 swi_init_workitem(&scd->scd_buf_wi, scd,
315                                   srpc_add_buffer, lst_sched_test[i]);
316
317                 if (i != 0 && srpc_serv_is_framework(svc)) {
318                         /* NB: framework service only needs srpc_service_cd for
319                          * one partition, but we allocate for all to make
320                          * it easier to implement, it will waste a little
321                          * memory but nobody should care about this */
322                         continue;
323                 }
324
325                 for (j = 0; j < nrpcs; j++) {
326                         LIBCFS_CPT_ALLOC(rpc, lnet_cpt_table(),
327                                          i, sizeof(*rpc));
328                         if (rpc == NULL) {
329                                 srpc_service_fini(svc);
330                                 return -ENOMEM;
331                         }
332                         cfs_list_add(&rpc->srpc_list, &scd->scd_rpc_free);
333                 }
334         }
335
336         return 0;
337 }
338
339 int
340 srpc_add_service(struct srpc_service *sv)
341 {
342         int id = sv->sv_id;
343
344         LASSERT(0 <= id && id <= SRPC_SERVICE_MAX_ID);
345
346         if (srpc_service_init(sv) != 0)
347                 return -ENOMEM;
348
349         spin_lock(&srpc_data.rpc_glock);
350
351         LASSERT(srpc_data.rpc_state == SRPC_STATE_RUNNING);
352
353         if (srpc_data.rpc_services[id] != NULL) {
354                 spin_unlock(&srpc_data.rpc_glock);
355                 goto failed;
356         }
357
358         srpc_data.rpc_services[id] = sv;
359         spin_unlock(&srpc_data.rpc_glock);
360
361         CDEBUG(D_NET, "Adding service: id %d, name %s\n", id, sv->sv_name);
362         return 0;
363
364  failed:
365         srpc_service_fini(sv);
366         return -EBUSY;
367 }
368
369 int
370 srpc_remove_service (srpc_service_t *sv)
371 {
372         int id = sv->sv_id;
373
374         spin_lock(&srpc_data.rpc_glock);
375
376         if (srpc_data.rpc_services[id] != sv) {
377                 spin_unlock(&srpc_data.rpc_glock);
378                 return -ENOENT;
379         }
380
381         srpc_data.rpc_services[id] = NULL;
382         spin_unlock(&srpc_data.rpc_glock);
383         return 0;
384 }
385
386 int
387 srpc_post_passive_rdma(int portal, int local, __u64 matchbits, void *buf,
388                        int len, int options, lnet_process_id_t peer,
389                        lnet_handle_md_t *mdh, srpc_event_t *ev)
390 {
391         int              rc;
392         lnet_md_t        md;
393         lnet_handle_me_t meh;
394
395         rc = LNetMEAttach(portal, peer, matchbits, 0, LNET_UNLINK,
396                           local ? LNET_INS_LOCAL : LNET_INS_AFTER, &meh);
397         if (rc != 0) {
398                 CERROR ("LNetMEAttach failed: %d\n", rc);
399                 LASSERT (rc == -ENOMEM);
400                 return -ENOMEM;
401         }
402
403         md.threshold = 1;
404         md.user_ptr  = ev;
405         md.start     = buf;
406         md.length    = len;
407         md.options   = options;
408         md.eq_handle = srpc_data.rpc_lnet_eq;
409
410         rc = LNetMDAttach(meh, md, LNET_UNLINK, mdh);
411         if (rc != 0) {
412                 CERROR ("LNetMDAttach failed: %d\n", rc);
413                 LASSERT (rc == -ENOMEM);
414
415                 rc = LNetMEUnlink(meh);
416                 LASSERT (rc == 0);
417                 return -ENOMEM;
418         }
419
420         CDEBUG (D_NET,
421                 "Posted passive RDMA: peer %s, portal %d, matchbits "LPX64"\n",
422                 libcfs_id2str(peer), portal, matchbits);
423         return 0;
424 }
425
426 int
427 srpc_post_active_rdma(int portal, __u64 matchbits, void *buf, int len,
428                       int options, lnet_process_id_t peer, lnet_nid_t self,
429                       lnet_handle_md_t *mdh, srpc_event_t *ev)
430 {
431         int       rc;
432         lnet_md_t md;
433
434         md.user_ptr  = ev;
435         md.start     = buf;
436         md.length    = len;
437         md.eq_handle = srpc_data.rpc_lnet_eq;
438         md.threshold = ((options & LNET_MD_OP_GET) != 0) ? 2 : 1;
439         md.options   = options & ~(LNET_MD_OP_PUT | LNET_MD_OP_GET);
440
441         rc = LNetMDBind(md, LNET_UNLINK, mdh);
442         if (rc != 0) {
443                 CERROR ("LNetMDBind failed: %d\n", rc);
444                 LASSERT (rc == -ENOMEM);
445                 return -ENOMEM;
446         }
447
448         /* this is kind of an abuse of the LNET_MD_OP_{PUT,GET} options.
449          * they're only meaningful for MDs attached to an ME (i.e. passive
450          * buffers... */
451         if ((options & LNET_MD_OP_PUT) != 0) {
452                 rc = LNetPut(self, *mdh, LNET_NOACK_REQ, peer,
453                              portal, matchbits, 0, 0);
454         } else {
455                 LASSERT ((options & LNET_MD_OP_GET) != 0);
456
457                 rc = LNetGet(self, *mdh, peer, portal, matchbits, 0);
458         }
459
460         if (rc != 0) {
461                 CERROR ("LNet%s(%s, %d, "LPD64") failed: %d\n",
462                         ((options & LNET_MD_OP_PUT) != 0) ? "Put" : "Get",
463                         libcfs_id2str(peer), portal, matchbits, rc);
464
465                 /* The forthcoming unlink event will complete this operation
466                  * with failure, so fall through and return success here.
467                  */
468                 rc = LNetMDUnlink(*mdh);
469                 LASSERT (rc == 0);
470         } else {
471                 CDEBUG (D_NET,
472                         "Posted active RDMA: peer %s, portal %u, matchbits "LPX64"\n",
473                         libcfs_id2str(peer), portal, matchbits);
474         }
475         return 0;
476 }
477
478 int
479 srpc_post_active_rqtbuf(lnet_process_id_t peer, int service, void *buf,
480                         int len, lnet_handle_md_t *mdh, srpc_event_t *ev)
481 {
482         return srpc_post_active_rdma(srpc_serv_portal(service), service,
483                                      buf, len, LNET_MD_OP_PUT, peer,
484                                      LNET_NID_ANY, mdh, ev);
485 }
486
487 int
488 srpc_post_passive_rqtbuf(int service, int local, void *buf, int len,
489                          lnet_handle_md_t *mdh, srpc_event_t *ev)
490 {
491         lnet_process_id_t any = {0};
492
493         any.nid = LNET_NID_ANY;
494         any.pid = LNET_PID_ANY;
495
496         return srpc_post_passive_rdma(srpc_serv_portal(service),
497                                       local, service, buf, len,
498                                       LNET_MD_OP_PUT, any, mdh, ev);
499 }
500
501 int
502 srpc_service_post_buffer(struct srpc_service_cd *scd, struct srpc_buffer *buf)
503 {
504         struct srpc_service     *sv = scd->scd_svc;
505         struct srpc_msg         *msg = &buf->buf_msg;
506         int                     rc;
507
508         LNetInvalidateHandle(&buf->buf_mdh);
509         cfs_list_add(&buf->buf_list, &scd->scd_buf_posted);
510         scd->scd_buf_nposted++;
511         spin_unlock(&scd->scd_lock);
512
513         rc = srpc_post_passive_rqtbuf(sv->sv_id,
514                                       !srpc_serv_is_framework(sv),
515                                       msg, sizeof(*msg), &buf->buf_mdh,
516                                       &scd->scd_ev);
517
518         /* At this point, a RPC (new or delayed) may have arrived in
519          * msg and its event handler has been called. So we must add
520          * buf to scd_buf_posted _before_ dropping scd_lock */
521
522         spin_lock(&scd->scd_lock);
523
524         if (rc == 0) {
525                 if (!sv->sv_shuttingdown)
526                         return 0;
527
528                 spin_unlock(&scd->scd_lock);
529                 /* srpc_shutdown_service might have tried to unlink me
530                  * when my buf_mdh was still invalid */
531                 LNetMDUnlink(buf->buf_mdh);
532                 spin_lock(&scd->scd_lock);
533                 return 0;
534         }
535
536         scd->scd_buf_nposted--;
537         if (sv->sv_shuttingdown)
538                 return rc; /* don't allow to change scd_buf_posted */
539
540         cfs_list_del(&buf->buf_list);
541         spin_unlock(&scd->scd_lock);
542
543         LIBCFS_FREE(buf, sizeof(*buf));
544
545         spin_lock(&scd->scd_lock);
546         return rc;
547 }
548
549 int
550 srpc_add_buffer(struct swi_workitem *wi)
551 {
552         struct srpc_service_cd  *scd = wi->swi_workitem.wi_data;
553         struct srpc_buffer      *buf;
554         int                     rc = 0;
555
556         /* it's called by workitem scheduler threads, these threads
557          * should have been set CPT affinity, so buffers will be posted
558          * on CPT local list of Portal */
559         spin_lock(&scd->scd_lock);
560
561         while (scd->scd_buf_adjust > 0 &&
562                !scd->scd_svc->sv_shuttingdown) {
563                 scd->scd_buf_adjust--; /* consume it */
564                 scd->scd_buf_posting++;
565
566                 spin_unlock(&scd->scd_lock);
567
568                 LIBCFS_ALLOC(buf, sizeof(*buf));
569                 if (buf == NULL) {
570                         CERROR("Failed to add new buf to service: %s\n",
571                                scd->scd_svc->sv_name);
572                         spin_lock(&scd->scd_lock);
573                         rc = -ENOMEM;
574                         break;
575                 }
576
577                 spin_lock(&scd->scd_lock);
578                 if (scd->scd_svc->sv_shuttingdown) {
579                         spin_unlock(&scd->scd_lock);
580                         LIBCFS_FREE(buf, sizeof(*buf));
581
582                         spin_lock(&scd->scd_lock);
583                         rc = -ESHUTDOWN;
584                         break;
585                 }
586
587                 rc = srpc_service_post_buffer(scd, buf);
588                 if (rc != 0)
589                         break; /* buf has been freed inside */
590
591                 LASSERT(scd->scd_buf_posting > 0);
592                 scd->scd_buf_posting--;
593                 scd->scd_buf_total++;
594                 scd->scd_buf_low = MAX(2, scd->scd_buf_total / 4);
595         }
596
597         if (rc != 0) {
598                 scd->scd_buf_err_stamp = cfs_time_current_sec();
599                 scd->scd_buf_err = rc;
600
601                 LASSERT(scd->scd_buf_posting > 0);
602                 scd->scd_buf_posting--;
603         }
604
605         spin_unlock(&scd->scd_lock);
606         return 0;
607 }
608
609 int
610 srpc_service_add_buffers(struct srpc_service *sv, int nbuffer)
611 {
612         struct srpc_service_cd  *scd;
613         int                     rc = 0;
614         int                     i;
615
616         LASSERTF(nbuffer > 0, "nbuffer must be positive: %d\n", nbuffer);
617
618         cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
619                 spin_lock(&scd->scd_lock);
620
621                 scd->scd_buf_err = 0;
622                 scd->scd_buf_err_stamp = 0;
623                 scd->scd_buf_posting = 0;
624                 scd->scd_buf_adjust = nbuffer;
625                 /* start to post buffers */
626                 swi_schedule_workitem(&scd->scd_buf_wi);
627                 spin_unlock(&scd->scd_lock);
628
629                 /* framework service only post buffer for one partition  */
630                 if (srpc_serv_is_framework(sv))
631                         break;
632         }
633
634         cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
635                 spin_lock(&scd->scd_lock);
636                 /*
637                  * NB: srpc_service_add_buffers() can be called inside
638                  * thread context of lst_sched_serial, and we don't normally
639                  * allow to sleep inside thread context of WI scheduler
640                  * because it will block current scheduler thread from doing
641                  * anything else, even worse, it could deadlock if it's
642                  * waiting on result from another WI of the same scheduler.
643                  * However, it's safe at here because scd_buf_wi is scheduled
644                  * by thread in a different WI scheduler (lst_sched_test),
645                  * so we don't have any risk of deadlock, though this could
646                  * block all WIs pending on lst_sched_serial for a moment
647                  * which is not good but not fatal.
648                  */
649                 lst_wait_until(scd->scd_buf_err != 0 ||
650                                (scd->scd_buf_adjust == 0 &&
651                                 scd->scd_buf_posting == 0),
652                                scd->scd_lock, "waiting for adding buffer\n");
653
654                 if (scd->scd_buf_err != 0 && rc == 0)
655                         rc = scd->scd_buf_err;
656
657                 spin_unlock(&scd->scd_lock);
658         }
659
660         return rc;
661 }
662
663 void
664 srpc_service_remove_buffers(struct srpc_service *sv, int nbuffer)
665 {
666         struct srpc_service_cd  *scd;
667         int                     num;
668         int                     i;
669
670         LASSERT(!sv->sv_shuttingdown);
671
672         cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
673                 spin_lock(&scd->scd_lock);
674
675                 num = scd->scd_buf_total + scd->scd_buf_posting;
676                 scd->scd_buf_adjust -= min(nbuffer, num);
677
678                 spin_unlock(&scd->scd_lock);
679         }
680 }
681
682 /* returns 1 if sv has finished, otherwise 0 */
683 int
684 srpc_finish_service(struct srpc_service *sv)
685 {
686         struct srpc_service_cd  *scd;
687         struct srpc_server_rpc  *rpc;
688         int                     i;
689
690         LASSERT(sv->sv_shuttingdown); /* srpc_shutdown_service called */
691
692         cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
693                 spin_lock(&scd->scd_lock);
694                 if (!swi_deschedule_workitem(&scd->scd_buf_wi)) {
695                         spin_unlock(&scd->scd_lock);
696                         return 0;
697                 }
698
699                 if (scd->scd_buf_nposted > 0) {
700                         CDEBUG(D_NET, "waiting for %d posted buffers to unlink",
701                                scd->scd_buf_nposted);
702                         spin_unlock(&scd->scd_lock);
703                         return 0;
704                 }
705
706                 if (cfs_list_empty(&scd->scd_rpc_active)) {
707                         spin_unlock(&scd->scd_lock);
708                         continue;
709                 }
710
711                 rpc = cfs_list_entry(scd->scd_rpc_active.next,
712                                      struct srpc_server_rpc, srpc_list);
713                 CNETERR("Active RPC %p on shutdown: sv %s, peer %s, "
714                         "wi %s scheduled %d running %d, "
715                         "ev fired %d type %d status %d lnet %d\n",
716                         rpc, sv->sv_name, libcfs_id2str(rpc->srpc_peer),
717                         swi_state2str(rpc->srpc_wi.swi_state),
718                         rpc->srpc_wi.swi_workitem.wi_scheduled,
719                         rpc->srpc_wi.swi_workitem.wi_running,
720                         rpc->srpc_ev.ev_fired, rpc->srpc_ev.ev_type,
721                         rpc->srpc_ev.ev_status, rpc->srpc_ev.ev_lnet);
722                 spin_unlock(&scd->scd_lock);
723                 return 0;
724         }
725
726         /* no lock needed from now on */
727         srpc_service_fini(sv);
728         return 1;
729 }
730
731 /* called with sv->sv_lock held */
732 void
733 srpc_service_recycle_buffer(struct srpc_service_cd *scd, srpc_buffer_t *buf)
734 {
735         if (!scd->scd_svc->sv_shuttingdown && scd->scd_buf_adjust >= 0) {
736                 if (srpc_service_post_buffer(scd, buf) != 0) {
737                         CWARN("Failed to post %s buffer\n",
738                               scd->scd_svc->sv_name);
739                 }
740                 return;
741         }
742
743         /* service is shutting down, or we want to recycle some buffers */
744         scd->scd_buf_total--;
745
746         if (scd->scd_buf_adjust < 0) {
747                 scd->scd_buf_adjust++;
748                 if (scd->scd_buf_adjust < 0 &&
749                     scd->scd_buf_total == 0 && scd->scd_buf_posting == 0) {
750                         CDEBUG(D_INFO,
751                                "Try to recyle %d buffers but nothing left\n",
752                                scd->scd_buf_adjust);
753                         scd->scd_buf_adjust = 0;
754                 }
755         }
756
757         spin_unlock(&scd->scd_lock);
758         LIBCFS_FREE(buf, sizeof(*buf));
759         spin_lock(&scd->scd_lock);
760 }
761
762 void
763 srpc_abort_service(struct srpc_service *sv)
764 {
765         struct srpc_service_cd  *scd;
766         struct srpc_server_rpc  *rpc;
767         int                     i;
768
769         CDEBUG(D_NET, "Aborting service: id %d, name %s\n",
770                sv->sv_id, sv->sv_name);
771
772         cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
773                 spin_lock(&scd->scd_lock);
774
775                 /* schedule in-flight RPCs to notice the abort, NB:
776                  * racing with incoming RPCs; complete fix should make test
777                  * RPCs carry session ID in its headers */
778                 cfs_list_for_each_entry(rpc, &scd->scd_rpc_active, srpc_list) {
779                         rpc->srpc_aborted = 1;
780                         swi_schedule_workitem(&rpc->srpc_wi);
781                 }
782
783                 spin_unlock(&scd->scd_lock);
784         }
785 }
786
787 void
788 srpc_shutdown_service(srpc_service_t *sv)
789 {
790         struct srpc_service_cd  *scd;
791         struct srpc_server_rpc  *rpc;
792         srpc_buffer_t           *buf;
793         int                     i;
794
795         CDEBUG(D_NET, "Shutting down service: id %d, name %s\n",
796                sv->sv_id, sv->sv_name);
797
798         cfs_percpt_for_each(scd, i, sv->sv_cpt_data)
799                 spin_lock(&scd->scd_lock);
800
801         sv->sv_shuttingdown = 1; /* i.e. no new active RPC */
802
803         cfs_percpt_for_each(scd, i, sv->sv_cpt_data)
804                 spin_unlock(&scd->scd_lock);
805
806         cfs_percpt_for_each(scd, i, sv->sv_cpt_data) {
807                 spin_lock(&scd->scd_lock);
808
809                 /* schedule in-flight RPCs to notice the shutdown */
810                 cfs_list_for_each_entry(rpc, &scd->scd_rpc_active, srpc_list)
811                         swi_schedule_workitem(&rpc->srpc_wi);
812
813                 spin_unlock(&scd->scd_lock);
814
815                 /* OK to traverse scd_buf_posted without lock, since no one
816                  * touches scd_buf_posted now */
817                 cfs_list_for_each_entry(buf, &scd->scd_buf_posted, buf_list)
818                         LNetMDUnlink(buf->buf_mdh);
819         }
820 }
821
822 int
823 srpc_send_request (srpc_client_rpc_t *rpc)
824 {
825         srpc_event_t *ev = &rpc->crpc_reqstev;
826         int           rc;
827
828         ev->ev_fired = 0;
829         ev->ev_data  = rpc;
830         ev->ev_type  = SRPC_REQUEST_SENT;
831
832         rc = srpc_post_active_rqtbuf(rpc->crpc_dest, rpc->crpc_service,
833                                      &rpc->crpc_reqstmsg, sizeof(srpc_msg_t),
834                                      &rpc->crpc_reqstmdh, ev);
835         if (rc != 0) {
836                 LASSERT (rc == -ENOMEM);
837                 ev->ev_fired = 1;  /* no more event expected */
838         }
839         return rc;
840 }
841
842 int
843 srpc_prepare_reply (srpc_client_rpc_t *rpc)
844 {
845         srpc_event_t *ev = &rpc->crpc_replyev;
846         __u64        *id = &rpc->crpc_reqstmsg.msg_body.reqst.rpyid;
847         int           rc;
848
849         ev->ev_fired = 0;
850         ev->ev_data  = rpc;
851         ev->ev_type  = SRPC_REPLY_RCVD;
852
853         *id = srpc_next_id();
854
855         rc = srpc_post_passive_rdma(SRPC_RDMA_PORTAL, 0, *id,
856                                     &rpc->crpc_replymsg, sizeof(srpc_msg_t),
857                                     LNET_MD_OP_PUT, rpc->crpc_dest,
858                                     &rpc->crpc_replymdh, ev);
859         if (rc != 0) {
860                 LASSERT (rc == -ENOMEM);
861                 ev->ev_fired = 1;  /* no more event expected */
862         }
863         return rc;
864 }
865
866 int
867 srpc_prepare_bulk (srpc_client_rpc_t *rpc)
868 {
869         srpc_bulk_t  *bk = &rpc->crpc_bulk;
870         srpc_event_t *ev = &rpc->crpc_bulkev;
871         __u64        *id = &rpc->crpc_reqstmsg.msg_body.reqst.bulkid;
872         int           rc;
873         int           opt;
874
875         LASSERT (bk->bk_niov <= LNET_MAX_IOV);
876
877         if (bk->bk_niov == 0) return 0; /* nothing to do */
878
879         opt = bk->bk_sink ? LNET_MD_OP_PUT : LNET_MD_OP_GET;
880 #ifdef __KERNEL__
881         opt |= LNET_MD_KIOV;
882 #else
883         opt |= LNET_MD_IOVEC;
884 #endif
885
886         ev->ev_fired = 0;
887         ev->ev_data  = rpc;
888         ev->ev_type  = SRPC_BULK_REQ_RCVD;
889
890         *id = srpc_next_id();
891
892         rc = srpc_post_passive_rdma(SRPC_RDMA_PORTAL, 0, *id,
893                                     &bk->bk_iovs[0], bk->bk_niov, opt,
894                                     rpc->crpc_dest, &bk->bk_mdh, ev);
895         if (rc != 0) {
896                 LASSERT (rc == -ENOMEM);
897                 ev->ev_fired = 1;  /* no more event expected */
898         }
899         return rc;
900 }
901
902 int
903 srpc_do_bulk (srpc_server_rpc_t *rpc)
904 {
905         srpc_event_t  *ev = &rpc->srpc_ev;
906         srpc_bulk_t   *bk = rpc->srpc_bulk;
907         __u64          id = rpc->srpc_reqstbuf->buf_msg.msg_body.reqst.bulkid;
908         int            rc;
909         int            opt;
910
911         LASSERT (bk != NULL);
912
913         opt = bk->bk_sink ? LNET_MD_OP_GET : LNET_MD_OP_PUT;
914 #ifdef __KERNEL__
915         opt |= LNET_MD_KIOV;
916 #else
917         opt |= LNET_MD_IOVEC;
918 #endif
919
920         ev->ev_fired = 0;
921         ev->ev_data  = rpc;
922         ev->ev_type  = bk->bk_sink ? SRPC_BULK_GET_RPLD : SRPC_BULK_PUT_SENT;
923
924         rc = srpc_post_active_rdma(SRPC_RDMA_PORTAL, id,
925                                    &bk->bk_iovs[0], bk->bk_niov, opt,
926                                    rpc->srpc_peer, rpc->srpc_self,
927                                    &bk->bk_mdh, ev);
928         if (rc != 0)
929                 ev->ev_fired = 1;  /* no more event expected */
930         return rc;
931 }
932
933 /* only called from srpc_handle_rpc */
934 void
935 srpc_server_rpc_done(srpc_server_rpc_t *rpc, int status)
936 {
937         struct srpc_service_cd  *scd = rpc->srpc_scd;
938         struct srpc_service     *sv  = scd->scd_svc;
939         srpc_buffer_t           *buffer;
940
941         LASSERT (status != 0 || rpc->srpc_wi.swi_state == SWI_STATE_DONE);
942
943         rpc->srpc_status = status;
944
945         CDEBUG_LIMIT (status == 0 ? D_NET : D_NETERROR,
946                 "Server RPC %p done: service %s, peer %s, status %s:%d\n",
947                 rpc, sv->sv_name, libcfs_id2str(rpc->srpc_peer),
948                 swi_state2str(rpc->srpc_wi.swi_state), status);
949
950         if (status != 0) {
951                 spin_lock(&srpc_data.rpc_glock);
952                 srpc_data.rpc_counters.rpcs_dropped++;
953                 spin_unlock(&srpc_data.rpc_glock);
954         }
955
956         if (rpc->srpc_done != NULL)
957                 (*rpc->srpc_done) (rpc);
958         LASSERT(rpc->srpc_bulk == NULL);
959
960         spin_lock(&scd->scd_lock);
961
962         if (rpc->srpc_reqstbuf != NULL) {
963                 /* NB might drop sv_lock in srpc_service_recycle_buffer, but
964                  * sv won't go away for scd_rpc_active must not be empty */
965                 srpc_service_recycle_buffer(scd, rpc->srpc_reqstbuf);
966                 rpc->srpc_reqstbuf = NULL;
967         }
968
969         cfs_list_del(&rpc->srpc_list); /* from scd->scd_rpc_active */
970
971         /*
972          * No one can schedule me now since:
973          * - I'm not on scd_rpc_active.
974          * - all LNet events have been fired.
975          * Cancel pending schedules and prevent future schedule attempts:
976          */
977         LASSERT(rpc->srpc_ev.ev_fired);
978         swi_exit_workitem(&rpc->srpc_wi);
979
980         if (!sv->sv_shuttingdown && !cfs_list_empty(&scd->scd_buf_blocked)) {
981                 buffer = cfs_list_entry(scd->scd_buf_blocked.next,
982                                         srpc_buffer_t, buf_list);
983                 cfs_list_del(&buffer->buf_list);
984
985                 srpc_init_server_rpc(rpc, scd, buffer);
986                 cfs_list_add_tail(&rpc->srpc_list, &scd->scd_rpc_active);
987                 swi_schedule_workitem(&rpc->srpc_wi);
988         } else {
989                 cfs_list_add(&rpc->srpc_list, &scd->scd_rpc_free);
990         }
991
992         spin_unlock(&scd->scd_lock);
993         return;
994 }
995
996 /* handles an incoming RPC */
997 int
998 srpc_handle_rpc(swi_workitem_t *wi)
999 {
1000         struct srpc_server_rpc  *rpc = wi->swi_workitem.wi_data;
1001         struct srpc_service_cd  *scd = rpc->srpc_scd;
1002         struct srpc_service     *sv = scd->scd_svc;
1003         srpc_event_t            *ev = &rpc->srpc_ev;
1004         int                     rc = 0;
1005
1006         LASSERT(wi == &rpc->srpc_wi);
1007
1008         spin_lock(&scd->scd_lock);
1009
1010         if (sv->sv_shuttingdown || rpc->srpc_aborted) {
1011                 spin_unlock(&scd->scd_lock);
1012
1013                 if (rpc->srpc_bulk != NULL)
1014                         LNetMDUnlink(rpc->srpc_bulk->bk_mdh);
1015                 LNetMDUnlink(rpc->srpc_replymdh);
1016
1017                 if (ev->ev_fired) { /* no more event, OK to finish */
1018                         srpc_server_rpc_done(rpc, -ESHUTDOWN);
1019                         return 1;
1020                 }
1021                 return 0;
1022         }
1023
1024         spin_unlock(&scd->scd_lock);
1025
1026         switch (wi->swi_state) {
1027         default:
1028                 LBUG ();
1029         case SWI_STATE_NEWBORN: {
1030                 srpc_msg_t           *msg;
1031                 srpc_generic_reply_t *reply;
1032
1033                 msg = &rpc->srpc_reqstbuf->buf_msg;
1034                 reply = &rpc->srpc_replymsg.msg_body.reply;
1035
1036                 if (msg->msg_magic == 0) {
1037                         /* moaned already in srpc_lnet_ev_handler */
1038                         srpc_server_rpc_done(rpc, EBADMSG);
1039                         return 1;
1040                 }
1041
1042                 srpc_unpack_msg_hdr(msg);
1043                 if (msg->msg_version != SRPC_MSG_VERSION) {
1044                         CWARN("Version mismatch: %u, %u expected, from %s\n",
1045                               msg->msg_version, SRPC_MSG_VERSION,
1046                               libcfs_id2str(rpc->srpc_peer));
1047                         reply->status = EPROTO;
1048                         /* drop through and send reply */
1049                 } else {
1050                         reply->status = 0;
1051                         rc = (*sv->sv_handler)(rpc);
1052                         LASSERT(reply->status == 0 || !rpc->srpc_bulk);
1053                         if (rc != 0) {
1054                                 srpc_server_rpc_done(rpc, rc);
1055                                 return 1;
1056                         }
1057                 }
1058
1059                 wi->swi_state = SWI_STATE_BULK_STARTED;
1060
1061                 if (rpc->srpc_bulk != NULL) {
1062                         rc = srpc_do_bulk(rpc);
1063                         if (rc == 0)
1064                                 return 0; /* wait for bulk */
1065
1066                         LASSERT (ev->ev_fired);
1067                         ev->ev_status = rc;
1068                 }
1069         }
1070         case SWI_STATE_BULK_STARTED:
1071                 LASSERT (rpc->srpc_bulk == NULL || ev->ev_fired);
1072
1073                 if (rpc->srpc_bulk != NULL) {
1074                         rc = ev->ev_status;
1075
1076                         if (sv->sv_bulk_ready != NULL)
1077                                 rc = (*sv->sv_bulk_ready) (rpc, rc);
1078
1079                         if (rc != 0) {
1080                                 srpc_server_rpc_done(rpc, rc);
1081                                 return 1;
1082                         }
1083                 }
1084
1085                 wi->swi_state = SWI_STATE_REPLY_SUBMITTED;
1086                 rc = srpc_send_reply(rpc);
1087                 if (rc == 0)
1088                         return 0; /* wait for reply */
1089                 srpc_server_rpc_done(rpc, rc);
1090                 return 1;
1091
1092         case SWI_STATE_REPLY_SUBMITTED:
1093                 if (!ev->ev_fired) {
1094                         CERROR("RPC %p: bulk %p, service %d\n",
1095                                rpc, rpc->srpc_bulk, sv->sv_id);
1096                         CERROR("Event: status %d, type %d, lnet %d\n",
1097                                ev->ev_status, ev->ev_type, ev->ev_lnet);
1098                         LASSERT (ev->ev_fired);
1099                 }
1100
1101                 wi->swi_state = SWI_STATE_DONE;
1102                 srpc_server_rpc_done(rpc, ev->ev_status);
1103                 return 1;
1104         }
1105
1106         return 0;
1107 }
1108
1109 void
1110 srpc_client_rpc_expired (void *data)
1111 {
1112         srpc_client_rpc_t *rpc = data;
1113
1114         CWARN ("Client RPC expired: service %d, peer %s, timeout %d.\n",
1115                rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1116                rpc->crpc_timeout);
1117
1118         spin_lock(&rpc->crpc_lock);
1119
1120         rpc->crpc_timeout = 0;
1121         srpc_abort_rpc(rpc, -ETIMEDOUT);
1122
1123         spin_unlock(&rpc->crpc_lock);
1124
1125         spin_lock(&srpc_data.rpc_glock);
1126         srpc_data.rpc_counters.rpcs_expired++;
1127         spin_unlock(&srpc_data.rpc_glock);
1128 }
1129
1130 inline void
1131 srpc_add_client_rpc_timer (srpc_client_rpc_t *rpc)
1132 {
1133         stt_timer_t *timer = &rpc->crpc_timer;
1134
1135         if (rpc->crpc_timeout == 0) return;
1136
1137         CFS_INIT_LIST_HEAD(&timer->stt_list);
1138         timer->stt_data    = rpc;
1139         timer->stt_func    = srpc_client_rpc_expired;
1140         timer->stt_expires = cfs_time_add(rpc->crpc_timeout,
1141                                           cfs_time_current_sec());
1142         stt_add_timer(timer);
1143         return;
1144 }
1145
1146 /*
1147  * Called with rpc->crpc_lock held.
1148  *
1149  * Upon exit the RPC expiry timer is not queued and the handler is not
1150  * running on any CPU. */
1151 void
1152 srpc_del_client_rpc_timer (srpc_client_rpc_t *rpc)
1153 {
1154         /* timer not planted or already exploded */
1155         if (rpc->crpc_timeout == 0)
1156                 return;
1157
1158         /* timer sucessfully defused */
1159         if (stt_del_timer(&rpc->crpc_timer))
1160                 return;
1161
1162 #ifdef __KERNEL__
1163         /* timer detonated, wait for it to explode */
1164         while (rpc->crpc_timeout != 0) {
1165                 spin_unlock(&rpc->crpc_lock);
1166
1167                 cfs_schedule();
1168
1169                 spin_lock(&rpc->crpc_lock);
1170         }
1171 #else
1172         LBUG(); /* impossible in single-threaded runtime */
1173 #endif
1174 }
1175
1176 void
1177 srpc_client_rpc_done (srpc_client_rpc_t *rpc, int status)
1178 {
1179         swi_workitem_t *wi = &rpc->crpc_wi;
1180
1181         LASSERT(status != 0 || wi->swi_state == SWI_STATE_DONE);
1182
1183         spin_lock(&rpc->crpc_lock);
1184
1185         rpc->crpc_closed = 1;
1186         if (rpc->crpc_status == 0)
1187                 rpc->crpc_status = status;
1188
1189         srpc_del_client_rpc_timer(rpc);
1190
1191         CDEBUG_LIMIT ((status == 0) ? D_NET : D_NETERROR,
1192                 "Client RPC done: service %d, peer %s, status %s:%d:%d\n",
1193                 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1194                 swi_state2str(wi->swi_state), rpc->crpc_aborted, status);
1195
1196         /*
1197          * No one can schedule me now since:
1198          * - RPC timer has been defused.
1199          * - all LNet events have been fired.
1200          * - crpc_closed has been set, preventing srpc_abort_rpc from
1201          *   scheduling me.
1202          * Cancel pending schedules and prevent future schedule attempts:
1203          */
1204         LASSERT (!srpc_event_pending(rpc));
1205         swi_exit_workitem(wi);
1206
1207         spin_unlock(&rpc->crpc_lock);
1208
1209         (*rpc->crpc_done)(rpc);
1210         return;
1211 }
1212
1213 /* sends an outgoing RPC */
1214 int
1215 srpc_send_rpc (swi_workitem_t *wi)
1216 {
1217         int                rc = 0;
1218         srpc_client_rpc_t *rpc;
1219         srpc_msg_t        *reply;
1220         int                do_bulk;
1221
1222         LASSERT(wi != NULL);
1223
1224         rpc = wi->swi_workitem.wi_data;
1225
1226         LASSERT (rpc != NULL);
1227         LASSERT (wi == &rpc->crpc_wi);
1228
1229         reply = &rpc->crpc_replymsg;
1230         do_bulk = rpc->crpc_bulk.bk_niov > 0;
1231
1232         spin_lock(&rpc->crpc_lock);
1233
1234         if (rpc->crpc_aborted) {
1235                 spin_unlock(&rpc->crpc_lock);
1236                 goto abort;
1237         }
1238
1239         spin_unlock(&rpc->crpc_lock);
1240
1241         switch (wi->swi_state) {
1242         default:
1243                 LBUG ();
1244         case SWI_STATE_NEWBORN:
1245                 LASSERT (!srpc_event_pending(rpc));
1246
1247                 rc = srpc_prepare_reply(rpc);
1248                 if (rc != 0) {
1249                         srpc_client_rpc_done(rpc, rc);
1250                         return 1;
1251                 }
1252
1253                 rc = srpc_prepare_bulk(rpc);
1254                 if (rc != 0) break;
1255
1256                 wi->swi_state = SWI_STATE_REQUEST_SUBMITTED;
1257                 rc = srpc_send_request(rpc);
1258                 break;
1259
1260         case SWI_STATE_REQUEST_SUBMITTED:
1261                 /* CAVEAT EMPTOR: rqtev, rpyev, and bulkev may come in any
1262                  * order; however, they're processed in a strict order:
1263                  * rqt, rpy, and bulk. */
1264                 if (!rpc->crpc_reqstev.ev_fired) break;
1265
1266                 rc = rpc->crpc_reqstev.ev_status;
1267                 if (rc != 0) break;
1268
1269                 wi->swi_state = SWI_STATE_REQUEST_SENT;
1270                 /* perhaps more events, fall thru */
1271         case SWI_STATE_REQUEST_SENT: {
1272                 srpc_msg_type_t type = srpc_service2reply(rpc->crpc_service);
1273
1274                 if (!rpc->crpc_replyev.ev_fired) break;
1275
1276                 rc = rpc->crpc_replyev.ev_status;
1277                 if (rc != 0) break;
1278
1279                 srpc_unpack_msg_hdr(reply);
1280                 if (reply->msg_type != type ||
1281                     (reply->msg_magic != SRPC_MSG_MAGIC &&
1282                      reply->msg_magic != __swab32(SRPC_MSG_MAGIC))) {
1283                         CWARN ("Bad message from %s: type %u (%d expected),"
1284                                " magic %u (%d expected).\n",
1285                                libcfs_id2str(rpc->crpc_dest),
1286                                reply->msg_type, type,
1287                                reply->msg_magic, SRPC_MSG_MAGIC);
1288                         rc = -EBADMSG;
1289                         break;
1290                 }
1291
1292                 if (do_bulk && reply->msg_body.reply.status != 0) {
1293                         CWARN ("Remote error %d at %s, unlink bulk buffer in "
1294                                "case peer didn't initiate bulk transfer\n",
1295                                reply->msg_body.reply.status,
1296                                libcfs_id2str(rpc->crpc_dest));
1297                         LNetMDUnlink(rpc->crpc_bulk.bk_mdh);
1298                 }
1299
1300                 wi->swi_state = SWI_STATE_REPLY_RECEIVED;
1301         }
1302         case SWI_STATE_REPLY_RECEIVED:
1303                 if (do_bulk && !rpc->crpc_bulkev.ev_fired) break;
1304
1305                 rc = do_bulk ? rpc->crpc_bulkev.ev_status : 0;
1306
1307                 /* Bulk buffer was unlinked due to remote error. Clear error
1308                  * since reply buffer still contains valid data.
1309                  * NB rpc->crpc_done shouldn't look into bulk data in case of
1310                  * remote error. */
1311                 if (do_bulk && rpc->crpc_bulkev.ev_lnet == LNET_EVENT_UNLINK &&
1312                     rpc->crpc_status == 0 && reply->msg_body.reply.status != 0)
1313                         rc = 0;
1314
1315                 wi->swi_state = SWI_STATE_DONE;
1316                 srpc_client_rpc_done(rpc, rc);
1317                 return 1;
1318         }
1319
1320         if (rc != 0) {
1321                 spin_lock(&rpc->crpc_lock);
1322                 srpc_abort_rpc(rpc, rc);
1323                 spin_unlock(&rpc->crpc_lock);
1324         }
1325
1326 abort:
1327         if (rpc->crpc_aborted) {
1328                 LNetMDUnlink(rpc->crpc_reqstmdh);
1329                 LNetMDUnlink(rpc->crpc_replymdh);
1330                 LNetMDUnlink(rpc->crpc_bulk.bk_mdh);
1331
1332                 if (!srpc_event_pending(rpc)) {
1333                         srpc_client_rpc_done(rpc, -EINTR);
1334                         return 1;
1335                 }
1336         }
1337         return 0;
1338 }
1339
1340 srpc_client_rpc_t *
1341 srpc_create_client_rpc (lnet_process_id_t peer, int service,
1342                         int nbulkiov, int bulklen,
1343                         void (*rpc_done)(srpc_client_rpc_t *),
1344                         void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
1345 {
1346         srpc_client_rpc_t *rpc;
1347
1348         LIBCFS_ALLOC(rpc, offsetof(srpc_client_rpc_t,
1349                                    crpc_bulk.bk_iovs[nbulkiov]));
1350         if (rpc == NULL)
1351                 return NULL;
1352
1353         srpc_init_client_rpc(rpc, peer, service, nbulkiov,
1354                              bulklen, rpc_done, rpc_fini, priv);
1355         return rpc;
1356 }
1357
1358 /* called with rpc->crpc_lock held */
1359 void
1360 srpc_abort_rpc (srpc_client_rpc_t *rpc, int why)
1361 {
1362         LASSERT (why != 0);
1363
1364         if (rpc->crpc_aborted || /* already aborted */
1365             rpc->crpc_closed)    /* callback imminent */
1366                 return;
1367
1368         CDEBUG (D_NET,
1369                 "Aborting RPC: service %d, peer %s, state %s, why %d\n",
1370                 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1371                 swi_state2str(rpc->crpc_wi.swi_state), why);
1372
1373         rpc->crpc_aborted = 1;
1374         rpc->crpc_status  = why;
1375         swi_schedule_workitem(&rpc->crpc_wi);
1376         return;
1377 }
1378
1379 /* called with rpc->crpc_lock held */
1380 void
1381 srpc_post_rpc (srpc_client_rpc_t *rpc)
1382 {
1383         LASSERT (!rpc->crpc_aborted);
1384         LASSERT (srpc_data.rpc_state == SRPC_STATE_RUNNING);
1385
1386         CDEBUG (D_NET, "Posting RPC: peer %s, service %d, timeout %d\n",
1387                 libcfs_id2str(rpc->crpc_dest), rpc->crpc_service,
1388                 rpc->crpc_timeout);
1389
1390         srpc_add_client_rpc_timer(rpc);
1391         swi_schedule_workitem(&rpc->crpc_wi);
1392         return;
1393 }
1394
1395
1396 int
1397 srpc_send_reply(struct srpc_server_rpc *rpc)
1398 {
1399         srpc_event_t            *ev = &rpc->srpc_ev;
1400         struct srpc_msg         *msg = &rpc->srpc_replymsg;
1401         struct srpc_buffer      *buffer = rpc->srpc_reqstbuf;
1402         struct srpc_service_cd  *scd = rpc->srpc_scd;
1403         struct srpc_service     *sv = scd->scd_svc;
1404         __u64                   rpyid;
1405         int                     rc;
1406
1407         LASSERT(buffer != NULL);
1408         rpyid = buffer->buf_msg.msg_body.reqst.rpyid;
1409
1410         spin_lock(&scd->scd_lock);
1411
1412         if (!sv->sv_shuttingdown && !srpc_serv_is_framework(sv)) {
1413                 /* Repost buffer before replying since test client
1414                  * might send me another RPC once it gets the reply */
1415                 if (srpc_service_post_buffer(scd, buffer) != 0)
1416                         CWARN("Failed to repost %s buffer\n", sv->sv_name);
1417                 rpc->srpc_reqstbuf = NULL;
1418         }
1419
1420         spin_unlock(&scd->scd_lock);
1421
1422         ev->ev_fired = 0;
1423         ev->ev_data  = rpc;
1424         ev->ev_type  = SRPC_REPLY_SENT;
1425
1426         msg->msg_magic   = SRPC_MSG_MAGIC;
1427         msg->msg_version = SRPC_MSG_VERSION;
1428         msg->msg_type    = srpc_service2reply(sv->sv_id);
1429
1430         rc = srpc_post_active_rdma(SRPC_RDMA_PORTAL, rpyid, msg,
1431                                    sizeof(*msg), LNET_MD_OP_PUT,
1432                                    rpc->srpc_peer, rpc->srpc_self,
1433                                    &rpc->srpc_replymdh, ev);
1434         if (rc != 0)
1435                 ev->ev_fired = 1;  /* no more event expected */
1436         return rc;
1437 }
1438
1439 /* when in kernel always called with LNET_LOCK() held, and in thread context */
1440 void
1441 srpc_lnet_ev_handler(lnet_event_t *ev)
1442 {
1443         struct srpc_service_cd  *scd;
1444         srpc_event_t      *rpcev = ev->md.user_ptr;
1445         srpc_client_rpc_t *crpc;
1446         srpc_server_rpc_t *srpc;
1447         srpc_buffer_t     *buffer;
1448         srpc_service_t    *sv;
1449         srpc_msg_t        *msg;
1450         srpc_msg_type_t    type;
1451
1452         LASSERT (!cfs_in_interrupt());
1453
1454         if (ev->status != 0) {
1455                 spin_lock(&srpc_data.rpc_glock);
1456                 srpc_data.rpc_counters.errors++;
1457                 spin_unlock(&srpc_data.rpc_glock);
1458         }
1459
1460         rpcev->ev_lnet = ev->type;
1461
1462         switch (rpcev->ev_type) {
1463         default:
1464                 CERROR("Unknown event: status %d, type %d, lnet %d\n",
1465                        rpcev->ev_status, rpcev->ev_type, rpcev->ev_lnet);
1466                 LBUG ();
1467         case SRPC_REQUEST_SENT:
1468                 if (ev->status == 0 && ev->type != LNET_EVENT_UNLINK) {
1469                         spin_lock(&srpc_data.rpc_glock);
1470                         srpc_data.rpc_counters.rpcs_sent++;
1471                         spin_unlock(&srpc_data.rpc_glock);
1472                 }
1473         case SRPC_REPLY_RCVD:
1474         case SRPC_BULK_REQ_RCVD:
1475                 crpc = rpcev->ev_data;
1476
1477                 if (rpcev != &crpc->crpc_reqstev &&
1478                     rpcev != &crpc->crpc_replyev &&
1479                     rpcev != &crpc->crpc_bulkev) {
1480                         CERROR("rpcev %p, crpc %p, reqstev %p, replyev %p, bulkev %p\n",
1481                                rpcev, crpc, &crpc->crpc_reqstev,
1482                                &crpc->crpc_replyev, &crpc->crpc_bulkev);
1483                         CERROR("Bad event: status %d, type %d, lnet %d\n",
1484                                rpcev->ev_status, rpcev->ev_type, rpcev->ev_lnet);
1485                         LBUG ();
1486                 }
1487
1488                 spin_lock(&crpc->crpc_lock);
1489
1490                 LASSERT(rpcev->ev_fired == 0);
1491                 rpcev->ev_fired  = 1;
1492                 rpcev->ev_status = (ev->type == LNET_EVENT_UNLINK) ?
1493                                                 -EINTR : ev->status;
1494                 swi_schedule_workitem(&crpc->crpc_wi);
1495
1496                 spin_unlock(&crpc->crpc_lock);
1497                 break;
1498
1499         case SRPC_REQUEST_RCVD:
1500                 scd = rpcev->ev_data;
1501                 sv = scd->scd_svc;
1502
1503                 LASSERT(rpcev == &scd->scd_ev);
1504
1505                 spin_lock(&scd->scd_lock);
1506
1507                 LASSERT (ev->unlinked);
1508                 LASSERT (ev->type == LNET_EVENT_PUT ||
1509                          ev->type == LNET_EVENT_UNLINK);
1510                 LASSERT (ev->type != LNET_EVENT_UNLINK ||
1511                          sv->sv_shuttingdown);
1512
1513                 buffer = container_of(ev->md.start, srpc_buffer_t, buf_msg);
1514                 buffer->buf_peer = ev->initiator;
1515                 buffer->buf_self = ev->target.nid;
1516
1517                 LASSERT(scd->scd_buf_nposted > 0);
1518                 scd->scd_buf_nposted--;
1519
1520                 if (sv->sv_shuttingdown) {
1521                         /* Leave buffer on scd->scd_buf_nposted since
1522                          * srpc_finish_service needs to traverse it. */
1523                         spin_unlock(&scd->scd_lock);
1524                         break;
1525                 }
1526
1527                 if (scd->scd_buf_err_stamp != 0 &&
1528                     scd->scd_buf_err_stamp < cfs_time_current_sec()) {
1529                         /* re-enable adding buffer */
1530                         scd->scd_buf_err_stamp = 0;
1531                         scd->scd_buf_err = 0;
1532                 }
1533
1534                 if (scd->scd_buf_err == 0 && /* adding buffer is enabled */
1535                     scd->scd_buf_adjust == 0 &&
1536                     scd->scd_buf_nposted < scd->scd_buf_low) {
1537                         scd->scd_buf_adjust = MAX(scd->scd_buf_total / 2,
1538                                                   SFW_TEST_WI_MIN);
1539                         swi_schedule_workitem(&scd->scd_buf_wi);
1540                 }
1541
1542                 cfs_list_del(&buffer->buf_list); /* from scd->scd_buf_posted */
1543                 msg = &buffer->buf_msg;
1544                 type = srpc_service2request(sv->sv_id);
1545
1546                 if (ev->status != 0 || ev->mlength != sizeof(*msg) ||
1547                     (msg->msg_type != type &&
1548                      msg->msg_type != __swab32(type)) ||
1549                     (msg->msg_magic != SRPC_MSG_MAGIC &&
1550                      msg->msg_magic != __swab32(SRPC_MSG_MAGIC))) {
1551                         CERROR ("Dropping RPC (%s) from %s: "
1552                                 "status %d mlength %d type %u magic %u.\n",
1553                                 sv->sv_name, libcfs_id2str(ev->initiator),
1554                                 ev->status, ev->mlength,
1555                                 msg->msg_type, msg->msg_magic);
1556
1557                         /* NB can't call srpc_service_recycle_buffer here since
1558                          * it may call LNetM[DE]Attach. The invalid magic tells
1559                          * srpc_handle_rpc to drop this RPC */
1560                         msg->msg_magic = 0;
1561                 }
1562
1563                 if (!cfs_list_empty(&scd->scd_rpc_free)) {
1564                         srpc = cfs_list_entry(scd->scd_rpc_free.next,
1565                                               struct srpc_server_rpc,
1566                                               srpc_list);
1567                         cfs_list_del(&srpc->srpc_list);
1568
1569                         srpc_init_server_rpc(srpc, scd, buffer);
1570                         cfs_list_add_tail(&srpc->srpc_list,
1571                                           &scd->scd_rpc_active);
1572                         swi_schedule_workitem(&srpc->srpc_wi);
1573                 } else {
1574                         cfs_list_add_tail(&buffer->buf_list,
1575                                           &scd->scd_buf_blocked);
1576                 }
1577
1578                 spin_unlock(&scd->scd_lock);
1579
1580                 spin_lock(&srpc_data.rpc_glock);
1581                 srpc_data.rpc_counters.rpcs_rcvd++;
1582                 spin_unlock(&srpc_data.rpc_glock);
1583                 break;
1584
1585         case SRPC_BULK_GET_RPLD:
1586                 LASSERT (ev->type == LNET_EVENT_SEND ||
1587                          ev->type == LNET_EVENT_REPLY ||
1588                          ev->type == LNET_EVENT_UNLINK);
1589
1590                 if (!ev->unlinked)
1591                         break; /* wait for final event */
1592
1593         case SRPC_BULK_PUT_SENT:
1594                 if (ev->status == 0 && ev->type != LNET_EVENT_UNLINK) {
1595                         spin_lock(&srpc_data.rpc_glock);
1596
1597                         if (rpcev->ev_type == SRPC_BULK_GET_RPLD)
1598                                 srpc_data.rpc_counters.bulk_get += ev->mlength;
1599                         else
1600                                 srpc_data.rpc_counters.bulk_put += ev->mlength;
1601
1602                         spin_unlock(&srpc_data.rpc_glock);
1603                 }
1604         case SRPC_REPLY_SENT:
1605                 srpc = rpcev->ev_data;
1606                 scd  = srpc->srpc_scd;
1607
1608                 LASSERT(rpcev == &srpc->srpc_ev);
1609
1610                 spin_lock(&scd->scd_lock);
1611
1612                 rpcev->ev_fired  = 1;
1613                 rpcev->ev_status = (ev->type == LNET_EVENT_UNLINK) ?
1614                                    -EINTR : ev->status;
1615                 swi_schedule_workitem(&srpc->srpc_wi);
1616
1617                 spin_unlock(&scd->scd_lock);
1618                 break;
1619         }
1620 }
1621
1622 #ifndef __KERNEL__
1623
1624 int
1625 srpc_check_event (int timeout)
1626 {
1627         lnet_event_t ev;
1628         int          rc;
1629         int          i;
1630
1631         rc = LNetEQPoll(&srpc_data.rpc_lnet_eq, 1,
1632                         timeout * 1000, &ev, &i);
1633         if (rc == 0) return 0;
1634
1635         LASSERT (rc == -EOVERFLOW || rc == 1);
1636
1637         /* We can't affort to miss any events... */
1638         if (rc == -EOVERFLOW) {
1639                 CERROR ("Dropped an event!!!\n");
1640                 abort();
1641         }
1642
1643         srpc_lnet_ev_handler(&ev);
1644         return 1;
1645 }
1646
1647 #endif
1648
1649 int
1650 srpc_startup (void)
1651 {
1652         int rc;
1653
1654         memset(&srpc_data, 0, sizeof(struct smoketest_rpc));
1655         spin_lock_init(&srpc_data.rpc_glock);
1656
1657         /* 1 second pause to avoid timestamp reuse */
1658         cfs_pause(cfs_time_seconds(1));
1659         srpc_data.rpc_matchbits = ((__u64) cfs_time_current_sec()) << 48;
1660
1661         srpc_data.rpc_state = SRPC_STATE_NONE;
1662
1663 #ifdef __KERNEL__
1664         rc = LNetNIInit(LUSTRE_SRV_LNET_PID);
1665 #else
1666         if (the_lnet.ln_server_mode_flag)
1667                 rc = LNetNIInit(LUSTRE_SRV_LNET_PID);
1668         else
1669                 rc = LNetNIInit(getpid() | LNET_PID_USERFLAG);
1670 #endif
1671         if (rc < 0) {
1672                 CERROR ("LNetNIInit() has failed: %d\n", rc);
1673                 return rc;
1674         }
1675
1676         srpc_data.rpc_state = SRPC_STATE_NI_INIT;
1677
1678         LNetInvalidateHandle(&srpc_data.rpc_lnet_eq);
1679 #ifdef __KERNEL__
1680         rc = LNetEQAlloc(0, srpc_lnet_ev_handler, &srpc_data.rpc_lnet_eq);
1681 #else
1682         rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &srpc_data.rpc_lnet_eq);
1683 #endif
1684         if (rc != 0) {
1685                 CERROR("LNetEQAlloc() has failed: %d\n", rc);
1686                 goto bail;
1687         }
1688
1689         rc = LNetSetLazyPortal(SRPC_FRAMEWORK_REQUEST_PORTAL);
1690         LASSERT(rc == 0);
1691         rc = LNetSetLazyPortal(SRPC_REQUEST_PORTAL);
1692         LASSERT(rc == 0);
1693
1694         srpc_data.rpc_state = SRPC_STATE_EQ_INIT;
1695
1696         rc = stt_startup();
1697
1698 bail:
1699         if (rc != 0)
1700                 srpc_shutdown();
1701         else
1702                 srpc_data.rpc_state = SRPC_STATE_RUNNING;
1703
1704         return rc;
1705 }
1706
1707 void
1708 srpc_shutdown (void)
1709 {
1710         int i;
1711         int rc;
1712         int state;
1713
1714         state = srpc_data.rpc_state;
1715         srpc_data.rpc_state = SRPC_STATE_STOPPING;
1716
1717         switch (state) {
1718         default:
1719                 LBUG ();
1720         case SRPC_STATE_RUNNING:
1721                 spin_lock(&srpc_data.rpc_glock);
1722
1723                 for (i = 0; i <= SRPC_SERVICE_MAX_ID; i++) {
1724                         srpc_service_t *sv = srpc_data.rpc_services[i];
1725
1726                         LASSERTF (sv == NULL,
1727                                   "service not empty: id %d, name %s\n",
1728                                   i, sv->sv_name);
1729                 }
1730
1731                 spin_unlock(&srpc_data.rpc_glock);
1732
1733                 stt_shutdown();
1734
1735         case SRPC_STATE_EQ_INIT:
1736                 rc = LNetClearLazyPortal(SRPC_FRAMEWORK_REQUEST_PORTAL);
1737                 rc = LNetClearLazyPortal(SRPC_REQUEST_PORTAL);
1738                 LASSERT (rc == 0);
1739                 rc = LNetEQFree(srpc_data.rpc_lnet_eq);
1740                 LASSERT (rc == 0); /* the EQ should have no user by now */
1741
1742         case SRPC_STATE_NI_INIT:
1743                 LNetNIFini();
1744         }
1745
1746         return;
1747 }