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