Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lnet / utils / lstclient.c
1 /*
2  * -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
3  * vim:expandtab:shiftwidth=8:tabstop=8:
4  *
5  * GPL HEADER START
6  *
7  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 only,
11  * as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License version 2 for more details (a copy is included
17  * in the LICENSE file that accompanied this code).
18  *
19  * You should have received a copy of the GNU General Public License
20  * version 2 along with this program; If not, see [sun.com URL with a
21  * copy of GPLv2].
22  *
23  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24  * CA 95054 USA or visit www.sun.com if you need additional information or
25  * have any questions.
26  *
27  * GPL HEADER END
28  */
29 /*
30  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
31  * Use is subject to license terms.
32  */
33 /*
34  * This file is part of Lustre, http://www.lustre.org/
35  * Lustre is a trademark of Sun Microsystems, Inc.
36  *
37  * lnet/selftest/conctl.c
38  *
39  * Author: Liang Zhen <liangzhen@clusterfs.com>
40  */
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <getopt.h>
45 #include <errno.h>
46 #include <pwd.h>
47 #include <lnet/lnetctl.h>
48 #include <lnet/lnetst.h>
49 #include "../selftest/rpc.h"
50 #include "../selftest/selftest.h"
51
52 static int lstjn_stopping = 0;
53 static int lstjn_intialized = 0;
54
55 unsigned int libcfs_subsystem_debug = ~0 - (S_LNET | S_LND);
56 unsigned int libcfs_debug = 0;
57
58 static struct option lstjn_options[] =
59 {
60         {"sesid",       required_argument,  0, 's' },
61         {"group",       required_argument,  0, 'g' },
62         {"server_mode", no_argument,        0, 'm' },
63         {0,             0,                  0,  0  }
64 };
65
66 void
67 lstjn_stop (int sig)
68 {
69         lstjn_stopping = 1;
70 }
71
72 void
73 lstjn_rpc_done(srpc_client_rpc_t *rpc)
74 {
75         if (!lstjn_intialized)
76                 lstjn_intialized = 1;
77 }
78
79 int
80 lstjn_join_session(char *ses, char *grp)
81 {
82         lnet_process_id_t  sesid;
83         srpc_client_rpc_t *rpc;
84         srpc_join_reqst_t *req;
85         srpc_join_reply_t *rep;
86         srpc_mksn_reqst_t *sreq;
87         srpc_mksn_reply_t *srep;
88         int                rc;
89
90         sesid.pid = LUSTRE_LNET_PID;
91         sesid.nid = libcfs_str2nid(ses);
92         if (sesid.nid == LNET_NID_ANY) {
93                 fprintf(stderr, "Invalid session NID: %s\n", ses);
94                 return -1;
95         }
96
97         rpc = sfw_create_rpc(sesid, SRPC_SERVICE_JOIN, 0,
98                              0, lstjn_rpc_done, NULL);
99         if (rpc == NULL) {
100                 fprintf(stderr, "Out of memory\n");
101                 return -1;
102         }
103
104         req = &rpc->crpc_reqstmsg.msg_body.join_reqst;
105
106         req->join_sid = LST_INVALID_SID;
107         strncpy(req->join_group, grp, LST_NAME_SIZE);
108
109         sfw_post_rpc(rpc);
110
111         for (;;) {
112                 rc = selftest_wait_events();
113
114                 if (lstjn_intialized)
115                         break;
116         }
117
118         if (rpc->crpc_status != 0) {
119                 fprintf(stderr, "Failed to send RPC to console: %s\n",
120                         strerror(rpc->crpc_status));
121                 srpc_client_rpc_decref(rpc);
122                 return -1;
123         }
124
125         sfw_unpack_message(&rpc->crpc_replymsg);
126
127         rep = &rpc->crpc_replymsg.msg_body.join_reply;
128         if (rep->join_status != 0) {
129                 fprintf(stderr, "Can't join session %s group %s: %s\n",
130                         ses, grp, strerror(rep->join_status));
131                 srpc_client_rpc_decref(rpc);
132                 return -1;
133         }
134
135         sreq = &rpc->crpc_reqstmsg.msg_body.mksn_reqst;
136         sreq->mksn_sid     = rep->join_sid;
137         sreq->mksn_force   = 0;
138         strcpy(sreq->mksn_name, rep->join_session);
139
140         srep = &rpc->crpc_replymsg.msg_body.mksn_reply;
141
142         rc = sfw_make_session(sreq, srep);
143         if (rc != 0 || srep->mksn_status != 0) {
144                 fprintf(stderr, "Can't create session: %d, %s\n",
145                         rc, strerror(srep->mksn_status));
146                 srpc_client_rpc_decref(rpc);
147                 return -1;
148         }
149
150         fprintf(stdout, "Session %s, ID: %s, %Lu\n",
151                 ses, libcfs_nid2str(rep->join_sid.ses_nid),
152                 rep->join_sid.ses_stamp);
153
154         srpc_client_rpc_decref(rpc);
155
156         return 0;
157 }
158
159 int
160 main(int argc, char **argv)
161 {
162         char   *ses = NULL;
163         char   *grp = NULL;
164         int     server_mode_flag = 0;
165         int     optidx;
166         int     c;
167         int     rc;
168
169         const char *usage_string =
170                 "Usage: lstclient --sesid ID --group GROUP [--server_mode]\n";
171
172         while (1) {
173                 c = getopt_long(argc, argv, "s:g:m",
174                                 lstjn_options, &optidx);
175
176                 if (c == -1)
177                         break;
178
179                 switch (c) {
180                 case 's':
181                         ses = optarg;
182                         break;
183                 case 'g':
184                         grp = optarg;
185                         break;
186                 case 'm':
187                         server_mode_flag = 1;
188                         break;
189                 default:
190                         fprintf(stderr, usage_string);
191                         return -1;
192                 }
193         }
194
195         if (optind != argc || grp == NULL || ses == NULL) {
196                 fprintf(stderr, usage_string);
197                 return -1;
198         }
199
200         rc = libcfs_debug_init(5 * 1024 * 1024);
201         if (rc != 0) {
202                 CERROR("libcfs_debug_init() failed: %d\n", rc);
203                 return -1;
204         }
205
206         rc = LNetInit();
207         if (rc != 0) {
208                 CERROR("LNetInit() failed: %d\n", rc);
209                 libcfs_debug_cleanup();
210                 return -1;
211         }
212
213         if (server_mode_flag)
214                 lnet_server_mode();
215
216         rc = lnet_selftest_init();
217         if (rc != 0) {
218                 fprintf(stderr, "Can't startup selftest\n");
219                 LNetFini();
220                 libcfs_debug_cleanup();
221
222                 return -1;
223         }
224
225         rc = lstjn_join_session(ses, grp);
226         if (rc != 0)
227                 goto out;
228
229         signal(SIGINT, lstjn_stop);
230
231         fprintf(stdout, "Start handling selftest requests, Ctl-C to stop\n");
232
233         while (!lstjn_stopping) {
234                 selftest_wait_events();
235
236                 if (!sfw_session_removed())
237                         continue;
238
239                 fprintf(stdout, "Session ended\n");
240                 break;
241         }
242
243 out:
244         lnet_selftest_fini();
245
246         LNetFini();
247
248         libcfs_debug_cleanup();
249
250         return rc;
251 }