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