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