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