Whamcloud - gitweb
LU-1422 cray: Cray platforms set the initial connection value to high
[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 extern int class_handle_ioctl(unsigned int cmd, unsigned long arg);
50
51 static int liblustre_ioctl(int dev_id, unsigned int opc, void *ptr)
52 {
53         int   rc = -EINVAL;
54         
55         switch (dev_id) {
56         default:
57                 fprintf(stderr, "Unexpected device id %d\n", dev_id);
58                 abort();
59                 break;
60                 
61         case OBD_DEV_ID:
62                 rc = class_handle_ioctl(opc, (unsigned long)ptr);
63                 break;
64         }
65
66         return rc;
67 }
68
69 static char *echo_server_nid = NULL;
70 static char *echo_server_ostname = "obd1";
71 static char *osc_dev_name = "OSC_DEV_NAME";
72 static char *echo_dev_name = "ECHO_CLIENT_DEV_NAME";
73
74 static int connect_echo_client(void)
75 {
76         struct lustre_cfg *lcfg;
77         struct lustre_cfg_bufs bufs;
78         lnet_nid_t nid;
79         char *peer = "ECHO_PEER_NID";
80         class_uuid_t osc_uuid, echo_uuid;
81         struct obd_uuid osc_uuid_str, echo_uuid_str;
82         int err;
83         ENTRY;
84
85         ll_generate_random_uuid(osc_uuid);
86         class_uuid_unparse(osc_uuid, &osc_uuid_str);
87         ll_generate_random_uuid(echo_uuid);
88         class_uuid_unparse(echo_uuid, &echo_uuid_str);
89
90         nid = libcfs_str2nid(echo_server_nid);
91         if (nid == LNET_NID_ANY) {
92                 CERROR("Can't parse NID %s\n", echo_server_nid);
93                 RETURN(-EINVAL);
94         }
95
96         /* add uuid */
97         lustre_cfg_bufs_reset(&bufs, NULL);
98         lustre_cfg_bufs_set_string(&bufs, 1, peer);
99         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
100         lcfg->lcfg_nid = nid;
101         err = class_process_config(lcfg);
102         lustre_cfg_free(lcfg);
103         if (err < 0) {
104                 CERROR("failed add_uuid\n");
105                 RETURN(-EINVAL);
106         }
107
108         /* attach osc */
109         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
110         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_OSC_NAME);
111         lustre_cfg_bufs_set_string(&bufs, 2, osc_uuid_str.uuid);
112         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
113         err = class_process_config(lcfg);
114         lustre_cfg_free(lcfg);
115         if (err < 0) {
116                 CERROR("failed attach osc\n");
117                 RETURN(-EINVAL);
118         }
119
120         /* setup osc */
121         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
122         lustre_cfg_bufs_set_string(&bufs, 1, echo_server_ostname);
123         lustre_cfg_bufs_set_string(&bufs, 2, peer);
124         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
125         err = class_process_config(lcfg);
126         lustre_cfg_free(lcfg);
127         if (err < 0) {
128                 CERROR("failed setup osc\n");
129                 RETURN(-EINVAL);
130         }
131
132         /* attach echo_client */
133         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
134         lustre_cfg_bufs_set_string(&bufs, 1, "echo_client");
135         lustre_cfg_bufs_set_string(&bufs, 2, echo_uuid_str.uuid);
136         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
137         err = class_process_config(lcfg);
138         lustre_cfg_free(lcfg);
139         if (err < 0) {
140                 CERROR("failed attach echo_client\n");
141                 RETURN(-EINVAL);
142         }
143
144         /* setup echo_client */
145         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
146         lustre_cfg_bufs_set_string(&bufs, 1, osc_dev_name);
147         lustre_cfg_bufs_set_string(&bufs, 2, NULL);
148         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
149         err = class_process_config(lcfg);
150         lustre_cfg_free(lcfg);
151         if (err < 0) {
152                 CERROR("failed setup echo_client\n");
153                 RETURN(-EINVAL);
154         }
155
156         RETURN(0);
157 }
158
159 static int disconnect_echo_client(void)
160 {
161         struct lustre_cfg_bufs bufs;
162         struct lustre_cfg *lcfg = NULL;
163         int err;
164         ENTRY;
165
166         /* cleanup echo_client */
167         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
168         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
169         err = class_process_config(lcfg);
170         if (err < 0) {
171                 lustre_cfg_free(lcfg);
172                 CERROR("failed cleanup echo_client\n");
173                 RETURN(-EINVAL);
174         }
175
176         /* detach echo_client */
177         lcfg->lcfg_command = LCFG_DETACH;
178         err = class_process_config(lcfg);
179         lustre_cfg_free(lcfg);
180         if (err < 0) {
181                 CERROR("failed detach echo_client\n");
182                 RETURN(-EINVAL);
183         }
184
185         /* cleanup osc */
186         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
187         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
188         err = class_process_config(lcfg);
189         if (err < 0) {
190                 lustre_cfg_free(lcfg);
191                 CERROR("failed cleanup osc device\n");
192                 RETURN(-EINVAL);
193         }
194
195         /* detach osc */
196         lcfg->lcfg_command = LCFG_DETACH;
197         err = class_process_config(lcfg);
198         lustre_cfg_free(lcfg);
199         if (err < 0) {
200                 CERROR("failed detach osc device\n");
201                 RETURN(-EINVAL);
202         }
203
204         RETURN(0);
205 }
206
207 static void usage(const char *s)
208 {
209         printf("Usage: %s -s ost_host_name [-n ost_name] [-x lctl_options ...]\n", s);
210         printf("    ost_host_name: the host name of echo server\n");
211         printf("    ost_name: ost name, default is \"obd1\"\n");
212         printf("    lctl_options: options to pass to lctl.\n");
213         printf("            (e.g. -x --device 1 test_getattr 10000 -5)\n");
214 }
215
216 extern int time_ptlwait1;
217 extern int time_ptlwait2;
218 extern int time_ptlselect;
219
220 int main(int argc, char **argv) 
221 {
222         int c, rc;
223         int xindex  = -1;  /* index of -x option */
224
225         /* loop until all options are consumed or we hit
226          * a -x option 
227          */
228         while ((c = getopt(argc, argv, "s:n:x:")) != -1 && 
229                xindex == -1) {
230                 switch (c) {
231                 case 's':
232                         echo_server_nid = optarg;
233                         break;
234                 case 'n':
235                         echo_server_ostname = optarg;
236                         break;
237                 case 'x':
238                         xindex = optind-1;
239                         break;
240                 default:
241                         usage(argv[0]);
242                         return 1;
243                 }
244         }
245
246         /*
247          * Only warn with usage() if the -x option isn't specificed
248          * because when using -x this check is not valid.
249          */
250         if (optind != argc && xindex == -1)
251                 usage(argv[0]);
252
253         if (!echo_server_nid) {
254                 usage(argv[0]);
255                 return 1;
256         }
257
258         libcfs_debug = 0;
259         libcfs_subsystem_debug = 0;
260
261         liblustre_init_random();
262
263         if (liblustre_init_current(argv[0]) ||
264             init_obdclass() || init_lib_portals() ||
265             ptlrpc_init() ||
266             lmv_init() ||
267             mdc_init() ||
268             lov_init() ||
269             osc_init() ||
270             echo_client_init()) {
271                 printf("error\n");
272                 return 1;
273         }
274
275         rc = connect_echo_client();
276         if (rc)
277                 return rc;
278
279         set_ioc_handler(liblustre_ioctl);
280
281
282         /*
283          * If the -x option is not specified pass no args to lctl
284          * otherwise pass all the options after the "-x" to lctl
285          *
286          * HACK: in the case when the -x option is specified
287          * lctl sees argv[0] == "-x" and not the real argv[0] seen
288          * in this function.  If that is a problem, a mapping will
289          * have to be done to fix that.  However for normal functioning
290          * it seems to be irrelavant
291          */
292         if( xindex == -1 )
293                 rc = lctl_main(1, &argv[0]);
294         else
295                 rc = lctl_main(argc-xindex+1, &argv[xindex-1]);
296
297         rc |= disconnect_echo_client();
298
299         return rc;
300 }