1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5 * Author: Isaac Huang <isaac@clusterfs.com>
8 #ifndef __SELFTEST_SELFTEST_H__
9 #define __SELFTEST_SELFTEST_H__
15 /* XXX workaround XXX */
16 #ifdef HAVE_SYS_TYPES_H
17 #include <sys/types.h>
20 /* TODO: remove these when libcfs provides proper primitives for userspace
22 * Dummy implementations of spinlock_t and atomic_t work since userspace
23 * selftest is completely single-threaded, even using multi-threaded usocklnd.
25 typedef struct { } spinlock_t;
26 static inline void spin_lock(spinlock_t *l) {return;}
27 static inline void spin_unlock(spinlock_t *l) {return;}
28 static inline void spin_lock_init(spinlock_t *l) {return;}
30 typedef struct { volatile int counter; } atomic_t;
31 #define atomic_read(a) ((a)->counter)
32 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
33 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
34 #define atomic_inc(a) (((a)->counter)++)
35 #define atomic_dec(a) do { (a)->counter--; } while (0)
38 #include <libcfs/libcfs.h>
39 #include <lnet/lnet.h>
40 #include <lnet/lib-lnet.h>
41 #include <lnet/lib-types.h>
42 #include <lnet/lnetst.h>
47 #ifndef MADE_WITHOUT_COMPROMISE
48 #define MADE_WITHOUT_COMPROMISE
52 #define SWI_STATE_NEWBORN 0
53 #define SWI_STATE_REPLY_SUBMITTED 1
54 #define SWI_STATE_REPLY_SENT 2
55 #define SWI_STATE_REQUEST_SUBMITTED 3
56 #define SWI_STATE_REQUEST_SENT 4
57 #define SWI_STATE_REPLY_RECEIVED 5
58 #define SWI_STATE_BULK_STARTED 6
59 #define SWI_STATE_BULK_ERRORED 7
60 #define SWI_STATE_DONE 10
66 struct sfw_test_instance;
69 * A workitems is deferred work with these semantics:
70 * - a workitem always runs in thread context.
71 * - a workitem can be concurrent with other workitems but is strictly
72 * serialized with respect to itself.
73 * - no CPU affinity, a workitem does not necessarily run on the same CPU
74 * that schedules it. However, this might change in the future.
75 * - if a workitem is scheduled again before it has a chance to run, it
77 * - if a workitem is scheduled while it runs, it runs again after it
78 * completes; this ensures that events occurring while other events are
79 * being processed receive due attention. This behavior also allows a
80 * workitem to reschedule itself.
83 * - a workitem can sleep but it should be aware of how that sleep might
85 * - a workitem runs inside a kernel thread so there's no user space to access.
86 * - do not use a workitem if the scheduling latency can't be tolerated.
88 * When wi_action returns non-zero, it means the workitem has either been
89 * freed or reused and workitem scheduler won't touch it any more.
91 typedef int (*swi_action_t) (struct swi_workitem *);
92 typedef struct swi_workitem {
93 struct list_head wi_list; /* chain on runq */
95 swi_action_t wi_action;
97 unsigned int wi_running:1;
98 unsigned int wi_scheduled:1;
102 swi_init_workitem (swi_workitem_t *wi, void *data, swi_action_t action)
104 CFS_INIT_LIST_HEAD(&wi->wi_list);
107 wi->wi_scheduled = 0;
109 wi->wi_action = action;
110 wi->wi_state = SWI_STATE_NEWBORN;
113 #define SWI_RESCHED 128 /* # workitem scheduler loops before reschedule */
115 /* services below SRPC_FRAMEWORK_SERVICE_MAX_ID are framework
116 * services, e.g. create/modify session.
118 #define SRPC_SERVICE_DEBUG 0
119 #define SRPC_SERVICE_MAKE_SESSION 1
120 #define SRPC_SERVICE_REMOVE_SESSION 2
121 #define SRPC_SERVICE_BATCH 3
122 #define SRPC_SERVICE_TEST 4
123 #define SRPC_SERVICE_QUERY_STAT 5
124 #define SRPC_SERVICE_JOIN 6
125 #define SRPC_FRAMEWORK_SERVICE_MAX_ID 10
126 /* other services start from SRPC_FRAMEWORK_SERVICE_MAX_ID+1 */
127 #define SRPC_SERVICE_BRW 11
128 #define SRPC_SERVICE_PING 12
129 #define SRPC_SERVICE_MAX_ID 12
131 #define SRPC_REQUEST_PORTAL 50
132 /* a lazy portal for framework RPC requests */
133 #define SRPC_FRAMEWORK_REQUEST_PORTAL 51
134 /* all reply/bulk RDMAs go to this portal */
135 #define SRPC_RDMA_PORTAL 52
137 static inline srpc_msg_type_t
138 srpc_service2request (int service)
143 case SRPC_SERVICE_DEBUG:
144 return SRPC_MSG_DEBUG_REQST;
146 case SRPC_SERVICE_MAKE_SESSION:
147 return SRPC_MSG_MKSN_REQST;
149 case SRPC_SERVICE_REMOVE_SESSION:
150 return SRPC_MSG_RMSN_REQST;
152 case SRPC_SERVICE_BATCH:
153 return SRPC_MSG_BATCH_REQST;
155 case SRPC_SERVICE_TEST:
156 return SRPC_MSG_TEST_REQST;
158 case SRPC_SERVICE_QUERY_STAT:
159 return SRPC_MSG_STAT_REQST;
161 case SRPC_SERVICE_BRW:
162 return SRPC_MSG_BRW_REQST;
164 case SRPC_SERVICE_PING:
165 return SRPC_MSG_PING_REQST;
167 case SRPC_SERVICE_JOIN:
168 return SRPC_MSG_JOIN_REQST;
172 static inline srpc_msg_type_t
173 srpc_service2reply (int service)
175 return srpc_service2request(service) + 1;
179 SRPC_BULK_REQ_RCVD = 0, /* passive bulk request(PUT sink/GET source) received */
180 SRPC_BULK_PUT_SENT = 1, /* active bulk PUT sent (source) */
181 SRPC_BULK_GET_RPLD = 2, /* active bulk GET replied (sink) */
182 SRPC_REPLY_RCVD = 3, /* incoming reply received */
183 SRPC_REPLY_SENT = 4, /* outgoing reply sent */
184 SRPC_REQUEST_RCVD = 5, /* incoming request received */
185 SRPC_REQUEST_SENT = 6, /* outgoing request sent */
190 srpc_event_type_t ev_type; /* what's up */
191 lnet_event_kind_t ev_lnet; /* LNet event type */
192 int ev_fired; /* LNet event fired? */
193 int ev_status; /* LNet event status */
194 void *ev_data; /* owning server/client RPC */
198 int bk_len; /* len of bulk data */
199 lnet_handle_md_t bk_mdh;
200 int bk_sink; /* sink/source */
201 int bk_niov; /* # iov in bk_iovs */
203 lnet_kiov_t bk_iovs[0];
205 cfs_page_t **bk_pages;
206 lnet_md_iovec_t bk_iovs[0];
208 } srpc_bulk_t; /* bulk descriptor */
210 typedef struct srpc_peer {
211 struct list_head stp_list; /* chain on peer hash */
212 struct list_head stp_rpcq; /* q of non-control RPCs */
213 struct list_head stp_ctl_rpcq; /* q of control RPCs */
214 spinlock_t stp_lock; /* serialize */
216 int stp_credits; /* available credits */
219 /* message buffer descriptor */
221 struct list_head buf_list; /* chain on srpc_service::*_msgq */
223 lnet_handle_md_t buf_mdh;
225 lnet_process_id_t buf_peer;
228 /* server-side state of a RPC */
229 typedef struct srpc_server_rpc {
230 struct list_head srpc_list; /* chain on srpc_service::*_rpcq */
231 struct srpc_service *srpc_service;
232 swi_workitem_t srpc_wi;
233 srpc_event_t srpc_ev; /* bulk/reply event */
234 lnet_nid_t srpc_self;
235 lnet_process_id_t srpc_peer;
236 srpc_msg_t srpc_replymsg;
237 lnet_handle_md_t srpc_replymdh;
238 srpc_buffer_t *srpc_reqstbuf;
239 srpc_bulk_t *srpc_bulk;
242 void (*srpc_done)(struct srpc_server_rpc *);
245 /* client-side state of a RPC */
246 typedef struct srpc_client_rpc {
247 struct list_head crpc_list; /* chain on user's lists */
248 struct list_head crpc_privl; /* chain on srpc_peer_t::*rpcq */
249 spinlock_t crpc_lock; /* serialize */
251 atomic_t crpc_refcount;
252 int crpc_timeout; /* # seconds to wait for reply */
253 stt_timer_t crpc_timer;
254 swi_workitem_t crpc_wi;
255 lnet_process_id_t crpc_dest;
256 srpc_peer_t *crpc_peer;
258 void (*crpc_done)(struct srpc_client_rpc *);
259 void (*crpc_fini)(struct srpc_client_rpc *);
260 int crpc_status; /* completion status */
261 void *crpc_priv; /* caller data */
264 unsigned int crpc_aborted:1; /* being given up */
265 unsigned int crpc_closed:1; /* completed */
268 srpc_event_t crpc_bulkev; /* bulk event */
269 srpc_event_t crpc_reqstev; /* request event */
270 srpc_event_t crpc_replyev; /* reply event */
272 /* bulk, request(reqst), and reply exchanged on wire */
273 srpc_msg_t crpc_reqstmsg;
274 srpc_msg_t crpc_replymsg;
275 lnet_handle_md_t crpc_reqstmdh;
276 lnet_handle_md_t crpc_replymdh;
277 srpc_bulk_t crpc_bulk;
280 #define srpc_client_rpc_size(rpc) \
281 offsetof(srpc_client_rpc_t, crpc_bulk.bk_iovs[(rpc)->crpc_bulk.bk_niov])
283 #define srpc_client_rpc_addref(rpc) \
285 CDEBUG(D_NET, "RPC[%p] -> %s (%d)++\n", \
286 (rpc), libcfs_id2str((rpc)->crpc_dest), \
287 atomic_read(&(rpc)->crpc_refcount)); \
288 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
289 atomic_inc(&(rpc)->crpc_refcount); \
292 #define srpc_client_rpc_decref(rpc) \
294 CDEBUG(D_NET, "RPC[%p] -> %s (%d)--\n", \
295 (rpc), libcfs_id2str((rpc)->crpc_dest), \
296 atomic_read(&(rpc)->crpc_refcount)); \
297 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
298 if (atomic_dec_and_test(&(rpc)->crpc_refcount)) \
299 srpc_destroy_client_rpc(rpc); \
302 #define srpc_event_pending(rpc) ((rpc)->crpc_bulkev.ev_fired == 0 || \
303 (rpc)->crpc_reqstev.ev_fired == 0 || \
304 (rpc)->crpc_replyev.ev_fired == 0)
306 typedef struct srpc_service {
307 int sv_id; /* service id */
308 const char *sv_name; /* human readable name */
309 int sv_nprune; /* # posted RPC to be pruned */
310 int sv_concur; /* max # concurrent RPCs */
314 srpc_event_t sv_ev; /* LNet event */
315 int sv_nposted_msg; /* # posted message buffers */
316 struct list_head sv_free_rpcq; /* free RPC descriptors */
317 struct list_head sv_active_rpcq; /* in-flight RPCs */
318 struct list_head sv_posted_msgq; /* posted message buffers */
319 struct list_head sv_blocked_msgq; /* blocked for RPC descriptor */
321 /* Service callbacks:
322 * - sv_handler: process incoming RPC request
323 * - sv_bulk_ready: notify bulk data
325 int (*sv_handler) (srpc_server_rpc_t *);
326 int (*sv_bulk_ready) (srpc_server_rpc_t *, int);
329 #define SFW_POST_BUFFERS 8
330 #define SFW_SERVICE_CONCURRENCY (SFW_POST_BUFFERS/2)
333 struct list_head sn_list; /* chain on fw_zombie_sessions */
334 lst_sid_t sn_id; /* unique identifier */
335 unsigned int sn_timeout; /* # seconds' inactivity to expire */
337 stt_timer_t sn_timer;
338 struct list_head sn_batches; /* list of batches */
339 char sn_name[LST_NAME_SIZE];
340 atomic_t sn_brw_errors;
341 atomic_t sn_ping_errors;
344 #define sfw_sid_equal(sid0, sid1) ((sid0).ses_nid == (sid1).ses_nid && \
345 (sid0).ses_stamp == (sid1).ses_stamp)
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 */
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;
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 */
372 /* public parameter for all test units */
373 int tsi_is_client:1; /* is test client */
374 int tsi_stoptsu_onerr:1; /* stop tsu on error */
375 int tsi_concur; /* concurrency */
376 int tsi_loop; /* loop count */
378 /* status of test instance */
379 spinlock_t tsi_lock; /* serialize */
380 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 */
387 test_bulk_req_t bulk; /* bulk parameter */
388 test_ping_req_t ping; /* ping parameter */
390 } sfw_test_instance_t;
392 /* XXX: trailing (CFS_PAGE_SIZE % sizeof(lnet_process_id_t)) bytes at
393 * the end of pages are not used */
394 #define SFW_MAX_CONCUR LST_MAX_CONCUR
395 #define SFW_ID_PER_PAGE (CFS_PAGE_SIZE / sizeof(lnet_process_id_t))
396 #define SFW_MAX_NDESTS (LNET_MAX_IOV * SFW_ID_PER_PAGE)
397 #define sfw_id_pages(n) (((n) + SFW_ID_PER_PAGE - 1) / SFW_ID_PER_PAGE)
399 typedef struct sfw_test_unit {
400 struct list_head tsu_list; /* chain on lst_test_instance */
401 lnet_process_id_t tsu_dest; /* id of dest node */
402 int tsu_loop; /* loop count of the test */
403 sfw_test_instance_t *tsu_instance; /* pointer to test instance */
404 void *tsu_private; /* private data */
405 swi_workitem_t tsu_worker; /* workitem of the test unit */
409 struct list_head tsc_list; /* chain on fw_tests */
410 srpc_service_t *tsc_srv_service; /* test service */
411 sfw_test_client_ops_t *tsc_cli_ops; /* ops of test client */
416 sfw_create_rpc(lnet_process_id_t peer, int service, int nbulkiov, int bulklen,
417 void (*done) (srpc_client_rpc_t *), void *priv);
418 int sfw_create_test_rpc(sfw_test_unit_t *tsu, lnet_process_id_t peer,
419 int nblk, int blklen, srpc_client_rpc_t **rpc);
420 void sfw_abort_rpc(srpc_client_rpc_t *rpc);
421 void sfw_post_rpc(srpc_client_rpc_t *rpc);
422 void sfw_client_rpc_done(srpc_client_rpc_t *rpc);
423 void sfw_unpack_message(srpc_msg_t *msg);
424 void sfw_free_pages(srpc_server_rpc_t *rpc);
425 void sfw_add_bulk_page(srpc_bulk_t *bk, cfs_page_t *pg, int i);
426 int sfw_alloc_pages(srpc_server_rpc_t *rpc, int npages, int sink);
429 srpc_create_client_rpc(lnet_process_id_t peer, int service,
430 int nbulkiov, int bulklen,
431 void (*rpc_done)(srpc_client_rpc_t *),
432 void (*rpc_fini)(srpc_client_rpc_t *), void *priv);
433 void srpc_post_rpc(srpc_client_rpc_t *rpc);
434 void srpc_abort_rpc(srpc_client_rpc_t *rpc, int why);
435 void srpc_free_bulk(srpc_bulk_t *bk);
436 srpc_bulk_t *srpc_alloc_bulk(int npages, int sink);
437 int srpc_send_rpc(swi_workitem_t *wi);
438 int srpc_send_reply(srpc_server_rpc_t *rpc);
439 int srpc_add_service(srpc_service_t *sv);
440 int srpc_remove_service(srpc_service_t *sv);
441 void srpc_shutdown_service(srpc_service_t *sv);
442 int srpc_finish_service(srpc_service_t *sv);
443 int srpc_service_add_buffers(srpc_service_t *sv, int nbuffer);
444 void srpc_service_remove_buffers(srpc_service_t *sv, int nbuffer);
445 void srpc_get_counters(srpc_counters_t *cnt);
446 void srpc_set_counters(const srpc_counters_t *cnt);
448 void swi_kill_workitem(swi_workitem_t *wi);
449 void swi_schedule_workitem(swi_workitem_t *wi);
450 void swi_schedule_serial_workitem(swi_workitem_t *wi);
451 int swi_startup(void);
452 int sfw_startup(void);
453 int srpc_startup(void);
454 void swi_shutdown(void);
455 void sfw_shutdown(void);
456 void srpc_shutdown(void);
459 srpc_destroy_client_rpc (srpc_client_rpc_t *rpc)
461 LASSERT (rpc != NULL);
462 LASSERT (!srpc_event_pending(rpc));
463 LASSERT (list_empty(&rpc->crpc_privl));
464 LASSERT (atomic_read(&rpc->crpc_refcount) == 0);
466 LASSERT (rpc->crpc_bulk.bk_pages == NULL);
469 if (rpc->crpc_fini == NULL) {
470 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
472 (*rpc->crpc_fini) (rpc);
479 srpc_init_client_rpc (srpc_client_rpc_t *rpc, lnet_process_id_t peer,
480 int service, int nbulkiov, int bulklen,
481 void (*rpc_done)(srpc_client_rpc_t *),
482 void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
484 LASSERT (nbulkiov <= LNET_MAX_IOV);
486 memset(rpc, 0, offsetof(srpc_client_rpc_t,
487 crpc_bulk.bk_iovs[nbulkiov]));
489 CFS_INIT_LIST_HEAD(&rpc->crpc_list);
490 CFS_INIT_LIST_HEAD(&rpc->crpc_privl);
491 swi_init_workitem(&rpc->crpc_wi, rpc, srpc_send_rpc);
492 spin_lock_init(&rpc->crpc_lock);
493 atomic_set(&rpc->crpc_refcount, 1); /* 1 ref for caller */
495 rpc->crpc_dest = peer;
496 rpc->crpc_priv = priv;
497 rpc->crpc_service = service;
498 rpc->crpc_bulk.bk_len = bulklen;
499 rpc->crpc_bulk.bk_niov = nbulkiov;
500 rpc->crpc_done = rpc_done;
501 rpc->crpc_fini = rpc_fini;
504 rpc->crpc_bulk.bk_mdh = LNET_INVALID_HANDLE;
506 /* no event is expected at this point */
507 rpc->crpc_bulkev.ev_fired =
508 rpc->crpc_reqstev.ev_fired =
509 rpc->crpc_replyev.ev_fired = 1;
511 rpc->crpc_reqstmsg.msg_magic = SRPC_MSG_MAGIC;
512 rpc->crpc_reqstmsg.msg_version = SRPC_MSG_VERSION;
513 rpc->crpc_reqstmsg.msg_type = srpc_service2request(service);
517 static inline const char *
518 swi_state2str (int state)
520 #define STATE2STR(x) case x: return #x
524 STATE2STR(SWI_STATE_NEWBORN);
525 STATE2STR(SWI_STATE_REPLY_SUBMITTED);
526 STATE2STR(SWI_STATE_REPLY_SENT);
527 STATE2STR(SWI_STATE_REQUEST_SUBMITTED);
528 STATE2STR(SWI_STATE_REQUEST_SENT);
529 STATE2STR(SWI_STATE_REPLY_RECEIVED);
530 STATE2STR(SWI_STATE_BULK_STARTED);
531 STATE2STR(SWI_STATE_BULK_ERRORED);
532 STATE2STR(SWI_STATE_DONE);
537 #define UNUSED(x) ( (void)(x) )
541 int stt_poll_interval(void);
542 int sfw_session_removed(void);
544 int stt_check_events(void);
545 int swi_check_events(void);
546 int srpc_check_event(int timeout);
548 int lnet_selftest_init(void);
549 void lnet_selftest_fini(void);
550 int selftest_wait_events(void);
554 #define selftest_wait_events() cfs_pause(cfs_time_seconds(1))
558 #define lst_wait_until(cond, lock, fmt, a...) \
563 CDEBUG(((__I & (-__I)) == __I) ? D_WARNING : \
566 spin_unlock(&(lock)); \
568 selftest_wait_events(); \
570 spin_lock(&(lock)); \
575 srpc_wait_service_shutdown (srpc_service_t *sv)
579 spin_lock(&sv->sv_lock);
580 LASSERT (sv->sv_shuttingdown);
581 spin_unlock(&sv->sv_lock);
583 while (srpc_finish_service(sv) == 0) {
585 CDEBUG (((i & -i) == i) ? D_WARNING : D_NET,
586 "Waiting for %s service to shutdown...\n",
588 selftest_wait_events();
592 #endif /* __SELFTEST_SELFTEST_H__ */