Whamcloud - gitweb
Land b_smallfix onto HEAD (20040428_2142)
[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 #include <stdio.h>
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7 #include <arpa/inet.h>
8
9 #include <portals/ptlctl.h>     /* needed for parse_dump */
10
11
12 #include <liblustre.h>
13 #include <linux/obd.h>
14 #include <linux/obd_class.h>
15 #include <procbridge.h>
16
17 #define LIBLUSTRE_TEST 1
18 #include "../utils/lctl.c"
19
20 struct ldlm_namespace;
21 struct ldlm_res_id;
22 struct obd_import;
23
24 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_QSWNAL | S_SOCKNAL |
25                                             S_GMNAL | S_IBNAL);
26
27 void *inter_module_get(char *arg)
28 {
29         if (!strcmp(arg, "tcpnal_ni"))
30                 return &tcpnal_ni;
31         else if (!strcmp(arg, "ldlm_cli_cancel_unused"))
32                 return ldlm_cli_cancel_unused;
33         else if (!strcmp(arg, "ldlm_namespace_cleanup"))
34                 return ldlm_namespace_cleanup;
35         else if (!strcmp(arg, "ldlm_replay_locks"))
36                 return ldlm_replay_locks;
37         else
38                 return NULL;
39 }
40
41 /* XXX move to proper place */
42 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
43 {
44         switch(nal){
45         case TCPNAL:
46                 /* userspace NAL */
47         case SOCKNAL:
48                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u",
49                          (__u32)(nid >> 32), HIPQUAD(nid));
50                 break;
51         case QSWNAL:
52         case GMNAL:
53         case IBNAL:
54                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u",
55                          (__u32)(nid >> 32), (__u32)nid);
56                 break;
57         default:
58                 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx",
59                          nal, (long long)nid);
60                 break;
61         }
62         return str;
63 }
64
65 ptl_handle_ni_t         tcpnal_ni;
66
67 struct pingcli_args {
68         ptl_nid_t mynid;
69         ptl_nid_t nid;
70         ptl_pid_t port;
71         int count;
72         int size;
73 };
74
75 struct task_struct *current;
76
77 int
78 libcfs_nal_cmd(struct portals_cfg *pcfg)
79 {
80         CERROR("empty function!!!\n");
81         return 0;
82 }
83
84 int init_current(int argc, char **argv)
85
86         current = malloc(sizeof(*current));
87         strncpy(current->comm, argv[0], sizeof(current->comm));
88         current->pid = getpid();
89         return 0;
90 }
91
92 ptl_nid_t tcpnal_mynid;
93
94 int init_lib_portals()
95 {
96         int max_interfaces;
97         int rc;
98
99         rc = PtlInit(&max_interfaces);
100         if (rc != 0) {
101                 CERROR("ksocknal: PtlNIInit failed: error %d\n", rc);
102                 RETURN (rc);
103         }
104         return rc;
105 }
106
107 extern int class_handle_ioctl(unsigned int cmd, unsigned long arg);
108
109 int liblustre_ioctl(int dev_id, unsigned int opc, void *ptr)
110 {
111         int   rc = -EINVAL;
112         
113         switch (dev_id) {
114         default:
115                 fprintf(stderr, "Unexpected device id %d\n", dev_id);
116                 abort();
117                 break;
118                 
119         case OBD_DEV_ID:
120                 rc = class_handle_ioctl(opc, (unsigned long)ptr);
121                 break;
122         }
123
124         return rc;
125 }
126
127 static void generate_random_uuid(unsigned char uuid_out[16])
128 {
129         int *arr = (int*)uuid_out;
130         int i;
131
132         for (i = 0; i < sizeof(uuid_out)/sizeof(int); i++)
133                 arr[i] = rand();
134 }
135
136 static char *echo_server_nid = NULL;
137 static char *echo_server_ostname = "obd1";
138 static char *osc_dev_name = "OSC_DEV_NAME";
139 static char *echo_dev_name = "ECHO_CLIENT_DEV_NAME";
140
141 static int connect_echo_client(void)
142 {
143         struct lustre_cfg lcfg;
144         ptl_nid_t nid;
145         char *peer = "ECHO_PEER_NID";
146         class_uuid_t osc_uuid, echo_uuid;
147         struct obd_uuid osc_uuid_str, echo_uuid_str;
148         int nal, err;
149         ENTRY;
150
151         generate_random_uuid(osc_uuid);
152         class_uuid_unparse(osc_uuid, &osc_uuid_str);
153         generate_random_uuid(echo_uuid);
154         class_uuid_unparse(echo_uuid, &echo_uuid_str);
155
156         if (ptl_parse_nid(&nid, echo_server_nid)) {
157                 CERROR("Can't parse NID %s\n", echo_server_nid);
158                 RETURN(-EINVAL);
159         }
160         nal = ptl_name2nal("tcp");
161         if (nal <= 0) {
162                 CERROR("Can't parse NAL tcp\n");
163                 RETURN(-EINVAL);
164         }
165
166         /* add uuid */
167         LCFG_INIT(lcfg, LCFG_ADD_UUID, NULL);
168         lcfg.lcfg_nid = nid;
169         lcfg.lcfg_inllen1 = strlen(peer) + 1;
170         lcfg.lcfg_inlbuf1 = peer;
171         lcfg.lcfg_nal = nal;
172         err = class_process_config(&lcfg);
173         if (err < 0) {
174                 CERROR("failed add_uuid\n");
175                 RETURN(-EINVAL);
176         }
177
178         /* attach osc */
179         LCFG_INIT(lcfg, LCFG_ATTACH, osc_dev_name);
180         lcfg.lcfg_inlbuf1 = "osc";
181         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
182         lcfg.lcfg_inlbuf2 = osc_uuid_str.uuid;
183         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
184         err = class_process_config(&lcfg);
185         if (err < 0) {
186                 CERROR("failed attach osc\n");
187                 RETURN(-EINVAL);
188         }
189
190         /* setup osc */
191         LCFG_INIT(lcfg, LCFG_SETUP, osc_dev_name);
192         lcfg.lcfg_inlbuf1 = echo_server_ostname;
193         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
194         lcfg.lcfg_inlbuf2 = peer;
195         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
196         err = class_process_config(&lcfg);
197         if (err < 0) {
198                 CERROR("failed setup osc\n");
199                 RETURN(-EINVAL);
200         }
201
202         /* attach echo_client */
203         LCFG_INIT(lcfg, LCFG_ATTACH, echo_dev_name);
204         lcfg.lcfg_inlbuf1 = "echo_client";
205         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
206         lcfg.lcfg_inlbuf2 = echo_uuid_str.uuid;
207         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
208         err = class_process_config(&lcfg);
209         if (err < 0) {
210                 CERROR("failed attach echo_client\n");
211                 RETURN(-EINVAL);
212         }
213
214         /* setup echo_client */
215         LCFG_INIT(lcfg, LCFG_SETUP, echo_dev_name);
216         lcfg.lcfg_inlbuf1 = osc_dev_name;
217         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
218         lcfg.lcfg_inlbuf2 = NULL;
219         lcfg.lcfg_inllen2 = 0;
220         err = class_process_config(&lcfg);
221         if (err < 0) {
222                 CERROR("failed setup echo_client\n");
223                 RETURN(-EINVAL);
224         }
225
226         RETURN(0);
227 }
228
229 static int disconnect_echo_client(void)
230 {
231         struct lustre_cfg lcfg;
232         int err;
233         ENTRY;
234
235         /* cleanup echo_client */
236         LCFG_INIT(lcfg, LCFG_CLEANUP, echo_dev_name);
237         err = class_process_config(&lcfg);
238         if (err < 0) {
239                 CERROR("failed cleanup echo_client\n");
240                 RETURN(-EINVAL);
241         }
242
243         /* detach echo_client */
244         LCFG_INIT(lcfg, LCFG_DETACH, echo_dev_name);
245         err = class_process_config(&lcfg);
246         if (err < 0) {
247                 CERROR("failed detach echo_client\n");
248                 RETURN(-EINVAL);
249         }
250
251         /* cleanup osc */
252         LCFG_INIT(lcfg, LCFG_CLEANUP, osc_dev_name);
253         err = class_process_config(&lcfg);
254         if (err < 0) {
255                 CERROR("failed cleanup osc device\n");
256                 RETURN(-EINVAL);
257         }
258
259         /* detach osc */
260         LCFG_INIT(lcfg, LCFG_DETACH, osc_dev_name);
261         err = class_process_config(&lcfg);
262         if (err < 0) {
263                 CERROR("failed detach osc device\n");
264                 RETURN(-EINVAL);
265         }
266
267         RETURN(0);
268 }
269
270 static void usage(const char *s)
271 {
272         printf("Usage: %s -s ost_host_name [-n ost_name]\n", s);
273         printf("    ost_host_name: the host name of echo server\n");
274         printf("    ost_name: ost name, default is \"obd1\"\n");
275 }
276
277 extern int time_ptlwait1;
278 extern int time_ptlwait2;
279 extern int time_ptlselect;
280
281 int main(int argc, char **argv) 
282 {
283         int c, rc;
284
285         while ((c = getopt(argc, argv, "s:n:")) != -1) {
286                 switch (c) {
287                 case 's':
288                         echo_server_nid = optarg;
289                         break;
290                 case 'n':
291                         echo_server_ostname = optarg;
292                         break;
293                 default:
294                         usage(argv[0]);
295                         return 1;
296                 }
297         }
298
299         if (optind != argc)
300                 usage(argv[0]);
301
302         if (!echo_server_nid) {
303                 usage(argv[0]);
304                 return 1;
305         }
306
307         srand(time(NULL));
308
309         tcpnal_mynid = rand();
310 #if 1
311         portal_debug = 0;
312         portal_subsystem_debug = 0;
313 #endif
314
315         if (init_current(argc, argv) ||
316             init_obdclass() || init_lib_portals() ||
317             ptlrpc_init() ||
318             mdc_init() ||
319             lov_init() ||
320             osc_init() ||
321             echo_client_init()) {
322                 printf("error\n");
323                 return 1;
324         }
325
326         rc = connect_echo_client();
327         if (rc)
328                 return rc;
329
330         set_ioc_handler(liblustre_ioctl);
331
332         rc = lctl_main(1, &argv[0]);
333
334         rc |= disconnect_echo_client();
335
336         return rc;
337 }