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