Whamcloud - gitweb
LU-1154 clio: rename coo_attr_set to coo_attr_update
[fs/lustre-release.git] / lnet / selftest / selftest.h
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  * copy of GPLv2].
20  *
21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22  * CA 95054 USA or visit www.sun.com if you need additional information or
23  * have any questions.
24  *
25  * GPL HEADER END
26  */
27 /*
28  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
29  * Use is subject to license terms.
30  *
31  * Copyright (c) 2012, 2013, Intel Corporation.
32  */
33 /*
34  * This file is part of Lustre, http://www.lustre.org/
35  * Lustre is a trademark of Sun Microsystems, Inc.
36  *
37  * lnet/selftest/selftest.h
38  *
39  * Author: Isaac Huang <isaac@clusterfs.com>
40  */
41 #ifndef __SELFTEST_SELFTEST_H__
42 #define __SELFTEST_SELFTEST_H__
43
44 #define LNET_ONLY
45
46 #ifndef __KERNEL__
47
48 /* XXX workaround XXX */
49 #ifdef HAVE_SYS_TYPES_H
50 #include <sys/types.h>
51 #endif
52
53 #endif
54 #include <libcfs/libcfs.h>
55 #include <lnet/lnet.h>
56 #include <lnet/lib-lnet.h>
57 #include <lnet/lib-types.h>
58 #include <lnet/lnetst.h>
59
60 #include "rpc.h"
61 #include "timer.h"
62
63 #ifndef MADE_WITHOUT_COMPROMISE
64 #define MADE_WITHOUT_COMPROMISE
65 #endif
66
67
68 #define SWI_STATE_NEWBORN                  0
69 #define SWI_STATE_REPLY_SUBMITTED          1
70 #define SWI_STATE_REPLY_SENT               2
71 #define SWI_STATE_REQUEST_SUBMITTED        3
72 #define SWI_STATE_REQUEST_SENT             4
73 #define SWI_STATE_REPLY_RECEIVED           5
74 #define SWI_STATE_BULK_STARTED             6
75 #define SWI_STATE_DONE                     10
76
77 /* forward refs */
78 struct srpc_service;
79 struct srpc_service_cd;
80 struct sfw_test_unit;
81 struct sfw_test_instance;
82
83 /* services below SRPC_FRAMEWORK_SERVICE_MAX_ID are framework
84  * services, e.g. create/modify session.
85  */
86 #define SRPC_SERVICE_DEBUG              0
87 #define SRPC_SERVICE_MAKE_SESSION       1
88 #define SRPC_SERVICE_REMOVE_SESSION     2
89 #define SRPC_SERVICE_BATCH              3
90 #define SRPC_SERVICE_TEST               4
91 #define SRPC_SERVICE_QUERY_STAT         5
92 #define SRPC_SERVICE_JOIN               6
93 #define SRPC_FRAMEWORK_SERVICE_MAX_ID   10
94 /* other services start from SRPC_FRAMEWORK_SERVICE_MAX_ID+1 */
95 #define SRPC_SERVICE_BRW                11
96 #define SRPC_SERVICE_PING               12
97 #define SRPC_SERVICE_MAX_ID             12
98
99 #define SRPC_REQUEST_PORTAL             50
100 /* a lazy portal for framework RPC requests */
101 #define SRPC_FRAMEWORK_REQUEST_PORTAL   51
102 /* all reply/bulk RDMAs go to this portal */
103 #define SRPC_RDMA_PORTAL                52
104
105 static inline srpc_msg_type_t
106 srpc_service2request (int service)
107 {
108         switch (service) {
109         default:
110                 LBUG ();
111         case SRPC_SERVICE_DEBUG:
112                 return SRPC_MSG_DEBUG_REQST;
113
114         case SRPC_SERVICE_MAKE_SESSION:
115                 return SRPC_MSG_MKSN_REQST;
116
117         case SRPC_SERVICE_REMOVE_SESSION:
118                 return SRPC_MSG_RMSN_REQST;
119
120         case SRPC_SERVICE_BATCH:
121                 return SRPC_MSG_BATCH_REQST;
122
123         case SRPC_SERVICE_TEST:
124                 return SRPC_MSG_TEST_REQST;
125
126         case SRPC_SERVICE_QUERY_STAT:
127                 return SRPC_MSG_STAT_REQST;
128
129         case SRPC_SERVICE_BRW:
130                 return SRPC_MSG_BRW_REQST;
131
132         case SRPC_SERVICE_PING:
133                 return SRPC_MSG_PING_REQST;
134
135         case SRPC_SERVICE_JOIN:
136                 return SRPC_MSG_JOIN_REQST;
137         }
138 }
139
140 static inline srpc_msg_type_t
141 srpc_service2reply (int service)
142 {
143         return srpc_service2request(service) + 1;
144 }
145
146 typedef enum {
147         SRPC_BULK_REQ_RCVD   = 1, /* passive bulk request(PUT sink/GET source) received */
148         SRPC_BULK_PUT_SENT   = 2, /* active bulk PUT sent (source) */
149         SRPC_BULK_GET_RPLD   = 3, /* active bulk GET replied (sink) */
150         SRPC_REPLY_RCVD      = 4, /* incoming reply received */
151         SRPC_REPLY_SENT      = 5, /* outgoing reply sent */
152         SRPC_REQUEST_RCVD    = 6, /* incoming request received */
153         SRPC_REQUEST_SENT    = 7, /* outgoing request sent */
154 } srpc_event_type_t;
155
156 /* RPC event */
157 typedef struct {
158         srpc_event_type_t ev_type;   /* what's up */
159         lnet_event_kind_t ev_lnet;   /* LNet event type */
160         int               ev_fired;  /* LNet event fired? */
161         int               ev_status; /* LNet event status */
162         void             *ev_data;   /* owning server/client RPC */
163 } srpc_event_t;
164
165 typedef struct {
166         int              bk_len;  /* len of bulk data */
167         lnet_handle_md_t bk_mdh;
168         int              bk_sink; /* sink/source */
169         int              bk_niov; /* # iov in bk_iovs */
170 #ifdef __KERNEL__
171         lnet_kiov_t      bk_iovs[0];
172 #else
173         struct page     **bk_pages;
174         lnet_md_iovec_t  bk_iovs[0];
175 #endif
176 } srpc_bulk_t; /* bulk descriptor */
177
178 /* message buffer descriptor */
179 typedef struct srpc_buffer {
180         struct list_head        buf_list; /* chain on srpc_service::*_msgq */
181         srpc_msg_t              buf_msg;
182         lnet_handle_md_t        buf_mdh;
183         lnet_nid_t              buf_self;
184         lnet_process_id_t       buf_peer;
185 } srpc_buffer_t;
186
187 struct swi_workitem;
188 typedef int (*swi_action_t) (struct swi_workitem *);
189
190 typedef struct swi_workitem {
191         struct cfs_wi_sched     *swi_sched;
192         cfs_workitem_t       swi_workitem;
193         swi_action_t         swi_action;
194         int                  swi_state;
195 } swi_workitem_t;
196
197 /* server-side state of a RPC */
198 typedef struct srpc_server_rpc {
199         /* chain on srpc_service::*_rpcq */
200         struct list_head        srpc_list;
201         struct srpc_service_cd *srpc_scd;
202         swi_workitem_t          srpc_wi;
203         srpc_event_t            srpc_ev;        /* bulk/reply event */
204         lnet_nid_t              srpc_self;
205         lnet_process_id_t       srpc_peer;
206         srpc_msg_t              srpc_replymsg;
207         lnet_handle_md_t        srpc_replymdh;
208         srpc_buffer_t           *srpc_reqstbuf;
209         srpc_bulk_t             *srpc_bulk;
210
211         unsigned int    srpc_aborted; /* being given up */
212         int             srpc_status;
213         void            (*srpc_done)(struct srpc_server_rpc *);
214 } srpc_server_rpc_t;
215
216 /* client-side state of a RPC */
217 typedef struct srpc_client_rpc {
218         struct list_head        crpc_list;      /* chain on user's lists */
219         spinlock_t              crpc_lock;      /* serialize */
220         int                     crpc_service;
221         atomic_t                crpc_refcount;
222         /* # seconds to wait for reply */
223         int                     crpc_timeout;
224         stt_timer_t             crpc_timer;
225         swi_workitem_t          crpc_wi;
226         lnet_process_id_t       crpc_dest;
227
228         void               (*crpc_done)(struct srpc_client_rpc *);
229         void               (*crpc_fini)(struct srpc_client_rpc *);
230         int                  crpc_status;    /* completion status */
231         void                *crpc_priv;      /* caller data */
232
233         /* state flags */
234         unsigned int         crpc_aborted:1; /* being given up */
235         unsigned int         crpc_closed:1;  /* completed */
236
237         /* RPC events */
238         srpc_event_t         crpc_bulkev;    /* bulk event */
239         srpc_event_t         crpc_reqstev;   /* request event */
240         srpc_event_t         crpc_replyev;   /* reply event */
241
242         /* bulk, request(reqst), and reply exchanged on wire */
243         srpc_msg_t           crpc_reqstmsg;
244         srpc_msg_t           crpc_replymsg;
245         lnet_handle_md_t     crpc_reqstmdh;
246         lnet_handle_md_t     crpc_replymdh;
247         srpc_bulk_t          crpc_bulk;
248 } srpc_client_rpc_t;
249
250 #define srpc_client_rpc_size(rpc)                                       \
251 offsetof(srpc_client_rpc_t, crpc_bulk.bk_iovs[(rpc)->crpc_bulk.bk_niov])
252
253 #define srpc_client_rpc_addref(rpc)                                     \
254 do {                                                                    \
255         CDEBUG(D_NET, "RPC[%p] -> %s (%d)++\n",                         \
256                (rpc), libcfs_id2str((rpc)->crpc_dest),                  \
257                atomic_read(&(rpc)->crpc_refcount));                 \
258         LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0);            \
259         atomic_inc(&(rpc)->crpc_refcount);                          \
260 } while (0)
261
262 #define srpc_client_rpc_decref(rpc)                                     \
263 do {                                                                    \
264         CDEBUG(D_NET, "RPC[%p] -> %s (%d)--\n",                         \
265                (rpc), libcfs_id2str((rpc)->crpc_dest),                  \
266                atomic_read(&(rpc)->crpc_refcount));                 \
267         LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0);            \
268         if (atomic_dec_and_test(&(rpc)->crpc_refcount))             \
269                 srpc_destroy_client_rpc(rpc);                           \
270 } while (0)
271
272 #define srpc_event_pending(rpc)   ((rpc)->crpc_bulkev.ev_fired == 0 ||  \
273                                    (rpc)->crpc_reqstev.ev_fired == 0 || \
274                                    (rpc)->crpc_replyev.ev_fired == 0)
275
276 /* CPU partition data of srpc service */
277 struct srpc_service_cd {
278         /** serialize */
279         spinlock_t              scd_lock;
280         /** backref to service */
281         struct srpc_service     *scd_svc;
282         /** event buffer */
283         srpc_event_t            scd_ev;
284         /** free RPC descriptors */
285         struct list_head        scd_rpc_free;
286         /** in-flight RPCs */
287         struct list_head        scd_rpc_active;
288         /** workitem for posting buffer */
289         swi_workitem_t          scd_buf_wi;
290         /** CPT id */
291         int                     scd_cpt;
292         /** error code for scd_buf_wi */
293         int                     scd_buf_err;
294         /** timestamp for scd_buf_err */
295         unsigned long           scd_buf_err_stamp;
296         /** total # request buffers */
297         int                     scd_buf_total;
298         /** # posted request buffers */
299         int                     scd_buf_nposted;
300         /** in progress of buffer posting */
301         int                     scd_buf_posting;
302         /** allocate more buffers if scd_buf_nposted < scd_buf_low */
303         int                     scd_buf_low;
304         /** increase/decrease some buffers */
305         int                     scd_buf_adjust;
306         /** posted message buffers */
307         struct list_head        scd_buf_posted;
308         /** blocked for RPC descriptor */
309         struct list_head        scd_buf_blocked;
310 };
311
312 /* number of server workitems (mini-thread) for testing service */
313 #define SFW_TEST_WI_MIN         256
314 #define SFW_TEST_WI_MAX         2048
315 /* extra buffers for tolerating buggy peers, or unbalanced number
316  * of peers between partitions  */
317 #define SFW_TEST_WI_EXTRA       64
318
319 /* number of server workitems (mini-thread) for framework service */
320 #define SFW_FRWK_WI_MIN         16
321 #define SFW_FRWK_WI_MAX         256
322
323 typedef struct srpc_service {
324         int                     sv_id;          /* service id */
325         const char              *sv_name;       /* human readable name */
326         int                     sv_wi_total;    /* total server workitems */
327         int                     sv_shuttingdown;
328         int                     sv_ncpts;
329         /* percpt data for srpc_service */
330         struct srpc_service_cd  **sv_cpt_data;
331         /* Service callbacks:
332          * - sv_handler: process incoming RPC request
333          * - sv_bulk_ready: notify bulk data
334          */
335         int              (*sv_handler) (srpc_server_rpc_t *);
336         int              (*sv_bulk_ready) (srpc_server_rpc_t *, int);
337 } srpc_service_t;
338
339 typedef struct {
340         /* chain on fw_zombie_sessions */
341         struct list_head        sn_list;
342         lst_sid_t               sn_id;          /* unique identifier */
343         /* # seconds' inactivity to expire */
344         unsigned int            sn_timeout;
345         int                     sn_timer_active;
346         unsigned int            sn_features;
347         stt_timer_t             sn_timer;
348         struct list_head        sn_batches;     /* list of batches */
349         char                    sn_name[LST_NAME_SIZE];
350         atomic_t                sn_refcount;
351         atomic_t                sn_brw_errors;
352         atomic_t                sn_ping_errors;
353         cfs_time_t              sn_started;
354 } sfw_session_t;
355
356 #define sfw_sid_equal(sid0, sid1)     ((sid0).ses_nid == (sid1).ses_nid && \
357                                        (sid0).ses_stamp == (sid1).ses_stamp)
358
359 typedef struct {
360         struct list_head        bat_list;       /* chain on sn_batches */
361         lst_bid_t               bat_id;         /* batch id */
362         int                     bat_error;      /* error code of batch */
363         sfw_session_t           *bat_session;   /* batch's session */
364         atomic_t                bat_nactive;    /* # of active tests */
365         struct list_head        bat_tests;      /* test instances */
366 } sfw_batch_t;
367
368 typedef struct {
369         int  (*tso_init)(struct sfw_test_instance *tsi); /* intialize test client */
370         void (*tso_fini)(struct sfw_test_instance *tsi); /* finalize test client */
371         int  (*tso_prep_rpc)(struct sfw_test_unit *tsu,
372                              lnet_process_id_t dest,
373                              srpc_client_rpc_t **rpc);   /* prep a tests rpc */
374         void (*tso_done_rpc)(struct sfw_test_unit *tsu,
375                              srpc_client_rpc_t *rpc);    /* done a test rpc */
376 } sfw_test_client_ops_t;
377
378 typedef struct sfw_test_instance {
379         struct list_head        tsi_list;       /* chain on batch */
380         int                     tsi_service;    /* test type */
381         sfw_batch_t             *tsi_batch;     /* batch */
382         sfw_test_client_ops_t   *tsi_ops;       /* test client operations */
383
384         /* public parameter for all test units */
385         unsigned int            tsi_is_client:1;     /* is test client */
386         unsigned int            tsi_stoptsu_onerr:1; /* stop tsu on error */
387         int                     tsi_concur;          /* concurrency */
388         int                     tsi_loop;            /* loop count */
389
390         /* status of test instance */
391         spinlock_t              tsi_lock;       /* serialize */
392         unsigned int            tsi_stopping:1; /* test is stopping */
393         atomic_t                tsi_nactive;    /* # of active test unit */
394         struct list_head        tsi_units;      /* test units */
395         struct list_head        tsi_free_rpcs;  /* free rpcs */
396         struct list_head        tsi_active_rpcs;/* active rpcs */
397
398         union {
399                 test_ping_req_t         ping;     /* ping parameter */
400                 test_bulk_req_t         bulk_v0;  /* bulk parameter */
401                 test_bulk_req_v1_t      bulk_v1;  /* bulk v1 parameter */
402         } tsi_u;
403 } sfw_test_instance_t;
404
405 /* XXX: trailing (PAGE_CACHE_SIZE % sizeof(lnet_process_id_t)) bytes at
406  * the end of pages are not used */
407 #define SFW_MAX_CONCUR     LST_MAX_CONCUR
408 #define SFW_ID_PER_PAGE    (PAGE_CACHE_SIZE / sizeof(lnet_process_id_packed_t))
409 #define SFW_MAX_NDESTS     (LNET_MAX_IOV * SFW_ID_PER_PAGE)
410 #define sfw_id_pages(n)    (((n) + SFW_ID_PER_PAGE - 1) / SFW_ID_PER_PAGE)
411
412 typedef struct sfw_test_unit {
413         struct list_head        tsu_list;       /* chain on lst_test_instance */
414         lnet_process_id_t       tsu_dest;       /* id of dest node */
415         int                     tsu_loop;       /* loop count of the test */
416         sfw_test_instance_t     *tsu_instance;  /* pointer to test instance */
417         void                    *tsu_private;   /* private data */
418         swi_workitem_t          tsu_worker;     /* workitem of the test unit */
419 } sfw_test_unit_t;
420
421 typedef struct sfw_test_case {
422         struct list_head        tsc_list;               /* chain on fw_tests */
423         srpc_service_t          *tsc_srv_service;       /* test service */
424         sfw_test_client_ops_t   *tsc_cli_ops;           /* ops of test client */
425 } sfw_test_case_t;
426
427 srpc_client_rpc_t *
428 sfw_create_rpc(lnet_process_id_t peer, int service,
429                unsigned features, int nbulkiov, int bulklen,
430                void (*done) (srpc_client_rpc_t *), void *priv);
431 int sfw_create_test_rpc(sfw_test_unit_t *tsu,
432                         lnet_process_id_t peer, unsigned features,
433                         int nblk, int blklen, srpc_client_rpc_t **rpc);
434 void sfw_abort_rpc(srpc_client_rpc_t *rpc);
435 void sfw_post_rpc(srpc_client_rpc_t *rpc);
436 void sfw_client_rpc_done(srpc_client_rpc_t *rpc);
437 void sfw_unpack_message(srpc_msg_t *msg);
438 void sfw_free_pages(srpc_server_rpc_t *rpc);
439 void sfw_add_bulk_page(srpc_bulk_t *bk, struct page *pg, int i);
440 int sfw_alloc_pages(srpc_server_rpc_t *rpc, int cpt, int npages, int len,
441                     int sink);
442 int sfw_make_session (srpc_mksn_reqst_t *request, srpc_mksn_reply_t *reply);
443
444 srpc_client_rpc_t *
445 srpc_create_client_rpc(lnet_process_id_t peer, int service,
446                        int nbulkiov, int bulklen,
447                        void (*rpc_done)(srpc_client_rpc_t *),
448                        void (*rpc_fini)(srpc_client_rpc_t *), void *priv);
449 void srpc_post_rpc(srpc_client_rpc_t *rpc);
450 void srpc_abort_rpc(srpc_client_rpc_t *rpc, int why);
451 void srpc_free_bulk(srpc_bulk_t *bk);
452 srpc_bulk_t *srpc_alloc_bulk(int cpt, unsigned bulk_npg, unsigned bulk_len,
453                              int sink);
454 int srpc_send_rpc(swi_workitem_t *wi);
455 int srpc_send_reply(srpc_server_rpc_t *rpc);
456 int srpc_add_service(srpc_service_t *sv);
457 int srpc_remove_service(srpc_service_t *sv);
458 void srpc_shutdown_service(srpc_service_t *sv);
459 void srpc_abort_service(srpc_service_t *sv);
460 int srpc_finish_service(srpc_service_t *sv);
461 int srpc_service_add_buffers(srpc_service_t *sv, int nbuffer);
462 void srpc_service_remove_buffers(srpc_service_t *sv, int nbuffer);
463 void srpc_get_counters(srpc_counters_t *cnt);
464 void srpc_set_counters(const srpc_counters_t *cnt);
465
466 extern struct cfs_wi_sched *lst_sched_serial;
467 extern struct cfs_wi_sched **lst_sched_test;
468
469 static inline int
470 srpc_serv_is_framework(struct srpc_service *svc)
471 {
472         return svc->sv_id < SRPC_FRAMEWORK_SERVICE_MAX_ID;
473 }
474
475 static inline int
476 swi_wi_action(cfs_workitem_t *wi)
477 {
478         swi_workitem_t *swi = container_of(wi, swi_workitem_t, swi_workitem);
479
480         return swi->swi_action(swi);
481 }
482
483 static inline void
484 swi_init_workitem(swi_workitem_t *swi, void *data,
485                   swi_action_t action, struct cfs_wi_sched *sched)
486 {
487         swi->swi_sched  = sched;
488         swi->swi_action = action;
489         swi->swi_state  = SWI_STATE_NEWBORN;
490         cfs_wi_init(&swi->swi_workitem, data, swi_wi_action);
491 }
492
493 static inline void
494 swi_schedule_workitem(swi_workitem_t *wi)
495 {
496         cfs_wi_schedule(wi->swi_sched, &wi->swi_workitem);
497 }
498
499 static inline void
500 swi_exit_workitem(swi_workitem_t *swi)
501 {
502         cfs_wi_exit(swi->swi_sched, &swi->swi_workitem);
503 }
504
505 static inline int
506 swi_deschedule_workitem(swi_workitem_t *swi)
507 {
508         return cfs_wi_deschedule(swi->swi_sched, &swi->swi_workitem);
509 }
510
511 #ifndef __KERNEL__
512 static inline int
513 swi_check_events(void)
514 {
515         return cfs_wi_check_events();
516 }
517 #endif
518
519 int sfw_startup(void);
520 int srpc_startup(void);
521 void sfw_shutdown(void);
522 void srpc_shutdown(void);
523
524 static inline void
525 srpc_destroy_client_rpc (srpc_client_rpc_t *rpc)
526 {
527         LASSERT (rpc != NULL);
528         LASSERT (!srpc_event_pending(rpc));
529         LASSERT (atomic_read(&rpc->crpc_refcount) == 0);
530 #ifndef __KERNEL__
531         LASSERT (rpc->crpc_bulk.bk_pages == NULL);
532 #endif
533
534         if (rpc->crpc_fini == NULL) {
535                 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
536         } else {
537                 (*rpc->crpc_fini) (rpc);
538         }
539
540         return;
541 }
542
543 static inline void
544 srpc_init_client_rpc(srpc_client_rpc_t *rpc, lnet_process_id_t peer,
545                      int service, int nbulkiov, int bulklen,
546                      void (*rpc_done)(srpc_client_rpc_t *),
547                      void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
548 {
549         LASSERT(nbulkiov <= LNET_MAX_IOV);
550
551         memset(rpc, 0, offsetof(srpc_client_rpc_t,
552                                 crpc_bulk.bk_iovs[nbulkiov]));
553
554         INIT_LIST_HEAD(&rpc->crpc_list);
555         swi_init_workitem(&rpc->crpc_wi, rpc, srpc_send_rpc,
556                           lst_sched_test[lnet_cpt_of_nid(peer.nid)]);
557         spin_lock_init(&rpc->crpc_lock);
558         atomic_set(&rpc->crpc_refcount, 1); /* 1 ref for caller */
559
560         rpc->crpc_dest         = peer;
561         rpc->crpc_priv         = priv;
562         rpc->crpc_service      = service;
563         rpc->crpc_bulk.bk_len  = bulklen;
564         rpc->crpc_bulk.bk_niov = nbulkiov;
565         rpc->crpc_done         = rpc_done;
566         rpc->crpc_fini         = rpc_fini;
567         LNetInvalidateHandle(&rpc->crpc_reqstmdh);
568         LNetInvalidateHandle(&rpc->crpc_replymdh);
569         LNetInvalidateHandle(&rpc->crpc_bulk.bk_mdh);
570
571         /* no event is expected at this point */
572         rpc->crpc_bulkev.ev_fired  =
573         rpc->crpc_reqstev.ev_fired =
574         rpc->crpc_replyev.ev_fired = 1;
575
576         rpc->crpc_reqstmsg.msg_magic   = SRPC_MSG_MAGIC;
577         rpc->crpc_reqstmsg.msg_version = SRPC_MSG_VERSION;
578         rpc->crpc_reqstmsg.msg_type    = srpc_service2request(service);
579         return;
580 }
581
582 static inline const char *
583 swi_state2str (int state)
584 {
585 #define STATE2STR(x) case x: return #x
586         switch(state) {
587                 default:
588                         LBUG();
589                 STATE2STR(SWI_STATE_NEWBORN);
590                 STATE2STR(SWI_STATE_REPLY_SUBMITTED);
591                 STATE2STR(SWI_STATE_REPLY_SENT);
592                 STATE2STR(SWI_STATE_REQUEST_SUBMITTED);
593                 STATE2STR(SWI_STATE_REQUEST_SENT);
594                 STATE2STR(SWI_STATE_REPLY_RECEIVED);
595                 STATE2STR(SWI_STATE_BULK_STARTED);
596                 STATE2STR(SWI_STATE_DONE);
597         }
598 #undef STATE2STR
599 }
600
601 #ifndef __KERNEL__
602
603 int stt_poll_interval(void);
604 int sfw_session_removed(void);
605
606 int stt_check_events(void);
607 int srpc_check_event(int timeout);
608
609 int lnet_selftest_init(void);
610 void lnet_selftest_fini(void);
611 int selftest_wait_events(void);
612
613 #else
614
615 #define selftest_wait_events()  cfs_pause(cfs_time_seconds(1) / 10)
616
617 #endif
618
619 #define lst_wait_until(cond, lock, fmt, ...)                            \
620 do {                                                                    \
621         int __I = 2;                                                    \
622         while (!(cond)) {                                               \
623                 CDEBUG(IS_PO2(++__I) ? D_WARNING : D_NET,               \
624                        fmt, ## __VA_ARGS__);                            \
625                 spin_unlock(&(lock));                                   \
626                                                                         \
627                 selftest_wait_events();                                 \
628                                                                         \
629                 spin_lock(&(lock));                                     \
630         }                                                               \
631 } while (0)
632
633 static inline void
634 srpc_wait_service_shutdown(srpc_service_t *sv)
635 {
636         int i = 2;
637
638         LASSERT(sv->sv_shuttingdown);
639
640         while (srpc_finish_service(sv) == 0) {
641                 i++;
642                 CDEBUG (((i & -i) == i) ? D_WARNING : D_NET,
643                         "Waiting for %s service to shutdown...\n",
644                         sv->sv_name);
645                 selftest_wait_events();
646         }
647 }
648
649 extern sfw_test_client_ops_t ping_test_client;
650 extern srpc_service_t        ping_test_service;
651 void ping_init_test_client(void);
652 void ping_init_test_service(void);
653
654 extern sfw_test_client_ops_t brw_test_client;
655 extern srpc_service_t        brw_test_service;
656 void brw_init_test_client(void);
657 void brw_init_test_service(void);
658
659 #endif /* __SELFTEST_SELFTEST_H__ */