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)
39 #include <libcfs/kp30.h>
40 #include <libcfs/libcfs.h>
41 #include <lnet/lnet.h>
42 #include <lnet/lib-lnet.h>
43 #include <lnet/lib-types.h>
44 #include <lnet/lnetst.h>
49 #ifndef MADE_WITHOUT_COMPROMISE
50 #define MADE_WITHOUT_COMPROMISE
54 #define SWI_STATE_NEWBORN 0
55 #define SWI_STATE_REPLY_SUBMITTED 1
56 #define SWI_STATE_REPLY_SENT 2
57 #define SWI_STATE_REQUEST_SUBMITTED 3
58 #define SWI_STATE_REQUEST_SENT 4
59 #define SWI_STATE_REPLY_RECEIVED 5
60 #define SWI_STATE_BULK_STARTED 6
61 #define SWI_STATE_DONE 10
67 struct sfw_test_instance;
70 * A workitems is deferred work with these semantics:
71 * - a workitem always runs in thread context.
72 * - a workitem can be concurrent with other workitems but is strictly
73 * serialized with respect to itself.
74 * - no CPU affinity, a workitem does not necessarily run on the same CPU
75 * that schedules it. However, this might change in the future.
76 * - if a workitem is scheduled again before it has a chance to run, it
78 * - if a workitem is scheduled while it runs, it runs again after it
79 * completes; this ensures that events occurring while other events are
80 * being processed receive due attention. This behavior also allows a
81 * workitem to reschedule itself.
84 * - a workitem can sleep but it should be aware of how that sleep might
86 * - a workitem runs inside a kernel thread so there's no user space to access.
87 * - do not use a workitem if the scheduling latency can't be tolerated.
89 * When wi_action returns non-zero, it means the workitem has either been
90 * freed or reused and workitem scheduler won't touch it any more.
92 typedef int (*swi_action_t) (struct swi_workitem *);
93 typedef struct swi_workitem {
94 struct list_head wi_list; /* chain on runq */
96 swi_action_t wi_action;
98 unsigned int wi_running:1;
99 unsigned int wi_scheduled:1;
103 swi_init_workitem (swi_workitem_t *wi, void *data, swi_action_t action)
105 CFS_INIT_LIST_HEAD(&wi->wi_list);
108 wi->wi_scheduled = 0;
110 wi->wi_action = action;
111 wi->wi_state = SWI_STATE_NEWBORN;
114 #define SWI_RESCHED 128 /* # workitem scheduler loops before reschedule */
116 /* services below SRPC_FRAMEWORK_SERVICE_MAX_ID are framework
117 * services, e.g. create/modify session.
119 #define SRPC_SERVICE_DEBUG 0
120 #define SRPC_SERVICE_MAKE_SESSION 1
121 #define SRPC_SERVICE_REMOVE_SESSION 2
122 #define SRPC_SERVICE_BATCH 3
123 #define SRPC_SERVICE_TEST 4
124 #define SRPC_SERVICE_QUERY_STAT 5
125 #define SRPC_SERVICE_JOIN 6
126 #define SRPC_FRAMEWORK_SERVICE_MAX_ID 10
127 /* other services start from SRPC_FRAMEWORK_SERVICE_MAX_ID+1 */
128 #define SRPC_SERVICE_BRW 11
129 #define SRPC_SERVICE_PING 12
130 #define SRPC_SERVICE_MAX_ID 12
132 #define SRPC_REQUEST_PORTAL 50
133 /* a lazy portal for framework RPC requests */
134 #define SRPC_FRAMEWORK_REQUEST_PORTAL 51
135 /* all reply/bulk RDMAs go to this portal */
136 #define SRPC_RDMA_PORTAL 52
138 static inline srpc_msg_type_t
139 srpc_service2request (int service)
144 case SRPC_SERVICE_DEBUG:
145 return SRPC_MSG_DEBUG_REQST;
147 case SRPC_SERVICE_MAKE_SESSION:
148 return SRPC_MSG_MKSN_REQST;
150 case SRPC_SERVICE_REMOVE_SESSION:
151 return SRPC_MSG_RMSN_REQST;
153 case SRPC_SERVICE_BATCH:
154 return SRPC_MSG_BATCH_REQST;
156 case SRPC_SERVICE_TEST:
157 return SRPC_MSG_TEST_REQST;
159 case SRPC_SERVICE_QUERY_STAT:
160 return SRPC_MSG_STAT_REQST;
162 case SRPC_SERVICE_BRW:
163 return SRPC_MSG_BRW_REQST;
165 case SRPC_SERVICE_PING:
166 return SRPC_MSG_PING_REQST;
168 case SRPC_SERVICE_JOIN:
169 return SRPC_MSG_JOIN_REQST;
173 static inline srpc_msg_type_t
174 srpc_service2reply (int service)
176 return srpc_service2request(service) + 1;
180 SRPC_BULK_REQ_RCVD = 0, /* passive bulk request(PUT sink/GET source) received */
181 SRPC_BULK_PUT_SENT = 1, /* active bulk PUT sent (source) */
182 SRPC_BULK_GET_RPLD = 2, /* active bulk GET replied (sink) */
183 SRPC_REPLY_RCVD = 3, /* incoming reply received */
184 SRPC_REPLY_SENT = 4, /* outgoing reply sent */
185 SRPC_REQUEST_RCVD = 5, /* incoming request received */
186 SRPC_REQUEST_SENT = 6, /* outgoing request sent */
191 srpc_event_type_t ev_type; /* what's up */
192 lnet_event_kind_t ev_lnet; /* LNet event type */
193 int ev_fired; /* LNet event fired? */
194 int ev_status; /* LNet event status */
195 void *ev_data; /* owning server/client RPC */
199 int bk_len; /* len of bulk data */
200 lnet_handle_md_t bk_mdh;
201 int bk_sink; /* sink/source */
202 int bk_niov; /* # iov in bk_iovs */
204 lnet_kiov_t bk_iovs[0];
206 cfs_page_t **bk_pages;
207 lnet_md_iovec_t bk_iovs[0];
209 } srpc_bulk_t; /* bulk descriptor */
211 typedef struct srpc_peer {
212 struct list_head stp_list; /* chain on peer hash */
213 struct list_head stp_rpcq; /* q of non-control RPCs */
214 struct list_head stp_ctl_rpcq; /* q of control RPCs */
215 spinlock_t stp_lock; /* serialize */
217 int stp_credits; /* available credits */
220 /* message buffer descriptor */
222 struct list_head buf_list; /* chain on srpc_service::*_msgq */
224 lnet_handle_md_t buf_mdh;
226 lnet_process_id_t buf_peer;
229 /* server-side state of a RPC */
230 typedef struct srpc_server_rpc {
231 struct list_head srpc_list; /* chain on srpc_service::*_rpcq */
232 struct srpc_service *srpc_service;
233 swi_workitem_t srpc_wi;
234 srpc_event_t srpc_ev; /* bulk/reply event */
235 lnet_nid_t srpc_self;
236 lnet_process_id_t srpc_peer;
237 srpc_msg_t srpc_replymsg;
238 lnet_handle_md_t srpc_replymdh;
239 srpc_buffer_t *srpc_reqstbuf;
240 srpc_bulk_t *srpc_bulk;
243 void (*srpc_done)(struct srpc_server_rpc *);
246 /* client-side state of a RPC */
247 typedef struct srpc_client_rpc {
248 struct list_head crpc_list; /* chain on user's lists */
249 struct list_head crpc_privl; /* chain on srpc_peer_t::*rpcq */
250 spinlock_t crpc_lock; /* serialize */
252 atomic_t crpc_refcount;
253 int crpc_timeout; /* # seconds to wait for reply */
254 stt_timer_t crpc_timer;
255 swi_workitem_t crpc_wi;
256 lnet_process_id_t crpc_dest;
257 srpc_peer_t *crpc_peer;
259 void (*crpc_done)(struct srpc_client_rpc *);
260 void (*crpc_fini)(struct srpc_client_rpc *);
261 int crpc_status; /* completion status */
262 void *crpc_priv; /* caller data */
265 unsigned int crpc_aborted:1; /* being given up */
266 unsigned int crpc_closed:1; /* completed */
269 srpc_event_t crpc_bulkev; /* bulk event */
270 srpc_event_t crpc_reqstev; /* request event */
271 srpc_event_t crpc_replyev; /* reply event */
273 /* bulk, request(reqst), and reply exchanged on wire */
274 srpc_msg_t crpc_reqstmsg;
275 srpc_msg_t crpc_replymsg;
276 lnet_handle_md_t crpc_reqstmdh;
277 lnet_handle_md_t crpc_replymdh;
278 srpc_bulk_t crpc_bulk;
281 #define srpc_client_rpc_size(rpc) \
282 offsetof(srpc_client_rpc_t, crpc_bulk.bk_iovs[(rpc)->crpc_bulk.bk_niov])
284 #define srpc_client_rpc_addref(rpc) \
286 CDEBUG(D_NET, "RPC[%p] -> %s (%d)++\n", \
287 (rpc), libcfs_id2str((rpc)->crpc_dest), \
288 atomic_read(&(rpc)->crpc_refcount)); \
289 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
290 atomic_inc(&(rpc)->crpc_refcount); \
293 #define srpc_client_rpc_decref(rpc) \
295 CDEBUG(D_NET, "RPC[%p] -> %s (%d)--\n", \
296 (rpc), libcfs_id2str((rpc)->crpc_dest), \
297 atomic_read(&(rpc)->crpc_refcount)); \
298 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
299 if (atomic_dec_and_test(&(rpc)->crpc_refcount)) \
300 srpc_destroy_client_rpc(rpc); \
303 #define srpc_event_pending(rpc) ((rpc)->crpc_bulkev.ev_fired == 0 || \
304 (rpc)->crpc_reqstev.ev_fired == 0 || \
305 (rpc)->crpc_replyev.ev_fired == 0)
307 typedef struct srpc_service {
308 int sv_id; /* service id */
309 const char *sv_name; /* human readable name */
310 int sv_nprune; /* # posted RPC to be pruned */
311 int sv_concur; /* max # concurrent RPCs */
315 srpc_event_t sv_ev; /* LNet event */
316 int sv_nposted_msg; /* # posted message buffers */
317 struct list_head sv_free_rpcq; /* free RPC descriptors */
318 struct list_head sv_active_rpcq; /* in-flight RPCs */
319 struct list_head sv_posted_msgq; /* posted message buffers */
320 struct list_head sv_blocked_msgq; /* blocked for RPC descriptor */
322 /* Service callbacks:
323 * - sv_handler: process incoming RPC request
324 * - sv_bulk_ready: notify bulk data
326 int (*sv_handler) (srpc_server_rpc_t *);
327 int (*sv_bulk_ready) (srpc_server_rpc_t *, int);
330 #define SFW_POST_BUFFERS 8
331 #define SFW_SERVICE_CONCURRENCY (SFW_POST_BUFFERS/2)
334 struct list_head sn_list; /* chain on fw_zombie_sessions */
335 lst_sid_t sn_id; /* unique identifier */
336 unsigned int sn_timeout; /* # seconds' inactivity to expire */
338 stt_timer_t sn_timer;
339 struct list_head sn_batches; /* list of batches */
340 char sn_name[LST_NAME_SIZE];
341 atomic_t sn_brw_errors;
342 atomic_t sn_ping_errors;
345 #define sfw_sid_equal(sid0, sid1) ((sid0).ses_nid == (sid1).ses_nid && \
346 (sid0).ses_stamp == (sid1).ses_stamp)
349 struct list_head bat_list; /* chain on sn_batches */
350 lst_bid_t bat_id; /* batch id */
351 int bat_error; /* error code of batch */
352 sfw_session_t *bat_session; /* batch's session */
353 atomic_t bat_nactive; /* # of active tests */
354 struct list_head bat_tests; /* test instances */
358 int (*tso_init)(struct sfw_test_instance *tsi); /* intialize test client */
359 void (*tso_fini)(struct sfw_test_instance *tsi); /* finalize test client */
360 int (*tso_prep_rpc)(struct sfw_test_unit *tsu,
361 lnet_process_id_t dest,
362 srpc_client_rpc_t **rpc); /* prep a tests rpc */
363 void (*tso_done_rpc)(struct sfw_test_unit *tsu,
364 srpc_client_rpc_t *rpc); /* done a test rpc */
365 } sfw_test_client_ops_t;
367 typedef struct sfw_test_instance {
368 struct list_head tsi_list; /* chain on batch */
369 int tsi_service; /* test type */
370 sfw_batch_t *tsi_batch; /* batch */
371 sfw_test_client_ops_t *tsi_ops; /* test client operations */
373 /* public parameter for all test units */
374 int tsi_is_client:1; /* is test client */
375 int tsi_stoptsu_onerr:1; /* stop tsu on error */
376 int tsi_concur; /* concurrency */
377 int tsi_loop; /* loop count */
379 /* status of test instance */
380 spinlock_t tsi_lock; /* serialize */
381 int tsi_stopping:1; /* test is stopping */
382 atomic_t tsi_nactive; /* # of active test 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 */
388 test_bulk_req_t bulk; /* bulk parameter */
389 test_ping_req_t ping; /* ping parameter */
391 } sfw_test_instance_t;
393 /* XXX: trailing (CFS_PAGE_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 (CFS_PAGE_SIZE / sizeof(lnet_process_id_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)
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 */
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 */
417 sfw_create_rpc(lnet_process_id_t peer, int service, int nbulkiov, int bulklen,
418 void (*done) (srpc_client_rpc_t *), void *priv);
419 int sfw_create_test_rpc(sfw_test_unit_t *tsu, lnet_process_id_t peer,
420 int nblk, int blklen, srpc_client_rpc_t **rpc);
421 void sfw_abort_rpc(srpc_client_rpc_t *rpc);
422 void sfw_post_rpc(srpc_client_rpc_t *rpc);
423 void sfw_client_rpc_done(srpc_client_rpc_t *rpc);
424 void sfw_unpack_message(srpc_msg_t *msg);
425 void sfw_free_pages(srpc_server_rpc_t *rpc);
426 void sfw_add_bulk_page(srpc_bulk_t *bk, cfs_page_t *pg, int i);
427 int sfw_alloc_pages(srpc_server_rpc_t *rpc, int npages, int sink);
430 srpc_create_client_rpc(lnet_process_id_t peer, int service,
431 int nbulkiov, int bulklen,
432 void (*rpc_done)(srpc_client_rpc_t *),
433 void (*rpc_fini)(srpc_client_rpc_t *), void *priv);
434 void srpc_post_rpc(srpc_client_rpc_t *rpc);
435 void srpc_abort_rpc(srpc_client_rpc_t *rpc, int why);
436 void srpc_free_bulk(srpc_bulk_t *bk);
437 srpc_bulk_t *srpc_alloc_bulk(int npages, int sink);
438 int srpc_send_rpc(swi_workitem_t *wi);
439 int srpc_send_reply(srpc_server_rpc_t *rpc);
440 int srpc_add_service(srpc_service_t *sv);
441 int srpc_remove_service(srpc_service_t *sv);
442 void srpc_shutdown_service(srpc_service_t *sv);
443 int srpc_finish_service(srpc_service_t *sv);
444 int srpc_service_add_buffers(srpc_service_t *sv, int nbuffer);
445 void srpc_service_remove_buffers(srpc_service_t *sv, int nbuffer);
446 void srpc_get_counters(srpc_counters_t *cnt);
447 void srpc_set_counters(const srpc_counters_t *cnt);
449 void swi_kill_workitem(swi_workitem_t *wi);
450 void swi_schedule_workitem(swi_workitem_t *wi);
451 void swi_schedule_serial_workitem(swi_workitem_t *wi);
452 int swi_startup(void);
453 int sfw_startup(void);
454 int srpc_startup(void);
455 void swi_shutdown(void);
456 void sfw_shutdown(void);
457 void srpc_shutdown(void);
460 srpc_destroy_client_rpc (srpc_client_rpc_t *rpc)
462 LASSERT (rpc != NULL);
463 LASSERT (!srpc_event_pending(rpc));
464 LASSERT (list_empty(&rpc->crpc_privl));
465 LASSERT (atomic_read(&rpc->crpc_refcount) == 0);
467 LASSERT (rpc->crpc_bulk.bk_pages == NULL);
470 if (rpc->crpc_fini == NULL) {
471 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
473 (*rpc->crpc_fini) (rpc);
480 srpc_init_client_rpc (srpc_client_rpc_t *rpc, lnet_process_id_t peer,
481 int service, int nbulkiov, int bulklen,
482 void (*rpc_done)(srpc_client_rpc_t *),
483 void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
485 LASSERT (nbulkiov <= LNET_MAX_IOV);
487 memset(rpc, 0, offsetof(srpc_client_rpc_t,
488 crpc_bulk.bk_iovs[nbulkiov]));
490 CFS_INIT_LIST_HEAD(&rpc->crpc_list);
491 CFS_INIT_LIST_HEAD(&rpc->crpc_privl);
492 swi_init_workitem(&rpc->crpc_wi, rpc, srpc_send_rpc);
493 spin_lock_init(&rpc->crpc_lock);
494 atomic_set(&rpc->crpc_refcount, 1); /* 1 ref for caller */
496 rpc->crpc_dest = peer;
497 rpc->crpc_priv = priv;
498 rpc->crpc_service = service;
499 rpc->crpc_bulk.bk_len = bulklen;
500 rpc->crpc_bulk.bk_niov = nbulkiov;
501 rpc->crpc_done = rpc_done;
502 rpc->crpc_fini = rpc_fini;
505 rpc->crpc_bulk.bk_mdh = LNET_INVALID_HANDLE;
507 /* no event is expected at this point */
508 rpc->crpc_bulkev.ev_fired =
509 rpc->crpc_reqstev.ev_fired =
510 rpc->crpc_replyev.ev_fired = 1;
512 rpc->crpc_reqstmsg.msg_magic = SRPC_MSG_MAGIC;
513 rpc->crpc_reqstmsg.msg_version = SRPC_MSG_VERSION;
514 rpc->crpc_reqstmsg.msg_type = srpc_service2request(service);
518 static inline const char *
519 swi_state2str (int state)
521 #define STATE2STR(x) case x: return #x
525 STATE2STR(SWI_STATE_NEWBORN);
526 STATE2STR(SWI_STATE_REPLY_SUBMITTED);
527 STATE2STR(SWI_STATE_REPLY_SENT);
528 STATE2STR(SWI_STATE_REQUEST_SUBMITTED);
529 STATE2STR(SWI_STATE_REQUEST_SENT);
530 STATE2STR(SWI_STATE_REPLY_RECEIVED);
531 STATE2STR(SWI_STATE_BULK_STARTED);
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__ */