Whamcloud - gitweb
LU-9522 test: sanity 27z fix update local variable
[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 = { .ses_nid = LNET_NID_ANY, .ses_stamp = -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(struct lnet_process_id_packed) * 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                 struct lnet_process_id_packed *dests;
798                 struct lnet_process_id_packed  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, struct lnet_process_id 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                                                           NULL)]);
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(struct lnet_process_id_packed) *
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(struct lnet_process_id 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 }