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