1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
23 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24 * CA 95054 USA or visit www.sun.com if you need additional information or
30 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
31 * Use is subject to license terms.
34 * This file is part of Lustre, http://www.lustre.org/
35 * Lustre is a trademark of Sun Microsystems, Inc.
37 * lnet/selftest/selftest.h
39 * Author: Isaac Huang <isaac@clusterfs.com>
41 #ifndef __SELFTEST_SELFTEST_H__
42 #define __SELFTEST_SELFTEST_H__
48 /* XXX workaround XXX */
49 #ifdef HAVE_SYS_TYPES_H
50 #include <sys/types.h>
54 #include <libcfs/libcfs.h>
55 #include <lnet/lnet.h>
56 #include <lnet/lib-lnet.h>
57 #include <lnet/lib-types.h>
58 #include <lnet/lnetst.h>
63 #ifndef MADE_WITHOUT_COMPROMISE
64 #define MADE_WITHOUT_COMPROMISE
68 #define SWI_STATE_NEWBORN 0
69 #define SWI_STATE_REPLY_SUBMITTED 1
70 #define SWI_STATE_REPLY_SENT 2
71 #define SWI_STATE_REQUEST_SUBMITTED 3
72 #define SWI_STATE_REQUEST_SENT 4
73 #define SWI_STATE_REPLY_RECEIVED 5
74 #define SWI_STATE_BULK_STARTED 6
75 #define SWI_STATE_BULK_ERRORED 7
76 #define SWI_STATE_DONE 10
82 struct sfw_test_instance;
85 * A workitems is deferred work with these semantics:
86 * - a workitem always runs in thread context.
87 * - a workitem can be concurrent with other workitems but is strictly
88 * serialized with respect to itself.
89 * - no CPU affinity, a workitem does not necessarily run on the same CPU
90 * that schedules it. However, this might change in the future.
91 * - if a workitem is scheduled again before it has a chance to run, it
93 * - if a workitem is scheduled while it runs, it runs again after it
94 * completes; this ensures that events occurring while other events are
95 * being processed receive due attention. This behavior also allows a
96 * workitem to reschedule itself.
99 * - a workitem can sleep but it should be aware of how that sleep might
101 * - a workitem runs inside a kernel thread so there's no user space to access.
102 * - do not use a workitem if the scheduling latency can't be tolerated.
104 * When wi_action returns non-zero, it means the workitem has either been
105 * freed or reused and workitem scheduler won't touch it any more.
107 typedef int (*swi_action_t) (struct swi_workitem *);
108 typedef struct swi_workitem {
109 struct list_head wi_list; /* chain on runq */
111 swi_action_t wi_action;
113 unsigned int wi_running:1;
114 unsigned int wi_scheduled:1;
118 swi_init_workitem (swi_workitem_t *wi, void *data, swi_action_t action)
120 CFS_INIT_LIST_HEAD(&wi->wi_list);
123 wi->wi_scheduled = 0;
125 wi->wi_action = action;
126 wi->wi_state = SWI_STATE_NEWBORN;
129 #define SWI_RESCHED 128 /* # workitem scheduler loops before reschedule */
131 /* services below SRPC_FRAMEWORK_SERVICE_MAX_ID are framework
132 * services, e.g. create/modify session.
134 #define SRPC_SERVICE_DEBUG 0
135 #define SRPC_SERVICE_MAKE_SESSION 1
136 #define SRPC_SERVICE_REMOVE_SESSION 2
137 #define SRPC_SERVICE_BATCH 3
138 #define SRPC_SERVICE_TEST 4
139 #define SRPC_SERVICE_QUERY_STAT 5
140 #define SRPC_SERVICE_JOIN 6
141 #define SRPC_FRAMEWORK_SERVICE_MAX_ID 10
142 /* other services start from SRPC_FRAMEWORK_SERVICE_MAX_ID+1 */
143 #define SRPC_SERVICE_BRW 11
144 #define SRPC_SERVICE_PING 12
145 #define SRPC_SERVICE_MAX_ID 12
147 #define SRPC_REQUEST_PORTAL 50
148 /* a lazy portal for framework RPC requests */
149 #define SRPC_FRAMEWORK_REQUEST_PORTAL 51
150 /* all reply/bulk RDMAs go to this portal */
151 #define SRPC_RDMA_PORTAL 52
153 static inline srpc_msg_type_t
154 srpc_service2request (int service)
159 case SRPC_SERVICE_DEBUG:
160 return SRPC_MSG_DEBUG_REQST;
162 case SRPC_SERVICE_MAKE_SESSION:
163 return SRPC_MSG_MKSN_REQST;
165 case SRPC_SERVICE_REMOVE_SESSION:
166 return SRPC_MSG_RMSN_REQST;
168 case SRPC_SERVICE_BATCH:
169 return SRPC_MSG_BATCH_REQST;
171 case SRPC_SERVICE_TEST:
172 return SRPC_MSG_TEST_REQST;
174 case SRPC_SERVICE_QUERY_STAT:
175 return SRPC_MSG_STAT_REQST;
177 case SRPC_SERVICE_BRW:
178 return SRPC_MSG_BRW_REQST;
180 case SRPC_SERVICE_PING:
181 return SRPC_MSG_PING_REQST;
183 case SRPC_SERVICE_JOIN:
184 return SRPC_MSG_JOIN_REQST;
188 static inline srpc_msg_type_t
189 srpc_service2reply (int service)
191 return srpc_service2request(service) + 1;
195 SRPC_BULK_REQ_RCVD = 0, /* passive bulk request(PUT sink/GET source) received */
196 SRPC_BULK_PUT_SENT = 1, /* active bulk PUT sent (source) */
197 SRPC_BULK_GET_RPLD = 2, /* active bulk GET replied (sink) */
198 SRPC_REPLY_RCVD = 3, /* incoming reply received */
199 SRPC_REPLY_SENT = 4, /* outgoing reply sent */
200 SRPC_REQUEST_RCVD = 5, /* incoming request received */
201 SRPC_REQUEST_SENT = 6, /* outgoing request sent */
206 srpc_event_type_t ev_type; /* what's up */
207 lnet_event_kind_t ev_lnet; /* LNet event type */
208 int ev_fired; /* LNet event fired? */
209 int ev_status; /* LNet event status */
210 void *ev_data; /* owning server/client RPC */
214 int bk_len; /* len of bulk data */
215 lnet_handle_md_t bk_mdh;
216 int bk_sink; /* sink/source */
217 int bk_niov; /* # iov in bk_iovs */
219 lnet_kiov_t bk_iovs[0];
221 cfs_page_t **bk_pages;
222 lnet_md_iovec_t bk_iovs[0];
224 } srpc_bulk_t; /* bulk descriptor */
226 typedef struct srpc_peer {
227 struct list_head stp_list; /* chain on peer hash */
228 struct list_head stp_rpcq; /* q of non-control RPCs */
229 struct list_head stp_ctl_rpcq; /* q of control RPCs */
230 spinlock_t stp_lock; /* serialize */
232 int stp_credits; /* available credits */
235 /* message buffer descriptor */
237 struct list_head buf_list; /* chain on srpc_service::*_msgq */
239 lnet_handle_md_t buf_mdh;
241 lnet_process_id_t buf_peer;
244 /* server-side state of a RPC */
245 typedef struct srpc_server_rpc {
246 struct list_head srpc_list; /* chain on srpc_service::*_rpcq */
247 struct srpc_service *srpc_service;
248 swi_workitem_t srpc_wi;
249 srpc_event_t srpc_ev; /* bulk/reply event */
250 lnet_nid_t srpc_self;
251 lnet_process_id_t srpc_peer;
252 srpc_msg_t srpc_replymsg;
253 lnet_handle_md_t srpc_replymdh;
254 srpc_buffer_t *srpc_reqstbuf;
255 srpc_bulk_t *srpc_bulk;
258 void (*srpc_done)(struct srpc_server_rpc *);
261 /* client-side state of a RPC */
262 typedef struct srpc_client_rpc {
263 struct list_head crpc_list; /* chain on user's lists */
264 struct list_head crpc_privl; /* chain on srpc_peer_t::*rpcq */
265 spinlock_t crpc_lock; /* serialize */
267 atomic_t crpc_refcount;
268 int crpc_timeout; /* # seconds to wait for reply */
269 stt_timer_t crpc_timer;
270 swi_workitem_t crpc_wi;
271 lnet_process_id_t crpc_dest;
272 srpc_peer_t *crpc_peer;
274 void (*crpc_done)(struct srpc_client_rpc *);
275 void (*crpc_fini)(struct srpc_client_rpc *);
276 int crpc_status; /* completion status */
277 void *crpc_priv; /* caller data */
280 unsigned int crpc_aborted:1; /* being given up */
281 unsigned int crpc_closed:1; /* completed */
284 srpc_event_t crpc_bulkev; /* bulk event */
285 srpc_event_t crpc_reqstev; /* request event */
286 srpc_event_t crpc_replyev; /* reply event */
288 /* bulk, request(reqst), and reply exchanged on wire */
289 srpc_msg_t crpc_reqstmsg;
290 srpc_msg_t crpc_replymsg;
291 lnet_handle_md_t crpc_reqstmdh;
292 lnet_handle_md_t crpc_replymdh;
293 srpc_bulk_t crpc_bulk;
296 #define srpc_client_rpc_size(rpc) \
297 offsetof(srpc_client_rpc_t, crpc_bulk.bk_iovs[(rpc)->crpc_bulk.bk_niov])
299 #define srpc_client_rpc_addref(rpc) \
301 CDEBUG(D_NET, "RPC[%p] -> %s (%d)++\n", \
302 (rpc), libcfs_id2str((rpc)->crpc_dest), \
303 atomic_read(&(rpc)->crpc_refcount)); \
304 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
305 atomic_inc(&(rpc)->crpc_refcount); \
308 #define srpc_client_rpc_decref(rpc) \
310 CDEBUG(D_NET, "RPC[%p] -> %s (%d)--\n", \
311 (rpc), libcfs_id2str((rpc)->crpc_dest), \
312 atomic_read(&(rpc)->crpc_refcount)); \
313 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
314 if (atomic_dec_and_test(&(rpc)->crpc_refcount)) \
315 srpc_destroy_client_rpc(rpc); \
318 #define srpc_event_pending(rpc) ((rpc)->crpc_bulkev.ev_fired == 0 || \
319 (rpc)->crpc_reqstev.ev_fired == 0 || \
320 (rpc)->crpc_replyev.ev_fired == 0)
322 typedef struct srpc_service {
323 int sv_id; /* service id */
324 const char *sv_name; /* human readable name */
325 int sv_nprune; /* # posted RPC to be pruned */
326 int sv_concur; /* max # concurrent RPCs */
330 srpc_event_t sv_ev; /* LNet event */
331 int sv_nposted_msg; /* # posted message buffers */
332 struct list_head sv_free_rpcq; /* free RPC descriptors */
333 struct list_head sv_active_rpcq; /* in-flight RPCs */
334 struct list_head sv_posted_msgq; /* posted message buffers */
335 struct list_head sv_blocked_msgq; /* blocked for RPC descriptor */
337 /* Service callbacks:
338 * - sv_handler: process incoming RPC request
339 * - sv_bulk_ready: notify bulk data
341 int (*sv_handler) (srpc_server_rpc_t *);
342 int (*sv_bulk_ready) (srpc_server_rpc_t *, int);
345 #define SFW_POST_BUFFERS 8
346 #define SFW_SERVICE_CONCURRENCY (SFW_POST_BUFFERS/2)
349 struct list_head sn_list; /* chain on fw_zombie_sessions */
350 lst_sid_t sn_id; /* unique identifier */
351 unsigned int sn_timeout; /* # seconds' inactivity to expire */
353 stt_timer_t sn_timer;
354 struct list_head sn_batches; /* list of batches */
355 char sn_name[LST_NAME_SIZE];
356 atomic_t sn_brw_errors;
357 atomic_t sn_ping_errors;
360 #define sfw_sid_equal(sid0, sid1) ((sid0).ses_nid == (sid1).ses_nid && \
361 (sid0).ses_stamp == (sid1).ses_stamp)
364 struct list_head bat_list; /* chain on sn_batches */
365 lst_bid_t bat_id; /* batch id */
366 int bat_error; /* error code of batch */
367 sfw_session_t *bat_session; /* batch's session */
368 atomic_t bat_nactive; /* # of active tests */
369 struct list_head bat_tests; /* test instances */
373 int (*tso_init)(struct sfw_test_instance *tsi); /* intialize test client */
374 void (*tso_fini)(struct sfw_test_instance *tsi); /* finalize test client */
375 int (*tso_prep_rpc)(struct sfw_test_unit *tsu,
376 lnet_process_id_t dest,
377 srpc_client_rpc_t **rpc); /* prep a tests rpc */
378 void (*tso_done_rpc)(struct sfw_test_unit *tsu,
379 srpc_client_rpc_t *rpc); /* done a test rpc */
380 } sfw_test_client_ops_t;
382 typedef struct sfw_test_instance {
383 struct list_head tsi_list; /* chain on batch */
384 int tsi_service; /* test type */
385 sfw_batch_t *tsi_batch; /* batch */
386 sfw_test_client_ops_t *tsi_ops; /* test client operations */
388 /* public parameter for all test units */
389 int tsi_is_client:1; /* is test client */
390 int tsi_stoptsu_onerr:1; /* stop tsu on error */
391 int tsi_concur; /* concurrency */
392 int tsi_loop; /* loop count */
394 /* status of test instance */
395 spinlock_t tsi_lock; /* serialize */
396 int tsi_stopping:1; /* test is stopping */
397 atomic_t tsi_nactive; /* # of active test unit */
398 struct list_head tsi_units; /* test units */
399 struct list_head tsi_free_rpcs; /* free rpcs */
400 struct list_head tsi_active_rpcs; /* active rpcs */
403 test_bulk_req_t bulk; /* bulk parameter */
404 test_ping_req_t ping; /* ping parameter */
406 } sfw_test_instance_t;
408 /* XXX: trailing (CFS_PAGE_SIZE % sizeof(lnet_process_id_t)) bytes at
409 * the end of pages are not used */
410 #define SFW_MAX_CONCUR LST_MAX_CONCUR
411 #define SFW_ID_PER_PAGE (CFS_PAGE_SIZE / sizeof(lnet_process_id_t))
412 #define SFW_MAX_NDESTS (LNET_MAX_IOV * SFW_ID_PER_PAGE)
413 #define sfw_id_pages(n) (((n) + SFW_ID_PER_PAGE - 1) / SFW_ID_PER_PAGE)
415 typedef struct sfw_test_unit {
416 struct list_head tsu_list; /* chain on lst_test_instance */
417 lnet_process_id_t tsu_dest; /* id of dest node */
418 int tsu_loop; /* loop count of the test */
419 sfw_test_instance_t *tsu_instance; /* pointer to test instance */
420 void *tsu_private; /* private data */
421 swi_workitem_t tsu_worker; /* workitem of the test unit */
425 struct list_head tsc_list; /* chain on fw_tests */
426 srpc_service_t *tsc_srv_service; /* test service */
427 sfw_test_client_ops_t *tsc_cli_ops; /* ops of test client */
432 sfw_create_rpc(lnet_process_id_t peer, int service, int nbulkiov, int bulklen,
433 void (*done) (srpc_client_rpc_t *), void *priv);
434 int sfw_create_test_rpc(sfw_test_unit_t *tsu, lnet_process_id_t peer,
435 int nblk, int blklen, srpc_client_rpc_t **rpc);
436 void sfw_abort_rpc(srpc_client_rpc_t *rpc);
437 void sfw_post_rpc(srpc_client_rpc_t *rpc);
438 void sfw_client_rpc_done(srpc_client_rpc_t *rpc);
439 void sfw_unpack_message(srpc_msg_t *msg);
440 void sfw_free_pages(srpc_server_rpc_t *rpc);
441 void sfw_add_bulk_page(srpc_bulk_t *bk, cfs_page_t *pg, int i);
442 int sfw_alloc_pages(srpc_server_rpc_t *rpc, int npages, int sink);
445 srpc_create_client_rpc(lnet_process_id_t peer, int service,
446 int nbulkiov, int bulklen,
447 void (*rpc_done)(srpc_client_rpc_t *),
448 void (*rpc_fini)(srpc_client_rpc_t *), void *priv);
449 void srpc_post_rpc(srpc_client_rpc_t *rpc);
450 void srpc_abort_rpc(srpc_client_rpc_t *rpc, int why);
451 void srpc_free_bulk(srpc_bulk_t *bk);
452 srpc_bulk_t *srpc_alloc_bulk(int npages, int sink);
453 int srpc_send_rpc(swi_workitem_t *wi);
454 int srpc_send_reply(srpc_server_rpc_t *rpc);
455 int srpc_add_service(srpc_service_t *sv);
456 int srpc_remove_service(srpc_service_t *sv);
457 void srpc_shutdown_service(srpc_service_t *sv);
458 int srpc_finish_service(srpc_service_t *sv);
459 int srpc_service_add_buffers(srpc_service_t *sv, int nbuffer);
460 void srpc_service_remove_buffers(srpc_service_t *sv, int nbuffer);
461 void srpc_get_counters(srpc_counters_t *cnt);
462 void srpc_set_counters(const srpc_counters_t *cnt);
464 void swi_kill_workitem(swi_workitem_t *wi);
465 void swi_schedule_workitem(swi_workitem_t *wi);
466 void swi_schedule_serial_workitem(swi_workitem_t *wi);
467 int swi_startup(void);
468 int sfw_startup(void);
469 int srpc_startup(void);
470 void swi_shutdown(void);
471 void sfw_shutdown(void);
472 void srpc_shutdown(void);
475 srpc_destroy_client_rpc (srpc_client_rpc_t *rpc)
477 LASSERT (rpc != NULL);
478 LASSERT (!srpc_event_pending(rpc));
479 LASSERT (list_empty(&rpc->crpc_privl));
480 LASSERT (atomic_read(&rpc->crpc_refcount) == 0);
482 LASSERT (rpc->crpc_bulk.bk_pages == NULL);
485 if (rpc->crpc_fini == NULL) {
486 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
488 (*rpc->crpc_fini) (rpc);
495 srpc_init_client_rpc (srpc_client_rpc_t *rpc, lnet_process_id_t peer,
496 int service, int nbulkiov, int bulklen,
497 void (*rpc_done)(srpc_client_rpc_t *),
498 void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
500 LASSERT (nbulkiov <= LNET_MAX_IOV);
502 memset(rpc, 0, offsetof(srpc_client_rpc_t,
503 crpc_bulk.bk_iovs[nbulkiov]));
505 CFS_INIT_LIST_HEAD(&rpc->crpc_list);
506 CFS_INIT_LIST_HEAD(&rpc->crpc_privl);
507 swi_init_workitem(&rpc->crpc_wi, rpc, srpc_send_rpc);
508 spin_lock_init(&rpc->crpc_lock);
509 atomic_set(&rpc->crpc_refcount, 1); /* 1 ref for caller */
511 rpc->crpc_dest = peer;
512 rpc->crpc_priv = priv;
513 rpc->crpc_service = service;
514 rpc->crpc_bulk.bk_len = bulklen;
515 rpc->crpc_bulk.bk_niov = nbulkiov;
516 rpc->crpc_done = rpc_done;
517 rpc->crpc_fini = rpc_fini;
520 rpc->crpc_bulk.bk_mdh = LNET_INVALID_HANDLE;
522 /* no event is expected at this point */
523 rpc->crpc_bulkev.ev_fired =
524 rpc->crpc_reqstev.ev_fired =
525 rpc->crpc_replyev.ev_fired = 1;
527 rpc->crpc_reqstmsg.msg_magic = SRPC_MSG_MAGIC;
528 rpc->crpc_reqstmsg.msg_version = SRPC_MSG_VERSION;
529 rpc->crpc_reqstmsg.msg_type = srpc_service2request(service);
533 static inline const char *
534 swi_state2str (int state)
536 #define STATE2STR(x) case x: return #x
540 STATE2STR(SWI_STATE_NEWBORN);
541 STATE2STR(SWI_STATE_REPLY_SUBMITTED);
542 STATE2STR(SWI_STATE_REPLY_SENT);
543 STATE2STR(SWI_STATE_REQUEST_SUBMITTED);
544 STATE2STR(SWI_STATE_REQUEST_SENT);
545 STATE2STR(SWI_STATE_REPLY_RECEIVED);
546 STATE2STR(SWI_STATE_BULK_STARTED);
547 STATE2STR(SWI_STATE_BULK_ERRORED);
548 STATE2STR(SWI_STATE_DONE);
553 #define UNUSED(x) ( (void)(x) )
557 int stt_poll_interval(void);
558 int sfw_session_removed(void);
560 int stt_check_events(void);
561 int swi_check_events(void);
562 int srpc_check_event(int timeout);
564 int lnet_selftest_init(void);
565 void lnet_selftest_fini(void);
566 int selftest_wait_events(void);
570 #define selftest_wait_events() cfs_pause(cfs_time_seconds(1))
574 #define lst_wait_until(cond, lock, fmt, a...) \
579 CDEBUG(((__I & (-__I)) == __I) ? D_WARNING : \
582 spin_unlock(&(lock)); \
584 selftest_wait_events(); \
586 spin_lock(&(lock)); \
591 srpc_wait_service_shutdown (srpc_service_t *sv)
595 spin_lock(&sv->sv_lock);
596 LASSERT (sv->sv_shuttingdown);
597 spin_unlock(&sv->sv_lock);
599 while (srpc_finish_service(sv) == 0) {
601 CDEBUG (((i & -i) == i) ? D_WARNING : D_NET,
602 "Waiting for %s service to shutdown...\n",
604 selftest_wait_events();
608 #endif /* __SELFTEST_SELFTEST_H__ */