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