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