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