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