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