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