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