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