Whamcloud - gitweb
Introduce .gitignore files.
[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                 cfs_list_for_each_entry_typed(trans, translist,
246                                               lstcon_rpc_trans_t, tas_link) {
247                         /* Can't enqueue two private transaction on
248                          * the same object */
249                         if ((trans->tas_opc & transop) == LST_TRANS_PRIVATE)
250                                 return -EPERM;
251                 }
252         }
253
254         /* create a trans group */
255         LIBCFS_ALLOC(trans, sizeof(*trans));
256         if (trans == NULL)
257                 return -ENOMEM;
258         
259         trans->tas_opc = transop;
260
261         if (translist == NULL)       
262                 CFS_INIT_LIST_HEAD(&trans->tas_olink);
263         else
264                 list_add_tail(&trans->tas_olink, translist);
265
266         list_add_tail(&trans->tas_link, &console_session.ses_trans_list);
267
268         CFS_INIT_LIST_HEAD(&trans->tas_rpcs_list);
269         atomic_set(&trans->tas_remaining, 0);
270         cfs_waitq_init(&trans->tas_waitq);
271
272         *transpp = trans;
273
274         return 0;
275 }
276
277 void
278 lstcon_rpc_trans_addreq(lstcon_rpc_trans_t *trans, lstcon_rpc_t *crpc)
279 {
280         list_add_tail(&crpc->crp_link, &trans->tas_rpcs_list);
281         crpc->crp_trans = trans;
282 }
283
284 void
285 lstcon_rpc_trans_abort(lstcon_rpc_trans_t *trans, int error)
286 {
287         srpc_client_rpc_t *rpc;
288         lstcon_rpc_t      *crpc;
289         lstcon_node_t     *nd;
290
291         cfs_list_for_each_entry_typed (crpc, &trans->tas_rpcs_list,
292                                        lstcon_rpc_t, crp_link) {
293                 rpc = crpc->crp_rpc;
294
295                 spin_lock(&rpc->crpc_lock);
296
297                 if (!crpc->crp_posted || crpc->crp_stamp != 0) {
298                         /* rpc done or aborted already */
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)) /* It's 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         cfs_list_for_each_entry_typed (crpc, &trans->tas_rpcs_list,
349                                        lstcon_rpc_t, crp_link) {
350                 LASSERT (!crpc->crp_posted);
351
352                 lstcon_rpc_post(crpc);
353         }
354
355         mutex_up(&console_session.ses_mutex);
356
357         cfs_waitq_wait_event_interruptible_timeout(trans->tas_waitq,
358                                               lstcon_rpc_trans_check(trans),
359                                               cfs_time_seconds(timeout), rc);
360
361         rc = (rc > 0)? 0: ((rc < 0)? -EINTR: -ETIMEDOUT);
362
363         mutex_down(&console_session.ses_mutex);
364
365         if (console_session.ses_shutdown)
366                 rc = -ESHUTDOWN;
367
368         if (rc != 0) {
369                 /* treat short timeout as canceled */
370                 if (rc == -ETIMEDOUT && timeout < LST_TRANS_MIN_TIMEOUT * 2)
371                         rc = -EINTR;
372
373                 lstcon_rpc_trans_abort(trans, rc);
374         }
375
376         CDEBUG(D_NET, "Transaction %s stopped: %d\n",
377                lstcon_rpc_trans_name(trans->tas_opc), rc);
378
379         lstcon_rpc_trans_stat(trans, lstcon_trans_stat());
380
381         return rc;
382 }
383
384 int
385 lstcon_rpc_get_reply(lstcon_rpc_t *crpc, srpc_msg_t **msgpp)
386 {
387         lstcon_node_t        *nd  = crpc->crp_node;
388         srpc_client_rpc_t    *rpc = crpc->crp_rpc;
389         srpc_generic_reply_t *rep;
390
391         LASSERT (nd != NULL && rpc != NULL);
392         LASSERT (crpc->crp_stamp != 0);
393
394         if (crpc->crp_status != 0) {
395                 *msgpp = NULL;
396                 return crpc->crp_status;
397         }
398
399         *msgpp = &rpc->crpc_replymsg;
400         if (!crpc->crp_unpacked) {
401                 sfw_unpack_message(*msgpp);
402                 crpc->crp_unpacked = 1;
403         }
404        
405         if (cfs_time_after(nd->nd_stamp, crpc->crp_stamp))
406                 return 0;
407
408         nd->nd_stamp = crpc->crp_stamp;
409         rep = &(*msgpp)->msg_body.reply;
410
411         if (rep->sid.ses_nid == LNET_NID_ANY)
412                 nd->nd_state = LST_NODE_UNKNOWN;
413         else if (lstcon_session_match(rep->sid))
414                 nd->nd_state = LST_NODE_ACTIVE;
415         else
416                 nd->nd_state = LST_NODE_BUSY;
417
418         return 0;
419 }
420
421 void
422 lstcon_rpc_trans_stat(lstcon_rpc_trans_t *trans, lstcon_trans_stat_t *stat)
423 {
424         lstcon_rpc_t      *crpc;
425         srpc_client_rpc_t *rpc;
426         srpc_msg_t        *rep;
427         int                error;
428
429         LASSERT (stat != NULL);
430
431         memset(stat, 0, sizeof(*stat));
432
433         cfs_list_for_each_entry_typed(crpc, &trans->tas_rpcs_list,
434                                       lstcon_rpc_t, crp_link) {
435                 lstcon_rpc_stat_total(stat, 1);
436
437                 rpc = crpc->crp_rpc;
438
439                 LASSERT (crpc->crp_stamp != 0);
440
441                 error = lstcon_rpc_get_reply(crpc, &rep);
442                 if (error != 0) {
443                         lstcon_rpc_stat_failure(stat, 1);
444                         if (stat->trs_rpc_errno == 0)
445                                 stat->trs_rpc_errno = -error;
446
447                         continue;
448                 }
449
450                 lstcon_rpc_stat_success(stat, 1);
451
452                 lstcon_rpc_stat_reply(trans->tas_opc, rep,
453                                       crpc->crp_node, stat);
454         }
455
456         CDEBUG(D_NET, "transaction %s : success %d, failure %d, total %d, "
457                       "RPC error(%d), Framework error(%d)\n",
458                lstcon_rpc_trans_name(trans->tas_opc),
459                lstcon_rpc_stat_success(stat, 0),
460                lstcon_rpc_stat_failure(stat, 0),
461                lstcon_rpc_stat_total(stat, 0),
462                stat->trs_rpc_errno, stat->trs_fwk_errno);
463
464         return;
465 }
466
467 int
468 lstcon_rpc_trans_interpreter(lstcon_rpc_trans_t *trans,
469                              struct list_head *head_up,
470                              lstcon_rpc_readent_func_t readent)
471 {
472         struct list_head      tmp;
473         struct list_head     *next;
474         lstcon_rpc_ent_t     *ent;
475         srpc_generic_reply_t *rep;
476         srpc_client_rpc_t    *rpc;
477         lstcon_rpc_t         *crpc;
478         srpc_msg_t           *msg;
479         lstcon_node_t        *nd;
480         cfs_duration_t        dur;
481         struct timeval        tv;
482         int                   error;
483
484         LASSERT (head_up != NULL);
485
486         next = head_up;
487
488         cfs_list_for_each_entry_typed(crpc, &trans->tas_rpcs_list,
489                                       lstcon_rpc_t, crp_link) {
490                 if (copy_from_user(&tmp, next, sizeof(struct list_head)))
491                         return -EFAULT;
492
493                 if (tmp.next == head_up)
494                         return 0;
495
496                 next = tmp.next;
497
498                 ent = list_entry(next, lstcon_rpc_ent_t, rpe_link);
499
500                 rpc = crpc->crp_rpc;
501
502                 LASSERT (crpc->crp_stamp != 0);
503
504                 error = lstcon_rpc_get_reply(crpc, &msg);
505
506                 nd = crpc->crp_node;
507
508                 dur = (cfs_duration_t)cfs_time_sub(crpc->crp_stamp,
509                       (cfs_time_t)console_session.ses_id.ses_stamp);
510                 cfs_duration_usec(dur, &tv);
511
512                 if (copy_to_user(&ent->rpe_peer,
513                                  &nd->nd_id, sizeof(lnet_process_id_t)) ||
514                     copy_to_user(&ent->rpe_stamp, &tv, sizeof(tv)) ||
515                     copy_to_user(&ent->rpe_state,
516                                  &nd->nd_state, sizeof(nd->nd_state)) ||
517                     copy_to_user(&ent->rpe_rpc_errno, &error, sizeof(error)))
518                         return -EFAULT;
519
520                 if (error != 0)
521                         continue;
522
523                 /* RPC is done */
524                 rep = (srpc_generic_reply_t *)&msg->msg_body.reply;
525
526                 if (copy_to_user(&ent->rpe_sid,
527                                  &rep->sid, sizeof(lst_sid_t)) ||
528                     copy_to_user(&ent->rpe_fwk_errno,
529                                  &rep->status, sizeof(rep->status)))
530                         return -EFAULT;
531
532                 if (readent == NULL)
533                         continue;
534
535                 if ((error = readent(trans->tas_opc, msg, ent)) != 0)
536                         return error;
537         }
538
539         return 0;
540 }
541
542 void
543 lstcon_rpc_trans_destroy(lstcon_rpc_trans_t *trans)
544 {
545         srpc_client_rpc_t *rpc;
546         lstcon_rpc_t      *crpc;
547         lstcon_rpc_t      *tmp;
548         int                count = 0;
549         
550         cfs_list_for_each_entry_safe_typed(crpc, tmp,
551                                            &trans->tas_rpcs_list,
552                                            lstcon_rpc_t, crp_link) {
553                 rpc = crpc->crp_rpc;
554
555                 spin_lock(&rpc->crpc_lock);
556
557                 /* free it if not posted or finished already */
558                 if (!crpc->crp_posted || crpc->crp_finished) {
559                         spin_unlock(&rpc->crpc_lock);
560
561                         list_del_init(&crpc->crp_link);
562                         lstcon_rpc_put(crpc);
563
564                         continue;
565                 }
566
567                 /* rpcs can be still not callbacked (even LNetMDUnlink is called)
568                  * because huge timeout for inaccessible network, don't make
569                  * user wait for them, just abandon them, they will be recycled 
570                  * in callback */
571
572                 LASSERT (crpc->crp_status != 0);
573
574                 crpc->crp_node  = NULL;
575                 crpc->crp_trans = NULL;
576                 list_del_init(&crpc->crp_link);
577                 count ++;
578
579                 spin_unlock(&rpc->crpc_lock);
580
581                 atomic_dec(&trans->tas_remaining);
582         }
583
584         LASSERT (atomic_read(&trans->tas_remaining) == 0);
585
586         list_del(&trans->tas_link);
587         if (!list_empty(&trans->tas_olink))
588                 list_del(&trans->tas_olink);
589
590         CDEBUG(D_NET, "Transaction %s destroyed with %d pending RPCs\n",
591                lstcon_rpc_trans_name(trans->tas_opc), count);
592
593         LIBCFS_FREE(trans, sizeof(*trans));
594
595         return;
596 }
597
598 int
599 lstcon_sesrpc_prep(lstcon_node_t *nd, int transop, lstcon_rpc_t **crpc)
600 {
601         srpc_mksn_reqst_t *msrq;
602         srpc_rmsn_reqst_t *rsrq;
603         int                rc;
604
605         switch (transop) {
606         case LST_TRANS_SESNEW:
607                 rc = lstcon_rpc_prep(nd, SRPC_SERVICE_MAKE_SESSION, 0, crpc);
608                 if (rc != 0)
609                         return rc;
610
611                 msrq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.mksn_reqst;
612                 msrq->mksn_sid     = console_session.ses_id;
613                 msrq->mksn_force   = console_session.ses_force;
614                 strncpy(msrq->mksn_name, console_session.ses_name,
615                         strlen(console_session.ses_name));
616                 break;
617
618         case LST_TRANS_SESEND:
619                 rc = lstcon_rpc_prep(nd, SRPC_SERVICE_REMOVE_SESSION, 0, crpc);
620                 if (rc != 0)
621                         return rc;
622
623                 rsrq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.rmsn_reqst;
624                 rsrq->rmsn_sid = console_session.ses_id;
625                 break;
626
627         default:
628                 LBUG();
629         }
630
631         return 0;
632 }
633
634 int
635 lstcon_dbgrpc_prep(lstcon_node_t *nd, lstcon_rpc_t **crpc)
636 {
637         srpc_debug_reqst_t *drq;
638         int                 rc;
639
640         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_DEBUG, 0, crpc);
641         if (rc != 0)
642                 return rc;
643
644         drq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.dbg_reqst;
645
646         drq->dbg_sid   = console_session.ses_id;
647         drq->dbg_flags = 0;
648         
649         return rc;
650 }
651
652 int
653 lstcon_batrpc_prep(lstcon_node_t *nd, int transop,
654                    lstcon_tsb_hdr_t *tsb, lstcon_rpc_t **crpc)
655 {
656         lstcon_batch_t     *batch;
657         srpc_batch_reqst_t *brq;
658         int                 rc;
659
660         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_BATCH, 0, crpc);
661         if (rc != 0)
662                 return rc;
663
664         brq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.bat_reqst;
665
666         brq->bar_sid     = console_session.ses_id;
667         brq->bar_bid     = tsb->tsb_id;
668         brq->bar_testidx = tsb->tsb_index;
669         brq->bar_opc     = transop == LST_TRANS_TSBRUN ? SRPC_BATCH_OPC_RUN :
670                            (transop == LST_TRANS_TSBSTOP ? SRPC_BATCH_OPC_STOP:
671                             SRPC_BATCH_OPC_QUERY);
672
673         if (transop != LST_TRANS_TSBRUN &&
674             transop != LST_TRANS_TSBSTOP)
675                 return 0;
676
677         LASSERT (tsb->tsb_index == 0);
678
679         batch = (lstcon_batch_t *)tsb;
680         brq->bar_arg = batch->bat_arg;
681         
682         return 0;
683 }
684
685 int
686 lstcon_statrpc_prep(lstcon_node_t *nd, lstcon_rpc_t **crpc)
687 {
688         srpc_stat_reqst_t *srq;
689         int                rc;
690
691         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_QUERY_STAT, 0, crpc);
692         if (rc != 0)
693                 return rc;
694
695         srq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.stat_reqst;
696
697         srq->str_sid  = console_session.ses_id;
698         srq->str_type = 0; /* XXX remove it */
699
700         return 0;
701 }
702
703 lnet_process_id_t *
704 lstcon_next_id(int idx, int nkiov, lnet_kiov_t *kiov)
705 {
706         lnet_process_id_t *pid;
707         int                i;
708
709         i = idx / (CFS_PAGE_SIZE / sizeof(lnet_process_id_t));
710         
711         LASSERT (i < nkiov);
712
713         pid = (lnet_process_id_t *)cfs_page_address(kiov[i].kiov_page);
714
715         return &pid[idx % (CFS_PAGE_SIZE / sizeof(lnet_process_id_t))];
716 }
717
718 int
719 lstcon_dstnodes_prep(lstcon_group_t *grp, int idx,
720                      int dist, int span, int nkiov, lnet_kiov_t *kiov)
721 {
722         lnet_process_id_t *pid;
723         lstcon_ndlink_t   *ndl;
724         lstcon_node_t     *nd;
725         int                start;
726         int                end;
727         int                i = 0;
728
729         LASSERT (dist >= 1);
730         LASSERT (span >= 1);
731         LASSERT (grp->grp_nnode >= 1);
732
733         if (span > grp->grp_nnode)
734                 return -EINVAL;
735
736         start = ((idx / dist) * span) % grp->grp_nnode;
737         end   = ((idx / dist) * span + span - 1) % grp->grp_nnode;
738
739         cfs_list_for_each_entry_typed(ndl, &grp->grp_ndl_list,
740                                       lstcon_ndlink_t, ndl_link) {
741                 nd = ndl->ndl_node;
742                 if (i < start) {
743                         i ++;
744                         continue;
745                 }
746
747                 if (i > (end >= start ? end: grp->grp_nnode))
748                         break;
749
750                 pid = lstcon_next_id((i - start), nkiov, kiov);
751                 *pid = nd->nd_id;
752                 i++;
753         }
754
755         if (start <= end) /* done */
756                 return 0;
757
758         cfs_list_for_each_entry_typed(ndl, &grp->grp_ndl_list,
759                                       lstcon_ndlink_t, ndl_link) {
760                 if (i > grp->grp_nnode + end)
761                         break;
762
763                 nd = ndl->ndl_node;
764                 pid = lstcon_next_id((i - start), nkiov, kiov);
765                 *pid = nd->nd_id;
766                 i++;
767         }
768
769         return 0;
770 }
771
772 int
773 lstcon_pingrpc_prep(lst_test_ping_param_t *param, srpc_test_reqst_t *req)
774 {
775         test_ping_req_t *prq = &req->tsr_u.ping;
776         
777         prq->png_size   = param->png_size;
778         prq->png_flags  = param->png_flags;
779         /* TODO dest */
780         return 0;
781 }
782
783 int
784 lstcon_bulkrpc_prep(lst_test_bulk_param_t *param, srpc_test_reqst_t *req)
785 {
786         test_bulk_req_t *brq = &req->tsr_u.bulk;
787
788         brq->blk_opc    = param->blk_opc;
789         brq->blk_npg    = (param->blk_size + CFS_PAGE_SIZE - 1) / CFS_PAGE_SIZE;
790         brq->blk_flags  = param->blk_flags;
791
792         return 0;
793 }
794
795 int
796 lstcon_testrpc_prep(lstcon_node_t *nd, int transop,
797                     lstcon_test_t *test, lstcon_rpc_t **crpc)
798 {
799         lstcon_group_t    *sgrp = test->tes_src_grp;
800         lstcon_group_t    *dgrp = test->tes_dst_grp;
801         srpc_test_reqst_t *trq;
802         srpc_bulk_t       *bulk;
803         int                i;
804         int                n  = 0;
805         int                rc = 0;
806
807         if (transop == LST_TRANS_TSBCLIADD)
808                 n = sfw_id_pages(test->tes_span);
809
810         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_TEST, n, crpc);
811         if (rc != 0) 
812                 return rc;
813
814         trq  = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.tes_reqst;
815
816         if (transop == LST_TRANS_TSBSRVADD) {
817                 int ndist = (sgrp->grp_nnode + test->tes_dist - 1) / test->tes_dist;
818                 int nspan = (dgrp->grp_nnode + test->tes_span - 1) / test->tes_span;
819                 int nmax = (ndist + nspan - 1) / nspan;
820
821                 trq->tsr_ndest = 0;
822                 trq->tsr_loop  = nmax * test->tes_dist * test->tes_concur;
823
824         } else {
825                 bulk = &(*crpc)->crp_rpc->crpc_bulk;
826
827                 for (i = 0; i < n; i++) {
828                         bulk->bk_iovs[i].kiov_offset = 0;
829                         bulk->bk_iovs[i].kiov_len    = CFS_PAGE_SIZE;
830                         bulk->bk_iovs[i].kiov_page   = cfs_alloc_page(CFS_ALLOC_STD);
831
832                         if (bulk->bk_iovs[i].kiov_page != NULL) 
833                                 continue;
834
835                         lstcon_rpc_put(*crpc);
836                         return -ENOMEM;
837                 }
838
839                 bulk->bk_sink = 0;
840
841                 LASSERT (transop == LST_TRANS_TSBCLIADD);
842
843                 rc = lstcon_dstnodes_prep(test->tes_dst_grp,
844                                           test->tes_cliidx++, test->tes_dist,
845                                           test->tes_span, n, &bulk->bk_iovs[0]);
846                 if (rc != 0) {
847                         lstcon_rpc_put(*crpc);
848                         return rc;
849                 }
850
851                 trq->tsr_ndest = test->tes_span;
852                 trq->tsr_loop  = test->tes_loop;
853         } 
854
855         trq->tsr_sid        = console_session.ses_id;
856         trq->tsr_bid        = test->tes_hdr.tsb_id;
857         trq->tsr_concur     = test->tes_concur;
858         trq->tsr_is_client  = (transop == LST_TRANS_TSBCLIADD) ? 1 : 0;
859         trq->tsr_stop_onerr = !!test->tes_stop_onerr;
860
861         switch (test->tes_type) {
862         case LST_TEST_PING:
863                 trq->tsr_service = SRPC_SERVICE_PING;
864                 rc = lstcon_pingrpc_prep((lst_test_ping_param_t *)&test->tes_param[0], trq);
865                 break;
866         case LST_TEST_BULK:
867                 trq->tsr_service = SRPC_SERVICE_BRW;
868                 rc = lstcon_bulkrpc_prep((lst_test_bulk_param_t *)&test->tes_param[0], trq);
869                 break;
870         default:
871                 LBUG();
872                 break;
873         }
874
875         return rc;
876 }
877
878 void
879 lstcon_rpc_stat_reply(int transop, srpc_msg_t *msg,
880                       lstcon_node_t *nd, lstcon_trans_stat_t *stat)
881 {
882         srpc_mksn_reply_t  *mksn_rep;
883         srpc_rmsn_reply_t  *rmsn_rep;
884         srpc_debug_reply_t *dbg_rep;
885         srpc_batch_reply_t *bat_rep;
886         srpc_test_reply_t  *test_rep;
887         srpc_stat_reply_t  *stat_rep;
888         int                 rc = 0;
889
890         switch (transop) {
891         case LST_TRANS_SESNEW:
892                 mksn_rep = &msg->msg_body.mksn_reply;
893
894                 if (mksn_rep->mksn_status == 0) {
895                         lstcon_sesop_stat_success(stat, 1);
896                         /* session timeout on remote node */
897                         nd->nd_timeout = mksn_rep->mksn_timeout;
898                         return;
899                 }
900
901                 LASSERT (mksn_rep->mksn_status == EBUSY ||
902                          mksn_rep->mksn_status == EINVAL);
903
904                 lstcon_sesop_stat_failure(stat, 1);
905                 rc = mksn_rep->mksn_status;
906                 break;
907
908         case LST_TRANS_SESEND:
909                 rmsn_rep = &msg->msg_body.rmsn_reply;
910                 /* ESRCH is not an error for end session */
911                 if (rmsn_rep->rmsn_status == 0 ||
912                     rmsn_rep->rmsn_status == ESRCH) {
913                         lstcon_sesop_stat_success(stat, 1);
914                         return;
915                 }
916
917                 LASSERT (rmsn_rep->rmsn_status == EBUSY ||
918                          rmsn_rep->rmsn_status == EINVAL);
919
920                 lstcon_sesop_stat_failure(stat, 1);
921                 rc = rmsn_rep->rmsn_status;
922                 break;
923
924         case LST_TRANS_SESQRY:
925         case LST_TRANS_SESPING:
926                 dbg_rep = &msg->msg_body.dbg_reply;
927
928                 if (dbg_rep->dbg_status == ESRCH) {
929                         lstcon_sesqry_stat_unknown(stat, 1);
930                         return;
931                 } 
932
933                 LASSERT (dbg_rep->dbg_status == 0);
934
935                 if (lstcon_session_match(dbg_rep->dbg_sid))
936                         lstcon_sesqry_stat_active(stat, 1);
937                 else
938                         lstcon_sesqry_stat_busy(stat, 1);
939                 return;
940
941         case LST_TRANS_TSBRUN:
942         case LST_TRANS_TSBSTOP:
943                 bat_rep = &msg->msg_body.bat_reply;
944
945                 if (bat_rep->bar_status == 0) {
946                         lstcon_tsbop_stat_success(stat, 1);
947                         return;
948                 }
949
950                 if (bat_rep->bar_status == EPERM && 
951                     transop == LST_TRANS_TSBSTOP) {
952                         lstcon_tsbop_stat_success(stat, 1);
953                         return;
954                 }
955
956                 lstcon_tsbop_stat_failure(stat, 1);
957                 rc = bat_rep->bar_status;
958                 break;
959
960         case LST_TRANS_TSBCLIQRY:
961         case LST_TRANS_TSBSRVQRY:
962                 bat_rep = &msg->msg_body.bat_reply;
963
964                 if (bat_rep->bar_active != 0) 
965                         lstcon_tsbqry_stat_run(stat, 1);
966                 else
967                         lstcon_tsbqry_stat_idle(stat, 1);
968
969                 if (bat_rep->bar_status == 0) 
970                         return;
971
972                 lstcon_tsbqry_stat_failure(stat, 1);
973                 rc = bat_rep->bar_status;
974                 break;
975
976         case LST_TRANS_TSBCLIADD:
977         case LST_TRANS_TSBSRVADD:
978                 test_rep = &msg->msg_body.tes_reply;
979
980                 if (test_rep->tsr_status == 0) {
981                         lstcon_tsbop_stat_success(stat, 1);
982                         return;
983                 }
984
985                 lstcon_tsbop_stat_failure(stat, 1);
986                 rc = test_rep->tsr_status;
987                 break;
988
989         case LST_TRANS_STATQRY:
990                 stat_rep = &msg->msg_body.stat_reply;
991
992                 if (stat_rep->str_status == 0) {
993                         lstcon_statqry_stat_success(stat, 1);
994                         return;
995                 }
996
997                 lstcon_statqry_stat_failure(stat, 1);
998                 rc = stat_rep->str_status;
999                 break;
1000
1001         default:
1002                 LBUG();
1003         }
1004
1005         if (stat->trs_fwk_errno == 0)
1006                 stat->trs_fwk_errno = rc;
1007
1008         return;
1009 }
1010
1011 int
1012 lstcon_rpc_trans_ndlist(struct list_head *ndlist,
1013                         struct list_head *translist, int transop,
1014                         void *arg, lstcon_rpc_cond_func_t condition,
1015                         lstcon_rpc_trans_t **transpp)
1016 {
1017         lstcon_rpc_trans_t *trans;
1018         lstcon_ndlink_t    *ndl;
1019         lstcon_node_t      *nd;
1020         lstcon_rpc_t       *rpc;
1021         int                 rc;
1022
1023         /* Creating session RPG for list of nodes */
1024
1025         rc = lstcon_rpc_trans_prep(translist, transop, &trans);
1026         if (rc != 0) {
1027                 CERROR("Can't create transaction %d: %d\n", transop, rc);
1028                 return rc;
1029         }
1030
1031         cfs_list_for_each_entry_typed(ndl, ndlist, lstcon_ndlink_t, ndl_link) {
1032                 rc = condition == NULL ? 1 :
1033                      condition(transop, ndl->ndl_node, arg);
1034
1035                 if (rc == 0)
1036                         continue;
1037
1038                 if (rc < 0) {
1039                         CDEBUG(D_NET, "Condition error while creating RPC "
1040                                       " for transaction %d: %d\n", transop, rc);
1041                         break;
1042                 }
1043
1044                 nd = ndl->ndl_node;
1045
1046                 switch (transop) {
1047                 case LST_TRANS_SESNEW:
1048                 case LST_TRANS_SESEND:
1049                         rc = lstcon_sesrpc_prep(nd, transop, &rpc);
1050                         break;
1051                 case LST_TRANS_SESQRY:
1052                 case LST_TRANS_SESPING:
1053                         rc = lstcon_dbgrpc_prep(nd, &rpc);
1054                         break;
1055                 case LST_TRANS_TSBCLIADD:
1056                 case LST_TRANS_TSBSRVADD:
1057                         rc = lstcon_testrpc_prep(nd, transop,
1058                                                  (lstcon_test_t *)arg, &rpc);
1059                         break;
1060                 case LST_TRANS_TSBRUN:
1061                 case LST_TRANS_TSBSTOP:
1062                 case LST_TRANS_TSBCLIQRY:
1063                 case LST_TRANS_TSBSRVQRY:
1064                         rc = lstcon_batrpc_prep(nd, transop,
1065                                                 (lstcon_tsb_hdr_t *)arg, &rpc);
1066                         break;
1067                 case LST_TRANS_STATQRY:
1068                         rc = lstcon_statrpc_prep(nd, &rpc);
1069                         break;
1070                 default:
1071                         rc = -EINVAL;
1072                         break;
1073                 }
1074
1075                 if (rc != 0) {
1076                         CERROR("Failed to create RPC for transaction %s: %d\n",
1077                                lstcon_rpc_trans_name(transop), rc);
1078                         break;
1079                 }
1080                                 
1081                 lstcon_rpc_trans_addreq(trans, rpc);
1082         }
1083
1084         if (rc == 0) {
1085                 *transpp = trans;
1086                 return 0;
1087         }
1088
1089         lstcon_rpc_trans_destroy(trans);
1090
1091         return rc;
1092 }
1093
1094 void
1095 lstcon_rpc_pinger(void *arg)
1096 {
1097         stt_timer_t        *ptimer = (stt_timer_t *)arg;
1098         lstcon_rpc_trans_t *trans;
1099         lstcon_rpc_t       *crpc;
1100         srpc_msg_t         *rep;
1101         srpc_debug_reqst_t *drq;
1102         lstcon_ndlink_t    *ndl;
1103         lstcon_node_t      *nd;
1104         time_t              intv;
1105         int                 count = 0;
1106         int                 rc;
1107
1108         /* RPC pinger is a special case of transaction,
1109          * it's called by timer at 8 seconds interval.
1110          */
1111         mutex_down(&console_session.ses_mutex);
1112
1113         if (console_session.ses_shutdown || console_session.ses_expired) {
1114                 mutex_up(&console_session.ses_mutex);
1115                 return;
1116         }
1117
1118         if (!console_session.ses_expired &&
1119             cfs_time_current_sec() - console_session.ses_laststamp >
1120             (time_t)console_session.ses_timeout)
1121                 console_session.ses_expired = 1;
1122
1123         trans = console_session.ses_ping;
1124
1125         LASSERT (trans != NULL);
1126
1127         cfs_list_for_each_entry_typed(ndl, &console_session.ses_ndl_list,
1128                                       lstcon_ndlink_t, ndl_link) {
1129                 nd = ndl->ndl_node;
1130
1131                 if (console_session.ses_expired) {
1132                         /* idle console, end session on all nodes */
1133                         if (nd->nd_state != LST_NODE_ACTIVE)
1134                                 continue;
1135
1136                         rc = lstcon_sesrpc_prep(nd, LST_TRANS_SESEND, &crpc);
1137                         if (rc != 0) {
1138                                 CERROR("Out of memory\n");
1139                                 break;
1140                         }
1141
1142                         lstcon_rpc_trans_addreq(trans, crpc);
1143                         lstcon_rpc_post(crpc);
1144
1145                         continue;
1146                 }
1147
1148                 crpc = &nd->nd_ping;
1149
1150                 if (crpc->crp_rpc != NULL) {
1151                         LASSERT (crpc->crp_trans == trans);
1152                         LASSERT (!list_empty(&crpc->crp_link));
1153
1154                         spin_lock(&crpc->crp_rpc->crpc_lock);
1155
1156                         LASSERT (crpc->crp_posted);
1157
1158                         if (!crpc->crp_finished) {
1159                                 /* in flight */
1160                                 spin_unlock(&crpc->crp_rpc->crpc_lock);
1161                                 continue;
1162                         }
1163
1164                         spin_unlock(&crpc->crp_rpc->crpc_lock);
1165
1166                         lstcon_rpc_get_reply(crpc, &rep);
1167
1168                         list_del_init(&crpc->crp_link);
1169                 
1170                         lstcon_rpc_put(crpc);
1171                 }
1172
1173                 if (nd->nd_state != LST_NODE_ACTIVE)
1174                         continue;
1175
1176                 intv = cfs_duration_sec(cfs_time_sub(cfs_time_current(),
1177                                                      nd->nd_stamp));
1178                 if (intv < (time_t)nd->nd_timeout / 2)
1179                         continue;
1180
1181                 rc = lstcon_rpc_init(nd, SRPC_SERVICE_DEBUG, 0, 0, crpc);
1182                 if (rc != 0) {
1183                         CERROR("Out of memory\n");
1184                         break;
1185                 }
1186
1187                 drq = &crpc->crp_rpc->crpc_reqstmsg.msg_body.dbg_reqst;
1188
1189                 drq->dbg_sid   = console_session.ses_id;
1190                 drq->dbg_flags = 0;
1191
1192                 lstcon_rpc_trans_addreq(trans, crpc);
1193                 lstcon_rpc_post(crpc);
1194
1195                 count ++;
1196         }
1197
1198         if (console_session.ses_expired) {
1199                 mutex_up(&console_session.ses_mutex);
1200                 return;
1201         }
1202
1203         CDEBUG(D_NET, "Ping %d nodes in session\n", count);
1204
1205         ptimer->stt_expires = (cfs_time_t)(cfs_time_current_sec() + LST_PING_INTERVAL);
1206         stt_add_timer(ptimer);
1207
1208         mutex_up(&console_session.ses_mutex);
1209 }
1210
1211 int
1212 lstcon_rpc_pinger_start(void)
1213 {
1214         stt_timer_t    *ptimer;
1215         int             rc;
1216
1217         LASSERT (list_empty(&console_session.ses_rpc_freelist));
1218         LASSERT (atomic_read(&console_session.ses_rpc_counter) == 0);
1219
1220         rc = lstcon_rpc_trans_prep(NULL, LST_TRANS_SESPING,
1221                                    &console_session.ses_ping);
1222         if (rc != 0) {
1223                 CERROR("Failed to create console pinger\n");
1224                 return rc;
1225         }
1226
1227         ptimer = &console_session.ses_ping_timer;
1228         ptimer->stt_expires = (cfs_time_t)(cfs_time_current_sec() + LST_PING_INTERVAL);
1229
1230         stt_add_timer(ptimer);
1231
1232         return 0;
1233 }
1234
1235 void
1236 lstcon_rpc_pinger_stop(void)
1237 {
1238         LASSERT (console_session.ses_shutdown);
1239
1240         stt_del_timer(&console_session.ses_ping_timer);
1241
1242         lstcon_rpc_trans_abort(console_session.ses_ping, -ESHUTDOWN);
1243         lstcon_rpc_trans_stat(console_session.ses_ping, lstcon_trans_stat());
1244         lstcon_rpc_trans_destroy(console_session.ses_ping);
1245
1246         memset(lstcon_trans_stat(), 0, sizeof(lstcon_trans_stat_t));
1247
1248         console_session.ses_ping = NULL;
1249 }
1250
1251 void
1252 lstcon_rpc_cleanup_wait(void)
1253 {
1254         lstcon_rpc_trans_t *trans;
1255         lstcon_rpc_t       *crpc;
1256         struct list_head   *pacer;
1257         struct list_head    zlist;
1258
1259         /* Called with hold of global mutex */
1260
1261         LASSERT (console_session.ses_shutdown);
1262
1263         while (!list_empty(&console_session.ses_trans_list)) { 
1264                 list_for_each(pacer, &console_session.ses_trans_list) {
1265                         trans = list_entry(pacer, lstcon_rpc_trans_t, tas_link);
1266
1267                         CDEBUG(D_NET, "Session closed, wakeup transaction %s\n",
1268                                lstcon_rpc_trans_name(trans->tas_opc));
1269
1270                         cfs_waitq_signal(&trans->tas_waitq);
1271                 }
1272
1273                 mutex_up(&console_session.ses_mutex);
1274
1275                 CWARN("Session is shutting down, "
1276                       "waiting for termination of transactions\n");
1277                 cfs_pause(cfs_time_seconds(1));
1278
1279                 mutex_down(&console_session.ses_mutex);
1280         }
1281
1282         spin_lock(&console_session.ses_rpc_lock);
1283
1284         lst_wait_until((atomic_read(&console_session.ses_rpc_counter) == 0),
1285                        console_session.ses_rpc_lock,
1286                        "Network is not accessable or target is down, "
1287                        "waiting for %d console RPCs to being recycled\n",
1288                        atomic_read(&console_session.ses_rpc_counter));
1289
1290         list_add(&zlist, &console_session.ses_rpc_freelist);
1291         list_del_init(&console_session.ses_rpc_freelist);
1292
1293         spin_unlock(&console_session.ses_rpc_lock);
1294
1295         while (!list_empty(&zlist)) {
1296                 crpc = list_entry(zlist.next, lstcon_rpc_t, crp_link);
1297
1298                 list_del(&crpc->crp_link);
1299                 LIBCFS_FREE(crpc, sizeof(lstcon_rpc_t));
1300         }
1301 }
1302
1303 int
1304 lstcon_rpc_module_init(void)
1305 {
1306         CFS_INIT_LIST_HEAD(&console_session.ses_ping_timer.stt_list);
1307         console_session.ses_ping_timer.stt_func = lstcon_rpc_pinger;
1308         console_session.ses_ping_timer.stt_data = &console_session.ses_ping_timer;
1309
1310         console_session.ses_ping = NULL;
1311
1312         spin_lock_init(&console_session.ses_rpc_lock);
1313         atomic_set(&console_session.ses_rpc_counter, 0);
1314         CFS_INIT_LIST_HEAD(&console_session.ses_rpc_freelist);
1315
1316         return 0;
1317 }
1318
1319 void
1320 lstcon_rpc_module_fini(void)
1321 {
1322         LASSERT (list_empty(&console_session.ses_rpc_freelist));
1323         LASSERT (atomic_read(&console_session.ses_rpc_counter) == 0);
1324 }
1325
1326 #endif