Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / liblustre / libtest.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 void *inter_module_get(char *arg)
23 {
24         if (!strcmp(arg, "tcpnal_ni"))
25                 return &tcpnal_ni;
26         else if (!strcmp(arg, "ldlm_cli_cancel_unused"))
27                 return ldlm_cli_cancel_unused;
28         else if (!strcmp(arg, "ldlm_namespace_cleanup"))
29                 return ldlm_namespace_cleanup;
30         else if (!strcmp(arg, "ldlm_replay_locks"))
31                 return ldlm_replay_locks;
32         else
33                 return NULL;
34 }
35
36 /* XXX move to proper place */
37 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
38 {
39         switch(nal){
40         case TCPNAL:
41                 /* userspace NAL */
42         case SOCKNAL:
43                 sprintf(str, "%u:%d.%d.%d.%d", (__u32)(nid >> 32),
44                         HIPQUAD(nid));
45                 break;
46         case QSWNAL:
47         case GMNAL:
48         case IBNAL:
49         case TOENAL:
50         case SCIMACNAL:
51                 sprintf(str, "%u:%u", (__u32)(nid >> 32), (__u32)nid);
52                 break;
53         default:
54                 return NULL;
55         }
56         return str;
57 }
58
59 ptl_handle_ni_t         tcpnal_ni;
60
61 struct pingcli_args {
62         ptl_nid_t mynid;
63         ptl_nid_t nid;
64         ptl_pid_t port;
65         int count;
66         int size;
67 };
68
69 struct task_struct *current;
70
71 struct obd_class_user_state ocus;
72
73 /* portals interfaces */
74 ptl_handle_ni_t *
75 kportal_get_ni (int nal)
76 {
77         switch (nal)
78         {
79         case SOCKNAL:
80                 return &tcpnal_ni;
81         default:
82                 return NULL;
83         }
84 }
85
86 inline void
87 kportal_put_ni (int nal)
88 {
89         return;
90 }
91
92 int
93 kportal_nal_cmd(struct portals_cfg *pcfg)
94 {
95 #if 0
96         __u32 nal = pcfg->pcfg_nal;
97         int rc = -EINVAL;
98
99         ENTRY;
100
101         down(&nal_cmd_sem);
102         if (nal > 0 && nal <= NAL_MAX_NR && nal_cmd[nal].nch_handler) {
103                 CDEBUG(D_IOCTL, "calling handler nal: %d, cmd: %d\n", nal, 
104                        pcfg->pcfg_command);
105                 rc = nal_cmd[nal].nch_handler(pcfg, nal_cmd[nal].nch_private);
106         }
107         up(&nal_cmd_sem);
108         RETURN(rc);
109 #else
110         CERROR("empty function!!!\n");
111         return 0;
112 #endif
113 }
114
115 int init_current(int argc, char **argv)
116
117         current = malloc(sizeof(*current));
118         strncpy(current->comm, argv[0], sizeof(current->comm));
119         current->pid = getpid();
120         return 0;
121 }
122
123 ptl_nid_t tcpnal_mynid;
124
125 int init_lib_portals()
126 {
127         int rc;
128
129         PtlInit();
130         rc = PtlNIInit(procbridge_interface, 0, 0, 0, &tcpnal_ni);
131         if (rc != 0) {
132                 CERROR("ksocknal: PtlNIInit failed: error %d\n", rc);
133                 PtlFini();
134                 RETURN (rc);
135         }
136         PtlNIDebug(tcpnal_ni, ~0);
137         return rc;
138 }
139
140 extern int class_handle_ioctl(struct obd_class_user_state *ocus, unsigned int cmd, unsigned long arg);
141
142
143 int lib_ioctl_nalcmd(int dev_id, int opc, void * ptr)
144 {
145         struct portal_ioctl_data *ptldata;
146
147         if (opc == IOC_PORTAL_NAL_CMD) {
148                 ptldata = (struct portal_ioctl_data *) ptr;
149
150                 if (ptldata->ioc_nal_cmd == NAL_CMD_REGISTER_MYNID) {
151                         tcpnal_mynid = ptldata->ioc_nid;
152                         printf("mynid: %u.%u.%u.%u\n",
153                                 (unsigned)(tcpnal_mynid>>24) & 0xFF,
154                                 (unsigned)(tcpnal_mynid>>16) & 0xFF,
155                                 (unsigned)(tcpnal_mynid>>8) & 0xFF,
156                                 (unsigned)(tcpnal_mynid) & 0xFF);
157                 }
158         }
159
160         return (0);
161 }
162
163 int lib_ioctl(int dev_id, int opc, void * ptr)
164 {
165
166         if (dev_id == OBD_DEV_ID) {
167                 class_handle_ioctl(&ocus, opc, (unsigned long)ptr);
168
169                 /* you _may_ need to call obd_ioctl_unpack or some
170                    other verification function if you want to use ioc
171                    directly here */
172 #if 0
173                 printf ("processing ioctl cmd: %x buf len: %d\n", 
174                         opc,  ioc->ioc_len);
175 #endif
176         }
177         return (0);
178 }
179
180 int liblustre_ioctl(int dev_id, int opc, void *ptr)
181 {
182         int   rc = -EINVAL;
183         
184         switch (dev_id) {
185         default:
186                 fprintf(stderr, "Unexpected device id %d\n", dev_id);
187                 abort();
188                 break;
189                 
190         case OBD_DEV_ID:
191                 rc = class_handle_ioctl(&ocus, opc, (unsigned long)ptr);
192                 break;
193         }
194
195         return rc;
196 }
197
198 extern int time_ptlwait1;
199 extern int time_ptlwait2;
200 extern int time_ptlselect;
201 int main(int argc, char **argv) 
202 {
203         char *config_file;
204
205         if (argc > 2) {
206                 printf("Usage: %s [config_file]\n", argv[0]);
207                 return 1;
208         }
209
210         if (argc == 2) {
211                 config_file = argv[1];
212                 argc--;
213                 argv++;
214         } else
215                 config_file = "/tmp/DUMP_FILE";
216
217         srand(time(NULL));
218
219         INIT_LIST_HEAD(&ocus.ocus_conns);
220 #if 1
221         portal_debug = 0;
222         portal_subsystem_debug = 0;
223 #endif
224         parse_dump(config_file, lib_ioctl_nalcmd);
225
226         if (init_current(argc, argv) ||
227             init_obdclass() || init_lib_portals() ||
228             ptlrpc_init() ||
229             ldlm_init() ||
230             mdc_init() ||
231             lov_init() ||
232             osc_init() ||
233             echo_client_init()) {
234                 printf("error\n");
235                 return 1;
236         }
237
238         parse_dump(config_file, lib_ioctl);
239
240         set_ioc_handler(liblustre_ioctl);
241 #if 0   
242         portal_debug = -1;
243         portal_subsystem_debug = -1;
244 #endif
245         return lctl_main(argc, argv);
246 }
247