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