Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lnet / tests / ping_cli.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002, Lawrence Livermore National Labs (LLNL)
5  * Author: Brian Behlendorf <behlendorf1@llnl.gov>
6  *         Kedar Sovani (kedar@calsoftinc.com)
7  *         Amey Inamdar (amey@calsoftinc.com)
8  *
9  * This file is part of Portals, http://www.sf.net/projects/lustre/
10  *
11  * Portals is free software; you can redistribute it and/or
12  * modify it under the terms of version 2 of the GNU General Public
13  * License as published by the Free Software Foundation.
14  *
15  * Portals is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Portals; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26 #define DEBUG_SUBSYSTEM S_PINGER
27
28 #include <linux/kp30.h>
29 #include <portals/p30.h>
30 #include <linux/module.h>
31 #include <linux/proc_fs.h>
32 #include <linux/init.h>
33 #include <linux/poll.h>
34 #include "ping.h"
35 /* int portal_debug = D_PING_CLI;  */
36
37
38 #define STDSIZE (sizeof(int) + sizeof(int) + sizeof(struct timeval))
39
40 #define MAX_TIME 100000
41
42 /* This should be enclosed in a structure */
43
44 static struct pingcli_data *client = NULL;
45
46 static int count = 0;
47
48 static void
49 pingcli_shutdown(int err)
50 {
51         int rc;
52
53         /* Yes, we are intentionally allowing us to fall through each
54          * case in to the next.  This allows us to pass an error
55          * code to just clean up the right stuff.
56          */
57         switch (err) {
58                 case 1:
59                         /* Unlink any memory descriptors we may have used */
60                         if ((rc = PtlMDUnlink (client->md_out_head_h)))
61                                 PDEBUG ("PtlMDUnlink", rc);
62                 case 2:
63                         if ((rc = PtlMDUnlink (client->md_in_head_h)))
64                                 PDEBUG ("PtlMDUnlink", rc);
65
66                         /* Free the event queue */
67                         if ((rc = PtlEQFree (client->eq)))
68                                 PDEBUG ("PtlEQFree", rc);
69
70                         if ((rc = PtlMEUnlink (client->me)))
71                                 PDEBUG ("PtlMEUnlink", rc);
72                 case 3:
73                         kportal_put_ni (client->args->ioc_nal);
74
75                 case 4:
76                         /* Free our buffers */
77
78                         if (client != NULL)
79                                 PORTAL_FREE (client,
80                                                 sizeof(struct pingcli_data));
81         }
82
83
84         CDEBUG (D_OTHER, "ping client released resources\n");
85 } /* pingcli_shutdown() */
86
87 static int pingcli_callback(ptl_event_t *ev)
88 {
89         int i, magic;
90         i = *(int *)(ev->mem_desc.start + ev->offset + sizeof(unsigned));
91         magic = *(int *)(ev->mem_desc.start + ev->offset);
92
93         if(magic != 0xcafebabe) {
94                 printk ("LustreError: Unexpected response \n");
95                 return 1;
96         }
97
98         if((i == count) || !count)
99                 wake_up_process (client->tsk);
100         else
101                 printk ("LustreError: Received response after timeout for %d\n",i);
102         return 1;
103 }
104
105
106 static struct pingcli_data *
107 pingcli_start(struct portal_ioctl_data *args)
108 {
109         ptl_handle_ni_t *nip;
110         unsigned ping_head_magic = PING_HEADER_MAGIC;
111         unsigned ping_bulk_magic = PING_BULK_MAGIC;
112         int rc;
113         struct timeval tv1, tv2;
114         char str[PTL_NALFMT_SIZE];
115         
116         client->tsk = current;
117         client->args = args;
118         CDEBUG (D_OTHER, "pingcli_setup args: nid "LPX64" (%s),  \
119                         nal %d, size %u, count: %u, timeout: %u\n",
120                         args->ioc_nid,
121                         portals_nid2str(args->ioc_nal, args->ioc_nid, str),
122                         args->ioc_nal, args->ioc_size,
123                         args->ioc_count, args->ioc_timeout);
124
125
126         PORTAL_ALLOC (client->outbuf, STDSIZE + args->ioc_size) ;
127         if (client->outbuf == NULL)
128         {
129                 CERROR ("Unable to allocate out_buf ("LPSZ" bytes)\n", STDSIZE);
130                 pingcli_shutdown (4);
131                 return (NULL);
132         }
133
134         PORTAL_ALLOC (client->inbuf,
135                         (args->ioc_size + STDSIZE) * args->ioc_count);
136         if (client->inbuf == NULL)
137         {
138                 CERROR ("Unable to allocate out_buf ("LPSZ" bytes)\n", STDSIZE);
139                 pingcli_shutdown (4);
140                 return (NULL);
141         }
142
143         /* Aquire and initialize the proper nal for portals. */
144         if ((nip = kportal_get_ni (args->ioc_nal)) == NULL)
145         {
146                 CERROR ("NAL %d not loaded\n", args->ioc_nal);
147                 pingcli_shutdown (4);
148                 return (NULL);
149         }
150
151         /* Based on the initialization aquire our unique portal ID. */
152         if ((rc = PtlGetId (*nip, &client->myid)))
153         {
154                 CERROR ("PtlGetId error %d\n", rc);
155                 pingcli_shutdown (2);
156                 return (NULL);
157         }
158
159         /* Setup the local match entries */
160         client->id_local.nid = PTL_NID_ANY;
161         client->id_local.pid = PTL_PID_ANY;
162
163         /* Setup the remote match entries */
164         client->id_remote.nid = args->ioc_nid;
165         client->id_remote.pid = 0;
166
167         if ((rc = PtlMEAttach (*nip, PTL_PING_CLIENT,
168                    client->id_local, 0, ~0, PTL_RETAIN,
169                    PTL_INS_AFTER, &client->me)))
170         {
171                 CERROR ("PtlMEAttach error %d\n", rc);
172                 pingcli_shutdown (2);
173                 return (NULL);
174         }
175
176         /* Allocate the event queue for this network interface */
177         if ((rc = PtlEQAlloc (*nip, 64, pingcli_callback, &client->eq)))
178         {
179                 CERROR ("PtlEQAlloc error %d\n", rc);
180                 pingcli_shutdown (2);
181                 return (NULL);
182         }
183
184         count = args->ioc_count;
185
186         client->md_in_head.start     = client->inbuf;
187         client->md_in_head.length    = (args->ioc_size + STDSIZE)
188                                                 * count;
189         client->md_in_head.threshold = PTL_MD_THRESH_INF;
190         client->md_in_head.options   = PTL_MD_OP_PUT;
191         client->md_in_head.user_ptr  = NULL;
192         client->md_in_head.eventq    = client->eq;
193         memset (client->inbuf, 0, (args->ioc_size + STDSIZE) * count);
194
195         /* Attach the incoming buffer */
196         if ((rc = PtlMDAttach (client->me, client->md_in_head,
197                               PTL_UNLINK, &client->md_in_head_h))) {
198                 CERROR ("PtlMDAttach error %d\n", rc);
199                 pingcli_shutdown (1);
200                 return (NULL);
201         }
202         /* Setup the outgoing ping header */
203         client->md_out_head.start     = client->outbuf;
204         client->md_out_head.length    = STDSIZE + args->ioc_size;
205         client->md_out_head.threshold = args->ioc_count;
206         client->md_out_head.options   = PTL_MD_OP_PUT;
207         client->md_out_head.user_ptr  = NULL;
208         client->md_out_head.eventq    = PTL_EQ_NONE;
209
210         memcpy (client->outbuf, &ping_head_magic, sizeof(ping_bulk_magic));
211
212         count = 0;
213
214         /* Bind the outgoing ping header */
215         if ((rc=PtlMDBind (*nip, client->md_out_head,
216                                         &client->md_out_head_h))) {
217                 CERROR ("PtlMDBind error %d\n", rc);
218                 pingcli_shutdown (1);
219                 return NULL;
220         }
221         while ((args->ioc_count - count)) {
222                 memcpy (client->outbuf + sizeof(unsigned),
223                        &(count), sizeof(unsigned));
224                  /* Put the ping packet */
225                 do_gettimeofday (&tv1);
226
227                 memcpy(client->outbuf+sizeof(unsigned)+sizeof(unsigned),&tv1,
228                        sizeof(struct timeval));
229
230                 if((rc = PtlPut (client->md_out_head_h, PTL_NOACK_REQ,
231                           client->id_remote, PTL_PING_SERVER, 0, 0, 0, 0))) {
232                          PDEBUG ("PtlPut (header)", rc);
233                          pingcli_shutdown (1);
234                          return NULL;
235                 }
236                 printk ("Lustre: sent msg no %d", count);
237
238                 set_current_state (TASK_INTERRUPTIBLE);
239                 rc = schedule_timeout (20 * args->ioc_timeout);
240                 if (rc == 0) {
241                         printk ("LustreError:   ::  timeout .....\n");
242                 } else {
243                         do_gettimeofday (&tv2);
244                         printk("Lustre:   ::  Reply in %u usec\n",
245                                 (unsigned)((tv2.tv_sec - tv1.tv_sec)
246                                  * 1000000 +  (tv2.tv_usec - tv1.tv_usec)));
247                 }
248                 count++;
249         }
250
251         if (client->outbuf != NULL)
252                 PORTAL_FREE (client->outbuf, STDSIZE + args->ioc_size);
253
254         if (client->inbuf != NULL)
255                 PORTAL_FREE (client->inbuf,
256                                (args->ioc_size + STDSIZE) * args->ioc_count);
257
258         pingcli_shutdown (2);
259
260         /* Success! */
261         return NULL;
262 } /* pingcli_setup() */
263
264
265
266 /* called by the portals_ioctl for ping requests */
267 int kping_client(struct portal_ioctl_data *args)
268 {
269         PORTAL_ALLOC (client, sizeof(struct pingcli_data));
270         if (client == NULL)
271         {
272                 CERROR ("Unable to allocate client structure\n");
273                 return (0);
274         }
275         memset (client, 0, sizeof(struct pingcli_data));
276         pingcli_start (args);
277
278         return 0;
279 } /* kping_client() */
280
281
282 static int __init pingcli_init(void)
283 {
284         PORTAL_SYMBOL_REGISTER(kping_client);
285         return 0;
286 } /* pingcli_init() */
287
288
289 static void /*__exit*/ pingcli_cleanup(void)
290 {
291         PORTAL_SYMBOL_UNREGISTER (kping_client);
292 } /* pingcli_cleanup() */
293
294
295 MODULE_AUTHOR("Brian Behlendorf (LLNL)");
296 MODULE_DESCRIPTION("A simple kernel space ping client for portals testing");
297 MODULE_LICENSE("GPL");
298
299 module_init(pingcli_init);
300 module_exit(pingcli_cleanup);
301
302 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
303 EXPORT_SYMBOL (kping_client);
304 #endif