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