Whamcloud - gitweb
Branch: HEAD
[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 <linux/obd.h>
26 #include <linux/obd_class.h>
27
28 #define LIBLUSTRE_TEST 1
29 #include "../utils/lctl.c"
30
31 #include "../lutil.h"
32
33 extern int class_handle_ioctl(unsigned int cmd, unsigned long arg);
34
35 struct pingcli_args {
36         ptl_nid_t mynid;
37         ptl_nid_t nid;
38         ptl_pid_t port;
39         int count;
40         int size;
41 };
42
43 /*      bug #4615       */
44 #if 0
45 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
46 {
47         switch(nal){
48         case TCPNAL:
49                 /* userspace NAL */
50         case IIBNAL:
51         case VIBNAL:
52         case OPENIBNAL:
53         case RANAL:
54         case SOCKNAL:
55                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u,%u",
56                          (__u32)(id.nid >> 32), HIPQUAD((id.nid)) , id.pid);
57                 break;
58         case QSWNAL:
59         case GMNAL:
60         case LONAL:
61                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u,%u",
62                          (__u32)(id.nid >> 32), (__u32)id.nid, id.pid);
63                 break;
64         default:
65                 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx,%lx",
66                          nal, (long long)id.nid, (long)id.pid );
67                 break;
68         }
69         return str;
70 }
71 #endif
72
73 static int liblustre_ioctl(int dev_id, unsigned int opc, void *ptr)
74 {
75         int   rc = -EINVAL;
76         
77         switch (dev_id) {
78         default:
79                 fprintf(stderr, "Unexpected device id %d\n", dev_id);
80                 abort();
81                 break;
82                 
83         case OBD_DEV_ID:
84                 rc = class_handle_ioctl(opc, (unsigned long)ptr);
85                 break;
86         }
87
88         return rc;
89 }
90
91 static char *echo_server_nid = NULL;
92 static char *echo_server_ostname = "obd1";
93 static char *osc_dev_name = "OSC_DEV_NAME";
94 static char *echo_dev_name = "ECHO_CLIENT_DEV_NAME";
95
96 static int connect_echo_client(void)
97 {
98         struct lustre_cfg *lcfg;
99         struct lustre_cfg_bufs bufs;
100         ptl_nid_t nid;
101         char *peer = "ECHO_PEER_NID";
102         class_uuid_t osc_uuid, echo_uuid;
103         struct obd_uuid osc_uuid_str, echo_uuid_str;
104         int nal, err;
105         ENTRY;
106
107         generate_random_uuid(osc_uuid);
108         class_uuid_unparse(osc_uuid, &osc_uuid_str);
109         generate_random_uuid(echo_uuid);
110         class_uuid_unparse(echo_uuid, &echo_uuid_str);
111
112         if (ptl_parse_nid(&nid, echo_server_nid)) {
113                 CERROR("Can't parse NID %s\n", echo_server_nid);
114                 RETURN(-EINVAL);
115         }
116         nal = ptl_name2nal("tcp");
117         if (nal <= 0) {
118                 CERROR("Can't parse NAL tcp\n");
119                 RETURN(-EINVAL);
120         }
121         /* add uuid */
122         lustre_cfg_bufs_reset(&bufs, NULL);
123         lustre_cfg_bufs_set_string(&bufs, 1, peer);
124         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
125         lcfg->lcfg_nid = nid;
126         lcfg->lcfg_nal = nal;
127         err = class_process_config(lcfg);
128         lustre_cfg_free(lcfg);
129         if (err < 0) {
130                 CERROR("failed add_uuid\n");
131                 RETURN(-EINVAL);
132         }
133         lustre_cfg_bufs_reset(&bufs, LUSTRE_OSC_NAME);
134         lustre_cfg_bufs_set_string(&bufs, 2, osc_uuid_str.uuid);
135         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
136         err = class_process_config(lcfg);
137         lustre_cfg_free(lcfg);
138         if (err < 0) {
139                 CERROR("failed attach osc\n");
140                 RETURN(-EINVAL);
141         }
142
143         /* setup osc */
144         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
145         lustre_cfg_bufs_set_string(&bufs, 1, echo_server_ostname);
146         lustre_cfg_bufs_set_string(&bufs, 2, peer);
147         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
148         err = class_process_config(lcfg);
149         lustre_cfg_free(lcfg);
150         if (err < 0) {
151                 CERROR("failed setup osc\n");
152                 RETURN(-EINVAL);
153         }
154
155         /* attach echo_client */
156         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
157         lustre_cfg_bufs_set_string(&bufs, 1, "echo_client");
158         lustre_cfg_bufs_set_string(&bufs, 2, echo_uuid_str.uuid);
159         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
160         err = class_process_config(lcfg);
161         lustre_cfg_free(lcfg);
162         if (err < 0) {
163                 CERROR("failed attach echo_client\n");
164                 RETURN(-EINVAL);
165         }
166
167         /* setup echo_client */
168         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
169         lustre_cfg_bufs_set_string(&bufs, 1, osc_dev_name);
170         lustre_cfg_bufs_set_string(&bufs, 2, NULL);
171         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
172         err = class_process_config(lcfg);
173         lustre_cfg_free(lcfg);
174         if (err < 0) {
175                 CERROR("failed setup echo_client\n");
176                 RETURN(-EINVAL);
177         }
178
179         RETURN(0);
180 }
181
182 static int disconnect_echo_client(void)
183 {
184         struct lustre_cfg lcfg;
185         int err;
186         ENTRY;
187
188         /* cleanup echo_client */
189         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
190         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
191         err = class_process_config(lcfg);
192         if (err < 0) {
193                 lustre_cfg_free(lcfg);
194                 CERROR("failed cleanup echo_client\n");
195                 RETURN(-EINVAL);
196         }
197
198         /* detach echo_client */
199         lcfg->lcfg_command = LCFG_DETACH;
200         err = class_process_config(lcfg);
201         lustre_cfg_free(lcfg);
202         if (err < 0) {
203                 CERROR("failed detach echo_client\n");
204                 RETURN(-EINVAL);
205         }
206
207         /* cleanup osc */
208         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
209         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
210         err = class_process_config(lcfg);
211         if (err < 0) {
212                 lustre_cfg_free(lcfg);
213                 CERROR("failed cleanup osc device\n");
214                 RETURN(-EINVAL);
215         }
216
217         /* detach osc */
218         lcfg->lcfg_command = LCFG_DETACH;
219         err = class_process_config(lcfg);
220         lustre_cfg_free(lcfg);
221         if (err < 0) {
222                 CERROR("failed detach osc device\n");
223                 RETURN(-EINVAL);
224         }
225
226         RETURN(0);
227 }
228
229 static void usage(const char *s)
230 {
231         printf("Usage: %s -s ost_host_name [-n ost_name]\n", s);
232         printf("    ost_host_name: the host name of echo server\n");
233         printf("    ost_name: ost name, default is \"obd1\"\n");
234 }
235
236 extern int time_ptlwait1;
237 extern int time_ptlwait2;
238 extern int time_ptlselect;
239
240 int main(int argc, char **argv) 
241 {
242         int c, rc;
243
244         while ((c = getopt(argc, argv, "s:n:")) != -1) {
245                 switch (c) {
246                 case 's':
247                         echo_server_nid = optarg;
248                         break;
249                 case 'n':
250                         echo_server_ostname = optarg;
251                         break;
252                 default:
253                         usage(argv[0]);
254                         return 1;
255                 }
256         }
257
258         if (optind != argc)
259                 usage(argv[0]);
260
261         if (!echo_server_nid) {
262                 usage(argv[0]);
263                 return 1;
264         }
265
266         portal_debug = 0;
267         portal_subsystem_debug = 0;
268
269         liblustre_init_random();
270         liblustre_set_nal_nid();
271
272         if (liblustre_init_current(argv[0]) ||
273             init_obdclass() || init_lib_portals() ||
274             ptlrpc_init() ||
275             mdc_init() ||
276             lov_init() ||
277             osc_init() ||
278             echo_client_init()) {
279                 printf("error\n");
280                 return 1;
281         }
282
283         rc = connect_echo_client();
284         if (rc)
285                 return rc;
286
287         set_ioc_handler(liblustre_ioctl);
288
289         rc = lctl_main(1, &argv[0]);
290
291         rc |= disconnect_echo_client();
292
293         return rc;
294 }