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