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