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