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