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