Whamcloud - gitweb
LU-653 Ignore last transno from clients with no outstanding transactions
[fs/lustre-release.git] / lnet / utils / lstclient.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  * Author: Liang Zhen <liangzhen@clusterfs.com>
39  */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <getopt.h>
44 #include <errno.h>
45 #include <pwd.h>
46 #include <lnet/lnetctl.h>
47 #include <lnet/lnetst.h>
48 #include "../selftest/rpc.h"
49 #include "../selftest/selftest.h"
50
51 static int lstjn_stopping = 0;
52 static int lstjn_intialized = 0;
53
54 unsigned int libcfs_subsystem_debug = ~0 - (S_LNET | S_LND);
55 unsigned int libcfs_debug = 0;
56
57 static struct option lstjn_options[] =
58 {
59         {"sesid",       required_argument,  0, 's' },
60         {"group",       required_argument,  0, 'g' },
61         {"server_mode", no_argument,        0, 'm' },
62         {0,             0,                  0,  0  }
63 };
64
65 void
66 lstjn_stop (int sig)
67 {
68         lstjn_stopping = 1;
69 }
70
71 void
72 lstjn_rpc_done(srpc_client_rpc_t *rpc)
73 {
74         if (!lstjn_intialized)
75                 lstjn_intialized = 1;
76 }
77
78 int
79 lstjn_join_session(char *ses, char *grp)
80 {
81         lnet_process_id_t  sesid;
82         srpc_client_rpc_t *rpc;
83         srpc_join_reqst_t *req;
84         srpc_join_reply_t *rep;
85         srpc_mksn_reqst_t *sreq;
86         srpc_mksn_reply_t *srep;
87         int                rc;
88
89         sesid.pid = LUSTRE_LNET_PID;
90         sesid.nid = libcfs_str2nid(ses);
91         if (sesid.nid == LNET_NID_ANY) {
92                 fprintf(stderr, "Invalid session NID: %s\n", ses);
93                 return -1;
94         }
95
96         rpc = sfw_create_rpc(sesid, SRPC_SERVICE_JOIN, 0,
97                              0, lstjn_rpc_done, NULL);
98         if (rpc == NULL) {
99                 fprintf(stderr, "Out of memory\n");
100                 return -1;
101         }
102
103         req = &rpc->crpc_reqstmsg.msg_body.join_reqst;
104
105         req->join_sid = LST_INVALID_SID;
106         strncpy(req->join_group, grp, LST_NAME_SIZE);
107
108         sfw_post_rpc(rpc);
109
110         for (;;) {
111                 rc = selftest_wait_events();
112
113                 if (lstjn_intialized)
114                         break;
115         }
116
117         if (rpc->crpc_status != 0) {
118                 fprintf(stderr, "Failed to send RPC to console: %s\n",
119                         strerror(rpc->crpc_status));
120                 srpc_client_rpc_decref(rpc);
121                 return -1;
122         }
123
124         sfw_unpack_message(&rpc->crpc_replymsg);
125
126         rep = &rpc->crpc_replymsg.msg_body.join_reply;
127         if (rep->join_status != 0) {
128                 fprintf(stderr, "Can't join session %s group %s: %s\n",
129                         ses, grp, strerror(rep->join_status));
130                 srpc_client_rpc_decref(rpc);
131                 return -1;
132         }
133
134         sreq = &rpc->crpc_reqstmsg.msg_body.mksn_reqst;
135         sreq->mksn_sid     = rep->join_sid;
136         sreq->mksn_force   = 0;
137         strcpy(sreq->mksn_name, rep->join_session);
138
139         srep = &rpc->crpc_replymsg.msg_body.mksn_reply;
140
141         rc = sfw_make_session(sreq, srep);
142         if (rc != 0 || srep->mksn_status != 0) {
143                 fprintf(stderr, "Can't create session: %d, %s\n",
144                         rc, strerror(srep->mksn_status));
145                 srpc_client_rpc_decref(rpc);
146                 return -1;
147         }
148
149         fprintf(stdout, "Session %s, ID: %s, "LPU64"\n",
150                 ses, libcfs_nid2str(rep->join_sid.ses_nid),
151                 rep->join_sid.ses_stamp);
152
153         srpc_client_rpc_decref(rpc);
154
155         return 0;
156 }
157
158 int
159 main(int argc, char **argv)
160 {
161         char   *ses = NULL;
162         char   *grp = NULL;
163         int     server_mode_flag = 0;
164         int     optidx;
165         int     c;
166         int     rc;
167
168         const char *usage_string =
169                 "Usage: lstclient --sesid ID --group GROUP [--server_mode]\n";
170
171         while (1) {
172                 c = getopt_long(argc, argv, "s:g:m",
173                                 lstjn_options, &optidx);
174
175                 if (c == -1)
176                         break;
177
178                 switch (c) {
179                 case 's':
180                         ses = optarg;
181                         break;
182                 case 'g':
183                         grp = optarg;
184                         break;
185                 case 'm':
186                         server_mode_flag = 1;
187                         break;
188                 default:
189                         fprintf(stderr, "%s", usage_string);
190                         return -1;
191                 }
192         }
193
194         if (optind != argc || grp == NULL || ses == NULL) {
195                 fprintf(stderr, "%s", usage_string);
196                 return -1;
197         }
198
199         rc = libcfs_debug_init(5 * 1024 * 1024);
200         if (rc != 0) {
201                 CERROR("libcfs_debug_init() failed: %d\n", rc);
202                 return -1;
203         }
204
205         rc = cfs_wi_startup();
206         if (rc != 0) {
207                 CERROR("cfs_wi_startup() failed: %d\n", rc);
208                 libcfs_debug_cleanup();
209                 return -1;
210         }
211
212         rc = LNetInit();
213         if (rc != 0) {
214                 CERROR("LNetInit() failed: %d\n", rc);
215                 cfs_wi_shutdown();
216                 libcfs_debug_cleanup();
217                 return -1;
218         }
219
220         if (server_mode_flag)
221                 lnet_server_mode();
222
223         rc = lnet_selftest_init();
224         if (rc != 0) {
225                 fprintf(stderr, "Can't startup selftest\n");
226                 LNetFini();
227                 cfs_wi_shutdown();
228                 libcfs_debug_cleanup();
229                 return -1;
230         }
231
232         rc = lstjn_join_session(ses, grp);
233         if (rc != 0)
234                 goto out;
235
236         signal(SIGINT, lstjn_stop);
237
238         fprintf(stdout, "Start handling selftest requests, Ctl-C to stop\n");
239
240         while (!lstjn_stopping) {
241                 selftest_wait_events();
242
243                 if (!sfw_session_removed())
244                         continue;
245
246                 fprintf(stdout, "Session ended\n");
247                 break;
248         }
249
250 out:
251         lnet_selftest_fini();
252
253         LNetFini();
254
255         cfs_wi_shutdown();
256
257         libcfs_debug_cleanup();
258
259         return rc;
260 }