1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
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)
9 * This file is part of Portals, http://www.sf.net/projects/lustre/
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.
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.
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.
26 #define DEBUG_SUBSYSTEM S_PINGER
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>
35 /* int portal_debug = D_PING_CLI; */
38 #define STDSIZE (sizeof(int) + sizeof(int) + sizeof(struct timeval))
40 #define MAX_TIME 100000
42 /* This should be enclosed in a structure */
44 static struct pingcli_data *client = NULL;
49 pingcli_shutdown(int err)
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.
59 /* Unlink any memory descriptors we may have used */
60 if ((rc = PtlMDUnlink (client->md_out_head_h)))
61 PDEBUG ("PtlMDUnlink", rc);
63 if ((rc = PtlMDUnlink (client->md_in_head_h)))
64 PDEBUG ("PtlMDUnlink", rc);
66 /* Free the event queue */
67 if ((rc = PtlEQFree (client->eq)))
68 PDEBUG ("PtlEQFree", rc);
70 if ((rc = PtlMEUnlink (client->me)))
71 PDEBUG ("PtlMEUnlink", rc);
73 kportal_put_ni (client->args->ioc_nal);
76 /* Free our buffers */
80 sizeof(struct pingcli_data));
84 CDEBUG (D_OTHER, "ping client released resources\n");
85 } /* pingcli_shutdown() */
87 static int pingcli_callback(ptl_event_t *ev)
90 i = *(int *)(ev->mem_desc.start + ev->offset + sizeof(unsigned));
91 magic = *(int *)(ev->mem_desc.start + ev->offset);
93 if(magic != 0xcafebabe) {
94 printk ("LustreError: Unexpected response \n");
98 if((i == count) || !count)
99 wake_up_process (client->tsk);
101 printk ("LustreError: Received response after timeout for %d\n",i);
106 static struct pingcli_data *
107 pingcli_start(struct portal_ioctl_data *args)
109 ptl_handle_ni_t *nip;
110 unsigned ping_head_magic = PING_HEADER_MAGIC;
111 unsigned ping_bulk_magic = PING_BULK_MAGIC;
113 struct timeval tv1, tv2;
114 char str[PTL_NALFMT_SIZE];
116 client->tsk = current;
118 CDEBUG (D_OTHER, "pingcli_setup args: nid "LPX64" (%s), \
119 nal %d, size %u, count: %u, timeout: %u\n",
121 portals_nid2str(args->ioc_nal, args->ioc_nid, str),
122 args->ioc_nal, args->ioc_size,
123 args->ioc_count, args->ioc_timeout);
126 PORTAL_ALLOC (client->outbuf, STDSIZE + args->ioc_size) ;
127 if (client->outbuf == NULL)
129 CERROR ("Unable to allocate out_buf ("LPSZ" bytes)\n", STDSIZE);
130 pingcli_shutdown (4);
134 PORTAL_ALLOC (client->inbuf,
135 (args->ioc_size + STDSIZE) * args->ioc_count);
136 if (client->inbuf == NULL)
138 CERROR ("Unable to allocate out_buf ("LPSZ" bytes)\n", STDSIZE);
139 pingcli_shutdown (4);
143 /* Aquire and initialize the proper nal for portals. */
144 if ((nip = kportal_get_ni (args->ioc_nal)) == NULL)
146 CERROR ("NAL %d not loaded\n", args->ioc_nal);
147 pingcli_shutdown (4);
151 /* Based on the initialization aquire our unique portal ID. */
152 if ((rc = PtlGetId (*nip, &client->myid)))
154 CERROR ("PtlGetId error %d\n", rc);
155 pingcli_shutdown (2);
159 /* Setup the local match entries */
160 client->id_local.nid = PTL_NID_ANY;
161 client->id_local.pid = PTL_PID_ANY;
163 /* Setup the remote match entries */
164 client->id_remote.nid = args->ioc_nid;
165 client->id_remote.pid = 0;
167 if ((rc = PtlMEAttach (*nip, PTL_PING_CLIENT,
168 client->id_local, 0, ~0, PTL_RETAIN,
169 PTL_INS_AFTER, &client->me)))
171 CERROR ("PtlMEAttach error %d\n", rc);
172 pingcli_shutdown (2);
176 /* Allocate the event queue for this network interface */
177 if ((rc = PtlEQAlloc (*nip, 64, pingcli_callback, &client->eq)))
179 CERROR ("PtlEQAlloc error %d\n", rc);
180 pingcli_shutdown (2);
184 count = args->ioc_count;
186 client->md_in_head.start = client->inbuf;
187 client->md_in_head.length = (args->ioc_size + STDSIZE)
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);
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);
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;
210 memcpy (client->outbuf, &ping_head_magic, sizeof(ping_bulk_magic));
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);
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);
227 memcpy(client->outbuf+sizeof(unsigned)+sizeof(unsigned),&tv1,
228 sizeof(struct timeval));
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);
236 printk ("Lustre: sent msg no %d", count);
238 set_current_state (TASK_INTERRUPTIBLE);
239 rc = schedule_timeout (20 * args->ioc_timeout);
241 printk ("LustreError: :: timeout .....\n");
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)));
251 if (client->outbuf != NULL)
252 PORTAL_FREE (client->outbuf, STDSIZE + args->ioc_size);
254 if (client->inbuf != NULL)
255 PORTAL_FREE (client->inbuf,
256 (args->ioc_size + STDSIZE) * args->ioc_count);
258 pingcli_shutdown (2);
262 } /* pingcli_setup() */
266 /* called by the portals_ioctl for ping requests */
267 int kping_client(struct portal_ioctl_data *args)
269 PORTAL_ALLOC (client, sizeof(struct pingcli_data));
272 CERROR ("Unable to allocate client structure\n");
275 memset (client, 0, sizeof(struct pingcli_data));
276 pingcli_start (args);
279 } /* kping_client() */
282 static int __init pingcli_init(void)
284 PORTAL_SYMBOL_REGISTER(kping_client);
286 } /* pingcli_init() */
289 static void /*__exit*/ pingcli_cleanup(void)
291 PORTAL_SYMBOL_UNREGISTER (kping_client);
292 } /* pingcli_cleanup() */
295 MODULE_AUTHOR("Brian Behlendorf (LLNL)");
296 MODULE_DESCRIPTION("A simple kernel space ping client for portals testing");
297 MODULE_LICENSE("GPL");
299 module_init(pingcli_init);
300 module_exit(pingcli_cleanup);
302 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
303 EXPORT_SYMBOL (kping_client);