Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[fs/lustre-release.git] / lustre / portals / 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 ("Unexpected response \n");
95                 return 1;
96         }
97
98         if((i == count) || !count)
99                 wake_up_process (client->tsk);
100         else
101                 printk ("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         client->tsk = current;
115         client->args = args;
116         CDEBUG (D_OTHER, "pingcli_setup args: nid "LPX64",  \
117                         nal %d, size %u, count: %u, timeout: %u\n",
118                         args->ioc_nid, args->ioc_nal, args->ioc_size,
119                         args->ioc_count, args->ioc_timeout);
120
121
122         PORTAL_ALLOC (client->outbuf, STDSIZE + args->ioc_size) ;
123         if (client->outbuf == NULL)
124         {
125                 CERROR ("Unable to allocate out_buf ("LPSZ" bytes)\n", STDSIZE);
126                 pingcli_shutdown (4);
127                 return (NULL);
128         }
129
130         PORTAL_ALLOC (client->inbuf,
131                         (args->ioc_size + STDSIZE) * args->ioc_count);
132         if (client->inbuf == NULL)
133         {
134                 CERROR ("Unable to allocate out_buf ("LPSZ" bytes)\n", STDSIZE);
135                 pingcli_shutdown (4);
136                 return (NULL);
137         }
138
139         /* Aquire and initialize the proper nal for portals. */
140         if ((nip = kportal_get_ni (args->ioc_nal)) == NULL)
141         {
142                 CERROR ("NAL %d not loaded\n", args->ioc_nal);
143                 pingcli_shutdown (4);
144                 return (NULL);
145         }
146
147         /* Based on the initialization aquire our unique portal ID. */
148         if ((rc = PtlGetId (*nip, &client->myid)))
149         {
150                 CERROR ("PtlGetId error %d\n", rc);
151                 pingcli_shutdown (2);
152                 return (NULL);
153         }
154
155         /* Setup the local match entries */
156         client->id_local.nid = PTL_NID_ANY;
157         client->id_local.pid = PTL_PID_ANY;
158
159         /* Setup the remote match entries */
160         client->id_remote.nid = args->ioc_nid;
161         client->id_remote.pid = 0;
162
163         if ((rc = PtlMEAttach (*nip, PTL_PING_CLIENT,
164                    client->id_local, 0, ~0, PTL_RETAIN,
165                    PTL_INS_AFTER, &client->me)))
166         {
167                 CERROR ("PtlMEAttach error %d\n", rc);
168                 pingcli_shutdown (2);
169                 return (NULL);
170         }
171
172         /* Allocate the event queue for this network interface */
173         if ((rc = PtlEQAlloc (*nip, 64, pingcli_callback, &client->eq)))
174         {
175                 CERROR ("PtlEQAlloc error %d\n", rc);
176                 pingcli_shutdown (2);
177                 return (NULL);
178         }
179
180         count = args->ioc_count;
181
182         client->md_in_head.start     = client->inbuf;
183         client->md_in_head.length    = (args->ioc_size + STDSIZE)
184                                                 * count;
185         client->md_in_head.threshold = PTL_MD_THRESH_INF;
186         client->md_in_head.options   = PTL_MD_OP_PUT;
187         client->md_in_head.user_ptr  = NULL;
188         client->md_in_head.eventq    = client->eq;
189         memset (client->inbuf, 0, (args->ioc_size + STDSIZE) * count);
190
191         /* Attach the incoming buffer */
192         if ((rc = PtlMDAttach (client->me, client->md_in_head,
193                               PTL_UNLINK, &client->md_in_head_h))) {
194                 CERROR ("PtlMDAttach error %d\n", rc);
195                 pingcli_shutdown (1);
196                 return (NULL);
197         }
198         /* Setup the outgoing ping header */
199         client->md_out_head.start     = client->outbuf;
200         client->md_out_head.length    = STDSIZE + args->ioc_size;
201         client->md_out_head.threshold = args->ioc_count;
202         client->md_out_head.options   = PTL_MD_OP_PUT;
203         client->md_out_head.user_ptr  = NULL;
204         client->md_out_head.eventq    = PTL_EQ_NONE;
205
206         memcpy (client->outbuf, &ping_head_magic, sizeof(ping_bulk_magic));
207
208         count = 0;
209
210         /* Bind the outgoing ping header */
211         if ((rc=PtlMDBind (*nip, client->md_out_head,
212                                         &client->md_out_head_h))) {
213                 CERROR ("PtlMDBind error %d\n", rc);
214                 pingcli_shutdown (1);
215                 return NULL;
216         }
217         while ((args->ioc_count - count)) {
218                 memcpy (client->outbuf + sizeof(unsigned),
219                        &(count), sizeof(unsigned));
220                  /* Put the ping packet */
221                 do_gettimeofday (&tv1);
222
223                 memcpy(client->outbuf+sizeof(unsigned)+sizeof(unsigned),&tv1,
224                        sizeof(struct timeval));
225
226                 if((rc = PtlPut (client->md_out_head_h, PTL_NOACK_REQ,
227                           client->id_remote, PTL_PING_SERVER, 0, 0, 0, 0))) {
228                          PDEBUG ("PtlPut (header)", rc);
229                          pingcli_shutdown (1);
230                          return NULL;
231                 }
232                 printk ("sent msg no %d", count);
233
234                 set_current_state (TASK_INTERRUPTIBLE);
235                 rc = schedule_timeout (20 * args->ioc_timeout);
236                 if (rc == 0) {
237                         printk ("   ::  timeout .....\n");
238                 } else {
239                         do_gettimeofday (&tv2);
240                         printk("   ::  Reply in %u usec\n",
241                                 (unsigned)((tv2.tv_sec - tv1.tv_sec)
242                                  * 1000000 +  (tv2.tv_usec - tv1.tv_usec)));
243                 }
244                 count++;
245         }
246
247         if (client->outbuf != NULL)
248                 PORTAL_FREE (client->outbuf, STDSIZE + args->ioc_size);
249
250         if (client->inbuf != NULL)
251                 PORTAL_FREE (client->inbuf,
252                                (args->ioc_size + STDSIZE) * args->ioc_count);
253
254         pingcli_shutdown (2);
255
256         /* Success! */
257         return NULL;
258 } /* pingcli_setup() */
259
260
261
262 /* called by the portals_ioctl for ping requests */
263 static int kping_client(struct portal_ioctl_data *args)
264 {
265         PORTAL_ALLOC (client, sizeof(struct pingcli_data));
266         if (client == NULL)
267         {
268                 CERROR ("Unable to allocate client structure\n");
269                 return (0);
270         }
271         memset (client, 0, sizeof(struct pingcli_data));
272         pingcli_start (args);
273
274         return 0;
275 } /* kping_client() */
276
277
278 static int __init pingcli_init(void)
279 {
280         PORTAL_SYMBOL_REGISTER(kping_client);
281         return 0;
282 } /* pingcli_init() */
283
284
285 static void __exit pingcli_cleanup(void)
286 {
287         PORTAL_SYMBOL_UNREGISTER (kping_client);
288 } /* pingcli_cleanup() */
289
290
291 MODULE_AUTHOR("Brian Behlendorf (LLNL)");
292 MODULE_DESCRIPTION("A simple kernel space ping client for portals testing");
293 MODULE_LICENSE("GPL");
294
295 module_init(pingcli_init);
296 module_exit(pingcli_cleanup);
297
298 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
299 EXPORT_SYMBOL (kping_client);
300 #endif