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_DONE 10
81 struct sfw_test_instance;
84 * A workitems is deferred work with these semantics:
85 * - a workitem always runs in thread context.
86 * - a workitem can be concurrent with other workitems but is strictly
87 * serialized with respect to itself.
88 * - no CPU affinity, a workitem does not necessarily run on the same CPU
89 * that schedules it. However, this might change in the future.
90 * - if a workitem is scheduled again before it has a chance to run, it
92 * - if a workitem is scheduled while it runs, it runs again after it
93 * completes; this ensures that events occurring while other events are
94 * being processed receive due attention. This behavior also allows a
95 * workitem to reschedule itself.
98 * - a workitem can sleep but it should be aware of how that sleep might
100 * - a workitem runs inside a kernel thread so there's no user space to access.
101 * - do not use a workitem if the scheduling latency can't be tolerated.
103 * When wi_action returns non-zero, it means the workitem has either been
104 * freed or reused and workitem scheduler won't touch it any more.
106 typedef int (*swi_action_t) (struct swi_workitem *);
107 typedef struct swi_workitem {
108 cfs_list_t wi_list; /* chain on runq */
110 swi_action_t wi_action;
112 unsigned int wi_running:1;
113 unsigned int wi_scheduled:1;
117 swi_init_workitem (swi_workitem_t *wi, void *data, swi_action_t action)
119 CFS_INIT_LIST_HEAD(&wi->wi_list);
122 wi->wi_scheduled = 0;
124 wi->wi_action = action;
125 wi->wi_state = SWI_STATE_NEWBORN;
128 #define SWI_RESCHED 128 /* # workitem scheduler loops before reschedule */
130 /* services below SRPC_FRAMEWORK_SERVICE_MAX_ID are framework
131 * services, e.g. create/modify session.
133 #define SRPC_SERVICE_DEBUG 0
134 #define SRPC_SERVICE_MAKE_SESSION 1
135 #define SRPC_SERVICE_REMOVE_SESSION 2
136 #define SRPC_SERVICE_BATCH 3
137 #define SRPC_SERVICE_TEST 4
138 #define SRPC_SERVICE_QUERY_STAT 5
139 #define SRPC_SERVICE_JOIN 6
140 #define SRPC_FRAMEWORK_SERVICE_MAX_ID 10
141 /* other services start from SRPC_FRAMEWORK_SERVICE_MAX_ID+1 */
142 #define SRPC_SERVICE_BRW 11
143 #define SRPC_SERVICE_PING 12
144 #define SRPC_SERVICE_MAX_ID 12
146 #define SRPC_REQUEST_PORTAL 50
147 /* a lazy portal for framework RPC requests */
148 #define SRPC_FRAMEWORK_REQUEST_PORTAL 51
149 /* all reply/bulk RDMAs go to this portal */
150 #define SRPC_RDMA_PORTAL 52
152 static inline srpc_msg_type_t
153 srpc_service2request (int service)
158 case SRPC_SERVICE_DEBUG:
159 return SRPC_MSG_DEBUG_REQST;
161 case SRPC_SERVICE_MAKE_SESSION:
162 return SRPC_MSG_MKSN_REQST;
164 case SRPC_SERVICE_REMOVE_SESSION:
165 return SRPC_MSG_RMSN_REQST;
167 case SRPC_SERVICE_BATCH:
168 return SRPC_MSG_BATCH_REQST;
170 case SRPC_SERVICE_TEST:
171 return SRPC_MSG_TEST_REQST;
173 case SRPC_SERVICE_QUERY_STAT:
174 return SRPC_MSG_STAT_REQST;
176 case SRPC_SERVICE_BRW:
177 return SRPC_MSG_BRW_REQST;
179 case SRPC_SERVICE_PING:
180 return SRPC_MSG_PING_REQST;
182 case SRPC_SERVICE_JOIN:
183 return SRPC_MSG_JOIN_REQST;
187 static inline srpc_msg_type_t
188 srpc_service2reply (int service)
190 return srpc_service2request(service) + 1;
194 SRPC_BULK_REQ_RCVD = 1, /* passive bulk request(PUT sink/GET source) received */
195 SRPC_BULK_PUT_SENT = 2, /* active bulk PUT sent (source) */
196 SRPC_BULK_GET_RPLD = 3, /* active bulk GET replied (sink) */
197 SRPC_REPLY_RCVD = 4, /* incoming reply received */
198 SRPC_REPLY_SENT = 5, /* outgoing reply sent */
199 SRPC_REQUEST_RCVD = 6, /* incoming request received */
200 SRPC_REQUEST_SENT = 7, /* outgoing request sent */
205 srpc_event_type_t ev_type; /* what's up */
206 lnet_event_kind_t ev_lnet; /* LNet event type */
207 int ev_fired; /* LNet event fired? */
208 int ev_status; /* LNet event status */
209 void *ev_data; /* owning server/client RPC */
213 int bk_len; /* len of bulk data */
214 lnet_handle_md_t bk_mdh;
215 int bk_sink; /* sink/source */
216 int bk_niov; /* # iov in bk_iovs */
218 lnet_kiov_t bk_iovs[0];
220 cfs_page_t **bk_pages;
221 lnet_md_iovec_t bk_iovs[0];
223 } srpc_bulk_t; /* bulk descriptor */
225 /* message buffer descriptor */
227 cfs_list_t buf_list; /* chain on srpc_service::*_msgq */
229 lnet_handle_md_t buf_mdh;
231 lnet_process_id_t buf_peer;
234 /* server-side state of a RPC */
235 typedef struct srpc_server_rpc {
236 cfs_list_t srpc_list; /* chain on srpc_service::*_rpcq */
237 struct srpc_service *srpc_service;
238 swi_workitem_t srpc_wi;
239 srpc_event_t srpc_ev; /* bulk/reply event */
240 lnet_nid_t srpc_self;
241 lnet_process_id_t srpc_peer;
242 srpc_msg_t srpc_replymsg;
243 lnet_handle_md_t srpc_replymdh;
244 srpc_buffer_t *srpc_reqstbuf;
245 srpc_bulk_t *srpc_bulk;
247 unsigned int srpc_aborted; /* being given up */
249 void (*srpc_done)(struct srpc_server_rpc *);
252 /* client-side state of a RPC */
253 typedef struct srpc_client_rpc {
254 cfs_list_t crpc_list; /* chain on user's lists */
255 cfs_spinlock_t crpc_lock; /* serialize */
257 cfs_atomic_t crpc_refcount;
258 int crpc_timeout; /* # seconds to wait for reply */
259 stt_timer_t crpc_timer;
260 swi_workitem_t crpc_wi;
261 lnet_process_id_t crpc_dest;
263 void (*crpc_done)(struct srpc_client_rpc *);
264 void (*crpc_fini)(struct srpc_client_rpc *);
265 int crpc_status; /* completion status */
266 void *crpc_priv; /* caller data */
269 unsigned int crpc_aborted:1; /* being given up */
270 unsigned int crpc_closed:1; /* completed */
273 srpc_event_t crpc_bulkev; /* bulk event */
274 srpc_event_t crpc_reqstev; /* request event */
275 srpc_event_t crpc_replyev; /* reply event */
277 /* bulk, request(reqst), and reply exchanged on wire */
278 srpc_msg_t crpc_reqstmsg;
279 srpc_msg_t crpc_replymsg;
280 lnet_handle_md_t crpc_reqstmdh;
281 lnet_handle_md_t crpc_replymdh;
282 srpc_bulk_t crpc_bulk;
285 #define srpc_client_rpc_size(rpc) \
286 offsetof(srpc_client_rpc_t, crpc_bulk.bk_iovs[(rpc)->crpc_bulk.bk_niov])
288 #define srpc_client_rpc_addref(rpc) \
290 CDEBUG(D_NET, "RPC[%p] -> %s (%d)++\n", \
291 (rpc), libcfs_id2str((rpc)->crpc_dest), \
292 cfs_atomic_read(&(rpc)->crpc_refcount)); \
293 LASSERT(cfs_atomic_read(&(rpc)->crpc_refcount) > 0); \
294 cfs_atomic_inc(&(rpc)->crpc_refcount); \
297 #define srpc_client_rpc_decref(rpc) \
299 CDEBUG(D_NET, "RPC[%p] -> %s (%d)--\n", \
300 (rpc), libcfs_id2str((rpc)->crpc_dest), \
301 cfs_atomic_read(&(rpc)->crpc_refcount)); \
302 LASSERT(cfs_atomic_read(&(rpc)->crpc_refcount) > 0); \
303 if (cfs_atomic_dec_and_test(&(rpc)->crpc_refcount)) \
304 srpc_destroy_client_rpc(rpc); \
307 #define srpc_event_pending(rpc) ((rpc)->crpc_bulkev.ev_fired == 0 || \
308 (rpc)->crpc_reqstev.ev_fired == 0 || \
309 (rpc)->crpc_replyev.ev_fired == 0)
311 typedef struct srpc_service {
312 int sv_id; /* service id */
313 const char *sv_name; /* human readable name */
314 int sv_nprune; /* # posted RPC to be pruned */
315 int sv_concur; /* max # concurrent RPCs */
317 cfs_spinlock_t sv_lock;
319 srpc_event_t sv_ev; /* LNet event */
320 int sv_nposted_msg; /* # posted message buffers */
321 cfs_list_t sv_free_rpcq; /* free RPC descriptors */
322 cfs_list_t sv_active_rpcq; /* in-flight RPCs */
323 cfs_list_t sv_posted_msgq; /* posted message buffers */
324 cfs_list_t sv_blocked_msgq; /* blocked for RPC descriptor */
326 /* Service callbacks:
327 * - sv_handler: process incoming RPC request
328 * - sv_bulk_ready: notify bulk data
330 int (*sv_handler) (srpc_server_rpc_t *);
331 int (*sv_bulk_ready) (srpc_server_rpc_t *, int);
334 #define SFW_POST_BUFFERS 256
335 #define SFW_SERVICE_CONCURRENCY (SFW_POST_BUFFERS/2)
338 cfs_list_t sn_list; /* chain on fw_zombie_sessions */
339 lst_sid_t sn_id; /* unique identifier */
340 unsigned int sn_timeout; /* # seconds' inactivity to expire */
342 stt_timer_t sn_timer;
343 cfs_list_t sn_batches; /* list of batches */
344 char sn_name[LST_NAME_SIZE];
345 cfs_atomic_t sn_refcount;
346 cfs_atomic_t sn_brw_errors;
347 cfs_atomic_t sn_ping_errors;
350 #define sfw_sid_equal(sid0, sid1) ((sid0).ses_nid == (sid1).ses_nid && \
351 (sid0).ses_stamp == (sid1).ses_stamp)
354 cfs_list_t bat_list; /* chain on sn_batches */
355 lst_bid_t bat_id; /* batch id */
356 int bat_error; /* error code of batch */
357 sfw_session_t *bat_session; /* batch's session */
358 cfs_atomic_t bat_nactive; /* # of active tests */
359 cfs_list_t bat_tests; /* test instances */
363 int (*tso_init)(struct sfw_test_instance *tsi); /* intialize test client */
364 void (*tso_fini)(struct sfw_test_instance *tsi); /* finalize test client */
365 int (*tso_prep_rpc)(struct sfw_test_unit *tsu,
366 lnet_process_id_t dest,
367 srpc_client_rpc_t **rpc); /* prep a tests rpc */
368 void (*tso_done_rpc)(struct sfw_test_unit *tsu,
369 srpc_client_rpc_t *rpc); /* done a test rpc */
370 } sfw_test_client_ops_t;
372 typedef struct sfw_test_instance {
373 cfs_list_t tsi_list; /* chain on batch */
374 int tsi_service; /* test type */
375 sfw_batch_t *tsi_batch; /* batch */
376 sfw_test_client_ops_t *tsi_ops; /* test client operations */
378 /* public parameter for all test units */
379 int tsi_is_client:1; /* is test client */
380 int tsi_stoptsu_onerr:1; /* stop tsu on error */
381 int tsi_concur; /* concurrency */
382 int tsi_loop; /* loop count */
384 /* status of test instance */
385 cfs_spinlock_t tsi_lock; /* serialize */
386 int tsi_stopping:1; /* test is stopping */
387 cfs_atomic_t tsi_nactive; /* # of active test unit */
388 cfs_list_t tsi_units; /* test units */
389 cfs_list_t tsi_free_rpcs; /* free rpcs */
390 cfs_list_t tsi_active_rpcs; /* active rpcs */
393 test_bulk_req_t bulk; /* bulk parameter */
394 test_ping_req_t ping; /* ping parameter */
396 } sfw_test_instance_t;
398 /* XXX: trailing (CFS_PAGE_SIZE % sizeof(lnet_process_id_t)) bytes at
399 * the end of pages are not used */
400 #define SFW_MAX_CONCUR LST_MAX_CONCUR
401 #define SFW_ID_PER_PAGE (CFS_PAGE_SIZE / sizeof(lnet_process_id_t))
402 #define SFW_MAX_NDESTS (LNET_MAX_IOV * SFW_ID_PER_PAGE)
403 #define sfw_id_pages(n) (((n) + SFW_ID_PER_PAGE - 1) / SFW_ID_PER_PAGE)
405 typedef struct sfw_test_unit {
406 cfs_list_t tsu_list; /* chain on lst_test_instance */
407 lnet_process_id_t tsu_dest; /* id of dest node */
408 int tsu_loop; /* loop count of the test */
409 sfw_test_instance_t *tsu_instance; /* pointer to test instance */
410 void *tsu_private; /* private data */
411 swi_workitem_t tsu_worker; /* workitem of the test unit */
415 cfs_list_t tsc_list; /* chain on fw_tests */
416 srpc_service_t *tsc_srv_service; /* test service */
417 sfw_test_client_ops_t *tsc_cli_ops; /* ops of test client */
422 sfw_create_rpc(lnet_process_id_t peer, int service, int nbulkiov, int bulklen,
423 void (*done) (srpc_client_rpc_t *), void *priv);
424 int sfw_create_test_rpc(sfw_test_unit_t *tsu, lnet_process_id_t peer,
425 int nblk, int blklen, srpc_client_rpc_t **rpc);
426 void sfw_abort_rpc(srpc_client_rpc_t *rpc);
427 void sfw_post_rpc(srpc_client_rpc_t *rpc);
428 void sfw_client_rpc_done(srpc_client_rpc_t *rpc);
429 void sfw_unpack_message(srpc_msg_t *msg);
430 void sfw_free_pages(srpc_server_rpc_t *rpc);
431 void sfw_add_bulk_page(srpc_bulk_t *bk, cfs_page_t *pg, int i);
432 int sfw_alloc_pages(srpc_server_rpc_t *rpc, int npages, int sink);
433 int sfw_make_session (srpc_mksn_reqst_t *request, srpc_mksn_reply_t *reply);
436 srpc_create_client_rpc(lnet_process_id_t peer, int service,
437 int nbulkiov, int bulklen,
438 void (*rpc_done)(srpc_client_rpc_t *),
439 void (*rpc_fini)(srpc_client_rpc_t *), void *priv);
440 void srpc_post_rpc(srpc_client_rpc_t *rpc);
441 void srpc_abort_rpc(srpc_client_rpc_t *rpc, int why);
442 void srpc_free_bulk(srpc_bulk_t *bk);
443 srpc_bulk_t *srpc_alloc_bulk(int npages, int sink);
444 int srpc_send_rpc(swi_workitem_t *wi);
445 int srpc_send_reply(srpc_server_rpc_t *rpc);
446 int srpc_add_service(srpc_service_t *sv);
447 int srpc_remove_service(srpc_service_t *sv);
448 void srpc_shutdown_service(srpc_service_t *sv);
449 void srpc_abort_service(srpc_service_t *sv);
450 int srpc_finish_service(srpc_service_t *sv);
451 int srpc_service_add_buffers(srpc_service_t *sv, int nbuffer);
452 void srpc_service_remove_buffers(srpc_service_t *sv, int nbuffer);
453 void srpc_get_counters(srpc_counters_t *cnt);
454 void srpc_set_counters(const srpc_counters_t *cnt);
456 void swi_kill_workitem(swi_workitem_t *wi);
457 void swi_schedule_workitem(swi_workitem_t *wi);
458 void swi_schedule_serial_workitem(swi_workitem_t *wi);
459 int swi_startup(void);
460 int sfw_startup(void);
461 int srpc_startup(void);
462 void swi_shutdown(void);
463 void sfw_shutdown(void);
464 void srpc_shutdown(void);
467 srpc_destroy_client_rpc (srpc_client_rpc_t *rpc)
469 LASSERT (rpc != NULL);
470 LASSERT (!srpc_event_pending(rpc));
471 LASSERT (cfs_atomic_read(&rpc->crpc_refcount) == 0);
473 LASSERT (rpc->crpc_bulk.bk_pages == NULL);
476 if (rpc->crpc_fini == NULL) {
477 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
479 (*rpc->crpc_fini) (rpc);
486 srpc_init_client_rpc (srpc_client_rpc_t *rpc, lnet_process_id_t peer,
487 int service, int nbulkiov, int bulklen,
488 void (*rpc_done)(srpc_client_rpc_t *),
489 void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
491 LASSERT (nbulkiov <= LNET_MAX_IOV);
493 memset(rpc, 0, offsetof(srpc_client_rpc_t,
494 crpc_bulk.bk_iovs[nbulkiov]));
496 CFS_INIT_LIST_HEAD(&rpc->crpc_list);
497 swi_init_workitem(&rpc->crpc_wi, rpc, srpc_send_rpc);
498 cfs_spin_lock_init(&rpc->crpc_lock);
499 cfs_atomic_set(&rpc->crpc_refcount, 1); /* 1 ref for caller */
501 rpc->crpc_dest = peer;
502 rpc->crpc_priv = priv;
503 rpc->crpc_service = service;
504 rpc->crpc_bulk.bk_len = bulklen;
505 rpc->crpc_bulk.bk_niov = nbulkiov;
506 rpc->crpc_done = rpc_done;
507 rpc->crpc_fini = rpc_fini;
508 LNetInvalidateHandle(&rpc->crpc_reqstmdh);
509 LNetInvalidateHandle(&rpc->crpc_replymdh);
510 LNetInvalidateHandle(&rpc->crpc_bulk.bk_mdh);
512 /* no event is expected at this point */
513 rpc->crpc_bulkev.ev_fired =
514 rpc->crpc_reqstev.ev_fired =
515 rpc->crpc_replyev.ev_fired = 1;
517 rpc->crpc_reqstmsg.msg_magic = SRPC_MSG_MAGIC;
518 rpc->crpc_reqstmsg.msg_version = SRPC_MSG_VERSION;
519 rpc->crpc_reqstmsg.msg_type = srpc_service2request(service);
523 static inline const char *
524 swi_state2str (int state)
526 #define STATE2STR(x) case x: return #x
530 STATE2STR(SWI_STATE_NEWBORN);
531 STATE2STR(SWI_STATE_REPLY_SUBMITTED);
532 STATE2STR(SWI_STATE_REPLY_SENT);
533 STATE2STR(SWI_STATE_REQUEST_SUBMITTED);
534 STATE2STR(SWI_STATE_REQUEST_SENT);
535 STATE2STR(SWI_STATE_REPLY_RECEIVED);
536 STATE2STR(SWI_STATE_BULK_STARTED);
537 STATE2STR(SWI_STATE_DONE);
542 #define UNUSED(x) ( (void)(x) )
546 int stt_poll_interval(void);
547 int sfw_session_removed(void);
549 int stt_check_events(void);
550 int swi_check_events(void);
551 int srpc_check_event(int timeout);
553 int lnet_selftest_init(void);
554 void lnet_selftest_fini(void);
555 int selftest_wait_events(void);
559 #define selftest_wait_events() cfs_pause(cfs_time_seconds(1))
563 #define lst_wait_until(cond, lock, fmt, ...) \
567 CDEBUG(IS_PO2(++__I) ? D_WARNING : D_NET, \
568 fmt, ## __VA_ARGS__); \
569 cfs_spin_unlock(&(lock)); \
571 selftest_wait_events(); \
573 cfs_spin_lock(&(lock)); \
578 srpc_wait_service_shutdown (srpc_service_t *sv)
582 cfs_spin_lock(&sv->sv_lock);
583 LASSERT (sv->sv_shuttingdown);
584 cfs_spin_unlock(&sv->sv_lock);
586 while (srpc_finish_service(sv) == 0) {
588 CDEBUG (((i & -i) == i) ? D_WARNING : D_NET,
589 "Waiting for %s service to shutdown...\n",
591 selftest_wait_events();
595 #endif /* __SELFTEST_SELFTEST_H__ */