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