Whamcloud - gitweb
LU-812 kernel: remove smp_lock.h
[fs/lustre-release.git] / lnet / selftest / framework.c
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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lnet/selftest/framework.c
35  *
36  * Author: Isaac Huang <isaac@clusterfs.com>
37  * Author: Liang Zhen  <liangzhen@clusterfs.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_LNET
41
42 #include "selftest.h"
43
44 lst_sid_t LST_INVALID_SID = {LNET_NID_ANY, -1};
45
46 static int session_timeout = 100;
47 CFS_MODULE_PARM(session_timeout, "i", int, 0444,
48                 "test session timeout in seconds (100 by default, 0 == never)");
49
50 static int rpc_timeout = 64;
51 CFS_MODULE_PARM(rpc_timeout, "i", int, 0644,
52                 "rpc timeout in seconds (64 by default, 0 == never)");
53
54 #define sfw_unpack_id(id)               \
55 do {                                    \
56         __swab64s(&(id).nid);           \
57         __swab32s(&(id).pid);           \
58 } while (0)
59
60 #define sfw_unpack_sid(sid)             \
61 do {                                    \
62         __swab64s(&(sid).ses_nid);      \
63         __swab64s(&(sid).ses_stamp);    \
64 } while (0)
65
66 #define sfw_unpack_fw_counters(fc)        \
67 do {                                      \
68         __swab32s(&(fc).running_ms);      \
69         __swab32s(&(fc).active_batches);  \
70         __swab32s(&(fc).zombie_sessions); \
71         __swab32s(&(fc).brw_errors);      \
72         __swab32s(&(fc).ping_errors);     \
73 } while (0)
74
75 #define sfw_unpack_rpc_counters(rc)     \
76 do {                                    \
77         __swab32s(&(rc).errors);        \
78         __swab32s(&(rc).rpcs_sent);     \
79         __swab32s(&(rc).rpcs_rcvd);     \
80         __swab32s(&(rc).rpcs_dropped);  \
81         __swab32s(&(rc).rpcs_expired);  \
82         __swab64s(&(rc).bulk_get);      \
83         __swab64s(&(rc).bulk_put);      \
84 } while (0)
85
86 #define sfw_unpack_lnet_counters(lc)    \
87 do {                                    \
88         __swab32s(&(lc).errors);        \
89         __swab32s(&(lc).msgs_max);      \
90         __swab32s(&(lc).msgs_alloc);    \
91         __swab32s(&(lc).send_count);    \
92         __swab32s(&(lc).recv_count);    \
93         __swab32s(&(lc).drop_count);    \
94         __swab32s(&(lc).route_count);   \
95         __swab64s(&(lc).send_length);   \
96         __swab64s(&(lc).recv_length);   \
97         __swab64s(&(lc).drop_length);   \
98         __swab64s(&(lc).route_length);  \
99 } while (0)
100
101 #define sfw_test_active(t)      (cfs_atomic_read(&(t)->tsi_nactive) != 0)
102 #define sfw_batch_active(b)     (cfs_atomic_read(&(b)->bat_nactive) != 0)
103
104 struct smoketest_framework {
105         cfs_list_t         fw_zombie_rpcs;     /* RPCs to be recycled */
106         cfs_list_t         fw_zombie_sessions; /* stopping sessions */
107         cfs_list_t         fw_tests;           /* registered test cases */
108         cfs_atomic_t       fw_nzombies;        /* # zombie sessions */
109         cfs_spinlock_t     fw_lock;            /* serialise */
110         sfw_session_t     *fw_session;         /* _the_ session */
111         int                fw_shuttingdown;    /* shutdown in progress */
112         srpc_server_rpc_t *fw_active_srpc;     /* running RPC */
113 } sfw_data;
114
115 /* forward ref's */
116 int sfw_stop_batch (sfw_batch_t *tsb, int force);
117 void sfw_destroy_session (sfw_session_t *sn);
118
119 static inline sfw_test_case_t *
120 sfw_find_test_case(int id)
121 {
122         sfw_test_case_t *tsc;
123
124         LASSERT (id <= SRPC_SERVICE_MAX_ID);
125         LASSERT (id > SRPC_FRAMEWORK_SERVICE_MAX_ID);
126
127         cfs_list_for_each_entry_typed (tsc, &sfw_data.fw_tests,
128                                        sfw_test_case_t, tsc_list) {
129                 if (tsc->tsc_srv_service->sv_id == id)
130                         return tsc;
131         }
132
133         return NULL;
134 }
135
136 static int
137 sfw_register_test (srpc_service_t *service, sfw_test_client_ops_t *cliops)
138 {
139         sfw_test_case_t *tsc;
140
141         if (sfw_find_test_case(service->sv_id) != NULL) {
142                 CERROR ("Failed to register test %s (%d)\n",
143                         service->sv_name, service->sv_id);
144                 return -EEXIST;
145         }
146
147         LIBCFS_ALLOC(tsc, sizeof(sfw_test_case_t));
148         if (tsc == NULL)
149                 return -ENOMEM;
150
151         memset(tsc, 0, sizeof(sfw_test_case_t));
152         tsc->tsc_cli_ops     = cliops;
153         tsc->tsc_srv_service = service;
154
155         cfs_list_add_tail(&tsc->tsc_list, &sfw_data.fw_tests);
156         return 0;
157 }
158
159 void
160 sfw_add_session_timer (void)
161 {
162         sfw_session_t *sn = sfw_data.fw_session;
163         stt_timer_t   *timer = &sn->sn_timer;
164
165         LASSERT (!sfw_data.fw_shuttingdown);
166
167         if (sn == NULL || sn->sn_timeout == 0)
168                 return;
169
170         LASSERT (!sn->sn_timer_active);
171
172         sn->sn_timer_active = 1;
173         timer->stt_expires = cfs_time_add(sn->sn_timeout,
174                                           cfs_time_current_sec());
175         stt_add_timer(timer);
176         return;
177 }
178
179 int
180 sfw_del_session_timer (void)
181 {
182         sfw_session_t *sn = sfw_data.fw_session;
183
184         if (sn == NULL || !sn->sn_timer_active)
185                 return 0;
186
187         LASSERT (sn->sn_timeout != 0);
188
189         if (stt_del_timer(&sn->sn_timer)) { /* timer defused */
190                 sn->sn_timer_active = 0;
191                 return 0;
192         }
193
194 #ifndef __KERNEL__
195         /* Racing is impossible in single-threaded userland selftest */
196         LBUG();
197 #endif
198         return EBUSY; /* racing with sfw_session_expired() */
199 }
200
201 /* called with sfw_data.fw_lock held */
202 static void
203 sfw_deactivate_session (void)
204 {
205         sfw_session_t *sn = sfw_data.fw_session;
206         int            nactive = 0;
207         sfw_batch_t   *tsb;
208         sfw_test_case_t *tsc;
209
210         if (sn == NULL) return;
211
212         LASSERT (!sn->sn_timer_active);
213
214         sfw_data.fw_session = NULL;
215         cfs_atomic_inc(&sfw_data.fw_nzombies);
216         cfs_list_add(&sn->sn_list, &sfw_data.fw_zombie_sessions);
217
218         cfs_spin_unlock(&sfw_data.fw_lock);
219
220         cfs_list_for_each_entry_typed (tsc, &sfw_data.fw_tests,
221                                        sfw_test_case_t, tsc_list) {
222                 srpc_abort_service(tsc->tsc_srv_service);
223         }
224
225         cfs_spin_lock(&sfw_data.fw_lock);
226
227         cfs_list_for_each_entry_typed (tsb, &sn->sn_batches,
228                                        sfw_batch_t, bat_list) {
229                 if (sfw_batch_active(tsb)) {
230                         nactive++;
231                         sfw_stop_batch(tsb, 1);
232                 }
233         }
234
235         if (nactive != 0)
236                 return;   /* wait for active batches to stop */
237
238         cfs_list_del_init(&sn->sn_list);
239         cfs_spin_unlock(&sfw_data.fw_lock);
240
241         sfw_destroy_session(sn);
242
243         cfs_spin_lock(&sfw_data.fw_lock);
244         return;
245 }
246
247 #ifndef __KERNEL__
248
249 int
250 sfw_session_removed (void)
251 {
252         return (sfw_data.fw_session == NULL) ? 1 : 0;
253 }
254
255 #endif
256
257 void
258 sfw_session_expired (void *data)
259 {
260         sfw_session_t *sn = data;
261
262         cfs_spin_lock(&sfw_data.fw_lock);
263
264         LASSERT (sn->sn_timer_active);
265         LASSERT (sn == sfw_data.fw_session);
266
267         CWARN ("Session expired! sid: %s-"LPU64", name: %s\n",
268                libcfs_nid2str(sn->sn_id.ses_nid),
269                sn->sn_id.ses_stamp, &sn->sn_name[0]);
270
271         sn->sn_timer_active = 0;
272         sfw_deactivate_session();
273
274         cfs_spin_unlock(&sfw_data.fw_lock);
275         return;
276 }
277
278 static inline void
279 sfw_init_session(sfw_session_t *sn, lst_sid_t sid,
280                  unsigned features, const char *name)
281 {
282         stt_timer_t *timer = &sn->sn_timer;
283
284         memset(sn, 0, sizeof(sfw_session_t));
285         CFS_INIT_LIST_HEAD(&sn->sn_list);
286         CFS_INIT_LIST_HEAD(&sn->sn_batches);
287         cfs_atomic_set(&sn->sn_refcount, 1);        /* +1 for caller */
288         cfs_atomic_set(&sn->sn_brw_errors, 0);
289         cfs_atomic_set(&sn->sn_ping_errors, 0);
290         strncpy(&sn->sn_name[0], name, LST_NAME_SIZE);
291
292         sn->sn_timer_active = 0;
293         sn->sn_id           = sid;
294         sn->sn_features     = features;
295         sn->sn_timeout      = session_timeout;
296         sn->sn_started      = cfs_time_current();
297
298         timer->stt_data = sn;
299         timer->stt_func = sfw_session_expired;
300         CFS_INIT_LIST_HEAD(&timer->stt_list);
301 }
302
303 /* completion handler for incoming framework RPCs */
304 void
305 sfw_server_rpc_done(struct srpc_server_rpc *rpc)
306 {
307         struct srpc_service     *sv     = rpc->srpc_scd->scd_svc;
308         int                     status  = rpc->srpc_status;
309
310         CDEBUG (D_NET,
311                 "Incoming framework RPC done: "
312                 "service %s, peer %s, status %s:%d\n",
313                 sv->sv_name, libcfs_id2str(rpc->srpc_peer),
314                 swi_state2str(rpc->srpc_wi.swi_state),
315                 status);
316
317         if (rpc->srpc_bulk != NULL)
318                 sfw_free_pages(rpc);
319         return;
320 }
321
322 void
323 sfw_client_rpc_fini (srpc_client_rpc_t *rpc)
324 {
325         LASSERT (rpc->crpc_bulk.bk_niov == 0);
326         LASSERT (cfs_list_empty(&rpc->crpc_list));
327         LASSERT (cfs_atomic_read(&rpc->crpc_refcount) == 0);
328 #ifndef __KERNEL__
329         LASSERT (rpc->crpc_bulk.bk_pages == NULL);
330 #endif
331
332         CDEBUG (D_NET,
333                 "Outgoing framework RPC done: "
334                 "service %d, peer %s, status %s:%d:%d\n",
335                 rpc->crpc_service, libcfs_id2str(rpc->crpc_dest),
336                 swi_state2str(rpc->crpc_wi.swi_state),
337                 rpc->crpc_aborted, rpc->crpc_status);
338
339         cfs_spin_lock(&sfw_data.fw_lock);
340
341         /* my callers must finish all RPCs before shutting me down */
342         LASSERT (!sfw_data.fw_shuttingdown);
343         cfs_list_add(&rpc->crpc_list, &sfw_data.fw_zombie_rpcs);
344
345         cfs_spin_unlock(&sfw_data.fw_lock);
346         return;
347 }
348
349 sfw_batch_t *
350 sfw_find_batch (lst_bid_t bid)
351 {
352         sfw_session_t *sn = sfw_data.fw_session;
353         sfw_batch_t   *bat;
354
355         LASSERT (sn != NULL);
356
357         cfs_list_for_each_entry_typed (bat, &sn->sn_batches,
358                                        sfw_batch_t, bat_list) {
359                 if (bat->bat_id.bat_id == bid.bat_id)
360                         return bat;
361         }
362
363         return NULL;
364 }
365
366 sfw_batch_t *
367 sfw_bid2batch (lst_bid_t bid)
368 {
369         sfw_session_t *sn = sfw_data.fw_session;
370         sfw_batch_t   *bat;
371
372         LASSERT (sn != NULL);
373
374         bat = sfw_find_batch(bid);
375         if (bat != NULL)
376                 return bat;
377
378         LIBCFS_ALLOC(bat, sizeof(sfw_batch_t));
379         if (bat == NULL) 
380                 return NULL;
381
382         bat->bat_error    = 0;
383         bat->bat_session  = sn;
384         bat->bat_id       = bid;
385         cfs_atomic_set(&bat->bat_nactive, 0);
386         CFS_INIT_LIST_HEAD(&bat->bat_tests);
387
388         cfs_list_add_tail(&bat->bat_list, &sn->sn_batches);
389         return bat;
390 }
391
392 int
393 sfw_get_stats (srpc_stat_reqst_t *request, srpc_stat_reply_t *reply)
394 {
395         sfw_session_t  *sn = sfw_data.fw_session;
396         sfw_counters_t *cnt = &reply->str_fw;
397         sfw_batch_t    *bat;
398         struct timeval  tv;
399
400         reply->str_sid = (sn == NULL) ? LST_INVALID_SID : sn->sn_id;
401
402         if (request->str_sid.ses_nid == LNET_NID_ANY) {
403                 reply->str_status = EINVAL;
404                 return 0;
405         }
406
407         if (sn == NULL || !sfw_sid_equal(request->str_sid, sn->sn_id)) {
408                 reply->str_status = ESRCH;
409                 return 0;
410         }
411
412         lnet_counters_get(&reply->str_lnet);
413         srpc_get_counters(&reply->str_rpc);
414
415         /* send over the msecs since the session was started
416          - with 32 bits to send, this is ~49 days */
417         cfs_duration_usec(cfs_time_sub(cfs_time_current(),
418                                        sn->sn_started), &tv);
419
420         cnt->running_ms      = (__u32)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
421         cnt->brw_errors      = cfs_atomic_read(&sn->sn_brw_errors);
422         cnt->ping_errors     = cfs_atomic_read(&sn->sn_ping_errors);
423         cnt->zombie_sessions = cfs_atomic_read(&sfw_data.fw_nzombies);
424
425         cnt->active_batches = 0;
426         cfs_list_for_each_entry_typed (bat, &sn->sn_batches,
427                                        sfw_batch_t, bat_list) {
428                 if (cfs_atomic_read(&bat->bat_nactive) > 0)
429                         cnt->active_batches++;
430         }
431
432         reply->str_status = 0;
433         return 0;
434 }
435
436 int
437 sfw_make_session(srpc_mksn_reqst_t *request, srpc_mksn_reply_t *reply)
438 {
439         sfw_session_t *sn = sfw_data.fw_session;
440         srpc_msg_t    *msg = container_of(request, srpc_msg_t,
441                                           msg_body.mksn_reqst);
442
443         if (request->mksn_sid.ses_nid == LNET_NID_ANY) {
444                 reply->mksn_sid = (sn == NULL) ? LST_INVALID_SID : sn->sn_id;
445                 reply->mksn_status = EINVAL;
446                 return 0;
447         }
448
449         if (sn != NULL) {
450                 reply->mksn_status  = 0;
451                 reply->mksn_sid     = sn->sn_id;
452                 reply->mksn_timeout = sn->sn_timeout;
453
454                 if (sfw_sid_equal(request->mksn_sid, sn->sn_id)) {
455                         cfs_atomic_inc(&sn->sn_refcount);
456                         return 0;
457                 }
458
459                 if (!request->mksn_force) {
460                         reply->mksn_status = EBUSY;
461                         strncpy(&reply->mksn_name[0], &sn->sn_name[0], LST_NAME_SIZE);
462                         return 0;
463                 }
464         }
465
466         /* reject the request if it requires unknown features
467          * NB: old version will always accept all features because it's not
468          * aware of srpc_msg_t::msg_ses_feats, it's a defect but it's also
469          * harmless because it will return zero feature to console, and it's
470          * console's responsibility to make sure all nodes in a session have
471          * same feature mask. */
472         if ((msg->msg_ses_feats & ~LST_FEATS_MASK) != 0) {
473                 reply->mksn_status = EPROTO;
474                 return 0;
475         }
476
477         /* brand new or create by force */
478         LIBCFS_ALLOC(sn, sizeof(sfw_session_t));
479         if (sn == NULL) {
480                 CERROR ("Dropping RPC (mksn) under memory pressure.\n");
481                 return -ENOMEM;
482         }
483
484         sfw_init_session(sn, request->mksn_sid,
485                          msg->msg_ses_feats, &request->mksn_name[0]);
486
487         cfs_spin_lock(&sfw_data.fw_lock);
488
489         sfw_deactivate_session();
490         LASSERT (sfw_data.fw_session == NULL);
491         sfw_data.fw_session = sn;
492
493         cfs_spin_unlock(&sfw_data.fw_lock);
494
495         reply->mksn_status  = 0;
496         reply->mksn_sid     = sn->sn_id;
497         reply->mksn_timeout = sn->sn_timeout;
498         return 0;
499 }
500
501 int
502 sfw_remove_session (srpc_rmsn_reqst_t *request, srpc_rmsn_reply_t *reply)
503 {
504         sfw_session_t *sn = sfw_data.fw_session;
505
506         reply->rmsn_sid = (sn == NULL) ? LST_INVALID_SID : sn->sn_id;
507
508         if (request->rmsn_sid.ses_nid == LNET_NID_ANY) {
509                 reply->rmsn_status = EINVAL;
510                 return 0;
511         }
512
513         if (sn == NULL || !sfw_sid_equal(request->rmsn_sid, sn->sn_id)) {
514                 reply->rmsn_status = (sn == NULL) ? ESRCH : EBUSY;
515                 return 0;
516         }
517
518         if (!cfs_atomic_dec_and_test(&sn->sn_refcount)) {
519                 reply->rmsn_status = 0;
520                 return 0;
521         }
522
523         cfs_spin_lock(&sfw_data.fw_lock);
524         sfw_deactivate_session();
525         cfs_spin_unlock(&sfw_data.fw_lock);
526
527         reply->rmsn_status = 0;
528         reply->rmsn_sid    = LST_INVALID_SID;
529         LASSERT (sfw_data.fw_session == NULL);
530         return 0;
531 }
532
533 int
534 sfw_debug_session (srpc_debug_reqst_t *request, srpc_debug_reply_t *reply)
535 {
536         sfw_session_t *sn = sfw_data.fw_session;
537
538         if (sn == NULL) {
539                 reply->dbg_status = ESRCH;
540                 reply->dbg_sid    = LST_INVALID_SID;
541                 return 0;
542         } 
543
544         reply->dbg_status  = 0;
545         reply->dbg_sid     = sn->sn_id;      
546         reply->dbg_timeout = sn->sn_timeout;
547         strncpy(reply->dbg_name, &sn->sn_name[0], LST_NAME_SIZE);
548
549         return 0;
550 }
551
552 void
553 sfw_test_rpc_fini (srpc_client_rpc_t *rpc)
554 {
555         sfw_test_unit_t     *tsu = rpc->crpc_priv;
556         sfw_test_instance_t *tsi = tsu->tsu_instance;
557
558         /* Called with hold of tsi->tsi_lock */
559         LASSERT (cfs_list_empty(&rpc->crpc_list));
560         cfs_list_add(&rpc->crpc_list, &tsi->tsi_free_rpcs);
561 }
562
563 static inline int
564 sfw_test_buffers(sfw_test_instance_t *tsi)
565 {
566         struct sfw_test_case    *tsc = sfw_find_test_case(tsi->tsi_service);
567         struct srpc_service     *svc = tsc->tsc_srv_service;
568         int                     nbuf;
569
570         nbuf = min(svc->sv_wi_total, tsi->tsi_loop) / svc->sv_ncpts;
571         return max(SFW_TEST_WI_MIN, nbuf + SFW_TEST_WI_EXTRA);
572 }
573
574 int
575 sfw_load_test(struct sfw_test_instance *tsi)
576 {
577         struct sfw_test_case    *tsc = sfw_find_test_case(tsi->tsi_service);
578         struct srpc_service     *svc = tsc->tsc_srv_service;
579         int                     nbuf = sfw_test_buffers(tsi);
580         int                     rc;
581
582         LASSERT(tsc != NULL);
583
584         if (tsi->tsi_is_client) {
585                 tsi->tsi_ops = tsc->tsc_cli_ops;
586                 return 0;
587         }
588
589         rc = srpc_service_add_buffers(svc, nbuf);
590         if (rc != 0) {
591                 CWARN("Failed to reserve enough buffers: "
592                       "service %s, %d needed: %d\n", svc->sv_name, nbuf, rc);
593                 /* NB: this error handler is not strictly correct, because
594                  * it may release more buffers than already allocated,
595                  * but it doesn't matter because request portal should
596                  * be lazy portal and will grow buffers if necessary. */
597                 srpc_service_remove_buffers(svc, nbuf);
598                 return -ENOMEM;
599         }
600
601         CDEBUG(D_NET, "Reserved %d buffers for test %s\n",
602                nbuf * (srpc_serv_is_framework(svc) ?
603                        1 : cfs_cpt_number(cfs_cpt_table)), svc->sv_name);
604         return 0;
605 }
606
607 void
608 sfw_unload_test(struct sfw_test_instance *tsi)
609 {
610         struct sfw_test_case *tsc = sfw_find_test_case(tsi->tsi_service);
611
612         LASSERT(tsc != NULL);
613
614         if (tsi->tsi_is_client)
615                 return;
616
617         /* shrink buffers, because request portal is lazy portal
618          * which can grow buffers at runtime so we may leave
619          * some buffers behind, but never mind... */
620         srpc_service_remove_buffers(tsc->tsc_srv_service,
621                                     sfw_test_buffers(tsi));
622         return;
623 }
624
625 void
626 sfw_destroy_test_instance (sfw_test_instance_t *tsi)
627 {
628         srpc_client_rpc_t *rpc;
629         sfw_test_unit_t   *tsu;
630
631         if (!tsi->tsi_is_client) goto clean;
632
633         tsi->tsi_ops->tso_fini(tsi);
634
635         LASSERT (!tsi->tsi_stopping);
636         LASSERT (cfs_list_empty(&tsi->tsi_active_rpcs));
637         LASSERT (!sfw_test_active(tsi));
638
639         while (!cfs_list_empty(&tsi->tsi_units)) {
640                 tsu = cfs_list_entry(tsi->tsi_units.next,
641                                      sfw_test_unit_t, tsu_list);
642                 cfs_list_del(&tsu->tsu_list);
643                 LIBCFS_FREE(tsu, sizeof(*tsu));
644         }
645
646         while (!cfs_list_empty(&tsi->tsi_free_rpcs)) {
647                 rpc = cfs_list_entry(tsi->tsi_free_rpcs.next,
648                                      srpc_client_rpc_t, crpc_list);
649                 cfs_list_del(&rpc->crpc_list);
650                 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
651         }
652
653 clean:
654         sfw_unload_test(tsi);
655         LIBCFS_FREE(tsi, sizeof(*tsi));
656         return;
657 }
658
659 void
660 sfw_destroy_batch (sfw_batch_t *tsb)
661 {
662         sfw_test_instance_t *tsi;
663
664         LASSERT (!sfw_batch_active(tsb));
665         LASSERT (cfs_list_empty(&tsb->bat_list));
666
667         while (!cfs_list_empty(&tsb->bat_tests)) {
668                 tsi = cfs_list_entry(tsb->bat_tests.next,
669                                      sfw_test_instance_t, tsi_list);
670                 cfs_list_del_init(&tsi->tsi_list);
671                 sfw_destroy_test_instance(tsi);
672         }
673
674         LIBCFS_FREE(tsb, sizeof(sfw_batch_t));
675         return;
676 }
677
678 void
679 sfw_destroy_session (sfw_session_t *sn)
680 {
681         sfw_batch_t *batch;
682
683         LASSERT (cfs_list_empty(&sn->sn_list));
684         LASSERT (sn != sfw_data.fw_session);
685
686         while (!cfs_list_empty(&sn->sn_batches)) {
687                 batch = cfs_list_entry(sn->sn_batches.next,
688                                        sfw_batch_t, bat_list);
689                 cfs_list_del_init(&batch->bat_list);
690                 sfw_destroy_batch(batch);
691         }
692
693         LIBCFS_FREE(sn, sizeof(*sn));
694         cfs_atomic_dec(&sfw_data.fw_nzombies);
695         return;
696 }
697
698 void
699 sfw_unpack_addtest_req(srpc_msg_t *msg)
700 {
701         srpc_test_reqst_t *req = &msg->msg_body.tes_reqst;
702
703         LASSERT (msg->msg_type == SRPC_MSG_TEST_REQST);
704         LASSERT (req->tsr_is_client);
705
706         if (msg->msg_magic == SRPC_MSG_MAGIC)
707                 return; /* no flipping needed */
708
709         LASSERT (msg->msg_magic == __swab32(SRPC_MSG_MAGIC));
710
711         if (req->tsr_service == SRPC_SERVICE_BRW) {
712                 if ((msg->msg_ses_feats & LST_FEAT_BULK_LEN) == 0) {
713                         test_bulk_req_t *bulk = &req->tsr_u.bulk_v0;
714
715                         __swab32s(&bulk->blk_opc);
716                         __swab32s(&bulk->blk_npg);
717                         __swab32s(&bulk->blk_flags);
718
719                 } else {
720                         test_bulk_req_v1_t *bulk = &req->tsr_u.bulk_v1;
721
722                         __swab16s(&bulk->blk_opc);
723                         __swab16s(&bulk->blk_flags);
724                         __swab32s(&bulk->blk_offset);
725                         __swab32s(&bulk->blk_len);
726                 }
727
728                 return;
729         }
730
731         if (req->tsr_service == SRPC_SERVICE_PING) {
732                 test_ping_req_t *ping = &req->tsr_u.ping;
733
734                 __swab32s(&ping->png_size);
735                 __swab32s(&ping->png_flags);
736                 return;
737         }
738
739         LBUG ();
740         return;
741 }
742
743 int
744 sfw_add_test_instance (sfw_batch_t *tsb, srpc_server_rpc_t *rpc)
745 {
746         srpc_msg_t          *msg = &rpc->srpc_reqstbuf->buf_msg;
747         srpc_test_reqst_t   *req = &msg->msg_body.tes_reqst;
748         srpc_bulk_t         *bk = rpc->srpc_bulk;
749         int                  ndest = req->tsr_ndest;
750         sfw_test_unit_t     *tsu;
751         sfw_test_instance_t *tsi;
752         int                  i;
753         int                  rc;
754
755         LIBCFS_ALLOC(tsi, sizeof(*tsi));
756         if (tsi == NULL) {
757                 CERROR ("Can't allocate test instance for batch: "LPU64"\n",
758                         tsb->bat_id.bat_id);
759                 return -ENOMEM;
760         }
761
762         memset(tsi, 0, sizeof(*tsi));
763         cfs_spin_lock_init(&tsi->tsi_lock);
764         cfs_atomic_set(&tsi->tsi_nactive, 0);
765         CFS_INIT_LIST_HEAD(&tsi->tsi_units);
766         CFS_INIT_LIST_HEAD(&tsi->tsi_free_rpcs);
767         CFS_INIT_LIST_HEAD(&tsi->tsi_active_rpcs);
768
769         tsi->tsi_stopping      = 0;
770         tsi->tsi_batch         = tsb;
771         tsi->tsi_loop          = req->tsr_loop;
772         tsi->tsi_concur        = req->tsr_concur;
773         tsi->tsi_service       = req->tsr_service;
774         tsi->tsi_is_client     = !!(req->tsr_is_client);
775         tsi->tsi_stoptsu_onerr = !!(req->tsr_stop_onerr);
776
777         rc = sfw_load_test(tsi);
778         if (rc != 0) {
779                 LIBCFS_FREE(tsi, sizeof(*tsi));
780                 return rc;
781         }
782
783         LASSERT (!sfw_batch_active(tsb));
784
785         if (!tsi->tsi_is_client) {
786                 /* it's test server, just add it to tsb */
787                 cfs_list_add_tail(&tsi->tsi_list, &tsb->bat_tests);
788                 return 0;
789         }
790
791         LASSERT (bk != NULL);
792 #ifndef __KERNEL__
793         LASSERT (bk->bk_pages != NULL);
794 #endif
795         LASSERT (bk->bk_niov * SFW_ID_PER_PAGE >= (unsigned int)ndest);
796         LASSERT((unsigned int)bk->bk_len >=
797                 sizeof(lnet_process_id_packed_t) * ndest);
798
799         sfw_unpack_addtest_req(msg);
800         memcpy(&tsi->tsi_u, &req->tsr_u, sizeof(tsi->tsi_u));
801
802         for (i = 0; i < ndest; i++) {
803                 lnet_process_id_packed_t *dests;
804                 lnet_process_id_packed_t  id;
805                 int                       j;
806
807 #ifdef __KERNEL__
808                 dests = cfs_page_address(bk->bk_iovs[i / SFW_ID_PER_PAGE].kiov_page);
809                 LASSERT (dests != NULL);  /* my pages are within KVM always */
810 #else
811                 dests = cfs_page_address(bk->bk_pages[i / SFW_ID_PER_PAGE]);
812 #endif
813                 id = dests[i % SFW_ID_PER_PAGE];
814                 if (msg->msg_magic != SRPC_MSG_MAGIC)
815                         sfw_unpack_id(id);
816
817                 for (j = 0; j < tsi->tsi_concur; j++) {
818                         LIBCFS_ALLOC(tsu, sizeof(sfw_test_unit_t));
819                         if (tsu == NULL) {
820                                 rc = -ENOMEM;
821                                 CERROR ("Can't allocate tsu for %d\n",
822                                         tsi->tsi_service);
823                                 goto error;
824                         }
825
826                         tsu->tsu_dest.nid = id.nid;
827                         tsu->tsu_dest.pid = id.pid;
828                         tsu->tsu_instance = tsi;
829                         tsu->tsu_private  = NULL;
830                         cfs_list_add_tail(&tsu->tsu_list, &tsi->tsi_units);
831                 }
832         }
833
834         rc = tsi->tsi_ops->tso_init(tsi);
835         if (rc == 0) {
836                 cfs_list_add_tail(&tsi->tsi_list, &tsb->bat_tests);
837                 return 0;
838         }
839
840 error:
841         LASSERT (rc != 0);
842         sfw_destroy_test_instance(tsi);
843         return rc;
844 }
845
846 static void
847 sfw_test_unit_done (sfw_test_unit_t *tsu)
848 {
849         sfw_test_instance_t *tsi = tsu->tsu_instance;
850         sfw_batch_t         *tsb = tsi->tsi_batch;
851         sfw_session_t       *sn = tsb->bat_session;
852
853         LASSERT (sfw_test_active(tsi));
854
855         if (!cfs_atomic_dec_and_test(&tsi->tsi_nactive))
856                 return;
857
858         /* the test instance is done */
859         cfs_spin_lock(&tsi->tsi_lock);
860
861         tsi->tsi_stopping = 0;
862
863         cfs_spin_unlock(&tsi->tsi_lock);
864
865         cfs_spin_lock(&sfw_data.fw_lock);
866
867         if (!cfs_atomic_dec_and_test(&tsb->bat_nactive) ||/* tsb still active */
868             sn == sfw_data.fw_session) {               /* sn also active */
869                 cfs_spin_unlock(&sfw_data.fw_lock);
870                 return;
871         }
872
873         LASSERT (!cfs_list_empty(&sn->sn_list)); /* I'm a zombie! */
874
875         cfs_list_for_each_entry_typed (tsb, &sn->sn_batches,
876                                        sfw_batch_t, bat_list) {
877                 if (sfw_batch_active(tsb)) {
878                         cfs_spin_unlock(&sfw_data.fw_lock);
879                         return;
880                 }
881         }
882
883         cfs_list_del_init(&sn->sn_list);
884         cfs_spin_unlock(&sfw_data.fw_lock);
885
886         sfw_destroy_session(sn);
887         return;
888 }
889
890 void
891 sfw_test_rpc_done (srpc_client_rpc_t *rpc)
892 {
893         sfw_test_unit_t     *tsu = rpc->crpc_priv;
894         sfw_test_instance_t *tsi = tsu->tsu_instance;
895         int                  done = 0;
896
897         tsi->tsi_ops->tso_done_rpc(tsu, rpc);
898
899         cfs_spin_lock(&tsi->tsi_lock);
900
901         LASSERT (sfw_test_active(tsi));
902         LASSERT (!cfs_list_empty(&rpc->crpc_list));
903
904         cfs_list_del_init(&rpc->crpc_list);
905
906         /* batch is stopping or loop is done or get error */
907         if (tsi->tsi_stopping ||
908             tsu->tsu_loop == 0 ||
909             (rpc->crpc_status != 0 && tsi->tsi_stoptsu_onerr))
910                 done = 1;
911
912         /* dec ref for poster */
913         srpc_client_rpc_decref(rpc);
914
915         cfs_spin_unlock(&tsi->tsi_lock);
916
917         if (!done) {
918                 swi_schedule_workitem(&tsu->tsu_worker);
919                 return;
920         }
921
922         sfw_test_unit_done(tsu);
923         return;
924 }
925
926 int
927 sfw_create_test_rpc(sfw_test_unit_t *tsu, lnet_process_id_t peer,
928                     unsigned features, int nblk, int blklen,
929                     srpc_client_rpc_t **rpcpp)
930 {
931         srpc_client_rpc_t   *rpc = NULL;
932         sfw_test_instance_t *tsi = tsu->tsu_instance;
933
934         cfs_spin_lock(&tsi->tsi_lock);
935
936         LASSERT (sfw_test_active(tsi));
937
938         if (!cfs_list_empty(&tsi->tsi_free_rpcs)) {
939                 /* pick request from buffer */
940                 rpc = cfs_list_entry(tsi->tsi_free_rpcs.next,
941                                      srpc_client_rpc_t, crpc_list);
942                 LASSERT (nblk == rpc->crpc_bulk.bk_niov);
943                 cfs_list_del_init(&rpc->crpc_list);
944         }
945
946         cfs_spin_unlock(&tsi->tsi_lock);
947
948         if (rpc == NULL) {
949                 rpc = srpc_create_client_rpc(peer, tsi->tsi_service, nblk,
950                                              blklen, sfw_test_rpc_done,
951                                              sfw_test_rpc_fini, tsu);
952         } else {
953                 srpc_init_client_rpc(rpc, peer, tsi->tsi_service, nblk,
954                                      blklen, sfw_test_rpc_done,
955                                      sfw_test_rpc_fini, tsu);
956         }
957
958         if (rpc == NULL) {
959                 CERROR("Can't create rpc for test %d\n", tsi->tsi_service);
960                 return -ENOMEM;
961         }
962
963         rpc->crpc_reqstmsg.msg_ses_feats = features;
964         *rpcpp = rpc;
965
966         return 0;
967 }
968
969 int
970 sfw_run_test (swi_workitem_t *wi)
971 {
972         sfw_test_unit_t     *tsu = wi->swi_workitem.wi_data;
973         sfw_test_instance_t *tsi = tsu->tsu_instance;
974         srpc_client_rpc_t   *rpc = NULL;
975
976         LASSERT (wi == &tsu->tsu_worker);
977
978         if (tsi->tsi_ops->tso_prep_rpc(tsu, tsu->tsu_dest, &rpc) != 0) {
979                 LASSERT (rpc == NULL);
980                 goto test_done;
981         }
982
983         LASSERT (rpc != NULL);
984
985         cfs_spin_lock(&tsi->tsi_lock);
986
987         if (tsi->tsi_stopping) {
988                 cfs_list_add(&rpc->crpc_list, &tsi->tsi_free_rpcs);
989                 cfs_spin_unlock(&tsi->tsi_lock);
990                 goto test_done;
991         }
992
993         if (tsu->tsu_loop > 0)
994                 tsu->tsu_loop--;
995
996         cfs_list_add_tail(&rpc->crpc_list, &tsi->tsi_active_rpcs);
997         cfs_spin_unlock(&tsi->tsi_lock);
998
999         rpc->crpc_timeout = rpc_timeout;
1000
1001         cfs_spin_lock(&rpc->crpc_lock);
1002         srpc_post_rpc(rpc);
1003         cfs_spin_unlock(&rpc->crpc_lock);
1004         return 0;
1005
1006 test_done:
1007         /*
1008          * No one can schedule me now since:
1009          * - previous RPC, if any, has done and
1010          * - no new RPC is initiated.
1011          * - my batch is still active; no one can run it again now.
1012          * Cancel pending schedules and prevent future schedule attempts:
1013          */
1014         swi_exit_workitem(wi);
1015         sfw_test_unit_done(tsu);
1016         return 1;
1017 }
1018
1019 int
1020 sfw_run_batch (sfw_batch_t *tsb)
1021 {
1022         swi_workitem_t      *wi;
1023         sfw_test_unit_t     *tsu;
1024         sfw_test_instance_t *tsi;
1025
1026         if (sfw_batch_active(tsb)) {
1027                 CDEBUG(D_NET, "Batch already active: "LPU64" (%d)\n",
1028                        tsb->bat_id.bat_id, cfs_atomic_read(&tsb->bat_nactive));
1029                 return 0;
1030         }
1031
1032         cfs_list_for_each_entry_typed (tsi, &tsb->bat_tests,
1033                                        sfw_test_instance_t, tsi_list) {
1034                 if (!tsi->tsi_is_client) /* skip server instances */
1035                         continue;
1036
1037                 LASSERT (!tsi->tsi_stopping);
1038                 LASSERT (!sfw_test_active(tsi));
1039
1040                 cfs_atomic_inc(&tsb->bat_nactive);
1041
1042                 cfs_list_for_each_entry_typed (tsu, &tsi->tsi_units,
1043                                                sfw_test_unit_t, tsu_list) {
1044                         cfs_atomic_inc(&tsi->tsi_nactive);
1045                         tsu->tsu_loop = tsi->tsi_loop;
1046                         wi = &tsu->tsu_worker;
1047                         swi_init_workitem(wi, tsu, sfw_run_test,
1048                                           lst_sched_test[\
1049                                           lnet_cpt_of_nid(tsu->tsu_dest.nid)]);
1050                         swi_schedule_workitem(wi);
1051                 }
1052         }
1053
1054         return 0;
1055 }
1056
1057 int
1058 sfw_stop_batch (sfw_batch_t *tsb, int force)
1059 {
1060         sfw_test_instance_t *tsi;
1061         srpc_client_rpc_t   *rpc;
1062
1063         if (!sfw_batch_active(tsb)) {
1064                 CDEBUG(D_NET, "Batch "LPU64" inactive\n", tsb->bat_id.bat_id);
1065                 return 0;
1066         }
1067
1068         cfs_list_for_each_entry_typed (tsi, &tsb->bat_tests,
1069                                        sfw_test_instance_t, tsi_list) {
1070                 cfs_spin_lock(&tsi->tsi_lock);
1071
1072                 if (!tsi->tsi_is_client ||
1073                     !sfw_test_active(tsi) || tsi->tsi_stopping) {
1074                         cfs_spin_unlock(&tsi->tsi_lock);
1075                         continue;
1076                 }
1077
1078                 tsi->tsi_stopping = 1;
1079
1080                 if (!force) {
1081                         cfs_spin_unlock(&tsi->tsi_lock);
1082                         continue;
1083                 }
1084
1085                 /* abort launched rpcs in the test */
1086                 cfs_list_for_each_entry_typed (rpc, &tsi->tsi_active_rpcs,
1087                                                srpc_client_rpc_t, crpc_list) {
1088                         cfs_spin_lock(&rpc->crpc_lock);
1089
1090                         srpc_abort_rpc(rpc, -EINTR);
1091
1092                         cfs_spin_unlock(&rpc->crpc_lock);
1093                 }
1094
1095                 cfs_spin_unlock(&tsi->tsi_lock);
1096         }
1097
1098         return 0;
1099 }
1100
1101 int
1102 sfw_query_batch (sfw_batch_t *tsb, int testidx, srpc_batch_reply_t *reply)
1103 {
1104         sfw_test_instance_t *tsi;
1105
1106         if (testidx < 0)
1107                 return -EINVAL;
1108
1109         if (testidx == 0) {
1110                 reply->bar_active = cfs_atomic_read(&tsb->bat_nactive);
1111                 return 0;
1112         }
1113
1114         cfs_list_for_each_entry_typed (tsi, &tsb->bat_tests,
1115                                        sfw_test_instance_t, tsi_list) {
1116                 if (testidx-- > 1)
1117                         continue;
1118
1119                 reply->bar_active = cfs_atomic_read(&tsi->tsi_nactive);
1120                 return 0;
1121         }
1122
1123         return -ENOENT;
1124 }
1125
1126 void
1127 sfw_free_pages (srpc_server_rpc_t *rpc)
1128 {
1129         srpc_free_bulk(rpc->srpc_bulk);
1130         rpc->srpc_bulk = NULL;
1131 }
1132
1133 int
1134 sfw_alloc_pages(struct srpc_server_rpc *rpc, int cpt, int npages, int len,
1135                 int sink)
1136 {
1137         LASSERT(rpc->srpc_bulk == NULL);
1138         LASSERT(npages > 0 && npages <= LNET_MAX_IOV);
1139
1140         rpc->srpc_bulk = srpc_alloc_bulk(cpt, npages, len, sink);
1141         if (rpc->srpc_bulk == NULL)
1142                 return -ENOMEM;
1143
1144         return 0;
1145 }
1146
1147 int
1148 sfw_add_test (srpc_server_rpc_t *rpc)
1149 {
1150         sfw_session_t     *sn = sfw_data.fw_session;
1151         srpc_test_reply_t *reply = &rpc->srpc_replymsg.msg_body.tes_reply;
1152         srpc_test_reqst_t *request;
1153         int                rc;
1154         sfw_batch_t       *bat;
1155
1156         request = &rpc->srpc_reqstbuf->buf_msg.msg_body.tes_reqst;
1157         reply->tsr_sid = (sn == NULL) ? LST_INVALID_SID : sn->sn_id;
1158
1159         if (request->tsr_loop == 0 ||
1160             request->tsr_concur == 0 ||
1161             request->tsr_sid.ses_nid == LNET_NID_ANY ||
1162             request->tsr_ndest > SFW_MAX_NDESTS ||
1163             (request->tsr_is_client && request->tsr_ndest == 0) ||
1164             request->tsr_concur > SFW_MAX_CONCUR ||
1165             request->tsr_service > SRPC_SERVICE_MAX_ID ||
1166             request->tsr_service <= SRPC_FRAMEWORK_SERVICE_MAX_ID) {
1167                 reply->tsr_status = EINVAL;
1168                 return 0;
1169         }
1170
1171         if (sn == NULL || !sfw_sid_equal(request->tsr_sid, sn->sn_id) ||
1172             sfw_find_test_case(request->tsr_service) == NULL) {
1173                 reply->tsr_status = ENOENT;
1174                 return 0;
1175         }
1176
1177         bat = sfw_bid2batch(request->tsr_bid);
1178         if (bat == NULL) {
1179                 CERROR ("Dropping RPC (%s) from %s under memory pressure.\n",
1180                         rpc->srpc_scd->scd_svc->sv_name,
1181                         libcfs_id2str(rpc->srpc_peer));
1182                 return -ENOMEM;
1183         }
1184
1185         if (sfw_batch_active(bat)) {
1186                 reply->tsr_status = EBUSY;
1187                 return 0;
1188         }
1189
1190         if (request->tsr_is_client && rpc->srpc_bulk == NULL) {
1191                 /* rpc will be resumed later in sfw_bulk_ready */
1192                 int     npg = sfw_id_pages(request->tsr_ndest);
1193                 int     len;
1194
1195                 if ((sn->sn_features & LST_FEAT_BULK_LEN) == 0) {
1196                         len = npg * CFS_PAGE_SIZE;
1197
1198                 } else  {
1199                         len = sizeof(lnet_process_id_packed_t) *
1200                               request->tsr_ndest;
1201                 }
1202
1203                 return sfw_alloc_pages(rpc, CFS_CPT_ANY, npg, len, 1);
1204         }
1205
1206         rc = sfw_add_test_instance(bat, rpc);
1207         CDEBUG (rc == 0 ? D_NET : D_WARNING,
1208                 "%s test: sv %d %s, loop %d, concur %d, ndest %d\n",
1209                 rc == 0 ? "Added" : "Failed to add", request->tsr_service,
1210                 request->tsr_is_client ? "client" : "server",
1211                 request->tsr_loop, request->tsr_concur, request->tsr_ndest);
1212
1213         reply->tsr_status = (rc < 0) ? -rc : rc;
1214         return 0;
1215 }
1216
1217 int
1218 sfw_control_batch (srpc_batch_reqst_t *request, srpc_batch_reply_t *reply)
1219 {
1220         sfw_session_t *sn = sfw_data.fw_session;
1221         int            rc = 0;
1222         sfw_batch_t   *bat;
1223
1224         reply->bar_sid = (sn == NULL) ? LST_INVALID_SID : sn->sn_id;
1225
1226         if (sn == NULL || !sfw_sid_equal(request->bar_sid, sn->sn_id)) {
1227                 reply->bar_status = ESRCH;
1228                 return 0;
1229         }
1230
1231         bat = sfw_find_batch(request->bar_bid);
1232         if (bat == NULL) {
1233                 reply->bar_status = ENOENT;
1234                 return 0;
1235         }
1236
1237         switch (request->bar_opc) {
1238         case SRPC_BATCH_OPC_RUN:
1239                 rc = sfw_run_batch(bat);
1240                 break;
1241
1242         case SRPC_BATCH_OPC_STOP:
1243                 rc = sfw_stop_batch(bat, request->bar_arg);
1244                 break;
1245
1246         case SRPC_BATCH_OPC_QUERY:
1247                 rc = sfw_query_batch(bat, request->bar_testidx, reply);
1248                 break;
1249
1250         default:
1251                 return -EINVAL; /* drop it */
1252         }
1253
1254         reply->bar_status = (rc < 0) ? -rc : rc;
1255         return 0;
1256 }
1257
1258 int
1259 sfw_handle_server_rpc(struct srpc_server_rpc *rpc)
1260 {
1261         struct srpc_service     *sv = rpc->srpc_scd->scd_svc;
1262         srpc_msg_t     *reply   = &rpc->srpc_replymsg;
1263         srpc_msg_t     *request = &rpc->srpc_reqstbuf->buf_msg;
1264         unsigned        features = LST_FEATS_MASK;
1265         int             rc = 0;
1266
1267         LASSERT (sfw_data.fw_active_srpc == NULL);
1268         LASSERT (sv->sv_id <= SRPC_FRAMEWORK_SERVICE_MAX_ID);
1269
1270         cfs_spin_lock(&sfw_data.fw_lock);
1271
1272         if (sfw_data.fw_shuttingdown) {
1273                 cfs_spin_unlock(&sfw_data.fw_lock);
1274                 return -ESHUTDOWN;
1275         }
1276
1277         /* Remove timer to avoid racing with it or expiring active session */
1278         if (sfw_del_session_timer() != 0) {
1279                 CERROR ("Dropping RPC (%s) from %s: racing with expiry timer.",
1280                         sv->sv_name, libcfs_id2str(rpc->srpc_peer));
1281                 cfs_spin_unlock(&sfw_data.fw_lock);
1282                 return -EAGAIN;
1283         }
1284
1285         sfw_data.fw_active_srpc = rpc;
1286         cfs_spin_unlock(&sfw_data.fw_lock);
1287
1288         sfw_unpack_message(request);
1289         LASSERT (request->msg_type == srpc_service2request(sv->sv_id));
1290
1291         /* rpc module should have checked this */
1292         LASSERT(request->msg_version == SRPC_MSG_VERSION);
1293
1294         if (sv->sv_id != SRPC_SERVICE_MAKE_SESSION &&
1295             sv->sv_id != SRPC_SERVICE_DEBUG) {
1296                 sfw_session_t *sn = sfw_data.fw_session;
1297
1298                 if (sn != NULL &&
1299                     sn->sn_features != request->msg_ses_feats) {
1300                         CNETERR("Features of framework RPC don't match "
1301                                 "features of current session: %x/%x\n",
1302                                 request->msg_ses_feats, sn->sn_features);
1303                         reply->msg_body.reply.status = EPROTO;
1304                         reply->msg_body.reply.sid    = sn->sn_id;
1305                         goto out;
1306                 }
1307
1308         } else if ((request->msg_ses_feats & ~LST_FEATS_MASK) != 0) {
1309                 /* NB: at this point, old version will ignore features and
1310                  * create new session anyway, so console should be able
1311                  * to handle this */
1312                 reply->msg_body.reply.status = EPROTO;
1313                 goto out;
1314         }
1315
1316         switch(sv->sv_id) {
1317         default:
1318                 LBUG ();
1319         case SRPC_SERVICE_TEST:
1320                 rc = sfw_add_test(rpc);
1321                 break;
1322
1323         case SRPC_SERVICE_BATCH:
1324                 rc = sfw_control_batch(&request->msg_body.bat_reqst,
1325                                        &reply->msg_body.bat_reply);
1326                 break;
1327
1328         case SRPC_SERVICE_QUERY_STAT:
1329                 rc = sfw_get_stats(&request->msg_body.stat_reqst,
1330                                    &reply->msg_body.stat_reply);
1331                 break;
1332
1333         case SRPC_SERVICE_DEBUG:
1334                 rc = sfw_debug_session(&request->msg_body.dbg_reqst,
1335                                        &reply->msg_body.dbg_reply);
1336                 break;
1337
1338         case SRPC_SERVICE_MAKE_SESSION:
1339                 rc = sfw_make_session(&request->msg_body.mksn_reqst,
1340                                       &reply->msg_body.mksn_reply);
1341                 break;
1342
1343         case SRPC_SERVICE_REMOVE_SESSION:
1344                 rc = sfw_remove_session(&request->msg_body.rmsn_reqst,
1345                                         &reply->msg_body.rmsn_reply);
1346                 break;
1347         }
1348
1349         if (sfw_data.fw_session != NULL)
1350                 features = sfw_data.fw_session->sn_features;
1351  out:
1352         reply->msg_ses_feats = features;
1353         rpc->srpc_done = sfw_server_rpc_done;
1354         cfs_spin_lock(&sfw_data.fw_lock);
1355
1356 #ifdef __KERNEL__
1357         if (!sfw_data.fw_shuttingdown)
1358                 sfw_add_session_timer();
1359 #else
1360         LASSERT (!sfw_data.fw_shuttingdown);
1361         sfw_add_session_timer();
1362 #endif
1363
1364         sfw_data.fw_active_srpc = NULL;
1365         cfs_spin_unlock(&sfw_data.fw_lock);
1366         return rc;
1367 }
1368
1369 int
1370 sfw_bulk_ready(struct srpc_server_rpc *rpc, int status)
1371 {
1372         struct srpc_service     *sv = rpc->srpc_scd->scd_svc;
1373         int                     rc;
1374
1375         LASSERT (rpc->srpc_bulk != NULL);
1376         LASSERT (sv->sv_id == SRPC_SERVICE_TEST);
1377         LASSERT (sfw_data.fw_active_srpc == NULL);
1378         LASSERT (rpc->srpc_reqstbuf->buf_msg.msg_body.tes_reqst.tsr_is_client);
1379
1380         cfs_spin_lock(&sfw_data.fw_lock);
1381
1382         if (status != 0) {
1383                 CERROR ("Bulk transfer failed for RPC: "
1384                         "service %s, peer %s, status %d\n",
1385                         sv->sv_name, libcfs_id2str(rpc->srpc_peer), status);
1386                 cfs_spin_unlock(&sfw_data.fw_lock);
1387                 return -EIO;
1388         }
1389
1390         if (sfw_data.fw_shuttingdown) {
1391                 cfs_spin_unlock(&sfw_data.fw_lock);
1392                 return -ESHUTDOWN;
1393         }
1394
1395         if (sfw_del_session_timer() != 0) {
1396                 CERROR ("Dropping RPC (%s) from %s: racing with expiry timer",
1397                         sv->sv_name, libcfs_id2str(rpc->srpc_peer));
1398                 cfs_spin_unlock(&sfw_data.fw_lock);
1399                 return -EAGAIN;
1400         }
1401
1402         sfw_data.fw_active_srpc = rpc;
1403         cfs_spin_unlock(&sfw_data.fw_lock);
1404
1405         rc = sfw_add_test(rpc);
1406
1407         cfs_spin_lock(&sfw_data.fw_lock);
1408
1409 #ifdef __KERNEL__
1410         if (!sfw_data.fw_shuttingdown)
1411                 sfw_add_session_timer();
1412 #else
1413         LASSERT (!sfw_data.fw_shuttingdown);
1414         sfw_add_session_timer();
1415 #endif
1416
1417         sfw_data.fw_active_srpc = NULL;
1418         cfs_spin_unlock(&sfw_data.fw_lock);
1419         return rc;
1420 }
1421
1422 srpc_client_rpc_t *
1423 sfw_create_rpc(lnet_process_id_t peer, int service,
1424                unsigned features, int nbulkiov, int bulklen,
1425                void (*done)(srpc_client_rpc_t *), void *priv)
1426 {
1427         srpc_client_rpc_t *rpc = NULL;
1428
1429         cfs_spin_lock(&sfw_data.fw_lock);
1430
1431         LASSERT (!sfw_data.fw_shuttingdown);
1432         LASSERT (service <= SRPC_FRAMEWORK_SERVICE_MAX_ID);
1433
1434         if (nbulkiov == 0 && !cfs_list_empty(&sfw_data.fw_zombie_rpcs)) {
1435                 rpc = cfs_list_entry(sfw_data.fw_zombie_rpcs.next,
1436                                      srpc_client_rpc_t, crpc_list);
1437                 cfs_list_del(&rpc->crpc_list);
1438
1439                 srpc_init_client_rpc(rpc, peer, service, 0, 0,
1440                                      done, sfw_client_rpc_fini, priv);
1441         }
1442
1443         cfs_spin_unlock(&sfw_data.fw_lock);
1444
1445         if (rpc == NULL) {
1446                 rpc = srpc_create_client_rpc(peer, service,
1447                                              nbulkiov, bulklen, done,
1448                                              nbulkiov != 0 ?  NULL :
1449                                              sfw_client_rpc_fini,
1450                                              priv);
1451         }
1452
1453         if (rpc != NULL) /* "session" is concept in framework */
1454                 rpc->crpc_reqstmsg.msg_ses_feats = features;
1455
1456         return rpc;
1457 }
1458
1459 void
1460 sfw_unpack_message (srpc_msg_t *msg)
1461 {
1462         if (msg->msg_magic == SRPC_MSG_MAGIC)
1463                 return; /* no flipping needed */
1464
1465         /* srpc module should guarantee I wouldn't get crap */
1466         LASSERT (msg->msg_magic == __swab32(SRPC_MSG_MAGIC));
1467
1468         if (msg->msg_type == SRPC_MSG_STAT_REQST) {
1469                 srpc_stat_reqst_t *req = &msg->msg_body.stat_reqst;
1470
1471                 __swab32s(&req->str_type);
1472                 __swab64s(&req->str_rpyid);
1473                 sfw_unpack_sid(req->str_sid);
1474                 return;
1475         }
1476
1477         if (msg->msg_type == SRPC_MSG_STAT_REPLY) {
1478                 srpc_stat_reply_t *rep = &msg->msg_body.stat_reply;
1479
1480                 __swab32s(&rep->str_status);
1481                 sfw_unpack_sid(rep->str_sid);
1482                 sfw_unpack_fw_counters(rep->str_fw);
1483                 sfw_unpack_rpc_counters(rep->str_rpc);
1484                 sfw_unpack_lnet_counters(rep->str_lnet);
1485                 return;
1486         }
1487
1488         if (msg->msg_type == SRPC_MSG_MKSN_REQST) {
1489                 srpc_mksn_reqst_t *req = &msg->msg_body.mksn_reqst;
1490
1491                 __swab64s(&req->mksn_rpyid);
1492                 __swab32s(&req->mksn_force);
1493                 sfw_unpack_sid(req->mksn_sid);
1494                 return;
1495         }
1496
1497         if (msg->msg_type == SRPC_MSG_MKSN_REPLY) {
1498                 srpc_mksn_reply_t *rep = &msg->msg_body.mksn_reply;
1499
1500                 __swab32s(&rep->mksn_status);
1501                 __swab32s(&rep->mksn_timeout);
1502                 sfw_unpack_sid(rep->mksn_sid);
1503                 return;
1504         }
1505
1506         if (msg->msg_type == SRPC_MSG_RMSN_REQST) {
1507                 srpc_rmsn_reqst_t *req = &msg->msg_body.rmsn_reqst;
1508
1509                 __swab64s(&req->rmsn_rpyid);
1510                 sfw_unpack_sid(req->rmsn_sid);
1511                 return;
1512         }
1513
1514         if (msg->msg_type == SRPC_MSG_RMSN_REPLY) {
1515                 srpc_rmsn_reply_t *rep = &msg->msg_body.rmsn_reply;
1516
1517                 __swab32s(&rep->rmsn_status);
1518                 sfw_unpack_sid(rep->rmsn_sid);
1519                 return;
1520         }
1521
1522         if (msg->msg_type == SRPC_MSG_DEBUG_REQST) {
1523                 srpc_debug_reqst_t *req = &msg->msg_body.dbg_reqst;
1524
1525                 __swab64s(&req->dbg_rpyid);
1526                 __swab32s(&req->dbg_flags);
1527                 sfw_unpack_sid(req->dbg_sid);
1528                 return;
1529         }
1530
1531         if (msg->msg_type == SRPC_MSG_DEBUG_REPLY) {
1532                 srpc_debug_reply_t *rep = &msg->msg_body.dbg_reply;
1533
1534                 __swab32s(&rep->dbg_nbatch);
1535                 __swab32s(&rep->dbg_timeout);
1536                 sfw_unpack_sid(rep->dbg_sid);
1537                 return;
1538         }
1539
1540         if (msg->msg_type == SRPC_MSG_BATCH_REQST) {
1541                 srpc_batch_reqst_t *req = &msg->msg_body.bat_reqst;
1542
1543                 __swab32s(&req->bar_opc);
1544                 __swab64s(&req->bar_rpyid);
1545                 __swab32s(&req->bar_testidx);
1546                 __swab32s(&req->bar_arg);
1547                 sfw_unpack_sid(req->bar_sid);
1548                 __swab64s(&req->bar_bid.bat_id);
1549                 return;
1550         }
1551
1552         if (msg->msg_type == SRPC_MSG_BATCH_REPLY) {
1553                 srpc_batch_reply_t *rep = &msg->msg_body.bat_reply;
1554
1555                 __swab32s(&rep->bar_status);
1556                 sfw_unpack_sid(rep->bar_sid);
1557                 return;
1558         }
1559
1560         if (msg->msg_type == SRPC_MSG_TEST_REQST) {
1561                 srpc_test_reqst_t *req = &msg->msg_body.tes_reqst;
1562
1563                 __swab64s(&req->tsr_rpyid);
1564                 __swab64s(&req->tsr_bulkid);
1565                 __swab32s(&req->tsr_loop);
1566                 __swab32s(&req->tsr_ndest);
1567                 __swab32s(&req->tsr_concur);
1568                 __swab32s(&req->tsr_service);
1569                 sfw_unpack_sid(req->tsr_sid);
1570                 __swab64s(&req->tsr_bid.bat_id);
1571                 return;
1572         }
1573
1574         if (msg->msg_type == SRPC_MSG_TEST_REPLY) {
1575                 srpc_test_reply_t *rep = &msg->msg_body.tes_reply;
1576
1577                 __swab32s(&rep->tsr_status);
1578                 sfw_unpack_sid(rep->tsr_sid);
1579                 return;
1580         }
1581
1582         if (msg->msg_type == SRPC_MSG_JOIN_REQST) {
1583                 srpc_join_reqst_t *req = &msg->msg_body.join_reqst;
1584
1585                 __swab64s(&req->join_rpyid);
1586                 sfw_unpack_sid(req->join_sid);
1587                 return;
1588         }
1589
1590         if (msg->msg_type == SRPC_MSG_JOIN_REPLY) {
1591                 srpc_join_reply_t *rep = &msg->msg_body.join_reply;
1592
1593                 __swab32s(&rep->join_status);
1594                 __swab32s(&rep->join_timeout);
1595                 sfw_unpack_sid(rep->join_sid);
1596                 return;
1597         }
1598
1599         LBUG ();
1600         return;
1601 }
1602
1603 void
1604 sfw_abort_rpc (srpc_client_rpc_t *rpc)
1605 {
1606         LASSERT (cfs_atomic_read(&rpc->crpc_refcount) > 0);
1607         LASSERT (rpc->crpc_service <= SRPC_FRAMEWORK_SERVICE_MAX_ID);
1608
1609         cfs_spin_lock(&rpc->crpc_lock);
1610         srpc_abort_rpc(rpc, -EINTR);
1611         cfs_spin_unlock(&rpc->crpc_lock);
1612         return;
1613 }
1614
1615 void
1616 sfw_post_rpc (srpc_client_rpc_t *rpc)
1617 {
1618         cfs_spin_lock(&rpc->crpc_lock);
1619
1620         LASSERT (!rpc->crpc_closed);
1621         LASSERT (!rpc->crpc_aborted);
1622         LASSERT (cfs_list_empty(&rpc->crpc_list));
1623         LASSERT (!sfw_data.fw_shuttingdown);
1624
1625         rpc->crpc_timeout = rpc_timeout;
1626         srpc_post_rpc(rpc);
1627
1628         cfs_spin_unlock(&rpc->crpc_lock);
1629         return;
1630 }
1631
1632 static srpc_service_t sfw_services[] = 
1633 {
1634         {
1635                 /* sv_id */    SRPC_SERVICE_DEBUG,
1636                 /* sv_name */  "debug",
1637                 0
1638         },
1639         {
1640                 /* sv_id */    SRPC_SERVICE_QUERY_STAT,
1641                 /* sv_name */  "query stats",
1642                 0
1643         },
1644         {
1645                 /* sv_id */    SRPC_SERVICE_MAKE_SESSION,
1646                 /* sv_name */  "make session",
1647                 0
1648         },
1649         {
1650                 /* sv_id */    SRPC_SERVICE_REMOVE_SESSION,
1651                 /* sv_name */  "remove session",
1652                 0
1653         },
1654         {
1655                 /* sv_id */    SRPC_SERVICE_BATCH,
1656                 /* sv_name */  "batch service",
1657                 0
1658         },
1659         {
1660                 /* sv_id */    SRPC_SERVICE_TEST,
1661                 /* sv_name */  "test service",
1662                 0
1663         },
1664         {
1665                 /* sv_id */    0,
1666                 /* sv_name */  NULL,
1667                 0
1668         }
1669 };
1670
1671 extern sfw_test_client_ops_t ping_test_client;
1672 extern srpc_service_t        ping_test_service;
1673 extern void ping_init_test_client(void);
1674 extern void ping_init_test_service(void);
1675
1676 extern sfw_test_client_ops_t brw_test_client;
1677 extern srpc_service_t        brw_test_service;
1678 extern void brw_init_test_client(void);
1679 extern void brw_init_test_service(void);
1680
1681
1682 int
1683 sfw_startup (void)
1684 {
1685         int              i;
1686         int              rc;
1687         int              error;
1688         srpc_service_t  *sv;
1689         sfw_test_case_t *tsc;
1690
1691 #ifndef __KERNEL__
1692         char *s;
1693
1694         s = getenv("SESSION_TIMEOUT");
1695         session_timeout = s != NULL ? atoi(s) : session_timeout;
1696
1697         s = getenv("RPC_TIMEOUT");
1698         rpc_timeout = s != NULL ? atoi(s) : rpc_timeout;
1699 #endif
1700
1701         if (session_timeout < 0) {
1702                 CERROR ("Session timeout must be non-negative: %d\n",
1703                         session_timeout);
1704                 return -EINVAL;
1705         }
1706
1707         if (rpc_timeout < 0) {
1708                 CERROR ("RPC timeout must be non-negative: %d\n",
1709                         rpc_timeout);
1710                 return -EINVAL;
1711         }
1712
1713         if (session_timeout == 0)
1714                 CWARN ("Zero session_timeout specified "
1715                        "- test sessions never expire.\n");
1716
1717         if (rpc_timeout == 0)
1718                 CWARN ("Zero rpc_timeout specified "
1719                        "- test RPC never expire.\n");
1720
1721         memset(&sfw_data, 0, sizeof(struct smoketest_framework));
1722
1723         sfw_data.fw_session     = NULL;
1724         sfw_data.fw_active_srpc = NULL;
1725         cfs_spin_lock_init(&sfw_data.fw_lock);
1726         cfs_atomic_set(&sfw_data.fw_nzombies, 0);
1727         CFS_INIT_LIST_HEAD(&sfw_data.fw_tests);
1728         CFS_INIT_LIST_HEAD(&sfw_data.fw_zombie_rpcs);
1729         CFS_INIT_LIST_HEAD(&sfw_data.fw_zombie_sessions);
1730
1731         brw_init_test_client();
1732         brw_init_test_service();
1733         rc = sfw_register_test(&brw_test_service, &brw_test_client);
1734         LASSERT (rc == 0);
1735
1736         ping_init_test_client();
1737         ping_init_test_service();
1738         rc = sfw_register_test(&ping_test_service, &ping_test_client);
1739         LASSERT (rc == 0);
1740
1741         error = 0;
1742         cfs_list_for_each_entry_typed (tsc, &sfw_data.fw_tests,
1743                                        sfw_test_case_t, tsc_list) {
1744                 sv = tsc->tsc_srv_service;
1745
1746                 rc = srpc_add_service(sv);
1747                 LASSERT (rc != -EBUSY);
1748                 if (rc != 0) {
1749                         CWARN ("Failed to add %s service: %d\n",
1750                                sv->sv_name, rc);
1751                         error = rc;
1752                 }
1753         }
1754
1755         for (i = 0; ; i++) {
1756                 sv = &sfw_services[i];
1757                 if (sv->sv_name == NULL) break;
1758
1759                 sv->sv_bulk_ready = NULL;
1760                 sv->sv_handler    = sfw_handle_server_rpc;
1761                 sv->sv_wi_total   = SFW_FRWK_WI_MAX;
1762                 if (sv->sv_id == SRPC_SERVICE_TEST)
1763                         sv->sv_bulk_ready = sfw_bulk_ready;
1764
1765                 rc = srpc_add_service(sv);
1766                 LASSERT (rc != -EBUSY);
1767                 if (rc != 0) {
1768                         CWARN ("Failed to add %s service: %d\n",
1769                                sv->sv_name, rc);
1770                         error = rc;
1771                 }
1772
1773                 /* about to sfw_shutdown, no need to add buffer */
1774                 if (error) continue;
1775
1776                 rc = srpc_service_add_buffers(sv, sv->sv_wi_total);
1777                 if (rc != 0) {
1778                         CWARN("Failed to reserve enough buffers: "
1779                               "service %s, %d needed: %d\n",
1780                               sv->sv_name, sv->sv_wi_total, rc);
1781                         error = -ENOMEM;
1782                 }
1783         }
1784
1785         if (error != 0)
1786                 sfw_shutdown();
1787         return error;
1788 }
1789
1790 void
1791 sfw_shutdown (void)
1792 {
1793         srpc_service_t  *sv;
1794         sfw_test_case_t *tsc;
1795         int              i;
1796
1797         cfs_spin_lock(&sfw_data.fw_lock);
1798
1799         sfw_data.fw_shuttingdown = 1;
1800 #ifdef __KERNEL__
1801         lst_wait_until(sfw_data.fw_active_srpc == NULL, sfw_data.fw_lock,
1802                        "waiting for active RPC to finish.\n");
1803 #else
1804         LASSERT (sfw_data.fw_active_srpc == NULL);
1805 #endif
1806
1807         if (sfw_del_session_timer() != 0)
1808                 lst_wait_until(sfw_data.fw_session == NULL, sfw_data.fw_lock,
1809                                "waiting for session timer to explode.\n");
1810
1811         sfw_deactivate_session();
1812         lst_wait_until(cfs_atomic_read(&sfw_data.fw_nzombies) == 0,
1813                        sfw_data.fw_lock,
1814                        "waiting for %d zombie sessions to die.\n",
1815                        cfs_atomic_read(&sfw_data.fw_nzombies));
1816
1817         cfs_spin_unlock(&sfw_data.fw_lock);
1818
1819         for (i = 0; ; i++) {
1820                 sv = &sfw_services[i];
1821                 if (sv->sv_name == NULL)
1822                         break;
1823
1824                 srpc_shutdown_service(sv);
1825                 srpc_remove_service(sv);
1826         }
1827
1828         cfs_list_for_each_entry_typed (tsc, &sfw_data.fw_tests,
1829                                        sfw_test_case_t, tsc_list) {
1830                 sv = tsc->tsc_srv_service;
1831                 srpc_shutdown_service(sv);
1832                 srpc_remove_service(sv);
1833         }
1834
1835         while (!cfs_list_empty(&sfw_data.fw_zombie_rpcs)) {
1836                 srpc_client_rpc_t *rpc;
1837
1838                 rpc = cfs_list_entry(sfw_data.fw_zombie_rpcs.next, 
1839                                      srpc_client_rpc_t, crpc_list);
1840                 cfs_list_del(&rpc->crpc_list);
1841
1842                 LIBCFS_FREE(rpc, srpc_client_rpc_size(rpc));
1843         }
1844
1845         for (i = 0; ; i++) {
1846                 sv = &sfw_services[i];
1847                 if (sv->sv_name == NULL)
1848                         break;
1849
1850                 srpc_wait_service_shutdown(sv);
1851         }
1852
1853         while (!cfs_list_empty(&sfw_data.fw_tests)) {
1854                 tsc = cfs_list_entry(sfw_data.fw_tests.next,
1855                                      sfw_test_case_t, tsc_list);
1856
1857                 srpc_wait_service_shutdown(tsc->tsc_srv_service);
1858
1859                 cfs_list_del(&tsc->tsc_list);
1860                 LIBCFS_FREE(tsc, sizeof(*tsc));
1861         }
1862
1863         return;
1864 }