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