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__
14 #include <liblustre.h> /* userland spinlock_t and atomic_t */
17 #include <libcfs/kp30.h>
18 #include <libcfs/libcfs.h>
19 #include <lnet/lnet.h>
20 #include <lnet/lib-lnet.h>
21 #include <lnet/lib-types.h>
22 #include <lnet/lnetst.h>
27 #ifndef MADE_WITHOUT_COMPROMISE
28 #define MADE_WITHOUT_COMPROMISE
32 #define SWI_STATE_NEWBORN 0
33 #define SWI_STATE_REPLY_SUBMITTED 1
34 #define SWI_STATE_REPLY_SENT 2
35 #define SWI_STATE_REQUEST_SUBMITTED 3
36 #define SWI_STATE_REQUEST_SENT 4
37 #define SWI_STATE_REPLY_RECEIVED 5
38 #define SWI_STATE_BULK_STARTED 6
39 #define SWI_STATE_DONE 10
45 struct sfw_test_instance;
48 * A workitems is deferred work with these semantics:
49 * - a workitem always runs in thread context.
50 * - a workitem can be concurrent with other workitems but is strictly
51 * serialized with respect to itself.
52 * - no CPU affinity, a workitem does not necessarily run on the same CPU
53 * that schedules it. However, this might change in the future.
54 * - if a workitem is scheduled again before it has a chance to run, it
56 * - if a workitem is scheduled while it runs, it runs again after it
57 * completes; this ensures that events occurring while other events are
58 * being processed receive due attention. This behavior also allows a
59 * workitem to reschedule itself.
62 * - a workitem can sleep but it should be aware of how that sleep might
64 * - a workitem runs inside a kernel thread so there's no user space to access.
65 * - do not use a workitem if the scheduling latency can't be tolerated.
67 * When wi_action returns non-zero, it means the workitem has either been
68 * freed or reused and workitem scheduler won't touch it any more.
70 typedef int (*swi_action_t) (struct swi_workitem *);
71 typedef struct swi_workitem {
72 struct list_head wi_list; /* chain on runq */
74 swi_action_t wi_action;
76 unsigned int wi_running:1;
77 unsigned int wi_scheduled:1;
81 swi_init_workitem (swi_workitem_t *wi, void *data, swi_action_t action)
83 CFS_INIT_LIST_HEAD(&wi->wi_list);
88 wi->wi_action = action;
89 wi->wi_state = SWI_STATE_NEWBORN;
92 #define SWI_RESCHED 128 /* # workitem scheduler loops before reschedule */
94 /* services below SRPC_FRAMEWORK_SERVICE_MAX_ID are framework
95 * services, e.g. create/modify session.
97 #define SRPC_SERVICE_DEBUG 0
98 #define SRPC_SERVICE_MAKE_SESSION 1
99 #define SRPC_SERVICE_REMOVE_SESSION 2
100 #define SRPC_SERVICE_BATCH 3
101 #define SRPC_SERVICE_TEST 4
102 #define SRPC_SERVICE_QUERY_STAT 5
103 #define SRPC_SERVICE_JOIN 6
104 #define SRPC_FRAMEWORK_SERVICE_MAX_ID 10
105 /* other services start from SRPC_FRAMEWORK_SERVICE_MAX_ID+1 */
106 #define SRPC_SERVICE_BRW 11
107 #define SRPC_SERVICE_PING 12
108 #define SRPC_SERVICE_MAX_ID 12
110 #define SRPC_REQUEST_PORTAL 50
111 /* a lazy portal for framework RPC requests */
112 #define SRPC_FRAMEWORK_REQUEST_PORTAL 51
113 /* all reply/bulk RDMAs go to this portal */
114 #define SRPC_RDMA_PORTAL 52
116 static inline srpc_msg_type_t
117 srpc_service2request (int service)
122 case SRPC_SERVICE_DEBUG:
123 return SRPC_MSG_DEBUG_REQST;
125 case SRPC_SERVICE_MAKE_SESSION:
126 return SRPC_MSG_MKSN_REQST;
128 case SRPC_SERVICE_REMOVE_SESSION:
129 return SRPC_MSG_RMSN_REQST;
131 case SRPC_SERVICE_BATCH:
132 return SRPC_MSG_BATCH_REQST;
134 case SRPC_SERVICE_TEST:
135 return SRPC_MSG_TEST_REQST;
137 case SRPC_SERVICE_QUERY_STAT:
138 return SRPC_MSG_STAT_REQST;
140 case SRPC_SERVICE_BRW:
141 return SRPC_MSG_BRW_REQST;
143 case SRPC_SERVICE_PING:
144 return SRPC_MSG_PING_REQST;
146 case SRPC_SERVICE_JOIN:
147 return SRPC_MSG_JOIN_REQST;
151 static inline srpc_msg_type_t
152 srpc_service2reply (int service)
154 return srpc_service2request(service) + 1;
158 SRPC_BULK_REQ_RCVD = 0, /* passive bulk request(PUT sink/GET source) received */
159 SRPC_BULK_PUT_SENT = 1, /* active bulk PUT sent (source) */
160 SRPC_BULK_GET_RPLD = 2, /* active bulk GET replied (sink) */
161 SRPC_REPLY_RCVD = 3, /* incoming reply received */
162 SRPC_REPLY_SENT = 4, /* outgoing reply sent */
163 SRPC_REQUEST_RCVD = 5, /* incoming request received */
164 SRPC_REQUEST_SENT = 6, /* outgoing request sent */
169 srpc_event_type_t ev_type; /* what's up */
170 lnet_event_kind_t ev_lnet; /* LNet event type */
171 int ev_fired; /* LNet event fired? */
172 int ev_status; /* LNet event status */
173 void *ev_data; /* owning server/client RPC */
177 int bk_len; /* len of bulk data */
178 lnet_handle_md_t bk_mdh;
179 int bk_sink; /* sink/source */
180 int bk_niov; /* # iov in bk_iovs */
182 lnet_kiov_t bk_iovs[0];
184 cfs_page_t **bk_pages;
185 lnet_md_iovec_t bk_iovs[0];
187 } srpc_bulk_t; /* bulk descriptor */
189 typedef struct srpc_peer {
190 struct list_head stp_list; /* chain on peer hash */
191 struct list_head stp_rpcq; /* q of non-control RPCs */
192 struct list_head stp_ctl_rpcq; /* q of control RPCs */
193 spinlock_t stp_lock; /* serialize */
195 int stp_credits; /* available credits */
198 /* message buffer descriptor */
200 struct list_head buf_list; /* chain on srpc_service::*_msgq */
202 lnet_handle_md_t buf_mdh;
204 lnet_process_id_t buf_peer;
207 /* server-side state of a RPC */
208 typedef struct srpc_server_rpc {
209 struct list_head srpc_list; /* chain on srpc_service::*_rpcq */
210 struct srpc_service *srpc_service;
211 swi_workitem_t srpc_wi;
212 srpc_event_t srpc_ev; /* bulk/reply event */
213 lnet_nid_t srpc_self;
214 lnet_process_id_t srpc_peer;
215 srpc_msg_t srpc_replymsg;
216 lnet_handle_md_t srpc_replymdh;
217 srpc_buffer_t *srpc_reqstbuf;
218 srpc_bulk_t *srpc_bulk;
221 void (*srpc_done)(struct srpc_server_rpc *);
224 /* client-side state of a RPC */
225 typedef struct srpc_client_rpc {
226 struct list_head crpc_list; /* chain on user's lists */
227 struct list_head crpc_privl; /* chain on srpc_peer_t::*rpcq */
228 spinlock_t crpc_lock; /* serialize */
230 atomic_t crpc_refcount;
231 int crpc_timeout; /* # seconds to wait for reply */
232 stt_timer_t crpc_timer;
233 swi_workitem_t crpc_wi;
234 lnet_process_id_t crpc_dest;
235 srpc_peer_t *crpc_peer;
237 void (*crpc_done)(struct srpc_client_rpc *);
238 void (*crpc_fini)(struct srpc_client_rpc *);
239 int crpc_status; /* completion status */
240 void *crpc_priv; /* caller data */
243 unsigned int crpc_aborted:1; /* being given up */
244 unsigned int crpc_closed:1; /* completed */
247 srpc_event_t crpc_bulkev; /* bulk event */
248 srpc_event_t crpc_reqstev; /* request event */
249 srpc_event_t crpc_replyev; /* reply event */
251 /* bulk, request(reqst), and reply exchanged on wire */
252 srpc_msg_t crpc_reqstmsg;
253 srpc_msg_t crpc_replymsg;
254 lnet_handle_md_t crpc_reqstmdh;
255 lnet_handle_md_t crpc_replymdh;
256 srpc_bulk_t crpc_bulk;
259 #define srpc_client_rpc_size(rpc) \
260 offsetof(srpc_client_rpc_t, crpc_bulk.bk_iovs[(rpc)->crpc_bulk.bk_niov])
262 #define srpc_client_rpc_addref(rpc) \
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 atomic_inc(&(rpc)->crpc_refcount); \
271 #define srpc_client_rpc_decref(rpc) \
273 CDEBUG(D_NET, "RPC[%p] -> %s (%d)--\n", \
274 (rpc), libcfs_id2str((rpc)->crpc_dest), \
275 atomic_read(&(rpc)->crpc_refcount)); \
276 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
277 if (atomic_dec_and_test(&(rpc)->crpc_refcount)) \
278 srpc_destroy_client_rpc(rpc); \
281 #define srpc_event_pending(rpc) ((rpc)->crpc_bulkev.ev_fired == 0 || \
282 (rpc)->crpc_reqstev.ev_fired == 0 || \
283 (rpc)->crpc_replyev.ev_fired == 0)
285 typedef struct srpc_service {
286 int sv_id; /* service id */
287 const char *sv_name; /* human readable name */
288 int sv_nprune; /* # posted RPC to be pruned */
289 int sv_concur; /* max # concurrent RPCs */
293 srpc_event_t sv_ev; /* LNet event */
294 int sv_nposted_msg; /* # posted message buffers */
295 struct list_head sv_free_rpcq; /* free RPC descriptors */
296 struct list_head sv_active_rpcq; /* in-flight RPCs */
297 struct list_head sv_posted_msgq; /* posted message buffers */
298 struct list_head sv_blocked_msgq; /* blocked for RPC descriptor */
300 /* Service callbacks:
301 * - sv_handler: process incoming RPC request
302 * - sv_bulk_ready: notify bulk data
304 int (*sv_handler) (srpc_server_rpc_t *);
305 int (*sv_bulk_ready) (srpc_server_rpc_t *, int);
308 #define SFW_POST_BUFFERS 8
309 #define SFW_SERVICE_CONCURRENCY (SFW_POST_BUFFERS/2)
312 struct list_head sn_list; /* chain on fw_zombie_sessions */
313 lst_sid_t sn_id; /* unique identifier */
314 unsigned int sn_timeout; /* # seconds' inactivity to expire */
316 stt_timer_t sn_timer;
317 struct list_head sn_batches; /* list of batches */
318 char sn_name[LST_NAME_SIZE];
319 atomic_t sn_brw_errors;
320 atomic_t sn_ping_errors;
323 #define sfw_sid_equal(sid0, sid1) ((sid0).ses_nid == (sid1).ses_nid && \
324 (sid0).ses_stamp == (sid1).ses_stamp)
327 struct list_head bat_list; /* chain on sn_batches */
328 lst_bid_t bat_id; /* batch id */
329 int bat_error; /* error code of batch */
330 sfw_session_t *bat_session; /* batch's session */
331 atomic_t bat_nactive; /* # of active tests */
332 struct list_head bat_tests; /* test instances */
336 int (*tso_init)(struct sfw_test_instance *tsi); /* intialize test client */
337 void (*tso_fini)(struct sfw_test_instance *tsi); /* finalize test client */
338 int (*tso_prep_rpc)(struct sfw_test_unit *tsu,
339 lnet_process_id_t dest,
340 srpc_client_rpc_t **rpc); /* prep a tests rpc */
341 void (*tso_done_rpc)(struct sfw_test_unit *tsu,
342 srpc_client_rpc_t *rpc); /* done a test rpc */
343 } sfw_test_client_ops_t;
345 typedef struct sfw_test_instance {
346 struct list_head tsi_list; /* chain on batch */
347 int tsi_service; /* test type */
348 sfw_batch_t *tsi_batch; /* batch */
349 sfw_test_client_ops_t *tsi_ops; /* test client operations */
351 /* public parameter for all test units */
352 int tsi_is_client:1; /* is test client */
353 int tsi_stoptsu_onerr:1; /* stop tsu on error */
354 int tsi_concur; /* concurrency */
355 int tsi_loop; /* loop count */
357 /* status of test instance */
358 spinlock_t tsi_lock; /* serialize */
359 int tsi_stopping:1; /* test is stopping */
360 atomic_t tsi_nactive; /* # of active test unit */
361 struct list_head tsi_units; /* test units */
362 struct list_head tsi_free_rpcs; /* free rpcs */
363 struct list_head tsi_active_rpcs; /* active rpcs */
366 test_bulk_req_t bulk; /* bulk parameter */
367 test_ping_req_t ping; /* ping parameter */
369 } sfw_test_instance_t;
371 /* XXX: trailing (CFS_PAGE_SIZE % sizeof(lnet_process_id_t)) bytes at
372 * the end of pages are not used */
373 #define SFW_MAX_CONCUR LST_MAX_CONCUR
374 #define SFW_ID_PER_PAGE (CFS_PAGE_SIZE / sizeof(lnet_process_id_t))
375 #define SFW_MAX_NDESTS (LNET_MAX_IOV * SFW_ID_PER_PAGE)
376 #define sfw_id_pages(n) (((n) + SFW_ID_PER_PAGE - 1) / SFW_ID_PER_PAGE)
378 typedef struct sfw_test_unit {
379 struct list_head tsu_list; /* chain on lst_test_instance */
380 lnet_process_id_t tsu_dest; /* id of dest node */
381 int tsu_loop; /* loop count of the test */
382 sfw_test_instance_t *tsu_instance; /* pointer to test instance */
383 void *tsu_private; /* private data */
384 swi_workitem_t tsu_worker; /* workitem of the test unit */
388 struct list_head tsc_list; /* chain on fw_tests */
389 srpc_service_t *tsc_srv_service; /* test service */
390 sfw_test_client_ops_t *tsc_cli_ops; /* ops of test client */
395 sfw_create_rpc(lnet_process_id_t peer, int service, int nbulkiov, int bulklen,
396 void (*done) (srpc_client_rpc_t *), void *priv);
397 int sfw_create_test_rpc(sfw_test_unit_t *tsu, lnet_process_id_t peer,
398 int nblk, int blklen, srpc_client_rpc_t **rpc);
399 void sfw_abort_rpc(srpc_client_rpc_t *rpc);
400 void sfw_post_rpc(srpc_client_rpc_t *rpc);
401 void sfw_client_rpc_done(srpc_client_rpc_t *rpc);
402 void sfw_unpack_message(srpc_msg_t *msg);
403 void sfw_free_pages(srpc_server_rpc_t *rpc);
404 void sfw_add_bulk_page(srpc_bulk_t *bk, cfs_page_t *pg, int i);
405 int sfw_alloc_pages(srpc_server_rpc_t *rpc, int npages, int sink);
408 srpc_create_client_rpc(lnet_process_id_t peer, int service,
409 int nbulkiov, int bulklen,
410 void (*rpc_done)(srpc_client_rpc_t *),
411 void (*rpc_fini)(srpc_client_rpc_t *), void *priv);
412 void srpc_post_rpc(srpc_client_rpc_t *rpc);
413 void srpc_abort_rpc(srpc_client_rpc_t *rpc, int why);
414 void srpc_free_bulk(srpc_bulk_t *bk);
415 srpc_bulk_t *srpc_alloc_bulk(int npages, int sink);
416 int srpc_send_rpc(swi_workitem_t *wi);
417 int srpc_send_reply(srpc_server_rpc_t *rpc);
418 int srpc_add_service(srpc_service_t *sv);
419 int srpc_remove_service(srpc_service_t *sv);
420 void srpc_shutdown_service(srpc_service_t *sv);
421 int srpc_finish_service(srpc_service_t *sv);
422 int srpc_service_add_buffers(srpc_service_t *sv, int nbuffer);
423 void srpc_service_remove_buffers(srpc_service_t *sv, int nbuffer);
424 void srpc_get_counters(srpc_counters_t *cnt);
425 void srpc_set_counters(const srpc_counters_t *cnt);
427 void swi_kill_workitem(swi_workitem_t *wi);
428 void swi_schedule_workitem(swi_workitem_t *wi);
429 void swi_schedule_serial_workitem(swi_workitem_t *wi);
430 int swi_startup(void);
431 int sfw_startup(void);
432 int srpc_startup(void);
433 void swi_shutdown(void);
434 void sfw_shutdown(void);
435 void srpc_shutdown(void);
438 srpc_destroy_client_rpc (srpc_client_rpc_t *rpc)
440 LASSERT (rpc != NULL);
441 LASSERT (!srpc_event_pending(rpc));
442 LASSERT (list_empty(&rpc->crpc_privl));
443 LASSERT (atomic_read(&rpc->crpc_refcount) == 0);
445 LASSERT (rpc->crpc_bulk.bk_pages == NULL);
448 if (rpc->crpc_fini == NULL) {
449 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
451 (*rpc->crpc_fini) (rpc);
458 srpc_init_client_rpc (srpc_client_rpc_t *rpc, lnet_process_id_t peer,
459 int service, int nbulkiov, int bulklen,
460 void (*rpc_done)(srpc_client_rpc_t *),
461 void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
463 LASSERT (nbulkiov <= LNET_MAX_IOV);
465 memset(rpc, 0, offsetof(srpc_client_rpc_t,
466 crpc_bulk.bk_iovs[nbulkiov]));
468 CFS_INIT_LIST_HEAD(&rpc->crpc_list);
469 CFS_INIT_LIST_HEAD(&rpc->crpc_privl);
470 swi_init_workitem(&rpc->crpc_wi, rpc, srpc_send_rpc);
471 spin_lock_init(&rpc->crpc_lock);
472 atomic_set(&rpc->crpc_refcount, 1); /* 1 ref for caller */
474 rpc->crpc_dest = peer;
475 rpc->crpc_priv = priv;
476 rpc->crpc_service = service;
477 rpc->crpc_bulk.bk_len = bulklen;
478 rpc->crpc_bulk.bk_niov = nbulkiov;
479 rpc->crpc_done = rpc_done;
480 rpc->crpc_fini = rpc_fini;
483 rpc->crpc_bulk.bk_mdh = LNET_INVALID_HANDLE;
485 /* no event is expected at this point */
486 rpc->crpc_bulkev.ev_fired =
487 rpc->crpc_reqstev.ev_fired =
488 rpc->crpc_replyev.ev_fired = 1;
490 rpc->crpc_reqstmsg.msg_magic = SRPC_MSG_MAGIC;
491 rpc->crpc_reqstmsg.msg_version = SRPC_MSG_VERSION;
492 rpc->crpc_reqstmsg.msg_type = srpc_service2request(service);
496 static inline const char *
497 swi_state2str (int state)
499 #define STATE2STR(x) case x: return #x
503 STATE2STR(SWI_STATE_NEWBORN);
504 STATE2STR(SWI_STATE_REPLY_SUBMITTED);
505 STATE2STR(SWI_STATE_REPLY_SENT);
506 STATE2STR(SWI_STATE_REQUEST_SUBMITTED);
507 STATE2STR(SWI_STATE_REQUEST_SENT);
508 STATE2STR(SWI_STATE_REPLY_RECEIVED);
509 STATE2STR(SWI_STATE_BULK_STARTED);
510 STATE2STR(SWI_STATE_DONE);
515 #define UNUSED(x) ( (void)(x) )
519 int stt_poll_interval(void);
520 int sfw_session_removed(void);
522 int stt_check_events(void);
523 int swi_check_events(void);
524 int srpc_check_event(int timeout);
526 int lnet_selftest_init(void);
527 void lnet_selftest_fini(void);
528 int selftest_wait_events(void);
532 #define selftest_wait_events() cfs_pause(cfs_time_seconds(1))
536 #define lst_wait_until(cond, lock, fmt, a...) \
541 CDEBUG(((__I & (-__I)) == __I) ? D_WARNING : \
544 spin_unlock(&(lock)); \
546 selftest_wait_events(); \
548 spin_lock(&(lock)); \
553 srpc_wait_service_shutdown (srpc_service_t *sv)
557 spin_lock(&sv->sv_lock);
558 LASSERT (sv->sv_shuttingdown);
559 spin_unlock(&sv->sv_lock);
561 while (srpc_finish_service(sv) == 0) {
563 CDEBUG (((i & -i) == i) ? D_WARNING : D_NET,
564 "Waiting for %s service to shutdown...\n",
566 selftest_wait_events();
570 #endif /* __SELFTEST_SELFTEST_H__ */