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