Whamcloud - gitweb
- landed b_hd_cray_merge3
[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         ptl_nid_t nid;
100         char *peer = "ECHO_PEER_NID";
101         class_uuid_t osc_uuid, echo_uuid;
102         struct obd_uuid osc_uuid_str, echo_uuid_str;
103         int nal, err;
104         ENTRY;
105
106         generate_random_uuid(osc_uuid);
107         class_uuid_unparse(osc_uuid, &osc_uuid_str);
108         generate_random_uuid(echo_uuid);
109         class_uuid_unparse(echo_uuid, &echo_uuid_str);
110
111         if (ptl_parse_nid(&nid, echo_server_nid)) {
112                 CERROR("Can't parse NID %s\n", echo_server_nid);
113                 RETURN(-EINVAL);
114         }
115         nal = ptl_name2nal("tcp");
116         if (nal <= 0) {
117                 CERROR("Can't parse NAL tcp\n");
118                 RETURN(-EINVAL);
119         }
120
121         /* add uuid */
122         LCFG_INIT(lcfg, LCFG_ADD_UUID, NULL);
123         lcfg.lcfg_nid = nid;
124         lcfg.lcfg_inllen1 = strlen(peer) + 1;
125         lcfg.lcfg_inlbuf1 = peer;
126         lcfg.lcfg_nal = nal;
127         err = class_process_config(&lcfg);
128         if (err < 0) {
129                 CERROR("failed add_uuid\n");
130                 RETURN(-EINVAL);
131         }
132
133         /* attach osc */
134         LCFG_INIT(lcfg, LCFG_ATTACH, osc_dev_name);
135         lcfg.lcfg_inlbuf1 = "osc";
136         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
137         lcfg.lcfg_inlbuf2 = osc_uuid_str.uuid;
138         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
139         err = class_process_config(&lcfg);
140         if (err < 0) {
141                 CERROR("failed attach osc\n");
142                 RETURN(-EINVAL);
143         }
144
145         /* setup osc */
146         LCFG_INIT(lcfg, LCFG_SETUP, osc_dev_name);
147         lcfg.lcfg_inlbuf1 = echo_server_ostname;
148         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
149         lcfg.lcfg_inlbuf2 = peer;
150         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
151         err = class_process_config(&lcfg);
152         if (err < 0) {
153                 CERROR("failed setup osc\n");
154                 RETURN(-EINVAL);
155         }
156
157         /* attach echo_client */
158         LCFG_INIT(lcfg, LCFG_ATTACH, echo_dev_name);
159         lcfg.lcfg_inlbuf1 = "echo_client";
160         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
161         lcfg.lcfg_inlbuf2 = echo_uuid_str.uuid;
162         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
163         err = class_process_config(&lcfg);
164         if (err < 0) {
165                 CERROR("failed attach echo_client\n");
166                 RETURN(-EINVAL);
167         }
168
169         /* setup echo_client */
170         LCFG_INIT(lcfg, LCFG_SETUP, echo_dev_name);
171         lcfg.lcfg_inlbuf1 = osc_dev_name;
172         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
173         lcfg.lcfg_inlbuf2 = NULL;
174         lcfg.lcfg_inllen2 = 0;
175         err = class_process_config(&lcfg);
176         if (err < 0) {
177                 CERROR("failed setup echo_client\n");
178                 RETURN(-EINVAL);
179         }
180
181         RETURN(0);
182 }
183
184 static int disconnect_echo_client(void)
185 {
186         struct lustre_cfg lcfg;
187         int err;
188         ENTRY;
189
190         /* cleanup echo_client */
191         LCFG_INIT(lcfg, LCFG_CLEANUP, echo_dev_name);
192         err = class_process_config(&lcfg);
193         if (err < 0) {
194                 CERROR("failed cleanup echo_client\n");
195                 RETURN(-EINVAL);
196         }
197
198         /* detach echo_client */
199         LCFG_INIT(lcfg, LCFG_DETACH, echo_dev_name);
200         err = class_process_config(&lcfg);
201         if (err < 0) {
202                 CERROR("failed detach echo_client\n");
203                 RETURN(-EINVAL);
204         }
205
206         /* cleanup osc */
207         LCFG_INIT(lcfg, LCFG_CLEANUP, osc_dev_name);
208         err = class_process_config(&lcfg);
209         if (err < 0) {
210                 CERROR("failed cleanup osc device\n");
211                 RETURN(-EINVAL);
212         }
213
214         /* detach osc */
215         LCFG_INIT(lcfg, LCFG_DETACH, osc_dev_name);
216         err = class_process_config(&lcfg);
217         if (err < 0) {
218                 CERROR("failed detach osc device\n");
219                 RETURN(-EINVAL);
220         }
221
222         RETURN(0);
223 }
224
225 static void usage(const char *s)
226 {
227         printf("Usage: %s -s ost_host_name [-n ost_name]\n", s);
228         printf("    ost_host_name: the host name of echo server\n");
229         printf("    ost_name: ost name, default is \"obd1\"\n");
230 }
231
232 extern int time_ptlwait1;
233 extern int time_ptlwait2;
234 extern int time_ptlselect;
235
236 int main(int argc, char **argv) 
237 {
238         int c, rc;
239
240         while ((c = getopt(argc, argv, "s:n:")) != -1) {
241                 switch (c) {
242                 case 's':
243                         echo_server_nid = optarg;
244                         break;
245                 case 'n':
246                         echo_server_ostname = optarg;
247                         break;
248                 default:
249                         usage(argv[0]);
250                         return 1;
251                 }
252         }
253
254         if (optind != argc)
255                 usage(argv[0]);
256
257         if (!echo_server_nid) {
258                 usage(argv[0]);
259                 return 1;
260         }
261
262         portal_debug = 0;
263         portal_subsystem_debug = 0;
264
265         liblustre_init_random();
266         liblustre_set_nal_nid();
267
268         if (liblustre_init_current(argv[0]) ||
269             init_obdclass() || init_lib_portals() ||
270             ptlrpc_init() ||
271             mdc_init() ||
272             lov_init() ||
273             osc_init() ||
274             echo_client_init()) {
275                 printf("error\n");
276                 return 1;
277         }
278
279         rc = connect_echo_client();
280         if (rc)
281                 return rc;
282
283         set_ioc_handler(liblustre_ioctl);
284
285         rc = lctl_main(1, &argv[0]);
286
287         rc |= disconnect_echo_client();
288
289         return rc;
290 }