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