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