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