Whamcloud - gitweb
Land b1_2_26 onto b1_2 (20040423_1204) - 2.6 build fixes
[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
15 #define LIBLUSTRE_TEST 1
16 #include "../utils/lctl.c"
17
18 struct ldlm_namespace;
19 struct ldlm_res_id;
20 struct obd_import;
21
22 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_QSWNAL | S_SOCKNAL |
23                                             S_GMNAL | S_IBNAL);
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         ptl_nid_t nid;
181         char *peer = "ECHO_PEER_NID";
182         class_uuid_t osc_uuid, echo_uuid;
183         struct obd_uuid osc_uuid_str, echo_uuid_str;
184         int nal, err;
185         ENTRY;
186
187         generate_random_uuid(osc_uuid);
188         class_uuid_unparse(osc_uuid, &osc_uuid_str);
189         generate_random_uuid(echo_uuid);
190         class_uuid_unparse(echo_uuid, &echo_uuid_str);
191
192         if (ptl_parse_nid(&nid, echo_server_nid)) {
193                 CERROR("Can't parse NID %s\n", echo_server_nid);
194                 RETURN(-EINVAL);
195         }
196         nal = ptl_name2nal("tcp");
197         if (nal <= 0) {
198                 CERROR("Can't parse NAL tcp\n");
199                 RETURN(-EINVAL);
200         }
201
202         /* add uuid */
203         LCFG_INIT(lcfg, LCFG_ADD_UUID, NULL);
204         lcfg.lcfg_nid = nid;
205         lcfg.lcfg_inllen1 = strlen(peer) + 1;
206         lcfg.lcfg_inlbuf1 = peer;
207         lcfg.lcfg_nal = nal;
208         err = class_process_config(&lcfg);
209         if (err < 0) {
210                 CERROR("failed add_uuid\n");
211                 RETURN(-EINVAL);
212         }
213
214         /* attach osc */
215         LCFG_INIT(lcfg, LCFG_ATTACH, osc_dev_name);
216         lcfg.lcfg_inlbuf1 = "osc";
217         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
218         lcfg.lcfg_inlbuf2 = osc_uuid_str.uuid;
219         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
220         err = class_process_config(&lcfg);
221         if (err < 0) {
222                 CERROR("failed attach osc\n");
223                 RETURN(-EINVAL);
224         }
225
226         /* setup osc */
227         LCFG_INIT(lcfg, LCFG_SETUP, osc_dev_name);
228         lcfg.lcfg_inlbuf1 = echo_server_ostname;
229         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
230         lcfg.lcfg_inlbuf2 = peer;
231         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
232         err = class_process_config(&lcfg);
233         if (err < 0) {
234                 CERROR("failed setup osc\n");
235                 RETURN(-EINVAL);
236         }
237
238         /* attach echo_client */
239         LCFG_INIT(lcfg, LCFG_ATTACH, echo_dev_name);
240         lcfg.lcfg_inlbuf1 = "echo_client";
241         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
242         lcfg.lcfg_inlbuf2 = echo_uuid_str.uuid;
243         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
244         err = class_process_config(&lcfg);
245         if (err < 0) {
246                 CERROR("failed attach echo_client\n");
247                 RETURN(-EINVAL);
248         }
249
250         /* setup echo_client */
251         LCFG_INIT(lcfg, LCFG_SETUP, echo_dev_name);
252         lcfg.lcfg_inlbuf1 = osc_dev_name;
253         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
254         lcfg.lcfg_inlbuf2 = NULL;
255         lcfg.lcfg_inllen2 = 0;
256         err = class_process_config(&lcfg);
257         if (err < 0) {
258                 CERROR("failed setup echo_client\n");
259                 RETURN(-EINVAL);
260         }
261
262         RETURN(0);
263 }
264
265 static int disconnect_echo_client(void)
266 {
267         struct lustre_cfg lcfg;
268         int err;
269         ENTRY;
270
271         /* cleanup echo_client */
272         LCFG_INIT(lcfg, LCFG_CLEANUP, echo_dev_name);
273         err = class_process_config(&lcfg);
274         if (err < 0) {
275                 CERROR("failed cleanup echo_client\n");
276                 RETURN(-EINVAL);
277         }
278
279         /* detach echo_client */
280         LCFG_INIT(lcfg, LCFG_DETACH, echo_dev_name);
281         err = class_process_config(&lcfg);
282         if (err < 0) {
283                 CERROR("failed detach echo_client\n");
284                 RETURN(-EINVAL);
285         }
286
287         /* cleanup osc */
288         LCFG_INIT(lcfg, LCFG_CLEANUP, osc_dev_name);
289         err = class_process_config(&lcfg);
290         if (err < 0) {
291                 CERROR("failed cleanup osc device\n");
292                 RETURN(-EINVAL);
293         }
294
295         /* detach osc */
296         LCFG_INIT(lcfg, LCFG_DETACH, osc_dev_name);
297         err = class_process_config(&lcfg);
298         if (err < 0) {
299                 CERROR("failed detach osc device\n");
300                 RETURN(-EINVAL);
301         }
302
303         RETURN(0);
304 }
305
306 static void usage(const char *s)
307 {
308         printf("Usage: %s -s ost_host_name [-n ost_name]\n", s);
309         printf("    ost_host_name: the host name of echo server\n");
310         printf("    ost_name: ost name, default is \"obd1\"\n");
311 }
312
313 extern int time_ptlwait1;
314 extern int time_ptlwait2;
315 extern int time_ptlselect;
316
317 int main(int argc, char **argv) 
318 {
319         int c, rc;
320
321         while ((c = getopt(argc, argv, "s:n:")) != -1) {
322                 switch (c) {
323                 case 's':
324                         echo_server_nid = optarg;
325                         break;
326                 case 'n':
327                         echo_server_ostname = optarg;
328                         break;
329                 default:
330                         usage(argv[0]);
331                         return 1;
332                 }
333         }
334
335         if (optind != argc)
336                 usage(argv[0]);
337
338         if (!echo_server_nid) {
339                 usage(argv[0]);
340                 return 1;
341         }
342
343         srand(time(NULL));
344
345         tcpnal_mynid = rand();
346 #if 1
347         portal_debug = 0;
348         portal_subsystem_debug = 0;
349 #endif
350
351         if (init_current(argc, argv) ||
352             init_obdclass() || init_lib_portals() ||
353             ptlrpc_init() ||
354             ldlm_init() ||
355             mdc_init() ||
356             lov_init() ||
357             osc_init() ||
358             echo_client_init()) {
359                 printf("error\n");
360                 return 1;
361         }
362
363         rc = connect_echo_client();
364         if (rc)
365                 return rc;
366
367         set_ioc_handler(liblustre_ioctl);
368
369         rc = lctl_main(1, &argv[0]);
370
371         rc |= disconnect_echo_client();
372
373         return rc;
374 }