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