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