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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/selftest/selftest.h
34 * Author: Isaac Huang <isaac@clusterfs.com>
36 #ifndef __SELFTEST_SELFTEST_H__
37 #define __SELFTEST_SELFTEST_H__
41 #include <libcfs/libcfs.h>
43 #include <lnet/lib-lnet.h>
44 #include <lnet/lib-types.h>
45 #include <lnet/lnetst.h>
50 #ifndef MADE_WITHOUT_COMPROMISE
51 #define MADE_WITHOUT_COMPROMISE
55 #define SWI_STATE_NEWBORN 0
56 #define SWI_STATE_REPLY_SUBMITTED 1
57 #define SWI_STATE_REPLY_SENT 2
58 #define SWI_STATE_REQUEST_SUBMITTED 3
59 #define SWI_STATE_REQUEST_SENT 4
60 #define SWI_STATE_REPLY_RECEIVED 5
61 #define SWI_STATE_BULK_STARTED 6
62 #define SWI_STATE_DONE 10
66 struct srpc_service_cd;
68 struct sfw_test_instance;
70 /* services below SRPC_FRAMEWORK_SERVICE_MAX_ID are framework
71 * services, e.g. create/modify session.
73 #define SRPC_SERVICE_DEBUG 0
74 #define SRPC_SERVICE_MAKE_SESSION 1
75 #define SRPC_SERVICE_REMOVE_SESSION 2
76 #define SRPC_SERVICE_BATCH 3
77 #define SRPC_SERVICE_TEST 4
78 #define SRPC_SERVICE_QUERY_STAT 5
79 #define SRPC_SERVICE_JOIN 6
80 #define SRPC_FRAMEWORK_SERVICE_MAX_ID 10
81 /* other services start from SRPC_FRAMEWORK_SERVICE_MAX_ID+1 */
82 #define SRPC_SERVICE_BRW 11
83 #define SRPC_SERVICE_PING 12
84 #define SRPC_SERVICE_MAX_ID 12
86 #define SRPC_REQUEST_PORTAL 50
87 /* a lazy portal for framework RPC requests */
88 #define SRPC_FRAMEWORK_REQUEST_PORTAL 51
89 /* all reply/bulk RDMAs go to this portal */
90 #define SRPC_RDMA_PORTAL 52
92 static inline srpc_msg_type_t
93 srpc_service2request (int service)
98 case SRPC_SERVICE_DEBUG:
99 return SRPC_MSG_DEBUG_REQST;
101 case SRPC_SERVICE_MAKE_SESSION:
102 return SRPC_MSG_MKSN_REQST;
104 case SRPC_SERVICE_REMOVE_SESSION:
105 return SRPC_MSG_RMSN_REQST;
107 case SRPC_SERVICE_BATCH:
108 return SRPC_MSG_BATCH_REQST;
110 case SRPC_SERVICE_TEST:
111 return SRPC_MSG_TEST_REQST;
113 case SRPC_SERVICE_QUERY_STAT:
114 return SRPC_MSG_STAT_REQST;
116 case SRPC_SERVICE_BRW:
117 return SRPC_MSG_BRW_REQST;
119 case SRPC_SERVICE_PING:
120 return SRPC_MSG_PING_REQST;
122 case SRPC_SERVICE_JOIN:
123 return SRPC_MSG_JOIN_REQST;
127 static inline srpc_msg_type_t
128 srpc_service2reply (int service)
130 return srpc_service2request(service) + 1;
134 SRPC_BULK_REQ_RCVD = 1, /* passive bulk request(PUT sink/GET source) received */
135 SRPC_BULK_PUT_SENT = 2, /* active bulk PUT sent (source) */
136 SRPC_BULK_GET_RPLD = 3, /* active bulk GET replied (sink) */
137 SRPC_REPLY_RCVD = 4, /* incoming reply received */
138 SRPC_REPLY_SENT = 5, /* outgoing reply sent */
139 SRPC_REQUEST_RCVD = 6, /* incoming request received */
140 SRPC_REQUEST_SENT = 7, /* outgoing request sent */
145 srpc_event_type_t ev_type; /* what's up */
146 enum lnet_event_kind ev_lnet; /* LNet event type */
147 int ev_fired; /* LNet event fired? */
148 int ev_status; /* LNet event status */
149 void *ev_data; /* owning server/client RPC */
153 int bk_len; /* len of bulk data */
154 struct lnet_handle_md bk_mdh;
155 int bk_sink; /* sink/source */
156 int bk_niov; /* # iov in bk_iovs */
157 lnet_kiov_t bk_iovs[0];
158 } srpc_bulk_t; /* bulk descriptor */
160 /* message buffer descriptor */
161 typedef struct srpc_buffer {
162 struct list_head buf_list; /* chain on srpc_service::*_msgq */
164 struct lnet_handle_md buf_mdh;
166 struct lnet_process_id buf_peer;
170 typedef int (*swi_action_t) (struct swi_workitem *);
172 typedef struct swi_workitem {
173 struct cfs_wi_sched *swi_sched;
174 struct cfs_workitem swi_workitem;
175 swi_action_t swi_action;
179 /* server-side state of a RPC */
180 typedef struct srpc_server_rpc {
181 /* chain on srpc_service::*_rpcq */
182 struct list_head srpc_list;
183 struct srpc_service_cd *srpc_scd;
184 swi_workitem_t srpc_wi;
185 srpc_event_t srpc_ev; /* bulk/reply event */
186 lnet_nid_t srpc_self;
187 struct lnet_process_id srpc_peer;
188 srpc_msg_t srpc_replymsg;
189 struct lnet_handle_md srpc_replymdh;
190 srpc_buffer_t *srpc_reqstbuf;
191 srpc_bulk_t *srpc_bulk;
193 unsigned int srpc_aborted; /* being given up */
195 void (*srpc_done)(struct srpc_server_rpc *);
198 /* client-side state of a RPC */
199 typedef struct srpc_client_rpc {
200 struct list_head crpc_list; /* chain on user's lists */
201 spinlock_t crpc_lock; /* serialize */
203 atomic_t crpc_refcount;
204 /* # seconds to wait for reply */
206 stt_timer_t crpc_timer;
207 swi_workitem_t crpc_wi;
208 struct lnet_process_id crpc_dest;
210 void (*crpc_done)(struct srpc_client_rpc *);
211 void (*crpc_fini)(struct srpc_client_rpc *);
212 int crpc_status; /* completion status */
213 void *crpc_priv; /* caller data */
216 unsigned int crpc_aborted:1; /* being given up */
217 unsigned int crpc_closed:1; /* completed */
220 srpc_event_t crpc_bulkev; /* bulk event */
221 srpc_event_t crpc_reqstev; /* request event */
222 srpc_event_t crpc_replyev; /* reply event */
224 /* bulk, request(reqst), and reply exchanged on wire */
225 srpc_msg_t crpc_reqstmsg;
226 srpc_msg_t crpc_replymsg;
227 struct lnet_handle_md crpc_reqstmdh;
228 struct lnet_handle_md crpc_replymdh;
229 srpc_bulk_t crpc_bulk;
232 #define srpc_client_rpc_size(rpc) \
233 offsetof(srpc_client_rpc_t, crpc_bulk.bk_iovs[(rpc)->crpc_bulk.bk_niov])
235 #define srpc_client_rpc_addref(rpc) \
237 CDEBUG(D_NET, "RPC[%p] -> %s (%d)++\n", \
238 (rpc), libcfs_id2str((rpc)->crpc_dest), \
239 atomic_read(&(rpc)->crpc_refcount)); \
240 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
241 atomic_inc(&(rpc)->crpc_refcount); \
244 #define srpc_client_rpc_decref(rpc) \
246 CDEBUG(D_NET, "RPC[%p] -> %s (%d)--\n", \
247 (rpc), libcfs_id2str((rpc)->crpc_dest), \
248 atomic_read(&(rpc)->crpc_refcount)); \
249 LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0); \
250 if (atomic_dec_and_test(&(rpc)->crpc_refcount)) \
251 srpc_destroy_client_rpc(rpc); \
254 #define srpc_event_pending(rpc) ((rpc)->crpc_bulkev.ev_fired == 0 || \
255 (rpc)->crpc_reqstev.ev_fired == 0 || \
256 (rpc)->crpc_replyev.ev_fired == 0)
258 /* CPU partition data of srpc service */
259 struct srpc_service_cd {
262 /** backref to service */
263 struct srpc_service *scd_svc;
266 /** free RPC descriptors */
267 struct list_head scd_rpc_free;
268 /** in-flight RPCs */
269 struct list_head scd_rpc_active;
270 /** workitem for posting buffer */
271 swi_workitem_t scd_buf_wi;
274 /** error code for scd_buf_wi */
276 /** timestamp for scd_buf_err */
277 time64_t scd_buf_err_stamp;
278 /** total # request buffers */
280 /** # posted request buffers */
282 /** in progress of buffer posting */
284 /** allocate more buffers if scd_buf_nposted < scd_buf_low */
286 /** increase/decrease some buffers */
288 /** posted message buffers */
289 struct list_head scd_buf_posted;
290 /** blocked for RPC descriptor */
291 struct list_head scd_buf_blocked;
294 /* number of server workitems (mini-thread) for testing service */
295 #define SFW_TEST_WI_MIN 256
296 #define SFW_TEST_WI_MAX 2048
297 /* extra buffers for tolerating buggy peers, or unbalanced number
298 * of peers between partitions */
299 #define SFW_TEST_WI_EXTRA 64
301 /* number of server workitems (mini-thread) for framework service */
302 #define SFW_FRWK_WI_MIN 16
303 #define SFW_FRWK_WI_MAX 256
305 typedef struct srpc_service {
306 int sv_id; /* service id */
307 const char *sv_name; /* human readable name */
308 int sv_wi_total; /* total server workitems */
311 /* percpt data for srpc_service */
312 struct srpc_service_cd **sv_cpt_data;
313 /* Service callbacks:
314 * - sv_handler: process incoming RPC request
315 * - sv_bulk_ready: notify bulk data
317 int (*sv_handler) (srpc_server_rpc_t *);
318 int (*sv_bulk_ready) (srpc_server_rpc_t *, int);
322 /* chain on fw_zombie_sessions */
323 struct list_head sn_list;
324 struct lst_sid sn_id; /* unique identifier */
325 /* # seconds' inactivity to expire */
326 unsigned int sn_timeout;
328 unsigned int sn_features;
329 stt_timer_t sn_timer;
330 struct list_head sn_batches; /* list of batches */
331 char sn_name[LST_NAME_SIZE];
332 atomic_t sn_refcount;
333 atomic_t sn_brw_errors;
334 atomic_t sn_ping_errors;
335 cfs_time_t sn_started;
338 #define sfw_sid_equal(sid0, sid1) ((sid0).ses_nid == (sid1).ses_nid && \
339 (sid0).ses_stamp == (sid1).ses_stamp)
342 struct list_head bat_list; /* chain on sn_batches */
343 struct lst_bid bat_id; /* batch id */
344 int bat_error; /* error code of batch */
345 sfw_session_t *bat_session; /* batch's session */
346 atomic_t bat_nactive; /* # of active tests */
347 struct list_head bat_tests; /* test instances */
351 int (*tso_init)(struct sfw_test_instance *tsi); /* intialize test client */
352 void (*tso_fini)(struct sfw_test_instance *tsi); /* finalize test client */
353 int (*tso_prep_rpc)(struct sfw_test_unit *tsu,
354 struct lnet_process_id dest,
355 srpc_client_rpc_t **rpc); /* prep a tests rpc */
356 void (*tso_done_rpc)(struct sfw_test_unit *tsu,
357 srpc_client_rpc_t *rpc); /* done a test rpc */
358 } sfw_test_client_ops_t;
360 typedef struct sfw_test_instance {
361 struct list_head tsi_list; /* chain on batch */
362 int tsi_service; /* test type */
363 sfw_batch_t *tsi_batch; /* batch */
364 sfw_test_client_ops_t *tsi_ops; /* test client operations */
366 /* public parameter for all test units */
367 unsigned int tsi_is_client:1; /* is test client */
368 unsigned int tsi_stoptsu_onerr:1; /* stop tsu on error */
369 int tsi_concur; /* concurrency */
370 int tsi_loop; /* loop count */
372 /* status of test instance */
373 spinlock_t tsi_lock; /* serialize */
374 unsigned int tsi_stopping:1; /* test is stopping */
375 atomic_t tsi_nactive; /* # of active test unit */
376 struct list_head tsi_units; /* test units */
377 struct list_head tsi_free_rpcs; /* free rpcs */
378 struct list_head tsi_active_rpcs;/* active rpcs */
381 test_ping_req_t ping; /* ping parameter */
382 test_bulk_req_t bulk_v0; /* bulk parameter */
383 test_bulk_req_v1_t bulk_v1; /* bulk v1 parameter */
385 } sfw_test_instance_t;
387 /* XXX: trailing (PAGE_SIZE % sizeof(struct lnet_process_id)) bytes at
388 * the end of pages are not used */
389 #define SFW_MAX_CONCUR LST_MAX_CONCUR
390 #define SFW_ID_PER_PAGE (PAGE_SIZE / sizeof(struct lnet_process_id_packed))
391 #define SFW_MAX_NDESTS (LNET_MAX_IOV * SFW_ID_PER_PAGE)
392 #define sfw_id_pages(n) (((n) + SFW_ID_PER_PAGE - 1) / SFW_ID_PER_PAGE)
394 typedef struct sfw_test_unit {
395 struct list_head tsu_list; /* chain on lst_test_instance */
396 struct lnet_process_id tsu_dest; /* id of dest node */
397 int tsu_loop; /* loop count of the test */
398 sfw_test_instance_t *tsu_instance; /* pointer to test instance */
399 void *tsu_private; /* private data */
400 swi_workitem_t tsu_worker; /* workitem of the test unit */
403 typedef struct sfw_test_case {
404 struct list_head tsc_list; /* chain on fw_tests */
405 srpc_service_t *tsc_srv_service; /* test service */
406 sfw_test_client_ops_t *tsc_cli_ops; /* ops of test client */
410 sfw_create_rpc(struct lnet_process_id peer, int service,
411 unsigned features, int nbulkiov, int bulklen,
412 void (*done) (srpc_client_rpc_t *), void *priv);
413 int sfw_create_test_rpc(sfw_test_unit_t *tsu,
414 struct lnet_process_id peer, unsigned int features,
415 int nblk, int blklen, srpc_client_rpc_t **rpc);
416 void sfw_abort_rpc(srpc_client_rpc_t *rpc);
417 void sfw_post_rpc(srpc_client_rpc_t *rpc);
418 void sfw_client_rpc_done(srpc_client_rpc_t *rpc);
419 void sfw_unpack_message(srpc_msg_t *msg);
420 void sfw_free_pages(srpc_server_rpc_t *rpc);
421 void sfw_add_bulk_page(srpc_bulk_t *bk, struct page *pg, int i);
422 int sfw_alloc_pages(srpc_server_rpc_t *rpc, int cpt, int npages, int len,
424 int sfw_make_session (srpc_mksn_reqst_t *request, srpc_mksn_reply_t *reply);
427 srpc_create_client_rpc(struct lnet_process_id peer, int service,
428 int nbulkiov, int bulklen,
429 void (*rpc_done)(srpc_client_rpc_t *),
430 void (*rpc_fini)(srpc_client_rpc_t *), void *priv);
431 void srpc_post_rpc(srpc_client_rpc_t *rpc);
432 void srpc_abort_rpc(srpc_client_rpc_t *rpc, int why);
433 void srpc_free_bulk(srpc_bulk_t *bk);
434 srpc_bulk_t *srpc_alloc_bulk(int cpt, unsigned off, unsigned bulk_npg,
435 unsigned bulk_len, int sink);
436 int srpc_send_rpc(swi_workitem_t *wi);
437 int srpc_send_reply(srpc_server_rpc_t *rpc);
438 int srpc_add_service(srpc_service_t *sv);
439 int srpc_remove_service(srpc_service_t *sv);
440 void srpc_shutdown_service(srpc_service_t *sv);
441 void srpc_abort_service(srpc_service_t *sv);
442 int srpc_finish_service(srpc_service_t *sv);
443 int srpc_service_add_buffers(srpc_service_t *sv, int nbuffer);
444 void srpc_service_remove_buffers(srpc_service_t *sv, int nbuffer);
445 void srpc_get_counters(struct srpc_counters *cnt);
446 void srpc_set_counters(const struct srpc_counters *cnt);
448 extern struct cfs_wi_sched *lst_sched_serial;
449 extern struct cfs_wi_sched **lst_sched_test;
452 srpc_serv_is_framework(struct srpc_service *svc)
454 return svc->sv_id < SRPC_FRAMEWORK_SERVICE_MAX_ID;
458 swi_wi_action(struct cfs_workitem *wi)
460 swi_workitem_t *swi = container_of(wi, swi_workitem_t, swi_workitem);
462 return swi->swi_action(swi);
466 swi_init_workitem(swi_workitem_t *swi, void *data,
467 swi_action_t action, struct cfs_wi_sched *sched)
469 swi->swi_sched = sched;
470 swi->swi_action = action;
471 swi->swi_state = SWI_STATE_NEWBORN;
472 cfs_wi_init(&swi->swi_workitem, data, swi_wi_action);
476 swi_schedule_workitem(swi_workitem_t *wi)
478 cfs_wi_schedule(wi->swi_sched, &wi->swi_workitem);
482 swi_exit_workitem(swi_workitem_t *swi)
484 cfs_wi_exit(swi->swi_sched, &swi->swi_workitem);
488 swi_deschedule_workitem(swi_workitem_t *swi)
490 return cfs_wi_deschedule(swi->swi_sched, &swi->swi_workitem);
493 int sfw_startup(void);
494 int srpc_startup(void);
495 void sfw_shutdown(void);
496 void srpc_shutdown(void);
499 srpc_destroy_client_rpc (srpc_client_rpc_t *rpc)
501 LASSERT (rpc != NULL);
502 LASSERT (!srpc_event_pending(rpc));
503 LASSERT (atomic_read(&rpc->crpc_refcount) == 0);
505 if (rpc->crpc_fini == NULL) {
506 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
508 (*rpc->crpc_fini) (rpc);
515 srpc_init_client_rpc(srpc_client_rpc_t *rpc, struct lnet_process_id peer,
516 int service, int nbulkiov, int bulklen,
517 void (*rpc_done)(srpc_client_rpc_t *),
518 void (*rpc_fini)(srpc_client_rpc_t *), void *priv)
520 LASSERT(nbulkiov <= LNET_MAX_IOV);
522 memset(rpc, 0, offsetof(srpc_client_rpc_t,
523 crpc_bulk.bk_iovs[nbulkiov]));
525 INIT_LIST_HEAD(&rpc->crpc_list);
526 swi_init_workitem(&rpc->crpc_wi, rpc, srpc_send_rpc,
527 lst_sched_test[lnet_cpt_of_nid(peer.nid, NULL)]);
528 spin_lock_init(&rpc->crpc_lock);
529 atomic_set(&rpc->crpc_refcount, 1); /* 1 ref for caller */
531 rpc->crpc_dest = peer;
532 rpc->crpc_priv = priv;
533 rpc->crpc_service = service;
534 rpc->crpc_bulk.bk_len = bulklen;
535 rpc->crpc_bulk.bk_niov = nbulkiov;
536 rpc->crpc_done = rpc_done;
537 rpc->crpc_fini = rpc_fini;
538 LNetInvalidateMDHandle(&rpc->crpc_reqstmdh);
539 LNetInvalidateMDHandle(&rpc->crpc_replymdh);
540 LNetInvalidateMDHandle(&rpc->crpc_bulk.bk_mdh);
542 /* no event is expected at this point */
543 rpc->crpc_bulkev.ev_fired =
544 rpc->crpc_reqstev.ev_fired =
545 rpc->crpc_replyev.ev_fired = 1;
547 rpc->crpc_reqstmsg.msg_magic = SRPC_MSG_MAGIC;
548 rpc->crpc_reqstmsg.msg_version = SRPC_MSG_VERSION;
549 rpc->crpc_reqstmsg.msg_type = srpc_service2request(service);
553 static inline const char *
554 swi_state2str (int state)
556 #define STATE2STR(x) case x: return #x
560 STATE2STR(SWI_STATE_NEWBORN);
561 STATE2STR(SWI_STATE_REPLY_SUBMITTED);
562 STATE2STR(SWI_STATE_REPLY_SENT);
563 STATE2STR(SWI_STATE_REQUEST_SUBMITTED);
564 STATE2STR(SWI_STATE_REQUEST_SENT);
565 STATE2STR(SWI_STATE_REPLY_RECEIVED);
566 STATE2STR(SWI_STATE_BULK_STARTED);
567 STATE2STR(SWI_STATE_DONE);
572 #define lst_wait_until(cond, lock, fmt, ...) \
576 CDEBUG(is_power_of_2(++__I) ? D_WARNING : D_NET, \
577 fmt, ## __VA_ARGS__); \
578 spin_unlock(&(lock)); \
580 set_current_state(TASK_UNINTERRUPTIBLE); \
581 schedule_timeout(cfs_time_seconds(1) / 10); \
583 spin_lock(&(lock)); \
588 srpc_wait_service_shutdown(srpc_service_t *sv)
592 LASSERT(sv->sv_shuttingdown);
594 while (srpc_finish_service(sv) == 0) {
596 CDEBUG(((i & -i) == i) ? D_WARNING : D_NET,
597 "Waiting for %s service to shutdown...\n",
599 set_current_state(TASK_UNINTERRUPTIBLE);
600 schedule_timeout(cfs_time_seconds(1) / 10);
604 extern sfw_test_client_ops_t ping_test_client;
605 extern srpc_service_t ping_test_service;
606 void ping_init_test_client(void);
607 void ping_init_test_service(void);
609 extern sfw_test_client_ops_t brw_test_client;
610 extern srpc_service_t brw_test_service;
611 void brw_init_test_client(void);
612 void brw_init_test_service(void);
614 #endif /* __SELFTEST_SELFTEST_H__ */