Whamcloud - gitweb
LU-6142 llite: Fix style issues for llite_mmap.c
[fs/lustre-release.git] / lnet / selftest / selftest.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lnet/selftest/selftest.h
32  *
33  * Author: Isaac Huang <isaac@clusterfs.com>
34  */
35 #ifndef __SELFTEST_SELFTEST_H__
36 #define __SELFTEST_SELFTEST_H__
37
38 #define LNET_ONLY
39
40 #include <libcfs/libcfs.h>
41 #include <lnet/api.h>
42 #include <lnet/lib-lnet.h>
43 #include <lnet/lib-types.h>
44 #include <uapi/linux/lnet/lnetst.h>
45 #include <linux/refcount.h>
46
47 #include "rpc.h"
48 #include "timer.h"
49
50 #ifndef MADE_WITHOUT_COMPROMISE
51 #define MADE_WITHOUT_COMPROMISE
52 #endif
53
54 /* enum lnet_selftest_session_attrs   - LNet selftest session Netlink
55  *                                      attributes
56  *
57  *  @LNET_SELFTEST_SESSION_UNSPEC:      unspecified attribute to catch errors
58  *  @LNET_SELFTEST_SESSION_PAD:         padding for 64-bit attributes, ignore
59  *
60  *  @LENT_SELFTEST_SESSION_HDR:         Netlink group this data is for
61  *                                      (NLA_NUL_STRING)
62  *  @LNET_SELFTEST_SESSION_NAME:        name of this session (NLA_STRING)
63  *  @LNET_SELFTEST_SESSION_KEY:         key used to represent the session
64  *                                      (NLA_U32)
65  *  @LNET_SELFTEST_SESSION_TIMESTAMP:   timestamp when the session was created
66  *                                      (NLA_S64)
67  *  @LNET_SELFTEST_SESSION_NID:         NID of the node selftest ran on
68  *                                      (NLA_STRING)
69  *  @LNET_SELFTEST_SESSION_NODE_COUNT:  Number of nodes in use (NLA_U16)
70  */
71 enum lnet_selftest_session_attrs {
72         LNET_SELFTEST_SESSION_UNSPEC = 0,
73         LNET_SELFTEST_SESSION_PAD = LNET_SELFTEST_SESSION_UNSPEC,
74
75         LNET_SELFTEST_SESSION_HDR,
76         LNET_SELFTEST_SESSION_NAME,
77         LNET_SELFTEST_SESSION_KEY,
78         LNET_SELFTEST_SESSION_TIMESTAMP,
79         LNET_SELFTEST_SESSION_NID,
80         LNET_SELFTEST_SESSION_NODE_COUNT,
81
82         __LNET_SELFTEST_SESSION_MAX_PLUS_ONE,
83 };
84
85 #define LNET_SELFTEST_SESSION_MAX       (__LNET_SELFTEST_SESSION_MAX_PLUS_ONE - 1)
86
87 /* enum lnet_selftest_group_attrs     - LNet selftest group Netlink attributes
88  *
89  *  @LNET_SELFTEST_GROUP_ATTR_UNSPEC:   unspecified attribute to catch errors
90  *
91  *  @LENT_SELFTEST_GROUP_ATTR_HDR:      Netlink group this data is for
92  *                                      (NLA_NUL_STRING)
93  *  @LNET_SELFTEST_GROUP_ATTR_NAME:     name of this group (NLA_STRING)
94  *  @LNET_SELFTEST_GROUP_ATTR_NODELIST: List of nodes belonging to the group
95  *                                      (NLA_NESTED)
96  */
97 enum lnet_selftest_group_attrs {
98         LNET_SELFTEST_GROUP_ATTR_UNSPEC = 0,
99
100         LNET_SELFTEST_GROUP_ATTR_HDR,
101         LNET_SELFTEST_GROUP_ATTR_NAME,
102         LNET_SELFTEST_GROUP_ATTR_NODELIST,
103
104         __LNET_SELFTEST_GROUP_MAX_PLUS_ONE,
105 };
106
107 #define LNET_SELFTEST_GROUP_MAX         (__LNET_SELFTEST_GROUP_MAX_PLUS_ONE - 1)
108
109 /* enum lnet_selftest_group_nodelist_prop_attrs       - Netlink attributes for
110  *                                                      the properties of the
111  *                                                      nodes that belong to a
112  *                                                      group
113  *
114  *  @LNET_SELFTEST_GROUP_NODELIST_PROP_ATTR_UNSPEC:     unspecified attribute
115  *                                                      to catch errors
116  *
117  *  @LENT_SELFTEST_GROUP_NODELIST_PROP_ATTR_NID:        Nodes's NID (NLA_STRING)
118  *  @LNET_SELFTEST_GROUP_NODELIST_PROP_ATTR_STATUS:     Status of the node
119  *                                                      (NLA_STRING)
120  */
121 enum lnet_selftest_group_nodelist_prop_attrs {
122         LNET_SELFTEST_GROUP_NODELIST_PROP_ATTR_UNSPEC = 0,
123
124         LNET_SELFTEST_GROUP_NODELIST_PROP_ATTR_NID,
125         LNET_SELFTEST_GROUP_NODELIST_PROP_ATTR_STATUS,
126         __LNET_SELFTEST_GROUP_NODELIST_PROP_MAX_PLUS_ONE,
127 };
128
129 #define LNET_SELFTEST_GROUP_NODELIST_PROP_MAX   (__LNET_SELFTEST_GROUP_NODELIST_PROP_MAX_PLUS_ONE - 1)
130
131 enum lsr_swi_state {
132         SWI_STATE_DONE = 0,
133         SWI_STATE_NEWBORN,
134         SWI_STATE_REPLY_SUBMITTED,
135         SWI_STATE_REPLY_SENT,
136         SWI_STATE_REQUEST_SUBMITTED,
137         SWI_STATE_REQUEST_SENT,
138         SWI_STATE_REPLY_RECEIVED,
139         SWI_STATE_BULK_STARTED,
140         SWI_STATE_RUNNING,
141         SWI_STATE_PAUSE,
142 };
143
144 /* forward refs */
145 struct srpc_service;
146 struct srpc_service_cd;
147 struct sfw_test_unit;
148 struct sfw_test_instance;
149
150 /* services below SRPC_FRAMEWORK_SERVICE_MAX_ID are framework
151  * services, e.g. create/modify session.
152  */
153 #define SRPC_SERVICE_DEBUG              0
154 #define SRPC_SERVICE_MAKE_SESSION       1
155 #define SRPC_SERVICE_REMOVE_SESSION     2
156 #define SRPC_SERVICE_BATCH              3
157 #define SRPC_SERVICE_TEST               4
158 #define SRPC_SERVICE_QUERY_STAT         5
159 #define SRPC_SERVICE_JOIN               6
160 #define SRPC_FRAMEWORK_SERVICE_MAX_ID   10
161 /* other services start from SRPC_FRAMEWORK_SERVICE_MAX_ID+1 */
162 #define SRPC_SERVICE_BRW                11
163 #define SRPC_SERVICE_PING               12
164 #define SRPC_SERVICE_MAX_ID             12
165
166 #define SRPC_REQUEST_PORTAL             50
167 /* a lazy portal for framework RPC requests */
168 #define SRPC_FRAMEWORK_REQUEST_PORTAL   51
169 /* all reply/bulk RDMAs go to this portal */
170 #define SRPC_RDMA_PORTAL                52
171
172 static inline enum srpc_msg_type
173 srpc_service2request(int service)
174 {
175         switch (service) {
176         default:
177                 LBUG();
178         case SRPC_SERVICE_DEBUG:
179                 return SRPC_MSG_DEBUG_REQST;
180
181         case SRPC_SERVICE_MAKE_SESSION:
182                 return SRPC_MSG_MKSN_REQST;
183
184         case SRPC_SERVICE_REMOVE_SESSION:
185                 return SRPC_MSG_RMSN_REQST;
186
187         case SRPC_SERVICE_BATCH:
188                 return SRPC_MSG_BATCH_REQST;
189
190         case SRPC_SERVICE_TEST:
191                 return SRPC_MSG_TEST_REQST;
192
193         case SRPC_SERVICE_QUERY_STAT:
194                 return SRPC_MSG_STAT_REQST;
195
196         case SRPC_SERVICE_BRW:
197                 return SRPC_MSG_BRW_REQST;
198
199         case SRPC_SERVICE_PING:
200                 return SRPC_MSG_PING_REQST;
201
202         case SRPC_SERVICE_JOIN:
203                 return SRPC_MSG_JOIN_REQST;
204         }
205 }
206
207 static inline enum srpc_msg_type
208 srpc_service2reply(int service)
209 {
210         return srpc_service2request(service) + 1;
211 }
212
213 enum srpc_event_type {
214         SRPC_BULK_REQ_RCVD   = 1, /* passive bulk request(PUT sink/GET source) received */
215         SRPC_BULK_PUT_SENT   = 2, /* active bulk PUT sent (source) */
216         SRPC_BULK_GET_RPLD   = 3, /* active bulk GET replied (sink) */
217         SRPC_REPLY_RCVD      = 4, /* incoming reply received */
218         SRPC_REPLY_SENT      = 5, /* outgoing reply sent */
219         SRPC_REQUEST_RCVD    = 6, /* incoming request received */
220         SRPC_REQUEST_SENT    = 7, /* outgoing request sent */
221 };
222
223 /* RPC event */
224 struct srpc_event {
225         enum srpc_event_type    ev_type;   /* what's up */
226         enum lnet_event_kind    ev_lnet;   /* LNet event type */
227         int               ev_fired;  /* LNet event fired? */
228         int               ev_status; /* LNet event status */
229         void             *ev_data;   /* owning server/client RPC */
230 };
231
232 /* bulk descriptor */
233 struct srpc_bulk {
234         int                     bk_len;  /* len of bulk data */
235         struct lnet_handle_md   bk_mdh;
236         int                     bk_sink; /* sink/source */
237         int                     bk_alloc; /* # allocated iov */
238         int                     bk_niov; /* # iov in bk_iovs */
239         struct bio_vec          bk_iovs[0];
240 };
241
242 /* message buffer descriptor */
243 struct srpc_buffer {
244         struct list_head        buf_list; /* chain on srpc_service::*_msgq */
245         struct srpc_msg         buf_msg;
246         struct lnet_handle_md   buf_mdh;
247         lnet_nid_t              buf_self;
248         struct lnet_process_id  buf_peer;
249 };
250
251 struct swi_workitem;
252 typedef void (*swi_action_t)(struct swi_workitem *);
253
254 struct swi_workitem {
255         struct workqueue_struct *swi_wq;
256         struct work_struct      swi_work;
257         swi_action_t            swi_action;
258         enum lsr_swi_state      swi_state;
259 };
260
261 /* server-side state of a RPC */
262 struct srpc_server_rpc {
263         /* chain on srpc_service::*_rpcq */
264         struct list_head        srpc_list;
265         struct srpc_service_cd *srpc_scd;
266         struct swi_workitem     srpc_wi;
267         struct srpc_event       srpc_ev;        /* bulk/reply event */
268         lnet_nid_t              srpc_self;
269         struct lnet_process_id  srpc_peer;
270         struct srpc_msg         srpc_replymsg;
271         struct lnet_handle_md   srpc_replymdh;
272         struct srpc_buffer     *srpc_reqstbuf;
273         struct srpc_bulk       *srpc_bulk;
274
275         unsigned int    srpc_aborted; /* being given up */
276         int             srpc_status;
277         void            (*srpc_done)(struct srpc_server_rpc *);
278 };
279
280 /* client-side state of a RPC */
281 struct srpc_client_rpc {
282         struct list_head        crpc_list;      /* chain on user's lists */
283         spinlock_t              crpc_lock;      /* serialize */
284         int                     crpc_service;
285         atomic_t                crpc_refcount;
286         /* # seconds to wait for reply */
287         int                     crpc_timeout;
288         struct stt_timer        crpc_timer;
289         struct swi_workitem     crpc_wi;
290         struct lnet_process_id  crpc_dest;
291
292         void               (*crpc_done)(struct srpc_client_rpc *);
293         void               (*crpc_fini)(struct srpc_client_rpc *);
294         int                  crpc_status;    /* completion status */
295         void                *crpc_priv;      /* caller data */
296
297         /* state flags */
298         unsigned int         crpc_aborted:1; /* being given up */
299         unsigned int         crpc_closed:1;  /* completed */
300
301         /* RPC events */
302         struct srpc_event       crpc_bulkev;    /* bulk event */
303         struct srpc_event       crpc_reqstev;   /* request event */
304         struct srpc_event       crpc_replyev;   /* reply event */
305
306         /* bulk, request(reqst), and reply exchanged on wire */
307         struct srpc_msg         crpc_reqstmsg;
308         struct srpc_msg         crpc_replymsg;
309         struct lnet_handle_md   crpc_reqstmdh;
310         struct lnet_handle_md   crpc_replymdh;
311         struct srpc_bulk        crpc_bulk;
312 };
313
314 #define srpc_client_rpc_size(rpc)                                       \
315 offsetof(struct srpc_client_rpc, crpc_bulk.bk_iovs[(rpc)->crpc_bulk.bk_niov])
316
317 #define srpc_client_rpc_addref(rpc)                                     \
318 do {                                                                    \
319         CDEBUG(D_NET, "RPC[%p] -> %s (%d)++\n",                         \
320                (rpc), libcfs_id2str((rpc)->crpc_dest),                  \
321                atomic_read(&(rpc)->crpc_refcount));                     \
322         LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0);                \
323         atomic_inc(&(rpc)->crpc_refcount);                              \
324 } while (0)
325
326 #define srpc_client_rpc_decref(rpc)                                     \
327 do {                                                                    \
328         CDEBUG(D_NET, "RPC[%p] -> %s (%d)--\n",                         \
329                (rpc), libcfs_id2str((rpc)->crpc_dest),                  \
330                atomic_read(&(rpc)->crpc_refcount));                     \
331         LASSERT(atomic_read(&(rpc)->crpc_refcount) > 0);                \
332         if (atomic_dec_and_test(&(rpc)->crpc_refcount))                 \
333                 srpc_destroy_client_rpc(rpc);                           \
334 } while (0)
335
336 #define srpc_event_pending(rpc)   ((rpc)->crpc_bulkev.ev_fired == 0 ||  \
337                                    (rpc)->crpc_reqstev.ev_fired == 0 || \
338                                    (rpc)->crpc_replyev.ev_fired == 0)
339
340 /* CPU partition data of srpc service */
341 struct srpc_service_cd {
342         /** serialize */
343         spinlock_t              scd_lock;
344         /** backref to service */
345         struct srpc_service     *scd_svc;
346         /** event buffer */
347         struct srpc_event       scd_ev;
348         /** free RPC descriptors */
349         struct list_head        scd_rpc_free;
350         /** in-flight RPCs */
351         struct list_head        scd_rpc_active;
352         /** workitem for posting buffer */
353         struct swi_workitem     scd_buf_wi;
354         /** CPT id */
355         int                     scd_cpt;
356         /** error code for scd_buf_wi */
357         int                     scd_buf_err;
358         /** timestamp for scd_buf_err */
359         time64_t                scd_buf_err_stamp;
360         /** total # request buffers */
361         int                     scd_buf_total;
362         /** # posted request buffers */
363         int                     scd_buf_nposted;
364         /** in progress of buffer posting */
365         int                     scd_buf_posting;
366         /** allocate more buffers if scd_buf_nposted < scd_buf_low */
367         int                     scd_buf_low;
368         /** increase/decrease some buffers */
369         int                     scd_buf_adjust;
370         /** posted message buffers */
371         struct list_head        scd_buf_posted;
372         /** blocked for RPC descriptor */
373         struct list_head        scd_buf_blocked;
374 };
375
376 /* number of server workitems (mini-thread) for testing service */
377 #define SFW_TEST_WI_MIN         256
378 #define SFW_TEST_WI_MAX         2048
379 /* extra buffers for tolerating buggy peers, or unbalanced number
380  * of peers between partitions  */
381 #define SFW_TEST_WI_EXTRA       64
382
383 /* number of server workitems (mini-thread) for framework service */
384 #define SFW_FRWK_WI_MIN         16
385 #define SFW_FRWK_WI_MAX         256
386
387 struct srpc_service {
388         int                     sv_id;          /* service id */
389         const char              *sv_name;       /* human readable name */
390         int                     sv_wi_total;    /* total server workitems */
391         int                     sv_shuttingdown;
392         int                     sv_ncpts;
393         /* percpt data for srpc_service */
394         struct srpc_service_cd  **sv_cpt_data;
395         /* Service callbacks:
396          * - sv_handler: process incoming RPC request
397          * - sv_bulk_ready: notify bulk data
398          */
399         int              (*sv_handler)(struct srpc_server_rpc *);
400         int              (*sv_bulk_ready)(struct srpc_server_rpc *, int);
401
402         /** Service side srpc constructor/destructor.
403          *  used for the bulk preallocation as usual.
404          */
405         int              (*sv_srpc_init)(struct srpc_server_rpc *, int);
406         void             (*sv_srpc_fini)(struct srpc_server_rpc *);
407 };
408
409 struct lst_session_id {
410         s64                     ses_stamp;      /* time stamp in milliseconds */
411         struct lnet_nid         ses_nid;        /* nid of console node */
412 };                                              /*** session id (large addr) */
413
414 extern struct lst_session_id LST_INVALID_SID;
415
416 struct sfw_session {
417         /* chain on fw_zombie_sessions */
418         struct list_head        sn_list;
419         struct lst_session_id   sn_id;          /* unique identifier */
420         /* # seconds' inactivity to expire */
421         unsigned int            sn_timeout;
422         int                     sn_timer_active;
423         unsigned int            sn_features;
424         struct stt_timer        sn_timer;
425         struct list_head        sn_batches;     /* list of batches */
426         char                    sn_name[LST_NAME_SIZE];
427         refcount_t              sn_refcount;
428         atomic_t                sn_brw_errors;
429         atomic_t                sn_ping_errors;
430         ktime_t                 sn_started;
431 };
432
433 static inline int sfw_sid_equal(struct lst_sid sid0,
434                                 struct lst_session_id sid1)
435 {
436         struct lnet_nid ses_nid;
437
438         lnet_nid4_to_nid(sid0.ses_nid, &ses_nid);
439
440         return ((sid0.ses_stamp == sid1.ses_stamp) &&
441                 nid_same(&ses_nid, &sid1.ses_nid));
442 }
443
444 struct sfw_batch {
445         struct list_head        bat_list;       /* chain on sn_batches */
446         struct lst_bid          bat_id;         /* batch id */
447         int                     bat_error;      /* error code of batch */
448         struct sfw_session      *bat_session;   /* batch's session */
449         atomic_t                bat_nactive;    /* # of active tests */
450         struct list_head        bat_tests;      /* test instances */
451 };
452
453 struct sfw_test_client_ops {
454         int  (*tso_init)(struct sfw_test_instance *tsi); /* intailize test client */
455         void (*tso_fini)(struct sfw_test_instance *tsi); /* finalize test client */
456         int  (*tso_prep_rpc)(struct sfw_test_unit *tsu,
457                              struct lnet_process_id dest,
458                              struct srpc_client_rpc **rpc); /* prep a tests rpc */
459         void (*tso_done_rpc)(struct sfw_test_unit *tsu,
460                              struct srpc_client_rpc *rpc);  /* done a test rpc */
461 };
462
463 struct sfw_test_instance {
464         struct list_head        tsi_list; /* chain on batch */
465         int                     tsi_service; /* test type */
466         struct sfw_batch        *tsi_batch; /* batch */
467         struct sfw_test_client_ops      *tsi_ops; /* test client operations */
468
469         /* public parameter for all test units */
470         unsigned int            tsi_is_client:1;     /* is test client */
471         unsigned int            tsi_stoptsu_onerr:1; /* stop tsu on error */
472         int                     tsi_concur;          /* concurrency */
473         int                     tsi_loop;            /* loop count */
474
475         /* status of test instance */
476         spinlock_t              tsi_lock;       /* serialize */
477         unsigned int            tsi_stopping:1; /* test is stopping */
478         atomic_t                tsi_nactive;    /* # of active test unit */
479         struct list_head        tsi_units;      /* test units */
480         struct list_head        tsi_free_rpcs;  /* free rpcs */
481         struct list_head        tsi_active_rpcs;/* active rpcs */
482
483         union {
484                 struct test_ping_req    ping;     /* ping parameter */
485                 struct test_bulk_req    bulk_v0;  /* bulk parameter */
486                 struct test_bulk_req_v1 bulk_v1;  /* bulk v1 parameter */
487         } tsi_u;
488 };
489
490 /* XXX: trailing (PAGE_SIZE % sizeof(struct lnet_process_id)) bytes at
491  * the end of pages are not used */
492 #define SFW_MAX_CONCUR     LST_MAX_CONCUR
493 #define SFW_ID_PER_PAGE    (PAGE_SIZE / sizeof(struct lnet_process_id_packed))
494 #define SFW_MAX_NDESTS     (LNET_MAX_IOV * SFW_ID_PER_PAGE)
495 #define sfw_id_pages(n)    (((n) + SFW_ID_PER_PAGE - 1) / SFW_ID_PER_PAGE)
496
497 struct sfw_test_unit {
498         struct list_head        tsu_list;       /* chain on lst_test_instance */
499         struct lnet_process_id  tsu_dest;       /* id of dest node */
500         int                     tsu_loop;       /* loop count of the test */
501         struct sfw_test_instance *tsu_instance; /* pointer to test instance */
502         void                    *tsu_private;   /* private data */
503         struct swi_workitem      tsu_worker;    /* workitem of the test unit */
504 };
505
506 struct sfw_test_case {
507         struct list_head                tsc_list; /* chain on fw_tests */
508         struct srpc_service             *tsc_srv_service; /* test service */
509         struct sfw_test_client_ops      *tsc_cli_ops; /* ops of test client */
510 };
511
512 struct srpc_client_rpc *
513 sfw_create_rpc(struct lnet_process_id peer, int service,
514                unsigned int features, int nbulkiov, int bulklen,
515                void (*done)(struct srpc_client_rpc *), void *priv);
516 int sfw_create_test_rpc(struct sfw_test_unit *tsu,
517                         struct lnet_process_id peer, unsigned int features,
518                         int nblk, int blklen, struct srpc_client_rpc **rpc);
519 void sfw_abort_rpc(struct srpc_client_rpc *rpc);
520 void sfw_post_rpc(struct srpc_client_rpc *rpc);
521 void sfw_client_rpc_done(struct srpc_client_rpc *rpc);
522 void sfw_unpack_message(struct srpc_msg *msg);
523 void sfw_add_bulk_page(struct srpc_bulk *bk, struct page *pg, int i);
524 int sfw_alloc_pages(struct srpc_server_rpc *rpc, int cpt, int len,
525                     int sink);
526 int sfw_make_session(struct srpc_mksn_reqst *request,
527                      struct srpc_mksn_reply *reply);
528
529 struct srpc_client_rpc *
530 srpc_create_client_rpc(struct lnet_process_id peer, int service,
531                        int nbulkiov, int bulklen,
532                        void (*rpc_done)(struct srpc_client_rpc *),
533                        void (*rpc_fini)(struct srpc_client_rpc *), void *priv);
534 void srpc_post_rpc(struct srpc_client_rpc *rpc);
535 void srpc_abort_rpc(struct srpc_client_rpc *rpc, int why);
536 void srpc_free_bulk(struct srpc_bulk *bk);
537
538 struct srpc_bulk *srpc_alloc_bulk(int cpt, unsigned int bulk_len);
539 void srpc_init_bulk(struct srpc_bulk *bk, unsigned int off,
540                     unsigned int bulk_len, int sink);
541
542 void srpc_send_rpc(struct swi_workitem *wi);
543 int srpc_send_reply(struct srpc_server_rpc *rpc);
544 int srpc_add_service(struct srpc_service *sv);
545 int srpc_remove_service(struct srpc_service *sv);
546 void srpc_shutdown_service(struct srpc_service *sv);
547 void srpc_abort_service(struct srpc_service *sv);
548 int srpc_finish_service(struct srpc_service *sv);
549 int srpc_service_add_buffers(struct srpc_service *sv, int nbuffer);
550 void srpc_service_remove_buffers(struct srpc_service *sv, int nbuffer);
551 void srpc_get_counters(struct srpc_counters *cnt);
552
553 extern struct workqueue_struct *lst_serial_wq;
554 extern struct workqueue_struct **lst_test_wq;
555
556 static inline int
557 srpc_serv_is_framework(struct srpc_service *svc)
558 {
559         return svc->sv_id < SRPC_FRAMEWORK_SERVICE_MAX_ID;
560 }
561
562 static void
563 swi_wi_action(struct work_struct *wi)
564 {
565         struct swi_workitem *swi;
566
567         swi = container_of(wi, struct swi_workitem, swi_work);
568         swi->swi_action(swi);
569 }
570
571 static inline void
572 swi_init_workitem(struct swi_workitem *swi,
573                   swi_action_t action, struct workqueue_struct *wq)
574 {
575         swi->swi_wq = wq;
576         swi->swi_action = action;
577         swi->swi_state  = SWI_STATE_NEWBORN;
578         INIT_WORK(&swi->swi_work, swi_wi_action);
579 }
580
581 static inline void
582 swi_schedule_workitem(struct swi_workitem *wi)
583 {
584         queue_work(wi->swi_wq, &wi->swi_work);
585 }
586
587 static inline int
588 swi_cancel_workitem(struct swi_workitem *swi)
589 {
590         swi->swi_state = SWI_STATE_DONE;
591         return cancel_work_sync(&swi->swi_work);
592 }
593
594 int sfw_startup(void);
595 int srpc_startup(void);
596 void sfw_shutdown(void);
597 void srpc_shutdown(void);
598
599 static inline void
600 srpc_destroy_client_rpc(struct srpc_client_rpc *rpc)
601 {
602         LASSERT(rpc != NULL);
603         LASSERT(!srpc_event_pending(rpc));
604         LASSERT(atomic_read(&rpc->crpc_refcount) == 0);
605
606         if (rpc->crpc_fini == NULL)
607                 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
608         else
609                 (*rpc->crpc_fini) (rpc);
610 }
611
612 static inline void
613 srpc_init_client_rpc(struct srpc_client_rpc *rpc, struct lnet_process_id peer,
614                      int service, int nbulkiov, int bulklen,
615                      void (*rpc_done)(struct srpc_client_rpc *),
616                      void (*rpc_fini)(struct srpc_client_rpc *), void *priv)
617 {
618         LASSERT(nbulkiov <= LNET_MAX_IOV);
619
620         memset(rpc, 0, offsetof(struct srpc_client_rpc,
621                                 crpc_bulk.bk_iovs[nbulkiov]));
622
623         INIT_LIST_HEAD(&rpc->crpc_list);
624         swi_init_workitem(&rpc->crpc_wi, srpc_send_rpc,
625                           lst_test_wq[lnet_cpt_of_nid(peer.nid, NULL)]);
626         spin_lock_init(&rpc->crpc_lock);
627         atomic_set(&rpc->crpc_refcount, 1); /* 1 ref for caller */
628
629         rpc->crpc_dest         = peer;
630         rpc->crpc_priv         = priv;
631         rpc->crpc_service      = service;
632         rpc->crpc_bulk.bk_len  = bulklen;
633         rpc->crpc_bulk.bk_niov = nbulkiov;
634         rpc->crpc_done         = rpc_done;
635         rpc->crpc_fini         = rpc_fini;
636         LNetInvalidateMDHandle(&rpc->crpc_reqstmdh);
637         LNetInvalidateMDHandle(&rpc->crpc_replymdh);
638         LNetInvalidateMDHandle(&rpc->crpc_bulk.bk_mdh);
639
640         /* no event is expected at this point */
641         rpc->crpc_bulkev.ev_fired  =
642         rpc->crpc_reqstev.ev_fired =
643         rpc->crpc_replyev.ev_fired = 1;
644
645         rpc->crpc_reqstmsg.msg_magic   = SRPC_MSG_MAGIC;
646         rpc->crpc_reqstmsg.msg_version = SRPC_MSG_VERSION;
647         rpc->crpc_reqstmsg.msg_type    = srpc_service2request(service);
648 }
649
650 static inline const char *
651 swi_state2str(int state)
652 {
653 #define STATE2STR(x) case x: return #x
654         switch (state) {
655         default:
656                 LBUG();
657         STATE2STR(SWI_STATE_NEWBORN);
658         STATE2STR(SWI_STATE_REPLY_SUBMITTED);
659         STATE2STR(SWI_STATE_REPLY_SENT);
660         STATE2STR(SWI_STATE_REQUEST_SUBMITTED);
661         STATE2STR(SWI_STATE_REQUEST_SENT);
662         STATE2STR(SWI_STATE_REPLY_RECEIVED);
663         STATE2STR(SWI_STATE_BULK_STARTED);
664         STATE2STR(SWI_STATE_DONE);
665         }
666 #undef STATE2STR
667 }
668
669 #define lst_wait_until(cond, lock, fmt, ...)                            \
670 do {                                                                    \
671         int __I = 2;                                                    \
672         while (!(cond)) {                                               \
673                 CDEBUG(is_power_of_2(++__I) ? D_WARNING : D_NET,        \
674                        fmt, ## __VA_ARGS__);                            \
675                 spin_unlock(&(lock));                                   \
676                                                                         \
677                 schedule_timeout_uninterruptible(                       \
678                         cfs_time_seconds(1) / 10);                      \
679                                                                         \
680                 spin_lock(&(lock));                                     \
681         }                                                               \
682 } while (0)
683
684 static inline void
685 srpc_wait_service_shutdown(struct srpc_service *sv)
686 {
687         int i = 2;
688
689         LASSERT(sv->sv_shuttingdown);
690
691         while (srpc_finish_service(sv) == 0) {
692                 i++;
693                 CDEBUG(((i & -i) == i) ? D_WARNING : D_NET,
694                        "Waiting for %s service to shutdown...\n",
695                        sv->sv_name);
696                 schedule_timeout_uninterruptible(cfs_time_seconds(1) / 10);
697         }
698 }
699
700 extern struct sfw_test_client_ops ping_test_client;
701 extern struct srpc_service ping_test_service;
702 void ping_init_test_client(void);
703 void ping_init_test_service(void);
704
705 extern struct sfw_test_client_ops brw_test_client;
706 extern struct srpc_service brw_test_service;
707 void brw_init_test_service(void);
708
709 #endif /* __SELFTEST_SELFTEST_H__ */