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