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