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