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