Whamcloud - gitweb
749714af20c0e39396d7c410022e933b21b640d0
[fs/lustre-release.git] / lnet / selftest / conrpc.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) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lnet/selftest/conctl.c
33  *
34  * Console framework rpcs
35  *
36  * Author: Liang Zhen <liang@whamcloud.com>
37  */
38
39
40 #include <libcfs/libcfs.h>
41 #include <lnet/lib-lnet.h>
42 #include "timer.h"
43 #include "conrpc.h"
44 #include "console.h"
45
46 void lstcon_rpc_stat_reply(lstcon_rpc_trans_t *, srpc_msg_t *,
47                            lstcon_node_t *, struct lstcon_trans_stat *);
48
49 static void
50 lstcon_rpc_done(srpc_client_rpc_t *rpc)
51 {
52         lstcon_rpc_t *crpc = (lstcon_rpc_t *)rpc->crpc_priv;
53
54         LASSERT(crpc != NULL && rpc == crpc->crp_rpc);
55         LASSERT(crpc->crp_posted && !crpc->crp_finished);
56
57         spin_lock(&rpc->crpc_lock);
58
59         if (crpc->crp_trans == NULL) {
60                 /* Orphan RPC is not in any transaction,
61                  * I'm just a poor body and nobody loves me */
62                 spin_unlock(&rpc->crpc_lock);
63
64                 /* release it */
65                 lstcon_rpc_put(crpc);
66                 return;
67         }
68
69         /* not an orphan RPC */
70         crpc->crp_finished = 1;
71
72         if (crpc->crp_stamp == 0) {
73                 /* not aborted */
74                 LASSERT (crpc->crp_status == 0);
75
76                 crpc->crp_stamp  = cfs_time_current();
77                 crpc->crp_status = rpc->crpc_status;
78         }
79
80         /* wakeup (transaction)thread if I'm the last RPC in the transaction */
81         if (atomic_dec_and_test(&crpc->crp_trans->tas_remaining))
82                 wake_up(&crpc->crp_trans->tas_waitq);
83
84         spin_unlock(&rpc->crpc_lock);
85 }
86
87 static int
88 lstcon_rpc_init(lstcon_node_t *nd, int service, unsigned feats,
89                 int bulk_npg, int bulk_len, int embedded, lstcon_rpc_t *crpc)
90 {
91         crpc->crp_rpc = sfw_create_rpc(nd->nd_id, service,
92                                        feats, bulk_npg, bulk_len,
93                                        lstcon_rpc_done, (void *)crpc);
94         if (crpc->crp_rpc == NULL)
95                 return -ENOMEM;
96
97         crpc->crp_trans    = NULL;
98         crpc->crp_node     = nd;
99         crpc->crp_posted   = 0;
100         crpc->crp_finished = 0;
101         crpc->crp_unpacked = 0;
102         crpc->crp_status   = 0;
103         crpc->crp_stamp    = 0;
104         crpc->crp_embedded = embedded;
105         INIT_LIST_HEAD(&crpc->crp_link);
106
107         atomic_inc(&console_session.ses_rpc_counter);
108
109         return 0;
110 }
111
112 static int
113 lstcon_rpc_prep(lstcon_node_t *nd, int service, unsigned feats,
114                 int bulk_npg, int bulk_len, lstcon_rpc_t **crpcpp)
115 {
116         lstcon_rpc_t  *crpc = NULL;
117         int            rc;
118
119         spin_lock(&console_session.ses_rpc_lock);
120
121         if (!list_empty(&console_session.ses_rpc_freelist)) {
122                 crpc = list_entry(console_session.ses_rpc_freelist.next,
123                                   lstcon_rpc_t, crp_link);
124                 list_del_init(&crpc->crp_link);
125         }
126
127         spin_unlock(&console_session.ses_rpc_lock);
128
129         if (crpc == NULL) {
130                 LIBCFS_ALLOC(crpc, sizeof(*crpc));
131                 if (crpc == NULL)
132                         return -ENOMEM;
133         }
134
135         rc = lstcon_rpc_init(nd, service, feats, bulk_npg, bulk_len, 0, crpc);
136         if (rc == 0) {
137                 *crpcpp = crpc;
138                 return 0;
139         }
140
141         LIBCFS_FREE(crpc, sizeof(*crpc));
142
143         return rc;
144 }
145
146 void
147 lstcon_rpc_put(lstcon_rpc_t *crpc)
148 {
149         srpc_bulk_t *bulk = &crpc->crp_rpc->crpc_bulk;
150         int          i;
151
152         LASSERT(list_empty(&crpc->crp_link));
153
154         for (i = 0; i < bulk->bk_niov; i++) {
155                 if (bulk->bk_iovs[i].kiov_page == NULL)
156                         continue;
157
158                 __free_page(bulk->bk_iovs[i].kiov_page);
159         }
160
161         srpc_client_rpc_decref(crpc->crp_rpc);
162
163         if (crpc->crp_embedded) {
164                 /* embedded RPC, don't recycle it */
165                 memset(crpc, 0, sizeof(*crpc));
166                 crpc->crp_embedded = 1;
167
168         } else {
169                 spin_lock(&console_session.ses_rpc_lock);
170
171                 list_add(&crpc->crp_link,
172                          &console_session.ses_rpc_freelist);
173
174                 spin_unlock(&console_session.ses_rpc_lock);
175         }
176
177         /* RPC is not alive now */
178         atomic_dec(&console_session.ses_rpc_counter);
179 }
180
181 static void
182 lstcon_rpc_post(lstcon_rpc_t *crpc)
183 {
184         lstcon_rpc_trans_t *trans = crpc->crp_trans;
185
186         LASSERT (trans != NULL);
187
188         atomic_inc(&trans->tas_remaining);
189         crpc->crp_posted = 1;
190
191         sfw_post_rpc(crpc->crp_rpc);
192 }
193
194 static char *
195 lstcon_rpc_trans_name(int transop)
196 {
197         if (transop == LST_TRANS_SESNEW)
198                 return "SESNEW";
199
200         if (transop == LST_TRANS_SESEND)
201                 return "SESEND";
202
203         if (transop == LST_TRANS_SESQRY)
204                 return "SESQRY";
205
206         if (transop == LST_TRANS_SESPING)
207                 return "SESPING";
208
209         if (transop == LST_TRANS_TSBCLIADD)
210                 return "TSBCLIADD";
211
212         if (transop == LST_TRANS_TSBSRVADD)
213                 return "TSBSRVADD";
214
215         if (transop == LST_TRANS_TSBRUN)
216                 return "TSBRUN";
217
218         if (transop == LST_TRANS_TSBSTOP)
219                 return "TSBSTOP";
220
221         if (transop == LST_TRANS_TSBCLIQRY)
222                 return "TSBCLIQRY";
223
224         if (transop == LST_TRANS_TSBSRVQRY)
225                 return "TSBSRVQRY";
226
227         if (transop == LST_TRANS_STATQRY)
228                 return "STATQRY";
229
230         return "Unknown";
231 }
232
233 int
234 lstcon_rpc_trans_prep(struct list_head *translist, int transop,
235                       lstcon_rpc_trans_t **transpp)
236 {
237         lstcon_rpc_trans_t *trans;
238
239         if (translist != NULL) {
240                 list_for_each_entry(trans, translist, tas_link) {
241                         /* Can't enqueue two private transaction on
242                          * the same object */
243                         if ((trans->tas_opc & transop) == LST_TRANS_PRIVATE)
244                                 return -EPERM;
245                 }
246         }
247
248         /* create a trans group */
249         LIBCFS_ALLOC(trans, sizeof(*trans));
250         if (trans == NULL)
251                 return -ENOMEM;
252
253         trans->tas_opc = transop;
254
255         if (translist == NULL)
256                 INIT_LIST_HEAD(&trans->tas_olink);
257         else
258                 list_add_tail(&trans->tas_olink, translist);
259
260         list_add_tail(&trans->tas_link, &console_session.ses_trans_list);
261
262         INIT_LIST_HEAD(&trans->tas_rpcs_list);
263         atomic_set(&trans->tas_remaining, 0);
264         init_waitqueue_head(&trans->tas_waitq);
265
266         spin_lock(&console_session.ses_rpc_lock);
267         trans->tas_features = console_session.ses_features;
268         spin_unlock(&console_session.ses_rpc_lock);
269
270         *transpp = trans;
271         return 0;
272 }
273
274 void
275 lstcon_rpc_trans_addreq(lstcon_rpc_trans_t *trans, lstcon_rpc_t *crpc)
276 {
277         list_add_tail(&crpc->crp_link, &trans->tas_rpcs_list);
278         crpc->crp_trans = trans;
279 }
280
281 void
282 lstcon_rpc_trans_abort(lstcon_rpc_trans_t *trans, int error)
283 {
284         srpc_client_rpc_t *rpc;
285         lstcon_rpc_t      *crpc;
286         lstcon_node_t     *nd;
287
288         list_for_each_entry(crpc, &trans->tas_rpcs_list, crp_link) {
289                 rpc = crpc->crp_rpc;
290
291                 spin_lock(&rpc->crpc_lock);
292
293                 if (!crpc->crp_posted || /* not posted */
294                     crpc->crp_stamp != 0) { /* rpc done or aborted already */
295                         if (crpc->crp_stamp == 0) {
296                                 crpc->crp_stamp = cfs_time_current();
297                                 crpc->crp_status = -EINTR;
298                         }
299                         spin_unlock(&rpc->crpc_lock);
300                         continue;
301                 }
302
303                 crpc->crp_stamp  = cfs_time_current();
304                 crpc->crp_status = error;
305
306                 spin_unlock(&rpc->crpc_lock);
307
308                 sfw_abort_rpc(rpc);
309
310                 if (error != -ETIMEDOUT)
311                         continue;
312
313                 nd = crpc->crp_node;
314                 if (cfs_time_after(nd->nd_stamp, crpc->crp_stamp))
315                         continue;
316
317                 nd->nd_stamp = crpc->crp_stamp;
318                 nd->nd_state = LST_NODE_DOWN;
319         }
320 }
321
322 static int
323 lstcon_rpc_trans_check(lstcon_rpc_trans_t *trans)
324 {
325         if (console_session.ses_shutdown &&
326             !list_empty(&trans->tas_olink)) /* Not an end session RPC */
327                 return 1;
328
329         return (atomic_read(&trans->tas_remaining) == 0) ? 1: 0;
330 }
331
332 int
333 lstcon_rpc_trans_postwait(lstcon_rpc_trans_t *trans, int timeout)
334 {
335         lstcon_rpc_t  *crpc;
336         int            rc;
337
338         if (list_empty(&trans->tas_rpcs_list))
339                 return 0;
340
341         if (timeout < LST_TRANS_MIN_TIMEOUT)
342                 timeout = LST_TRANS_MIN_TIMEOUT;
343
344         CDEBUG(D_NET, "Transaction %s started\n",
345         lstcon_rpc_trans_name(trans->tas_opc));
346
347         /* post all requests */
348         list_for_each_entry(crpc, &trans->tas_rpcs_list, crp_link) {
349                 LASSERT(!crpc->crp_posted);
350
351                 lstcon_rpc_post(crpc);
352         }
353
354         mutex_unlock(&console_session.ses_mutex);
355
356         rc = wait_event_interruptible_timeout(trans->tas_waitq,
357                                               lstcon_rpc_trans_check(trans),
358                                               cfs_time_seconds(timeout));
359
360         rc = (rc > 0)? 0: ((rc < 0)? -EINTR: -ETIMEDOUT);
361
362         mutex_lock(&console_session.ses_mutex);
363
364         if (console_session.ses_shutdown)
365                 rc = -ESHUTDOWN;
366
367         if (rc != 0 || atomic_read(&trans->tas_remaining) != 0) {
368                 /* treat short timeout as canceled */
369                 if (rc == -ETIMEDOUT && timeout < LST_TRANS_MIN_TIMEOUT * 2)
370                         rc = -EINTR;
371
372                 lstcon_rpc_trans_abort(trans, rc);
373         }
374
375         CDEBUG(D_NET, "Transaction %s stopped: %d\n",
376                lstcon_rpc_trans_name(trans->tas_opc), rc);
377
378         lstcon_rpc_trans_stat(trans, lstcon_trans_stat());
379
380         return rc;
381 }
382
383 static int
384 lstcon_rpc_get_reply(lstcon_rpc_t *crpc, srpc_msg_t **msgpp)
385 {
386         lstcon_node_t        *nd  = crpc->crp_node;
387         srpc_client_rpc_t    *rpc = crpc->crp_rpc;
388         srpc_generic_reply_t *rep;
389
390         LASSERT (nd != NULL && rpc != NULL);
391         LASSERT (crpc->crp_stamp != 0);
392
393         if (crpc->crp_status != 0) {
394                 *msgpp = NULL;
395                 return crpc->crp_status;
396         }
397
398         *msgpp = &rpc->crpc_replymsg;
399         if (!crpc->crp_unpacked) {
400                 sfw_unpack_message(*msgpp);
401                 crpc->crp_unpacked = 1;
402         }
403
404         if (cfs_time_after(nd->nd_stamp, crpc->crp_stamp))
405                 return 0;
406
407         nd->nd_stamp = crpc->crp_stamp;
408         rep = &(*msgpp)->msg_body.reply;
409
410         if (rep->sid.ses_nid == LNET_NID_ANY)
411                 nd->nd_state = LST_NODE_UNKNOWN;
412         else if (lstcon_session_match(rep->sid))
413                 nd->nd_state = LST_NODE_ACTIVE;
414         else
415                 nd->nd_state = LST_NODE_BUSY;
416
417         return 0;
418 }
419
420 void
421 lstcon_rpc_trans_stat(lstcon_rpc_trans_t *trans, struct lstcon_trans_stat *stat)
422 {
423         lstcon_rpc_t    *crpc;
424         srpc_msg_t      *rep;
425         int              error;
426
427         LASSERT(stat != NULL);
428
429         memset(stat, 0, sizeof(*stat));
430
431         list_for_each_entry(crpc, &trans->tas_rpcs_list, crp_link) {
432                 lstcon_rpc_stat_total(stat, 1);
433
434                 LASSERT(crpc->crp_stamp != 0);
435
436                 error = lstcon_rpc_get_reply(crpc, &rep);
437                 if (error != 0) {
438                         lstcon_rpc_stat_failure(stat, 1);
439                         if (stat->trs_rpc_errno == 0)
440                                 stat->trs_rpc_errno = -error;
441
442                         continue;
443                 }
444
445                 lstcon_rpc_stat_success(stat, 1);
446
447                 lstcon_rpc_stat_reply(trans, rep, crpc->crp_node, stat);
448         }
449
450         if (trans->tas_opc == LST_TRANS_SESNEW && stat->trs_fwk_errno == 0) {
451                 stat->trs_fwk_errno =
452                       lstcon_session_feats_check(trans->tas_features);
453         }
454
455         CDEBUG(D_NET, "transaction %s : success %d, failure %d, total %d, "
456                       "RPC error(%d), Framework error(%d)\n",
457                lstcon_rpc_trans_name(trans->tas_opc),
458                lstcon_rpc_stat_success(stat, 0),
459                lstcon_rpc_stat_failure(stat, 0),
460                lstcon_rpc_stat_total(stat, 0),
461                stat->trs_rpc_errno, stat->trs_fwk_errno);
462
463         return;
464 }
465
466 int
467 lstcon_rpc_trans_interpreter(lstcon_rpc_trans_t *trans,
468                              struct list_head __user *head_up,
469                              lstcon_rpc_readent_func_t readent)
470 {
471         struct list_head      tmp;
472         struct list_head     __user *next;
473         struct lstcon_rpc_ent     *ent;
474         srpc_generic_reply_t *rep;
475         lstcon_rpc_t         *crpc;
476         srpc_msg_t           *msg;
477         lstcon_node_t        *nd;
478         cfs_duration_t        dur;
479         struct timeval        tv;
480         int                   error;
481
482         LASSERT(head_up != NULL);
483
484         next = head_up;
485
486         list_for_each_entry(crpc, &trans->tas_rpcs_list, crp_link) {
487                 if (copy_from_user(&tmp, next,
488                                    sizeof(struct list_head)))
489                         return -EFAULT;
490
491                 if (tmp.next == head_up)
492                         return 0;
493
494                 next = tmp.next;
495
496                 ent = list_entry(next, struct lstcon_rpc_ent, rpe_link);
497
498                 LASSERT(crpc->crp_stamp != 0);
499
500                 error = lstcon_rpc_get_reply(crpc, &msg);
501
502                 nd = crpc->crp_node;
503
504                 dur = (cfs_duration_t)cfs_time_sub(crpc->crp_stamp,
505                       (cfs_time_t)console_session.ses_id.ses_stamp);
506                 jiffies_to_timeval(dur, &tv);
507
508                 if (copy_to_user(&ent->rpe_peer,
509                                  &nd->nd_id, sizeof(struct lnet_process_id)) ||
510                     copy_to_user(&ent->rpe_stamp, &tv, sizeof(tv)) ||
511                     copy_to_user(&ent->rpe_state,
512                                  &nd->nd_state, sizeof(nd->nd_state)) ||
513                     copy_to_user(&ent->rpe_rpc_errno, &error,
514                                      sizeof(error)))
515                         return -EFAULT;
516
517                 if (error != 0)
518                         continue;
519
520                 /* RPC is done */
521                 rep = (srpc_generic_reply_t *)&msg->msg_body.reply;
522
523                 if (copy_to_user(&ent->rpe_sid,
524                                  &rep->sid, sizeof(rep->sid)) ||
525                     copy_to_user(&ent->rpe_fwk_errno,
526                                  &rep->status, sizeof(rep->status)))
527                         return -EFAULT;
528
529                 if (readent == NULL)
530                         continue;
531
532                 error = readent(trans->tas_opc, msg, ent);
533                 if (error != 0)
534                         return error;
535         }
536
537         return 0;
538 }
539
540 void
541 lstcon_rpc_trans_destroy(lstcon_rpc_trans_t *trans)
542 {
543         srpc_client_rpc_t *rpc;
544         lstcon_rpc_t      *crpc;
545         lstcon_rpc_t      *tmp;
546         int                count = 0;
547
548         list_for_each_entry_safe(crpc, tmp, &trans->tas_rpcs_list, crp_link) {
549                 rpc = crpc->crp_rpc;
550
551                 spin_lock(&rpc->crpc_lock);
552
553                 /* free it if not posted or finished already */
554                 if (!crpc->crp_posted || crpc->crp_finished) {
555                         spin_unlock(&rpc->crpc_lock);
556
557                         list_del_init(&crpc->crp_link);
558                         lstcon_rpc_put(crpc);
559
560                         continue;
561                 }
562
563                 /* rpcs can be still not callbacked (even LNetMDUnlink is
564                  * called) because huge timeout for inaccessible network,
565                  * don't make user wait for them, just abandon them, they
566                  * will be recycled in callback */
567
568                 LASSERT(crpc->crp_status != 0);
569
570                 crpc->crp_node  = NULL;
571                 crpc->crp_trans = NULL;
572                 list_del_init(&crpc->crp_link);
573                 count++;
574
575                 spin_unlock(&rpc->crpc_lock);
576
577                 atomic_dec(&trans->tas_remaining);
578         }
579
580         LASSERT(atomic_read(&trans->tas_remaining) == 0);
581
582         list_del(&trans->tas_link);
583         if (!list_empty(&trans->tas_olink))
584                 list_del(&trans->tas_olink);
585
586         CDEBUG(D_NET, "Transaction %s destroyed with %d pending RPCs\n",
587                lstcon_rpc_trans_name(trans->tas_opc), count);
588
589         LIBCFS_FREE(trans, sizeof(*trans));
590
591         return;
592 }
593
594 int
595 lstcon_sesrpc_prep(lstcon_node_t *nd, int transop,
596                    unsigned feats, lstcon_rpc_t **crpc)
597 {
598         srpc_mksn_reqst_t *msrq;
599         srpc_rmsn_reqst_t *rsrq;
600         int                rc;
601
602         switch (transop) {
603         case LST_TRANS_SESNEW:
604                 rc = lstcon_rpc_prep(nd, SRPC_SERVICE_MAKE_SESSION,
605                                      feats, 0, 0, crpc);
606                 if (rc != 0)
607                         return rc;
608
609                 msrq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.mksn_reqst;
610                 msrq->mksn_sid     = console_session.ses_id;
611                 msrq->mksn_force   = console_session.ses_force;
612                 strlcpy(msrq->mksn_name, console_session.ses_name,
613                         sizeof(msrq->mksn_name));
614                 break;
615
616         case LST_TRANS_SESEND:
617                 rc = lstcon_rpc_prep(nd, SRPC_SERVICE_REMOVE_SESSION,
618                                      feats, 0, 0, crpc);
619                 if (rc != 0)
620                         return rc;
621
622                 rsrq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.rmsn_reqst;
623                 rsrq->rmsn_sid = console_session.ses_id;
624                 break;
625
626         default:
627                 LBUG();
628         }
629
630         return 0;
631 }
632
633 int
634 lstcon_dbgrpc_prep(lstcon_node_t *nd, unsigned feats, lstcon_rpc_t **crpc)
635 {
636         srpc_debug_reqst_t *drq;
637         int                 rc;
638
639         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_DEBUG, feats, 0, 0, crpc);
640         if (rc != 0)
641                 return rc;
642
643         drq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.dbg_reqst;
644
645         drq->dbg_sid   = console_session.ses_id;
646         drq->dbg_flags = 0;
647
648         return rc;
649 }
650
651 int
652 lstcon_batrpc_prep(lstcon_node_t *nd, int transop, unsigned feats,
653                    lstcon_tsb_hdr_t *tsb, lstcon_rpc_t **crpc)
654 {
655         lstcon_batch_t     *batch;
656         srpc_batch_reqst_t *brq;
657         int                 rc;
658
659         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_BATCH, feats, 0, 0, crpc);
660         if (rc != 0)
661                 return rc;
662
663         brq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.bat_reqst;
664
665         brq->bar_sid     = console_session.ses_id;
666         brq->bar_bid     = tsb->tsb_id;
667         brq->bar_testidx = tsb->tsb_index;
668         brq->bar_opc     = transop == LST_TRANS_TSBRUN ? SRPC_BATCH_OPC_RUN :
669                            (transop == LST_TRANS_TSBSTOP ? SRPC_BATCH_OPC_STOP:
670                             SRPC_BATCH_OPC_QUERY);
671
672         if (transop != LST_TRANS_TSBRUN &&
673             transop != LST_TRANS_TSBSTOP)
674                 return 0;
675
676         LASSERT (tsb->tsb_index == 0);
677
678         batch = (lstcon_batch_t *)tsb;
679         brq->bar_arg = batch->bat_arg;
680
681         return 0;
682 }
683
684 int
685 lstcon_statrpc_prep(lstcon_node_t *nd, unsigned feats, lstcon_rpc_t **crpc)
686 {
687         srpc_stat_reqst_t *srq;
688         int                rc;
689
690         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_QUERY_STAT, feats, 0, 0, crpc);
691         if (rc != 0)
692                 return rc;
693
694         srq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.stat_reqst;
695
696         srq->str_sid  = console_session.ses_id;
697         srq->str_type = 0; /* XXX remove it */
698
699         return 0;
700 }
701
702 static struct lnet_process_id_packed *
703 lstcon_next_id(int idx, int nkiov, lnet_kiov_t *kiov)
704 {
705         struct lnet_process_id_packed *pid;
706         int                       i;
707
708         i = idx / SFW_ID_PER_PAGE;
709
710         LASSERT (i < nkiov);
711
712         pid = (struct lnet_process_id_packed *)page_address(kiov[i].kiov_page);
713
714         return &pid[idx % SFW_ID_PER_PAGE];
715 }
716
717 static int
718 lstcon_dstnodes_prep(lstcon_group_t *grp, int idx,
719                      int dist, int span, int nkiov, lnet_kiov_t *kiov)
720 {
721         struct lnet_process_id_packed *pid;
722         lstcon_ndlink_t          *ndl;
723         lstcon_node_t            *nd;
724         int                       start;
725         int                       end;
726         int                       i = 0;
727
728         LASSERT (dist >= 1);
729         LASSERT (span >= 1);
730         LASSERT (grp->grp_nnode >= 1);
731
732         if (span > grp->grp_nnode)
733                 return -EINVAL;
734
735         start = ((idx / dist) * span) % grp->grp_nnode;
736         end   = ((idx / dist) * span + span - 1) % grp->grp_nnode;
737
738         list_for_each_entry(ndl, &grp->grp_ndl_list, ndl_link) {
739                 nd = ndl->ndl_node;
740                 if (i < start) {
741                         i++;
742                         continue;
743                 }
744
745                 if (i > (end >= start ? end : grp->grp_nnode))
746                         break;
747
748                 pid = lstcon_next_id((i - start), nkiov, kiov);
749                 pid->nid = nd->nd_id.nid;
750                 pid->pid = nd->nd_id.pid;
751                 i++;
752         }
753
754         if (start <= end)       /* done */
755                 return 0;
756
757         list_for_each_entry(ndl, &grp->grp_ndl_list, ndl_link) {
758                 if (i > grp->grp_nnode + end)
759                         break;
760
761                 nd = ndl->ndl_node;
762                 pid = lstcon_next_id((i - start), nkiov, kiov);
763                 pid->nid = nd->nd_id.nid;
764                 pid->pid = nd->nd_id.pid;
765                 i++;
766         }
767
768         return 0;
769 }
770
771 static int
772 lstcon_pingrpc_prep(struct lst_test_ping_param *param, srpc_test_reqst_t *req)
773 {
774         test_ping_req_t *prq = &req->tsr_u.ping;
775
776         prq->png_size   = param->png_size;
777         prq->png_flags  = param->png_flags;
778         /* TODO dest */
779         return 0;
780 }
781
782 static int
783 lstcon_bulkrpc_v0_prep(struct lst_test_bulk_param *param, srpc_test_reqst_t *req)
784 {
785         test_bulk_req_t *brq = &req->tsr_u.bulk_v0;
786
787         brq->blk_opc    = param->blk_opc;
788         brq->blk_npg    = (param->blk_size + PAGE_SIZE - 1) /
789                            PAGE_SIZE;
790         brq->blk_flags  = param->blk_flags;
791
792         return 0;
793 }
794
795 static int
796 lstcon_bulkrpc_v1_prep(struct lst_test_bulk_param *param, bool is_client,
797                        srpc_test_reqst_t *req)
798 {
799         test_bulk_req_v1_t *brq = &req->tsr_u.bulk_v1;
800
801         brq->blk_opc    = param->blk_opc;
802         brq->blk_flags  = param->blk_flags;
803         brq->blk_len    = param->blk_size;
804         brq->blk_offset = is_client ? param->blk_cli_off : param->blk_srv_off;
805
806         return 0;
807 }
808
809 int
810 lstcon_testrpc_prep(lstcon_node_t *nd, int transop, unsigned feats,
811                     lstcon_test_t *test, lstcon_rpc_t **crpc)
812 {
813         lstcon_group_t    *sgrp = test->tes_src_grp;
814         lstcon_group_t    *dgrp = test->tes_dst_grp;
815         srpc_test_reqst_t *trq;
816         srpc_bulk_t       *bulk;
817         int                i;
818         int                npg = 0;
819         int                nob = 0;
820         int                rc  = 0;
821
822         if (transop == LST_TRANS_TSBCLIADD) {
823                 npg = sfw_id_pages(test->tes_span);
824                 nob = (feats & LST_FEAT_BULK_LEN) == 0 ?
825                       npg * PAGE_SIZE :
826                       sizeof(struct lnet_process_id_packed) * test->tes_span;
827         }
828
829         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_TEST, feats, npg, nob, crpc);
830         if (rc != 0)
831                 return rc;
832
833         trq  = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.tes_reqst;
834
835         if (transop == LST_TRANS_TSBSRVADD) {
836                 int ndist = (sgrp->grp_nnode + test->tes_dist - 1) / test->tes_dist;
837                 int nspan = (dgrp->grp_nnode + test->tes_span - 1) / test->tes_span;
838                 int nmax = (ndist + nspan - 1) / nspan;
839
840                 trq->tsr_ndest = 0;
841                 trq->tsr_loop  = nmax * test->tes_dist * test->tes_concur;
842
843         } else {
844                 bulk = &(*crpc)->crp_rpc->crpc_bulk;
845
846                 for (i = 0; i < npg; i++) {
847                         int     len;
848
849                         LASSERT(nob > 0);
850
851                         len = (feats & LST_FEAT_BULK_LEN) == 0 ?
852                               PAGE_SIZE : min_t(int, nob, PAGE_SIZE);
853                         nob -= len;
854
855                         bulk->bk_iovs[i].kiov_offset = 0;
856                         bulk->bk_iovs[i].kiov_len    = len;
857                         bulk->bk_iovs[i].kiov_page   =
858                                 alloc_page(GFP_KERNEL);
859
860                         if (bulk->bk_iovs[i].kiov_page == NULL) {
861                                 lstcon_rpc_put(*crpc);
862                                 return -ENOMEM;
863                         }
864                 }
865
866                 bulk->bk_sink = 0;
867
868                 LASSERT (transop == LST_TRANS_TSBCLIADD);
869
870                 rc = lstcon_dstnodes_prep(test->tes_dst_grp,
871                                           test->tes_cliidx++,
872                                           test->tes_dist,
873                                           test->tes_span,
874                                           npg, &bulk->bk_iovs[0]);
875                 if (rc != 0) {
876                         lstcon_rpc_put(*crpc);
877                         return rc;
878                 }
879
880                 trq->tsr_ndest = test->tes_span;
881                 trq->tsr_loop  = test->tes_loop;
882         }
883
884         trq->tsr_sid        = console_session.ses_id;
885         trq->tsr_bid        = test->tes_hdr.tsb_id;
886         trq->tsr_concur     = test->tes_concur;
887         trq->tsr_is_client  = (transop == LST_TRANS_TSBCLIADD) ? 1 : 0;
888         trq->tsr_stop_onerr = !!test->tes_stop_onerr;
889
890         switch (test->tes_type) {
891         case LST_TEST_PING:
892                 trq->tsr_service = SRPC_SERVICE_PING;
893                 rc = lstcon_pingrpc_prep((struct lst_test_ping_param *)
894                                          &test->tes_param[0], trq);
895                 break;
896
897         case LST_TEST_BULK:
898                 trq->tsr_service = SRPC_SERVICE_BRW;
899                 if ((feats & LST_FEAT_BULK_LEN) == 0) {
900                         rc = lstcon_bulkrpc_v0_prep((struct lst_test_bulk_param *)
901                                                     &test->tes_param[0], trq);
902                 } else {
903                         rc = lstcon_bulkrpc_v1_prep((struct lst_test_bulk_param *)
904                                                     &test->tes_param[0],
905                                                     trq->tsr_is_client, trq);
906                 }
907
908                 break;
909         default:
910                 LBUG();
911                 break;
912         }
913
914         return rc;
915 }
916
917 static int
918 lstcon_sesnew_stat_reply(lstcon_rpc_trans_t *trans,
919                          lstcon_node_t *nd, srpc_msg_t *reply)
920 {
921         srpc_mksn_reply_t *mksn_rep = &reply->msg_body.mksn_reply;
922         int                status   = mksn_rep->mksn_status;
923
924         if (status == 0 &&
925             (reply->msg_ses_feats & ~LST_FEATS_MASK) != 0) {
926                 mksn_rep->mksn_status = EPROTO;
927                 status = EPROTO;
928         }
929
930         if (status == EPROTO) {
931                 CNETERR("session protocol error from %s: %u\n",
932                         libcfs_nid2str(nd->nd_id.nid),
933                         reply->msg_ses_feats);
934         }
935
936         if (status != 0)
937                 return status;
938
939         if (!trans->tas_feats_updated) {
940                 spin_lock(&console_session.ses_rpc_lock);
941                 if (!trans->tas_feats_updated) { /* recheck with lock */
942                         trans->tas_feats_updated = 1;
943                         trans->tas_features = reply->msg_ses_feats;
944                 }
945                 spin_unlock(&console_session.ses_rpc_lock);
946         }
947
948         if (reply->msg_ses_feats != trans->tas_features) {
949                 CNETERR("Framework features %x from %s is different with "
950                         "features on this transaction: %x\n",
951                          reply->msg_ses_feats, libcfs_nid2str(nd->nd_id.nid),
952                          trans->tas_features);
953                 status = mksn_rep->mksn_status = EPROTO;
954         }
955
956         if (status == 0) {
957                 /* session timeout on remote node */
958                 nd->nd_timeout = mksn_rep->mksn_timeout;
959         }
960
961         return status;
962 }
963
964 void
965 lstcon_rpc_stat_reply(lstcon_rpc_trans_t *trans, srpc_msg_t *msg,
966                       lstcon_node_t *nd, struct lstcon_trans_stat *stat)
967 {
968         srpc_rmsn_reply_t  *rmsn_rep;
969         srpc_debug_reply_t *dbg_rep;
970         srpc_batch_reply_t *bat_rep;
971         srpc_test_reply_t  *test_rep;
972         srpc_stat_reply_t  *stat_rep;
973         int                 rc = 0;
974
975         switch (trans->tas_opc) {
976         case LST_TRANS_SESNEW:
977                 rc = lstcon_sesnew_stat_reply(trans, nd, msg);
978                 if (rc == 0) {
979                         lstcon_sesop_stat_success(stat, 1);
980                         return;
981                 }
982
983                 lstcon_sesop_stat_failure(stat, 1);
984                 break;
985
986         case LST_TRANS_SESEND:
987                 rmsn_rep = &msg->msg_body.rmsn_reply;
988                 /* ESRCH is not an error for end session */
989                 if (rmsn_rep->rmsn_status == 0 ||
990                     rmsn_rep->rmsn_status == ESRCH) {
991                         lstcon_sesop_stat_success(stat, 1);
992                         return;
993                 }
994
995                 lstcon_sesop_stat_failure(stat, 1);
996                 rc = rmsn_rep->rmsn_status;
997                 break;
998
999         case LST_TRANS_SESQRY:
1000         case LST_TRANS_SESPING:
1001                 dbg_rep = &msg->msg_body.dbg_reply;
1002
1003                 if (dbg_rep->dbg_status == ESRCH) {
1004                         lstcon_sesqry_stat_unknown(stat, 1);
1005                         return;
1006                 }
1007
1008                 if (lstcon_session_match(dbg_rep->dbg_sid))
1009                         lstcon_sesqry_stat_active(stat, 1);
1010                 else
1011                         lstcon_sesqry_stat_busy(stat, 1);
1012                 return;
1013
1014         case LST_TRANS_TSBRUN:
1015         case LST_TRANS_TSBSTOP:
1016                 bat_rep = &msg->msg_body.bat_reply;
1017
1018                 if (bat_rep->bar_status == 0) {
1019                         lstcon_tsbop_stat_success(stat, 1);
1020                         return;
1021                 }
1022
1023                 if (bat_rep->bar_status == EPERM &&
1024                     trans->tas_opc == LST_TRANS_TSBSTOP) {
1025                         lstcon_tsbop_stat_success(stat, 1);
1026                         return;
1027                 }
1028
1029                 lstcon_tsbop_stat_failure(stat, 1);
1030                 rc = bat_rep->bar_status;
1031                 break;
1032
1033         case LST_TRANS_TSBCLIQRY:
1034         case LST_TRANS_TSBSRVQRY:
1035                 bat_rep = &msg->msg_body.bat_reply;
1036
1037                 if (bat_rep->bar_active != 0)
1038                         lstcon_tsbqry_stat_run(stat, 1);
1039                 else
1040                         lstcon_tsbqry_stat_idle(stat, 1);
1041
1042                 if (bat_rep->bar_status == 0)
1043                         return;
1044
1045                 lstcon_tsbqry_stat_failure(stat, 1);
1046                 rc = bat_rep->bar_status;
1047                 break;
1048
1049         case LST_TRANS_TSBCLIADD:
1050         case LST_TRANS_TSBSRVADD:
1051                 test_rep = &msg->msg_body.tes_reply;
1052
1053                 if (test_rep->tsr_status == 0) {
1054                         lstcon_tsbop_stat_success(stat, 1);
1055                         return;
1056                 }
1057
1058                 lstcon_tsbop_stat_failure(stat, 1);
1059                 rc = test_rep->tsr_status;
1060                 break;
1061
1062         case LST_TRANS_STATQRY:
1063                 stat_rep = &msg->msg_body.stat_reply;
1064
1065                 if (stat_rep->str_status == 0) {
1066                         lstcon_statqry_stat_success(stat, 1);
1067                         return;
1068                 }
1069
1070                 lstcon_statqry_stat_failure(stat, 1);
1071                 rc = stat_rep->str_status;
1072                 break;
1073
1074         default:
1075                 LBUG();
1076         }
1077
1078         if (stat->trs_fwk_errno == 0)
1079                 stat->trs_fwk_errno = rc;
1080
1081         return;
1082 }
1083
1084 int
1085 lstcon_rpc_trans_ndlist(struct list_head *ndlist,
1086                         struct list_head *translist, int transop,
1087                         void *arg, lstcon_rpc_cond_func_t condition,
1088                         lstcon_rpc_trans_t **transpp)
1089 {
1090         lstcon_rpc_trans_t *trans;
1091         lstcon_ndlink_t    *ndl;
1092         lstcon_node_t      *nd;
1093         lstcon_rpc_t       *rpc;
1094         unsigned            feats;
1095         int                 rc;
1096
1097         /* Creating session RPG for list of nodes */
1098
1099         rc = lstcon_rpc_trans_prep(translist, transop, &trans);
1100         if (rc != 0) {
1101                 CERROR("Can't create transaction %d: %d\n", transop, rc);
1102                 return rc;
1103         }
1104
1105         feats = trans->tas_features;
1106         list_for_each_entry(ndl, ndlist, ndl_link) {
1107                 rc = condition == NULL ? 1 :
1108                      condition(transop, ndl->ndl_node, arg);
1109
1110                 if (rc == 0)
1111                         continue;
1112
1113                 if (rc < 0) {
1114                         CDEBUG(D_NET, "Condition error while creating RPC "
1115                                       " for transaction %d: %d\n", transop, rc);
1116                         break;
1117                 }
1118
1119                 nd = ndl->ndl_node;
1120
1121                 switch (transop) {
1122                 case LST_TRANS_SESNEW:
1123                 case LST_TRANS_SESEND:
1124                         rc = lstcon_sesrpc_prep(nd, transop, feats, &rpc);
1125                         break;
1126                 case LST_TRANS_SESQRY:
1127                 case LST_TRANS_SESPING:
1128                         rc = lstcon_dbgrpc_prep(nd, feats, &rpc);
1129                         break;
1130                 case LST_TRANS_TSBCLIADD:
1131                 case LST_TRANS_TSBSRVADD:
1132                         rc = lstcon_testrpc_prep(nd, transop, feats,
1133                                                  (lstcon_test_t *)arg, &rpc);
1134                         break;
1135                 case LST_TRANS_TSBRUN:
1136                 case LST_TRANS_TSBSTOP:
1137                 case LST_TRANS_TSBCLIQRY:
1138                 case LST_TRANS_TSBSRVQRY:
1139                         rc = lstcon_batrpc_prep(nd, transop, feats,
1140                                                 (lstcon_tsb_hdr_t *)arg, &rpc);
1141                         break;
1142                 case LST_TRANS_STATQRY:
1143                         rc = lstcon_statrpc_prep(nd, feats, &rpc);
1144                         break;
1145                 default:
1146                         rc = -EINVAL;
1147                         break;
1148                 }
1149
1150                 if (rc != 0) {
1151                         CERROR("Failed to create RPC for transaction %s: %d\n",
1152                                lstcon_rpc_trans_name(transop), rc);
1153                         break;
1154                 }
1155
1156                 lstcon_rpc_trans_addreq(trans, rpc);
1157         }
1158
1159         if (rc == 0) {
1160                 *transpp = trans;
1161                 return 0;
1162         }
1163
1164         lstcon_rpc_trans_destroy(trans);
1165
1166         return rc;
1167 }
1168
1169 static void
1170 lstcon_rpc_pinger(void *arg)
1171 {
1172         stt_timer_t        *ptimer = (stt_timer_t *)arg;
1173         lstcon_rpc_trans_t *trans;
1174         lstcon_rpc_t       *crpc;
1175         srpc_msg_t         *rep;
1176         srpc_debug_reqst_t *drq;
1177         lstcon_ndlink_t    *ndl;
1178         lstcon_node_t      *nd;
1179         int intv;
1180         int                 count = 0;
1181         int                 rc;
1182
1183         /* RPC pinger is a special case of transaction,
1184          * it's called by timer at 8 seconds interval.
1185          */
1186         mutex_lock(&console_session.ses_mutex);
1187
1188         if (console_session.ses_shutdown || console_session.ses_expired) {
1189                 mutex_unlock(&console_session.ses_mutex);
1190                 return;
1191         }
1192
1193         if (!console_session.ses_expired &&
1194             ktime_get_real_seconds() - console_session.ses_laststamp >
1195             (time64_t)console_session.ses_timeout)
1196                 console_session.ses_expired = 1;
1197
1198         trans = console_session.ses_ping;
1199
1200         LASSERT(trans != NULL);
1201
1202         list_for_each_entry(ndl, &console_session.ses_ndl_list, ndl_link) {
1203                 nd = ndl->ndl_node;
1204
1205                 if (console_session.ses_expired) {
1206                         /* idle console, end session on all nodes */
1207                         if (nd->nd_state != LST_NODE_ACTIVE)
1208                                 continue;
1209
1210                         rc = lstcon_sesrpc_prep(nd, LST_TRANS_SESEND,
1211                                                 trans->tas_features, &crpc);
1212                         if (rc != 0) {
1213                                 CERROR("Out of memory\n");
1214                                 break;
1215                         }
1216
1217                         lstcon_rpc_trans_addreq(trans, crpc);
1218                         lstcon_rpc_post(crpc);
1219
1220                         continue;
1221                 }
1222
1223                 crpc = &nd->nd_ping;
1224
1225                 if (crpc->crp_rpc != NULL) {
1226                         LASSERT(crpc->crp_trans == trans);
1227                         LASSERT(!list_empty(&crpc->crp_link));
1228
1229                         spin_lock(&crpc->crp_rpc->crpc_lock);
1230
1231                         LASSERT(crpc->crp_posted);
1232
1233                         if (!crpc->crp_finished) {
1234                                 /* in flight */
1235                                 spin_unlock(&crpc->crp_rpc->crpc_lock);
1236                                 continue;
1237                         }
1238
1239                         spin_unlock(&crpc->crp_rpc->crpc_lock);
1240
1241                         lstcon_rpc_get_reply(crpc, &rep);
1242
1243                         list_del_init(&crpc->crp_link);
1244
1245                         lstcon_rpc_put(crpc);
1246                 }
1247
1248                 if (nd->nd_state != LST_NODE_ACTIVE)
1249                         continue;
1250
1251                 intv = cfs_duration_sec(jiffies - nd->nd_stamp);
1252                 if (intv < nd->nd_timeout / 2)
1253                         continue;
1254
1255                 rc = lstcon_rpc_init(nd, SRPC_SERVICE_DEBUG,
1256                                      trans->tas_features, 0, 0, 1, crpc);
1257                 if (rc != 0) {
1258                         CERROR("Out of memory\n");
1259                         break;
1260                 }
1261
1262                 drq = &crpc->crp_rpc->crpc_reqstmsg.msg_body.dbg_reqst;
1263
1264                 drq->dbg_sid   = console_session.ses_id;
1265                 drq->dbg_flags = 0;
1266
1267                 lstcon_rpc_trans_addreq(trans, crpc);
1268                 lstcon_rpc_post(crpc);
1269
1270                 count++;
1271         }
1272
1273         if (console_session.ses_expired) {
1274                 mutex_unlock(&console_session.ses_mutex);
1275                 return;
1276         }
1277
1278         CDEBUG(D_NET, "Ping %d nodes in session\n", count);
1279
1280         ptimer->stt_expires = ktime_get_real_seconds() + LST_PING_INTERVAL;
1281         stt_add_timer(ptimer);
1282
1283         mutex_unlock(&console_session.ses_mutex);
1284 }
1285
1286 int
1287 lstcon_rpc_pinger_start(void)
1288 {
1289         stt_timer_t     *ptimer;
1290         int              rc;
1291
1292         LASSERT(list_empty(&console_session.ses_rpc_freelist));
1293         LASSERT(atomic_read(&console_session.ses_rpc_counter) == 0);
1294
1295         rc = lstcon_rpc_trans_prep(NULL, LST_TRANS_SESPING,
1296                                    &console_session.ses_ping);
1297         if (rc != 0) {
1298                 CERROR("Failed to create console pinger\n");
1299                 return rc;
1300         }
1301
1302         ptimer = &console_session.ses_ping_timer;
1303         ptimer->stt_expires = ktime_get_real_seconds() + LST_PING_INTERVAL;
1304
1305         stt_add_timer(ptimer);
1306
1307         return 0;
1308 }
1309
1310 void
1311 lstcon_rpc_pinger_stop(void)
1312 {
1313         LASSERT (console_session.ses_shutdown);
1314
1315         stt_del_timer(&console_session.ses_ping_timer);
1316
1317         lstcon_rpc_trans_abort(console_session.ses_ping, -ESHUTDOWN);
1318         lstcon_rpc_trans_stat(console_session.ses_ping, lstcon_trans_stat());
1319         lstcon_rpc_trans_destroy(console_session.ses_ping);
1320
1321         memset(lstcon_trans_stat(), 0, sizeof(struct lstcon_trans_stat));
1322
1323         console_session.ses_ping = NULL;
1324 }
1325
1326 void
1327 lstcon_rpc_cleanup_wait(void)
1328 {
1329         lstcon_rpc_trans_t      *trans;
1330         lstcon_rpc_t            *crpc;
1331         struct list_head        *pacer;
1332         struct list_head         zlist;
1333
1334         /* Called with hold of global mutex */
1335
1336         LASSERT(console_session.ses_shutdown);
1337
1338         while (!list_empty(&console_session.ses_trans_list)) {
1339                 list_for_each(pacer, &console_session.ses_trans_list) {
1340                         trans = list_entry(pacer, lstcon_rpc_trans_t,
1341                                            tas_link);
1342
1343                         CDEBUG(D_NET, "Session closed, wakeup transaction %s\n",
1344                                lstcon_rpc_trans_name(trans->tas_opc));
1345
1346                         wake_up(&trans->tas_waitq);
1347                 }
1348
1349                 mutex_unlock(&console_session.ses_mutex);
1350
1351                 CWARN("Session is shutting down, "
1352                       "waiting for termination of transactions\n");
1353                 set_current_state(TASK_UNINTERRUPTIBLE);
1354                 schedule_timeout(cfs_time_seconds(1));
1355
1356                 mutex_lock(&console_session.ses_mutex);
1357         }
1358
1359         spin_lock(&console_session.ses_rpc_lock);
1360
1361         lst_wait_until((atomic_read(&console_session.ses_rpc_counter) == 0),
1362                        console_session.ses_rpc_lock,
1363                        "Network is not accessable or target is down, "
1364                        "waiting for %d console RPCs to being recycled\n",
1365                        atomic_read(&console_session.ses_rpc_counter));
1366
1367         list_add(&zlist, &console_session.ses_rpc_freelist);
1368         list_del_init(&console_session.ses_rpc_freelist);
1369
1370         spin_unlock(&console_session.ses_rpc_lock);
1371
1372         while (!list_empty(&zlist)) {
1373                 crpc = list_entry(zlist.next, lstcon_rpc_t, crp_link);
1374
1375                 list_del(&crpc->crp_link);
1376                 LIBCFS_FREE(crpc, sizeof(lstcon_rpc_t));
1377         }
1378 }
1379
1380 int
1381 lstcon_rpc_module_init(void)
1382 {
1383         INIT_LIST_HEAD(&console_session.ses_ping_timer.stt_list);
1384         console_session.ses_ping_timer.stt_func = lstcon_rpc_pinger;
1385         console_session.ses_ping_timer.stt_data = &console_session.ses_ping_timer;
1386
1387         console_session.ses_ping = NULL;
1388
1389         spin_lock_init(&console_session.ses_rpc_lock);
1390         atomic_set(&console_session.ses_rpc_counter, 0);
1391         INIT_LIST_HEAD(&console_session.ses_rpc_freelist);
1392
1393         return 0;
1394 }
1395
1396 void
1397 lstcon_rpc_module_fini(void)
1398 {
1399         LASSERT(list_empty(&console_session.ses_rpc_freelist));
1400         LASSERT(atomic_read(&console_session.ses_rpc_counter) == 0);
1401 }
1402