Whamcloud - gitweb
LU-8965 kernel: kernel update [SLES11 SP4 3.0.101-91]
[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         lnet_handle_eq_t 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         LNetInvalidateHandle(&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, lnet_process_id_t peer,
355                        lnet_handle_md_t *mdh, srpc_event_t *ev)
356 {
357         int              rc;
358         lnet_md_t        md;
359         lnet_handle_me_t 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, lnet_process_id_t peer, lnet_nid_t self,
395                       lnet_handle_md_t *mdh, srpc_event_t *ev)
396 {
397         int       rc;
398         lnet_md_t 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                          lnet_handle_md_t *mdh, srpc_event_t *ev)
447 {
448         lnet_process_id_t 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         LNetInvalidateHandle(&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 = cfs_time_add(rpc->crpc_timeout,
1095                                           ktime_get_real_seconds());
1096         stt_add_timer(timer);
1097         return;
1098 }
1099
1100 /*
1101  * Called with rpc->crpc_lock held.
1102  *
1103  * Upon exit the RPC expiry timer is not queued and the handler is not
1104  * running on any CPU. */
1105 static void
1106 srpc_del_client_rpc_timer (srpc_client_rpc_t *rpc)
1107 {
1108         /* timer not planted or already exploded */
1109         if (rpc->crpc_timeout == 0)
1110                 return;
1111
1112         /* timer successfully defused */
1113         if (stt_del_timer(&rpc->crpc_timer))
1114                 return;
1115
1116         /* timer detonated, wait for it to explode */
1117         while (rpc->crpc_timeout != 0) {
1118                 spin_unlock(&rpc->crpc_lock);
1119
1120                 schedule();
1121
1122                 spin_lock(&rpc->crpc_lock);
1123         }
1124 }
1125
1126 static void
1127 srpc_client_rpc_done (srpc_client_rpc_t *rpc, int status)
1128 {
1129         swi_workitem_t *wi = &rpc->crpc_wi;
1130
1131         LASSERT(status != 0 || wi->swi_state == SWI_STATE_DONE);
1132
1133         spin_lock(&rpc->crpc_lock);
1134
1135         rpc->crpc_closed = 1;
1136         if (rpc->crpc_status == 0)
1137                 rpc->crpc_status = status;
1138
1139         srpc_del_client_rpc_timer(rpc);
1140
1141         CDEBUG_LIMIT ((status == 0) ? D_NET : D_NETERROR,
1142                 "Client RPC done: service %d, peer %s, status %s:%d:%d\n",
1143                 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1144                 swi_state2str(wi->swi_state), rpc->crpc_aborted, status);
1145
1146         /*
1147          * No one can schedule me now since:
1148          * - RPC timer has been defused.
1149          * - all LNet events have been fired.
1150          * - crpc_closed has been set, preventing srpc_abort_rpc from
1151          *   scheduling me.
1152          * Cancel pending schedules and prevent future schedule attempts:
1153          */
1154         LASSERT (!srpc_event_pending(rpc));
1155         swi_exit_workitem(wi);
1156
1157         spin_unlock(&rpc->crpc_lock);
1158
1159         (*rpc->crpc_done)(rpc);
1160         return;
1161 }
1162
1163 /* sends an outgoing RPC */
1164 int
1165 srpc_send_rpc (swi_workitem_t *wi)
1166 {
1167         int                rc = 0;
1168         srpc_client_rpc_t *rpc;
1169         srpc_msg_t        *reply;
1170         int                do_bulk;
1171
1172         LASSERT(wi != NULL);
1173
1174         rpc = wi->swi_workitem.wi_data;
1175
1176         LASSERT (rpc != NULL);
1177         LASSERT (wi == &rpc->crpc_wi);
1178
1179         reply = &rpc->crpc_replymsg;
1180         do_bulk = rpc->crpc_bulk.bk_niov > 0;
1181
1182         spin_lock(&rpc->crpc_lock);
1183
1184         if (rpc->crpc_aborted) {
1185                 spin_unlock(&rpc->crpc_lock);
1186                 goto abort;
1187         }
1188
1189         spin_unlock(&rpc->crpc_lock);
1190
1191         switch (wi->swi_state) {
1192         default:
1193                 LBUG ();
1194         case SWI_STATE_NEWBORN:
1195                 LASSERT (!srpc_event_pending(rpc));
1196
1197                 rc = srpc_prepare_reply(rpc);
1198                 if (rc != 0) {
1199                         srpc_client_rpc_done(rpc, rc);
1200                         return 1;
1201                 }
1202
1203                 rc = srpc_prepare_bulk(rpc);
1204                 if (rc != 0) break;
1205
1206                 wi->swi_state = SWI_STATE_REQUEST_SUBMITTED;
1207                 rc = srpc_send_request(rpc);
1208                 break;
1209
1210         case SWI_STATE_REQUEST_SUBMITTED:
1211                 /* CAVEAT EMPTOR: rqtev, rpyev, and bulkev may come in any
1212                  * order; however, they're processed in a strict order:
1213                  * rqt, rpy, and bulk. */
1214                 if (!rpc->crpc_reqstev.ev_fired) break;
1215
1216                 rc = rpc->crpc_reqstev.ev_status;
1217                 if (rc != 0) break;
1218
1219                 wi->swi_state = SWI_STATE_REQUEST_SENT;
1220                 /* perhaps more events, fall thru */
1221         case SWI_STATE_REQUEST_SENT: {
1222                 srpc_msg_type_t type = srpc_service2reply(rpc->crpc_service);
1223
1224                 if (!rpc->crpc_replyev.ev_fired) break;
1225
1226                 rc = rpc->crpc_replyev.ev_status;
1227                 if (rc != 0) break;
1228
1229                 srpc_unpack_msg_hdr(reply);
1230                 if (reply->msg_type != type ||
1231                     (reply->msg_magic != SRPC_MSG_MAGIC &&
1232                      reply->msg_magic != __swab32(SRPC_MSG_MAGIC))) {
1233                         CWARN ("Bad message from %s: type %u (%d expected),"
1234                                " magic %u (%d expected).\n",
1235                                libcfs_id2str(rpc->crpc_dest),
1236                                reply->msg_type, type,
1237                                reply->msg_magic, SRPC_MSG_MAGIC);
1238                         rc = -EBADMSG;
1239                         break;
1240                 }
1241
1242                 if (do_bulk && reply->msg_body.reply.status != 0) {
1243                         CWARN ("Remote error %d at %s, unlink bulk buffer in "
1244                                "case peer didn't initiate bulk transfer\n",
1245                                reply->msg_body.reply.status,
1246                                libcfs_id2str(rpc->crpc_dest));
1247                         LNetMDUnlink(rpc->crpc_bulk.bk_mdh);
1248                 }
1249
1250                 wi->swi_state = SWI_STATE_REPLY_RECEIVED;
1251         }
1252         case SWI_STATE_REPLY_RECEIVED:
1253                 if (do_bulk && !rpc->crpc_bulkev.ev_fired) break;
1254
1255                 rc = do_bulk ? rpc->crpc_bulkev.ev_status : 0;
1256
1257                 /* Bulk buffer was unlinked due to remote error. Clear error
1258                  * since reply buffer still contains valid data.
1259                  * NB rpc->crpc_done shouldn't look into bulk data in case of
1260                  * remote error. */
1261                 if (do_bulk && rpc->crpc_bulkev.ev_lnet == LNET_EVENT_UNLINK &&
1262                     rpc->crpc_status == 0 && reply->msg_body.reply.status != 0)
1263                         rc = 0;
1264
1265                 wi->swi_state = SWI_STATE_DONE;
1266                 srpc_client_rpc_done(rpc, rc);
1267                 return 1;
1268         }
1269
1270         if (rc != 0) {
1271                 spin_lock(&rpc->crpc_lock);
1272                 srpc_abort_rpc(rpc, rc);
1273                 spin_unlock(&rpc->crpc_lock);
1274         }
1275
1276 abort:
1277         if (rpc->crpc_aborted) {
1278                 LNetMDUnlink(rpc->crpc_reqstmdh);
1279                 LNetMDUnlink(rpc->crpc_replymdh);
1280                 LNetMDUnlink(rpc->crpc_bulk.bk_mdh);
1281
1282                 if (!srpc_event_pending(rpc)) {
1283                         srpc_client_rpc_done(rpc, -EINTR);
1284                         return 1;
1285                 }
1286         }
1287         return 0;
1288 }
1289
1290 srpc_client_rpc_t *
1291 srpc_create_client_rpc (lnet_process_id_t peer, int service,
1292                         int nbulkiov, int bulklen,
1293                         void (*rpc_done)(srpc_client_rpc_t *),
1294                         void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
1295 {
1296         srpc_client_rpc_t *rpc;
1297
1298         LIBCFS_ALLOC(rpc, offsetof(srpc_client_rpc_t,
1299                                    crpc_bulk.bk_iovs[nbulkiov]));
1300         if (rpc == NULL)
1301                 return NULL;
1302
1303         srpc_init_client_rpc(rpc, peer, service, nbulkiov,
1304                              bulklen, rpc_done, rpc_fini, priv);
1305         return rpc;
1306 }
1307
1308 /* called with rpc->crpc_lock held */
1309 void
1310 srpc_abort_rpc (srpc_client_rpc_t *rpc, int why)
1311 {
1312         LASSERT (why != 0);
1313
1314         if (rpc->crpc_aborted || /* already aborted */
1315             rpc->crpc_closed)    /* callback imminent */
1316                 return;
1317
1318         CDEBUG (D_NET,
1319                 "Aborting RPC: service %d, peer %s, state %s, why %d\n",
1320                 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
1321                 swi_state2str(rpc->crpc_wi.swi_state), why);
1322
1323         rpc->crpc_aborted = 1;
1324         rpc->crpc_status  = why;
1325         swi_schedule_workitem(&rpc->crpc_wi);
1326         return;
1327 }
1328
1329 /* called with rpc->crpc_lock held */
1330 void
1331 srpc_post_rpc (srpc_client_rpc_t *rpc)
1332 {
1333         LASSERT (!rpc->crpc_aborted);
1334         LASSERT (srpc_data.rpc_state == SRPC_STATE_RUNNING);
1335
1336         CDEBUG (D_NET, "Posting RPC: peer %s, service %d, timeout %d\n",
1337                 libcfs_id2str(rpc->crpc_dest), rpc->crpc_service,
1338                 rpc->crpc_timeout);
1339
1340         srpc_add_client_rpc_timer(rpc);
1341         swi_schedule_workitem(&rpc->crpc_wi);
1342         return;
1343 }
1344
1345
1346 int
1347 srpc_send_reply(struct srpc_server_rpc *rpc)
1348 {
1349         srpc_event_t            *ev = &rpc->srpc_ev;
1350         struct srpc_msg         *msg = &rpc->srpc_replymsg;
1351         struct srpc_buffer      *buffer = rpc->srpc_reqstbuf;
1352         struct srpc_service_cd  *scd = rpc->srpc_scd;
1353         struct srpc_service     *sv = scd->scd_svc;
1354         __u64                   rpyid;
1355         int                     rc;
1356
1357         LASSERT(buffer != NULL);
1358         rpyid = buffer->buf_msg.msg_body.reqst.rpyid;
1359
1360         spin_lock(&scd->scd_lock);
1361
1362         if (!sv->sv_shuttingdown && !srpc_serv_is_framework(sv)) {
1363                 /* Repost buffer before replying since test client
1364                  * might send me another RPC once it gets the reply */
1365                 if (srpc_service_post_buffer(scd, buffer) != 0)
1366                         CWARN("Failed to repost %s buffer\n", sv->sv_name);
1367                 rpc->srpc_reqstbuf = NULL;
1368         }
1369
1370         spin_unlock(&scd->scd_lock);
1371
1372         ev->ev_fired = 0;
1373         ev->ev_data  = rpc;
1374         ev->ev_type  = SRPC_REPLY_SENT;
1375
1376         msg->msg_magic   = SRPC_MSG_MAGIC;
1377         msg->msg_version = SRPC_MSG_VERSION;
1378         msg->msg_type    = srpc_service2reply(sv->sv_id);
1379
1380         rc = srpc_post_active_rdma(SRPC_RDMA_PORTAL, rpyid, msg,
1381                                    sizeof(*msg), LNET_MD_OP_PUT,
1382                                    rpc->srpc_peer, rpc->srpc_self,
1383                                    &rpc->srpc_replymdh, ev);
1384         if (rc != 0)
1385                 ev->ev_fired = 1;  /* no more event expected */
1386         return rc;
1387 }
1388
1389 /* when in kernel always called with LNET_LOCK() held, and in thread context */
1390 static void
1391 srpc_lnet_ev_handler(lnet_event_t *ev)
1392 {
1393         struct srpc_service_cd  *scd;
1394         srpc_event_t      *rpcev = ev->md.user_ptr;
1395         srpc_client_rpc_t *crpc;
1396         srpc_server_rpc_t *srpc;
1397         srpc_buffer_t     *buffer;
1398         srpc_service_t    *sv;
1399         srpc_msg_t        *msg;
1400         srpc_msg_type_t    type;
1401
1402         LASSERT (!in_interrupt());
1403
1404         if (ev->status != 0) {
1405                 __u32 errors;
1406
1407                 spin_lock(&srpc_data.rpc_glock);
1408                 if (ev->status != -ECANCELED) /* cancellation is not error */
1409                         srpc_data.rpc_counters.errors++;
1410                 errors = srpc_data.rpc_counters.errors;
1411                 spin_unlock(&srpc_data.rpc_glock);
1412
1413                 CNETERR("LNet event status %d type %d, RPC errors %u\n",
1414                         ev->status, ev->type, errors);
1415         }
1416
1417         rpcev->ev_lnet = ev->type;
1418
1419         switch (rpcev->ev_type) {
1420         default:
1421                 CERROR("Unknown event: status %d, type %d, lnet %d\n",
1422                        rpcev->ev_status, rpcev->ev_type, rpcev->ev_lnet);
1423                 LBUG ();
1424         case SRPC_REQUEST_SENT:
1425                 if (ev->status == 0 && ev->type != LNET_EVENT_UNLINK) {
1426                         spin_lock(&srpc_data.rpc_glock);
1427                         srpc_data.rpc_counters.rpcs_sent++;
1428                         spin_unlock(&srpc_data.rpc_glock);
1429                 }
1430         case SRPC_REPLY_RCVD:
1431         case SRPC_BULK_REQ_RCVD:
1432                 crpc = rpcev->ev_data;
1433
1434                 if (rpcev != &crpc->crpc_reqstev &&
1435                     rpcev != &crpc->crpc_replyev &&
1436                     rpcev != &crpc->crpc_bulkev) {
1437                         CERROR("rpcev %p, crpc %p, reqstev %p, replyev %p, bulkev %p\n",
1438                                rpcev, crpc, &crpc->crpc_reqstev,
1439                                &crpc->crpc_replyev, &crpc->crpc_bulkev);
1440                         CERROR("Bad event: status %d, type %d, lnet %d\n",
1441                                rpcev->ev_status, rpcev->ev_type, rpcev->ev_lnet);
1442                         LBUG ();
1443                 }
1444
1445                 spin_lock(&crpc->crpc_lock);
1446
1447                 LASSERT(rpcev->ev_fired == 0);
1448                 rpcev->ev_fired  = 1;
1449                 rpcev->ev_status = (ev->type == LNET_EVENT_UNLINK) ?
1450                                                 -EINTR : ev->status;
1451                 swi_schedule_workitem(&crpc->crpc_wi);
1452
1453                 spin_unlock(&crpc->crpc_lock);
1454                 break;
1455
1456         case SRPC_REQUEST_RCVD:
1457                 scd = rpcev->ev_data;
1458                 sv = scd->scd_svc;
1459
1460                 LASSERT(rpcev == &scd->scd_ev);
1461
1462                 spin_lock(&scd->scd_lock);
1463
1464                 LASSERT (ev->unlinked);
1465                 LASSERT (ev->type == LNET_EVENT_PUT ||
1466                          ev->type == LNET_EVENT_UNLINK);
1467                 LASSERT (ev->type != LNET_EVENT_UNLINK ||
1468                          sv->sv_shuttingdown);
1469
1470                 buffer = container_of(ev->md.start, srpc_buffer_t, buf_msg);
1471                 buffer->buf_peer = ev->initiator;
1472                 buffer->buf_self = ev->target.nid;
1473
1474                 LASSERT(scd->scd_buf_nposted > 0);
1475                 scd->scd_buf_nposted--;
1476
1477                 if (sv->sv_shuttingdown) {
1478                         /* Leave buffer on scd->scd_buf_nposted since
1479                          * srpc_finish_service needs to traverse it. */
1480                         spin_unlock(&scd->scd_lock);
1481                         break;
1482                 }
1483
1484                 if (scd->scd_buf_err_stamp != 0 &&
1485                     scd->scd_buf_err_stamp < ktime_get_real_seconds()) {
1486                         /* re-enable adding buffer */
1487                         scd->scd_buf_err_stamp = 0;
1488                         scd->scd_buf_err = 0;
1489                 }
1490
1491                 if (scd->scd_buf_err == 0 && /* adding buffer is enabled */
1492                     scd->scd_buf_adjust == 0 &&
1493                     scd->scd_buf_nposted < scd->scd_buf_low) {
1494                         scd->scd_buf_adjust = MAX(scd->scd_buf_total / 2,
1495                                                   SFW_TEST_WI_MIN);
1496                         swi_schedule_workitem(&scd->scd_buf_wi);
1497                 }
1498
1499                 list_del(&buffer->buf_list); /* from scd->scd_buf_posted */
1500                 msg = &buffer->buf_msg;
1501                 type = srpc_service2request(sv->sv_id);
1502
1503                 if (ev->status != 0 || ev->mlength != sizeof(*msg) ||
1504                     (msg->msg_type != type &&
1505                      msg->msg_type != __swab32(type)) ||
1506                     (msg->msg_magic != SRPC_MSG_MAGIC &&
1507                      msg->msg_magic != __swab32(SRPC_MSG_MAGIC))) {
1508                         CERROR ("Dropping RPC (%s) from %s: "
1509                                 "status %d mlength %d type %u magic %u.\n",
1510                                 sv->sv_name, libcfs_id2str(ev->initiator),
1511                                 ev->status, ev->mlength,
1512                                 msg->msg_type, msg->msg_magic);
1513
1514                         /* NB can't call srpc_service_recycle_buffer here since
1515                          * it may call LNetM[DE]Attach. The invalid magic tells
1516                          * srpc_handle_rpc to drop this RPC */
1517                         msg->msg_magic = 0;
1518                 }
1519
1520                 if (!list_empty(&scd->scd_rpc_free)) {
1521                         srpc = list_entry(scd->scd_rpc_free.next,
1522                                           struct srpc_server_rpc,
1523                                           srpc_list);
1524                         list_del(&srpc->srpc_list);
1525
1526                         srpc_init_server_rpc(srpc, scd, buffer);
1527                         list_add_tail(&srpc->srpc_list,
1528                                       &scd->scd_rpc_active);
1529                         swi_schedule_workitem(&srpc->srpc_wi);
1530                 } else {
1531                         list_add_tail(&buffer->buf_list,
1532                                       &scd->scd_buf_blocked);
1533                 }
1534
1535                 spin_unlock(&scd->scd_lock);
1536
1537                 spin_lock(&srpc_data.rpc_glock);
1538                 srpc_data.rpc_counters.rpcs_rcvd++;
1539                 spin_unlock(&srpc_data.rpc_glock);
1540                 break;
1541
1542         case SRPC_BULK_GET_RPLD:
1543                 LASSERT (ev->type == LNET_EVENT_SEND ||
1544                          ev->type == LNET_EVENT_REPLY ||
1545                          ev->type == LNET_EVENT_UNLINK);
1546
1547                 if (!ev->unlinked)
1548                         break; /* wait for final event */
1549
1550         case SRPC_BULK_PUT_SENT:
1551                 if (ev->status == 0 && ev->type != LNET_EVENT_UNLINK) {
1552                         spin_lock(&srpc_data.rpc_glock);
1553
1554                         if (rpcev->ev_type == SRPC_BULK_GET_RPLD)
1555                                 srpc_data.rpc_counters.bulk_get += ev->mlength;
1556                         else
1557                                 srpc_data.rpc_counters.bulk_put += ev->mlength;
1558
1559                         spin_unlock(&srpc_data.rpc_glock);
1560                 }
1561         case SRPC_REPLY_SENT:
1562                 srpc = rpcev->ev_data;
1563                 scd  = srpc->srpc_scd;
1564
1565                 LASSERT(rpcev == &srpc->srpc_ev);
1566
1567                 spin_lock(&scd->scd_lock);
1568
1569                 rpcev->ev_fired  = 1;
1570                 rpcev->ev_status = (ev->type == LNET_EVENT_UNLINK) ?
1571                                    -EINTR : ev->status;
1572                 swi_schedule_workitem(&srpc->srpc_wi);
1573
1574                 spin_unlock(&scd->scd_lock);
1575                 break;
1576         }
1577 }
1578
1579
1580 int
1581 srpc_startup (void)
1582 {
1583         int rc;
1584
1585         memset(&srpc_data, 0, sizeof(struct smoketest_rpc));
1586         spin_lock_init(&srpc_data.rpc_glock);
1587
1588         /* 1 second pause to avoid timestamp reuse */
1589         set_current_state(TASK_UNINTERRUPTIBLE);
1590         schedule_timeout(cfs_time_seconds(1));
1591         srpc_data.rpc_matchbits = ((__u64) ktime_get_real_seconds()) << 48;
1592
1593         srpc_data.rpc_state = SRPC_STATE_NONE;
1594
1595         rc = LNetNIInit(LNET_PID_LUSTRE);
1596         if (rc < 0) {
1597                 CERROR ("LNetNIInit() has failed: %d\n", rc);
1598                 return rc;
1599         }
1600
1601         srpc_data.rpc_state = SRPC_STATE_NI_INIT;
1602
1603         LNetInvalidateHandle(&srpc_data.rpc_lnet_eq);
1604         rc = LNetEQAlloc(0, srpc_lnet_ev_handler, &srpc_data.rpc_lnet_eq);
1605         if (rc != 0) {
1606                 CERROR("LNetEQAlloc() has failed: %d\n", rc);
1607                 goto bail;
1608         }
1609
1610         rc = LNetSetLazyPortal(SRPC_FRAMEWORK_REQUEST_PORTAL);
1611         LASSERT(rc == 0);
1612         rc = LNetSetLazyPortal(SRPC_REQUEST_PORTAL);
1613         LASSERT(rc == 0);
1614
1615         srpc_data.rpc_state = SRPC_STATE_EQ_INIT;
1616
1617         rc = stt_startup();
1618
1619 bail:
1620         if (rc != 0)
1621                 srpc_shutdown();
1622         else
1623                 srpc_data.rpc_state = SRPC_STATE_RUNNING;
1624
1625         return rc;
1626 }
1627
1628 void
1629 srpc_shutdown (void)
1630 {
1631         int i;
1632         int rc;
1633         int state;
1634
1635         state = srpc_data.rpc_state;
1636         srpc_data.rpc_state = SRPC_STATE_STOPPING;
1637
1638         switch (state) {
1639         default:
1640                 LBUG ();
1641         case SRPC_STATE_RUNNING:
1642                 spin_lock(&srpc_data.rpc_glock);
1643
1644                 for (i = 0; i <= SRPC_SERVICE_MAX_ID; i++) {
1645                         srpc_service_t *sv = srpc_data.rpc_services[i];
1646
1647                         LASSERTF (sv == NULL,
1648                                   "service not empty: id %d, name %s\n",
1649                                   i, sv->sv_name);
1650                 }
1651
1652                 spin_unlock(&srpc_data.rpc_glock);
1653
1654                 stt_shutdown();
1655
1656         case SRPC_STATE_EQ_INIT:
1657                 rc = LNetClearLazyPortal(SRPC_FRAMEWORK_REQUEST_PORTAL);
1658                 rc = LNetClearLazyPortal(SRPC_REQUEST_PORTAL);
1659                 LASSERT (rc == 0);
1660                 rc = LNetEQFree(srpc_data.rpc_lnet_eq);
1661                 LASSERT (rc == 0); /* the EQ should have no user by now */
1662
1663         case SRPC_STATE_NI_INIT:
1664                 LNetNIFini();
1665         }
1666
1667         return;
1668 }