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