Whamcloud - gitweb
Move random uuid functions to prng.c
[fs/lustre-release.git] / lustre / liblustre / tests / echo_test.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light user test program
5  *
6  *  Copyright (c) 2002-2004 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <liblustre.h>
25 #include <obd.h>
26 #include <obd_class.h>
27 #include <obd_ost.h>
28
29 #define LIBLUSTRE_TEST 1
30 #include "../utils/lctl.c"
31
32 #include "../lutil.h"
33
34
35 #ifdef CRAY_XT3
36 int _sysio_lustre_init(void)
37 {
38         /*
39          * This is an aweful HACK.  Basically the problem is on
40          * Catamount, the build system links in liblustre.a to
41          * all the test executables, and at this point its not
42          * clear how to modify the build system to prevent this
43          * from happening.  So providing our own call to 
44          * _sysio_lustre_init() that does nothing, prevents
45          * liblustre.a from initializing.
46          *
47          * Why is liblustre.a initializing a problem anyway.  Well
48          * this main() in this module calls init_obdclass(), as 
49          * well as the llite_lib.c's _sysio_lustre_init().  Two
50          * calls to init_obdclass() cause an assertion.  Secondly
51          * it doesn't even logically make sense, this is module
52          * does not need lustre file system functionality, it's 
53          * just the echo_tester.
54          * 
55          */
56         /*lprintf("--> THIS OVERRIDES liblustre.a INITIALIZATION <--\n");*/
57         return 0;
58 }
59 #endif
60
61
62
63 extern int class_handle_ioctl(unsigned int cmd, unsigned long arg);
64
65 static int liblustre_ioctl(int dev_id, unsigned int opc, void *ptr)
66 {
67         int   rc = -EINVAL;
68         
69         switch (dev_id) {
70         default:
71                 fprintf(stderr, "Unexpected device id %d\n", dev_id);
72                 abort();
73                 break;
74                 
75         case OBD_DEV_ID:
76                 rc = class_handle_ioctl(opc, (unsigned long)ptr);
77                 break;
78         }
79
80         return rc;
81 }
82
83 static char *echo_server_nid = NULL;
84 static char *echo_server_ostname = "obd1";
85 static char *osc_dev_name = "OSC_DEV_NAME";
86 static char *echo_dev_name = "ECHO_CLIENT_DEV_NAME";
87
88 static int connect_echo_client(void)
89 {
90         struct lustre_cfg *lcfg;
91         struct lustre_cfg_bufs bufs;
92         lnet_nid_t nid;
93         char *peer = "ECHO_PEER_NID";
94         class_uuid_t osc_uuid, echo_uuid;
95         struct obd_uuid osc_uuid_str, echo_uuid_str;
96         int err;
97         ENTRY;
98
99         ll_generate_random_uuid(osc_uuid);
100         class_uuid_unparse(osc_uuid, &osc_uuid_str);
101         ll_generate_random_uuid(echo_uuid);
102         class_uuid_unparse(echo_uuid, &echo_uuid_str);
103
104         nid = libcfs_str2nid(echo_server_nid);
105         if (nid == LNET_NID_ANY) {
106                 CERROR("Can't parse NID %s\n", echo_server_nid);
107                 RETURN(-EINVAL);
108         }
109
110         /* add uuid */
111         lustre_cfg_bufs_reset(&bufs, NULL);
112         lustre_cfg_bufs_set_string(&bufs, 1, peer);
113         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
114         lcfg->lcfg_nid = nid;
115         err = class_process_config(lcfg);
116         lustre_cfg_free(lcfg);
117         if (err < 0) {
118                 CERROR("failed add_uuid\n");
119                 RETURN(-EINVAL);
120         }
121
122         /* attach osc */
123         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
124         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_OSC_NAME);
125         lustre_cfg_bufs_set_string(&bufs, 2, osc_uuid_str.uuid);
126         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
127         err = class_process_config(lcfg);
128         lustre_cfg_free(lcfg);
129         if (err < 0) {
130                 CERROR("failed attach osc\n");
131                 RETURN(-EINVAL);
132         }
133
134         /* setup osc */
135         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
136         lustre_cfg_bufs_set_string(&bufs, 1, echo_server_ostname);
137         lustre_cfg_bufs_set_string(&bufs, 2, peer);
138         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
139         err = class_process_config(lcfg);
140         lustre_cfg_free(lcfg);
141         if (err < 0) {
142                 CERROR("failed setup osc\n");
143                 RETURN(-EINVAL);
144         }
145
146         /* attach echo_client */
147         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
148         lustre_cfg_bufs_set_string(&bufs, 1, "echo_client");
149         lustre_cfg_bufs_set_string(&bufs, 2, echo_uuid_str.uuid);
150         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
151         err = class_process_config(lcfg);
152         lustre_cfg_free(lcfg);
153         if (err < 0) {
154                 CERROR("failed attach echo_client\n");
155                 RETURN(-EINVAL);
156         }
157
158         /* setup echo_client */
159         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
160         lustre_cfg_bufs_set_string(&bufs, 1, osc_dev_name);
161         lustre_cfg_bufs_set_string(&bufs, 2, NULL);
162         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
163         err = class_process_config(lcfg);
164         lustre_cfg_free(lcfg);
165         if (err < 0) {
166                 CERROR("failed setup echo_client\n");
167                 RETURN(-EINVAL);
168         }
169
170         RETURN(0);
171 }
172
173 static int disconnect_echo_client(void)
174 {
175         struct lustre_cfg_bufs bufs;
176         struct lustre_cfg *lcfg = NULL;
177         int err;
178         ENTRY;
179
180         /* cleanup echo_client */
181         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
182         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
183         err = class_process_config(lcfg);
184         if (err < 0) {
185                 lustre_cfg_free(lcfg);
186                 CERROR("failed cleanup echo_client\n");
187                 RETURN(-EINVAL);
188         }
189
190         /* detach echo_client */
191         lcfg->lcfg_command = LCFG_DETACH;
192         err = class_process_config(lcfg);
193         lustre_cfg_free(lcfg);
194         if (err < 0) {
195                 CERROR("failed detach echo_client\n");
196                 RETURN(-EINVAL);
197         }
198
199         /* cleanup osc */
200         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
201         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
202         err = class_process_config(lcfg);
203         if (err < 0) {
204                 lustre_cfg_free(lcfg);
205                 CERROR("failed cleanup osc device\n");
206                 RETURN(-EINVAL);
207         }
208
209         /* detach osc */
210         lcfg->lcfg_command = LCFG_DETACH;
211         err = class_process_config(lcfg);
212         lustre_cfg_free(lcfg);
213         if (err < 0) {
214                 CERROR("failed detach osc device\n");
215                 RETURN(-EINVAL);
216         }
217
218         RETURN(0);
219 }
220
221 static void usage(const char *s)
222 {
223         printf("Usage: %s -s ost_host_name [-n ost_name] [-x lctl_options ...]\n", s);
224         printf("    ost_host_name: the host name of echo server\n");
225         printf("    ost_name: ost name, default is \"obd1\"\n");
226         printf("    lctl_options: options to pass to lctl.\n");
227         printf("            (e.g. -x --device 1 test_getattr 10000 -5)\n");
228 }
229
230 extern int time_ptlwait1;
231 extern int time_ptlwait2;
232 extern int time_ptlselect;
233
234 int main(int argc, char **argv) 
235 {
236         int c, rc;
237         int xindex  = -1;  /* index of -x option */
238
239         /* loop until all options are consumed or we hit
240          * a -x option 
241          */
242         while ((c = getopt(argc, argv, "s:n:x:")) != -1 && 
243                xindex == -1) {
244                 switch (c) {
245                 case 's':
246                         echo_server_nid = optarg;
247                         break;
248                 case 'n':
249                         echo_server_ostname = optarg;
250                         break;
251                 case 'x':
252                         xindex = optind-1;
253                         break;
254                 default:
255                         usage(argv[0]);
256                         return 1;
257                 }
258         }
259
260         /*
261          * Only warn with usage() if the -x option isn't specificed
262          * because when using -x this check is not valid.
263          */
264         if (optind != argc && xindex == -1)
265                 usage(argv[0]);
266
267         if (!echo_server_nid) {
268                 usage(argv[0]);
269                 return 1;
270         }
271
272         libcfs_debug = 0;
273         libcfs_subsystem_debug = 0;
274
275         liblustre_init_random();
276
277         if (liblustre_init_current(argv[0]) ||
278             init_obdclass() || init_lib_portals() ||
279             ptlrpc_init() ||
280             mdc_init() ||
281             lov_init() ||
282             osc_init() ||
283             echo_client_init()) {
284                 printf("error\n");
285                 return 1;
286         }
287
288         rc = connect_echo_client();
289         if (rc)
290                 return rc;
291
292         set_ioc_handler(liblustre_ioctl);
293
294
295         /*
296          * If the -x option is not specified pass no args to lctl
297          * otherwise pass all the options after the "-x" to lctl
298          *
299          * HACK: in the case when the -x option is specified
300          * lctl sees argv[0] == "-x" and not the real argv[0] seen
301          * in this function.  If that is a problem, a mapping will
302          * have to be done to fix that.  However for normal functioning
303          * it seems to be irrelavant
304          */
305         if( xindex == -1 )
306                 rc = lctl_main(1, &argv[0]);
307         else
308                 rc = lctl_main(argc-xindex+1, &argv[xindex-1]);
309
310         rc |= disconnect_echo_client();
311
312         return rc;
313 }