4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
28 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
29 * Use is subject to license terms.
31 * Copyright (c) 2012, 2013, Intel Corporation.
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__
46 #include <libcfs/libcfs.h>
48 #include <lnet/lib-lnet.h>
49 #include <lnet/lib-types.h>
50 #include <lnet/lnetst.h>
55 #ifndef MADE_WITHOUT_COMPROMISE
56 #define MADE_WITHOUT_COMPROMISE
60 #define SWI_STATE_NEWBORN 0
61 #define SWI_STATE_REPLY_SUBMITTED 1
62 #define SWI_STATE_REPLY_SENT 2
63 #define SWI_STATE_REQUEST_SUBMITTED 3
64 #define SWI_STATE_REQUEST_SENT 4
65 #define SWI_STATE_REPLY_RECEIVED 5
66 #define SWI_STATE_BULK_STARTED 6
67 #define SWI_STATE_DONE 10
71 struct srpc_service_cd;
73 struct sfw_test_instance;
75 /* services below SRPC_FRAMEWORK_SERVICE_MAX_ID are framework
76 * services, e.g. create/modify session.
78 #define SRPC_SERVICE_DEBUG 0
79 #define SRPC_SERVICE_MAKE_SESSION 1
80 #define SRPC_SERVICE_REMOVE_SESSION 2
81 #define SRPC_SERVICE_BATCH 3
82 #define SRPC_SERVICE_TEST 4
83 #define SRPC_SERVICE_QUERY_STAT 5
84 #define SRPC_SERVICE_JOIN 6
85 #define SRPC_FRAMEWORK_SERVICE_MAX_ID 10
86 /* other services start from SRPC_FRAMEWORK_SERVICE_MAX_ID+1 */
87 #define SRPC_SERVICE_BRW 11
88 #define SRPC_SERVICE_PING 12
89 #define SRPC_SERVICE_MAX_ID 12
91 #define SRPC_REQUEST_PORTAL 50
92 /* a lazy portal for framework RPC requests */
93 #define SRPC_FRAMEWORK_REQUEST_PORTAL 51
94 /* all reply/bulk RDMAs go to this portal */
95 #define SRPC_RDMA_PORTAL 52
97 static inline srpc_msg_type_t
98 srpc_service2request (int service)
103 case SRPC_SERVICE_DEBUG:
104 return SRPC_MSG_DEBUG_REQST;
106 case SRPC_SERVICE_MAKE_SESSION:
107 return SRPC_MSG_MKSN_REQST;
109 case SRPC_SERVICE_REMOVE_SESSION:
110 return SRPC_MSG_RMSN_REQST;
112 case SRPC_SERVICE_BATCH:
113 return SRPC_MSG_BATCH_REQST;
115 case SRPC_SERVICE_TEST:
116 return SRPC_MSG_TEST_REQST;
118 case SRPC_SERVICE_QUERY_STAT:
119 return SRPC_MSG_STAT_REQST;
121 case SRPC_SERVICE_BRW:
122 return SRPC_MSG_BRW_REQST;
124 case SRPC_SERVICE_PING:
125 return SRPC_MSG_PING_REQST;
127 case SRPC_SERVICE_JOIN:
128 return SRPC_MSG_JOIN_REQST;
132 static inline srpc_msg_type_t
133 srpc_service2reply (int service)
135 return srpc_service2request(service) + 1;
139 SRPC_BULK_REQ_RCVD = 1, /* passive bulk request(PUT sink/GET source) received */
140 SRPC_BULK_PUT_SENT = 2, /* active bulk PUT sent (source) */
141 SRPC_BULK_GET_RPLD = 3, /* active bulk GET replied (sink) */
142 SRPC_REPLY_RCVD = 4, /* incoming reply received */
143 SRPC_REPLY_SENT = 5, /* outgoing reply sent */
144 SRPC_REQUEST_RCVD = 6, /* incoming request received */
145 SRPC_REQUEST_SENT = 7, /* outgoing request sent */
150 srpc_event_type_t ev_type; /* what's up */
151 lnet_event_kind_t ev_lnet; /* LNet event type */
152 int ev_fired; /* LNet event fired? */
153 int ev_status; /* LNet event status */
154 void *ev_data; /* owning server/client RPC */
158 int bk_len; /* len of bulk data */
159 lnet_handle_md_t bk_mdh;
160 int bk_sink; /* sink/source */
161 int bk_niov; /* # iov in bk_iovs */
162 lnet_kiov_t bk_iovs[0];
163 } srpc_bulk_t; /* bulk descriptor */
165 /* message buffer descriptor */
166 typedef struct srpc_buffer {
167 struct list_head buf_list; /* chain on srpc_service::*_msgq */
169 lnet_handle_md_t buf_mdh;
171 lnet_process_id_t buf_peer;
175 typedef int (*swi_action_t) (struct swi_workitem *);
177 typedef struct swi_workitem {
178 struct cfs_wi_sched *swi_sched;
179 cfs_workitem_t swi_workitem;
180 swi_action_t swi_action;
184 /* server-side state of a RPC */
185 typedef struct srpc_server_rpc {
186 /* chain on srpc_service::*_rpcq */
187 struct list_head srpc_list;
188 struct srpc_service_cd *srpc_scd;
189 swi_workitem_t srpc_wi;
190 srpc_event_t srpc_ev; /* bulk/reply event */
191 lnet_nid_t srpc_self;
192 lnet_process_id_t srpc_peer;
193 srpc_msg_t srpc_replymsg;
194 lnet_handle_md_t srpc_replymdh;
195 srpc_buffer_t *srpc_reqstbuf;
196 srpc_bulk_t *srpc_bulk;
198 unsigned int srpc_aborted; /* being given up */
200 void (*srpc_done)(struct srpc_server_rpc *);
203 /* client-side state of a RPC */
204 typedef struct srpc_client_rpc {
205 struct list_head crpc_list; /* chain on user's lists */
206 spinlock_t crpc_lock; /* serialize */
208 atomic_t crpc_refcount;
209 /* # seconds to wait for reply */
211 stt_timer_t crpc_timer;
212 swi_workitem_t crpc_wi;
213 lnet_process_id_t crpc_dest;
215 void (*crpc_done)(struct srpc_client_rpc *);
216 void (*crpc_fini)(struct srpc_client_rpc *);
217 int crpc_status; /* completion status */
218 void *crpc_priv; /* caller data */
221 unsigned int crpc_aborted:1; /* being given up */
222 unsigned int crpc_closed:1; /* completed */
225 srpc_event_t crpc_bulkev; /* bulk event */
226 srpc_event_t crpc_reqstev; /* request event */
227 srpc_event_t crpc_replyev; /* reply event */
229 /* bulk, request(reqst), and reply exchanged on wire */
230 srpc_msg_t crpc_reqstmsg;
231 srpc_msg_t crpc_replymsg;
232 lnet_handle_md_t crpc_reqstmdh;
233 lnet_handle_md_t crpc_replymdh;
234 srpc_bulk_t crpc_bulk;
237 #define srpc_client_rpc_size(rpc) \
238 offsetof(srpc_client_rpc_t, crpc_bulk.bk_iovs[(rpc)->crpc_bulk.bk_niov])
240 #define srpc_client_rpc_addref(rpc) \
242 CDEBUG(D_NET, "RPC[%p] -> %s (%d)++\n", \
243 (rpc), libcfs_id2str((rpc)->crpc_dest), \
244 atomic_read(&(rpc)->crpc_refcount)); \
245 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
246 atomic_inc(&(rpc)->crpc_refcount); \
249 #define srpc_client_rpc_decref(rpc) \
251 CDEBUG(D_NET, "RPC[%p] -> %s (%d)--\n", \
252 (rpc), libcfs_id2str((rpc)->crpc_dest), \
253 atomic_read(&(rpc)->crpc_refcount)); \
254 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
255 if (atomic_dec_and_test(&(rpc)->crpc_refcount)) \
256 srpc_destroy_client_rpc(rpc); \
259 #define srpc_event_pending(rpc) ((rpc)->crpc_bulkev.ev_fired == 0 || \
260 (rpc)->crpc_reqstev.ev_fired == 0 || \
261 (rpc)->crpc_replyev.ev_fired == 0)
263 /* CPU partition data of srpc service */
264 struct srpc_service_cd {
267 /** backref to service */
268 struct srpc_service *scd_svc;
271 /** free RPC descriptors */
272 struct list_head scd_rpc_free;
273 /** in-flight RPCs */
274 struct list_head scd_rpc_active;
275 /** workitem for posting buffer */
276 swi_workitem_t scd_buf_wi;
279 /** error code for scd_buf_wi */
281 /** timestamp for scd_buf_err */
282 unsigned long scd_buf_err_stamp;
283 /** total # request buffers */
285 /** # posted request buffers */
287 /** in progress of buffer posting */
289 /** allocate more buffers if scd_buf_nposted < scd_buf_low */
291 /** increase/decrease some buffers */
293 /** posted message buffers */
294 struct list_head scd_buf_posted;
295 /** blocked for RPC descriptor */
296 struct list_head scd_buf_blocked;
299 /* number of server workitems (mini-thread) for testing service */
300 #define SFW_TEST_WI_MIN 256
301 #define SFW_TEST_WI_MAX 2048
302 /* extra buffers for tolerating buggy peers, or unbalanced number
303 * of peers between partitions */
304 #define SFW_TEST_WI_EXTRA 64
306 /* number of server workitems (mini-thread) for framework service */
307 #define SFW_FRWK_WI_MIN 16
308 #define SFW_FRWK_WI_MAX 256
310 typedef struct srpc_service {
311 int sv_id; /* service id */
312 const char *sv_name; /* human readable name */
313 int sv_wi_total; /* total server workitems */
316 /* percpt data for srpc_service */
317 struct srpc_service_cd **sv_cpt_data;
318 /* Service callbacks:
319 * - sv_handler: process incoming RPC request
320 * - sv_bulk_ready: notify bulk data
322 int (*sv_handler) (srpc_server_rpc_t *);
323 int (*sv_bulk_ready) (srpc_server_rpc_t *, int);
327 /* chain on fw_zombie_sessions */
328 struct list_head sn_list;
329 lst_sid_t sn_id; /* unique identifier */
330 /* # seconds' inactivity to expire */
331 unsigned int sn_timeout;
333 unsigned int sn_features;
334 stt_timer_t sn_timer;
335 struct list_head sn_batches; /* list of batches */
336 char sn_name[LST_NAME_SIZE];
337 atomic_t sn_refcount;
338 atomic_t sn_brw_errors;
339 atomic_t sn_ping_errors;
340 cfs_time_t sn_started;
343 #define sfw_sid_equal(sid0, sid1) ((sid0).ses_nid == (sid1).ses_nid && \
344 (sid0).ses_stamp == (sid1).ses_stamp)
347 struct list_head bat_list; /* chain on sn_batches */
348 lst_bid_t bat_id; /* batch id */
349 int bat_error; /* error code of batch */
350 sfw_session_t *bat_session; /* batch's session */
351 atomic_t bat_nactive; /* # of active tests */
352 struct list_head bat_tests; /* test instances */
356 int (*tso_init)(struct sfw_test_instance *tsi); /* intialize test client */
357 void (*tso_fini)(struct sfw_test_instance *tsi); /* finalize test client */
358 int (*tso_prep_rpc)(struct sfw_test_unit *tsu,
359 lnet_process_id_t dest,
360 srpc_client_rpc_t **rpc); /* prep a tests rpc */
361 void (*tso_done_rpc)(struct sfw_test_unit *tsu,
362 srpc_client_rpc_t *rpc); /* done a test rpc */
363 } sfw_test_client_ops_t;
365 typedef struct sfw_test_instance {
366 struct list_head tsi_list; /* chain on batch */
367 int tsi_service; /* test type */
368 sfw_batch_t *tsi_batch; /* batch */
369 sfw_test_client_ops_t *tsi_ops; /* test client operations */
371 /* public parameter for all test units */
372 unsigned int tsi_is_client:1; /* is test client */
373 unsigned int tsi_stoptsu_onerr:1; /* stop tsu on error */
374 int tsi_concur; /* concurrency */
375 int tsi_loop; /* loop count */
377 /* status of test instance */
378 spinlock_t tsi_lock; /* serialize */
379 unsigned int tsi_stopping:1; /* test is stopping */
380 atomic_t tsi_nactive; /* # of active test unit */
381 struct list_head tsi_units; /* test units */
382 struct list_head tsi_free_rpcs; /* free rpcs */
383 struct list_head tsi_active_rpcs;/* active rpcs */
386 test_ping_req_t ping; /* ping parameter */
387 test_bulk_req_t bulk_v0; /* bulk parameter */
388 test_bulk_req_v1_t bulk_v1; /* bulk v1 parameter */
390 } sfw_test_instance_t;
392 /* XXX: trailing (PAGE_CACHE_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 (PAGE_CACHE_SIZE / sizeof(lnet_process_id_packed_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 */
408 typedef struct sfw_test_case {
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 */
415 sfw_create_rpc(lnet_process_id_t peer, int service,
416 unsigned features, int nbulkiov, int bulklen,
417 void (*done) (srpc_client_rpc_t *), void *priv);
418 int sfw_create_test_rpc(sfw_test_unit_t *tsu,
419 lnet_process_id_t peer, unsigned features,
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, struct page *pg, int i);
427 int sfw_alloc_pages(srpc_server_rpc_t *rpc, int cpt, int npages, int len,
429 int sfw_make_session (srpc_mksn_reqst_t *request, srpc_mksn_reply_t *reply);
432 srpc_create_client_rpc(lnet_process_id_t peer, int service,
433 int nbulkiov, int bulklen,
434 void (*rpc_done)(srpc_client_rpc_t *),
435 void (*rpc_fini)(srpc_client_rpc_t *), void *priv);
436 void srpc_post_rpc(srpc_client_rpc_t *rpc);
437 void srpc_abort_rpc(srpc_client_rpc_t *rpc, int why);
438 void srpc_free_bulk(srpc_bulk_t *bk);
439 srpc_bulk_t *srpc_alloc_bulk(int cpt, unsigned bulk_npg, unsigned bulk_len,
441 int srpc_send_rpc(swi_workitem_t *wi);
442 int srpc_send_reply(srpc_server_rpc_t *rpc);
443 int srpc_add_service(srpc_service_t *sv);
444 int srpc_remove_service(srpc_service_t *sv);
445 void srpc_shutdown_service(srpc_service_t *sv);
446 void srpc_abort_service(srpc_service_t *sv);
447 int srpc_finish_service(srpc_service_t *sv);
448 int srpc_service_add_buffers(srpc_service_t *sv, int nbuffer);
449 void srpc_service_remove_buffers(srpc_service_t *sv, int nbuffer);
450 void srpc_get_counters(srpc_counters_t *cnt);
451 void srpc_set_counters(const srpc_counters_t *cnt);
453 extern struct cfs_wi_sched *lst_sched_serial;
454 extern struct cfs_wi_sched **lst_sched_test;
457 srpc_serv_is_framework(struct srpc_service *svc)
459 return svc->sv_id < SRPC_FRAMEWORK_SERVICE_MAX_ID;
463 swi_wi_action(cfs_workitem_t *wi)
465 swi_workitem_t *swi = container_of(wi, swi_workitem_t, swi_workitem);
467 return swi->swi_action(swi);
471 swi_init_workitem(swi_workitem_t *swi, void *data,
472 swi_action_t action, struct cfs_wi_sched *sched)
474 swi->swi_sched = sched;
475 swi->swi_action = action;
476 swi->swi_state = SWI_STATE_NEWBORN;
477 cfs_wi_init(&swi->swi_workitem, data, swi_wi_action);
481 swi_schedule_workitem(swi_workitem_t *wi)
483 cfs_wi_schedule(wi->swi_sched, &wi->swi_workitem);
487 swi_exit_workitem(swi_workitem_t *swi)
489 cfs_wi_exit(swi->swi_sched, &swi->swi_workitem);
493 swi_deschedule_workitem(swi_workitem_t *swi)
495 return cfs_wi_deschedule(swi->swi_sched, &swi->swi_workitem);
498 int sfw_startup(void);
499 int srpc_startup(void);
500 void sfw_shutdown(void);
501 void srpc_shutdown(void);
504 srpc_destroy_client_rpc (srpc_client_rpc_t *rpc)
506 LASSERT (rpc != NULL);
507 LASSERT (!srpc_event_pending(rpc));
508 LASSERT (atomic_read(&rpc->crpc_refcount) == 0);
510 if (rpc->crpc_fini == NULL) {
511 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
513 (*rpc->crpc_fini) (rpc);
520 srpc_init_client_rpc(srpc_client_rpc_t *rpc, lnet_process_id_t peer,
521 int service, int nbulkiov, int bulklen,
522 void (*rpc_done)(srpc_client_rpc_t *),
523 void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
525 LASSERT(nbulkiov <= LNET_MAX_IOV);
527 memset(rpc, 0, offsetof(srpc_client_rpc_t,
528 crpc_bulk.bk_iovs[nbulkiov]));
530 INIT_LIST_HEAD(&rpc->crpc_list);
531 swi_init_workitem(&rpc->crpc_wi, rpc, srpc_send_rpc,
532 lst_sched_test[lnet_cpt_of_nid(peer.nid)]);
533 spin_lock_init(&rpc->crpc_lock);
534 atomic_set(&rpc->crpc_refcount, 1); /* 1 ref for caller */
536 rpc->crpc_dest = peer;
537 rpc->crpc_priv = priv;
538 rpc->crpc_service = service;
539 rpc->crpc_bulk.bk_len = bulklen;
540 rpc->crpc_bulk.bk_niov = nbulkiov;
541 rpc->crpc_done = rpc_done;
542 rpc->crpc_fini = rpc_fini;
543 LNetInvalidateHandle(&rpc->crpc_reqstmdh);
544 LNetInvalidateHandle(&rpc->crpc_replymdh);
545 LNetInvalidateHandle(&rpc->crpc_bulk.bk_mdh);
547 /* no event is expected at this point */
548 rpc->crpc_bulkev.ev_fired =
549 rpc->crpc_reqstev.ev_fired =
550 rpc->crpc_replyev.ev_fired = 1;
552 rpc->crpc_reqstmsg.msg_magic = SRPC_MSG_MAGIC;
553 rpc->crpc_reqstmsg.msg_version = SRPC_MSG_VERSION;
554 rpc->crpc_reqstmsg.msg_type = srpc_service2request(service);
558 static inline const char *
559 swi_state2str (int state)
561 #define STATE2STR(x) case x: return #x
565 STATE2STR(SWI_STATE_NEWBORN);
566 STATE2STR(SWI_STATE_REPLY_SUBMITTED);
567 STATE2STR(SWI_STATE_REPLY_SENT);
568 STATE2STR(SWI_STATE_REQUEST_SUBMITTED);
569 STATE2STR(SWI_STATE_REQUEST_SENT);
570 STATE2STR(SWI_STATE_REPLY_RECEIVED);
571 STATE2STR(SWI_STATE_BULK_STARTED);
572 STATE2STR(SWI_STATE_DONE);
577 #define lst_wait_until(cond, lock, fmt, ...) \
581 CDEBUG(IS_PO2(++__I) ? D_WARNING : D_NET, \
582 fmt, ## __VA_ARGS__); \
583 spin_unlock(&(lock)); \
585 set_current_state(TASK_UNINTERRUPTIBLE); \
586 schedule_timeout(cfs_time_seconds(1) / 10); \
588 spin_lock(&(lock)); \
593 srpc_wait_service_shutdown(srpc_service_t *sv)
597 LASSERT(sv->sv_shuttingdown);
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 set_current_state(TASK_UNINTERRUPTIBLE);
605 schedule_timeout(cfs_time_seconds(1) / 10);
609 extern sfw_test_client_ops_t ping_test_client;
610 extern srpc_service_t ping_test_service;
611 void ping_init_test_client(void);
612 void ping_init_test_service(void);
614 extern sfw_test_client_ops_t brw_test_client;
615 extern srpc_service_t brw_test_service;
616 void brw_init_test_client(void);
617 void brw_init_test_service(void);
619 #endif /* __SELFTEST_SELFTEST_H__ */