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