Whamcloud - gitweb
b=17167 libcfs: ensure all libcfs exported symbols to have cfs_ prefix
[fs/lustre-release.git] / lnet / selftest / ping_test.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/selftest/conctl.c
37  *
38  * Test client & Server
39  *
40  * Author: Liang Zhen <liangzhen@clusterfs.com>
41  */
42
43 #include "selftest.h"
44
45 #define LST_PING_TEST_MAGIC     0xbabeface
46
47 typedef struct {
48         cfs_spinlock_t  pnd_lock;       /* serialize */
49         int             pnd_counter;    /* sequence counter */
50 } lst_ping_data_t;
51
52 static lst_ping_data_t  lst_ping_data;
53
54 static int
55 ping_client_init(sfw_test_instance_t *tsi)
56 {
57         LASSERT (tsi->tsi_is_client);
58
59         cfs_spin_lock_init(&lst_ping_data.pnd_lock);
60         lst_ping_data.pnd_counter = 0;
61
62         return 0;
63 }
64
65 static void
66 ping_client_fini (sfw_test_instance_t *tsi)
67 {
68         sfw_session_t *sn = tsi->tsi_batch->bat_session;
69         int            errors;
70
71         LASSERT (sn != NULL);
72         LASSERT (tsi->tsi_is_client);
73
74         errors = cfs_atomic_read(&sn->sn_ping_errors);
75         if (errors)
76                 CWARN ("%d pings have failed.\n", errors);
77         else
78                 CDEBUG (D_NET, "Ping test finished OK.\n");
79 }
80
81 static int
82 ping_client_prep_rpc(sfw_test_unit_t *tsu,
83                      lnet_process_id_t dest, srpc_client_rpc_t **rpc)
84 {
85         srpc_ping_reqst_t *req;
86         struct timeval     tv;
87         int                rc;
88
89         rc = sfw_create_test_rpc(tsu, dest, 0, 0, rpc);
90         if (rc != 0)
91                 return rc;
92
93         req = &(*rpc)->crpc_reqstmsg.msg_body.ping_reqst;
94
95         req->pnr_magic = LST_PING_TEST_MAGIC;
96
97         cfs_spin_lock(&lst_ping_data.pnd_lock);
98         req->pnr_seq = lst_ping_data.pnd_counter ++;
99         cfs_spin_unlock(&lst_ping_data.pnd_lock);
100
101         cfs_fs_timeval(&tv);
102         req->pnr_time_sec  = tv.tv_sec;
103         req->pnr_time_usec = tv.tv_usec;
104
105         return rc;
106 }
107
108 static void
109 ping_client_done_rpc (sfw_test_unit_t *tsu, srpc_client_rpc_t *rpc)
110 {
111         sfw_test_instance_t *tsi = tsu->tsu_instance;
112         sfw_session_t       *sn = tsi->tsi_batch->bat_session;
113         srpc_ping_reqst_t   *reqst = &rpc->crpc_reqstmsg.msg_body.ping_reqst;
114         srpc_ping_reply_t   *reply = &rpc->crpc_replymsg.msg_body.ping_reply;
115         struct timeval       tv;
116
117         LASSERT (sn != NULL);
118
119         if (rpc->crpc_status != 0) {
120                 if (!tsi->tsi_stopping) /* rpc could have been aborted */
121                         cfs_atomic_inc(&sn->sn_ping_errors);
122                 CERROR ("Unable to ping %s (%d): %d\n",
123                         libcfs_id2str(rpc->crpc_dest),
124                         reqst->pnr_seq, rpc->crpc_status);
125                 return;
126         } 
127
128         if (rpc->crpc_replymsg.msg_magic != SRPC_MSG_MAGIC) {
129                 __swab32s(&reply->pnr_seq);
130                 __swab32s(&reply->pnr_magic);
131                 __swab32s(&reply->pnr_status);
132         }
133         
134         if (reply->pnr_magic != LST_PING_TEST_MAGIC) {
135                 rpc->crpc_status = -EBADMSG;
136                 cfs_atomic_inc(&sn->sn_ping_errors);
137                 CERROR ("Bad magic %u from %s, %u expected.\n",
138                         reply->pnr_magic, libcfs_id2str(rpc->crpc_dest),
139                         LST_PING_TEST_MAGIC);
140                 return;
141         } 
142         
143         if (reply->pnr_seq != reqst->pnr_seq) {
144                 rpc->crpc_status = -EBADMSG;
145                 cfs_atomic_inc(&sn->sn_ping_errors);
146                 CERROR ("Bad seq %u from %s, %u expected.\n",
147                         reply->pnr_seq, libcfs_id2str(rpc->crpc_dest),
148                         reqst->pnr_seq);
149                 return;
150         }
151
152         cfs_fs_timeval(&tv);
153         CDEBUG (D_NET, "%d reply in %u usec\n", reply->pnr_seq,
154                 (unsigned)((tv.tv_sec - (unsigned)reqst->pnr_time_sec) * 1000000
155                            + (tv.tv_usec - reqst->pnr_time_usec)));
156         return;
157 }
158
159 static int
160 ping_server_handle (srpc_server_rpc_t *rpc)
161 {
162         srpc_service_t    *sv  = rpc->srpc_service;
163         srpc_msg_t        *reqstmsg = &rpc->srpc_reqstbuf->buf_msg;
164         srpc_ping_reqst_t *req = &reqstmsg->msg_body.ping_reqst;
165         srpc_ping_reply_t *rep = &rpc->srpc_replymsg.msg_body.ping_reply;
166
167         LASSERT (sv->sv_id == SRPC_SERVICE_PING);
168
169         if (reqstmsg->msg_magic != SRPC_MSG_MAGIC) {
170                 LASSERT (reqstmsg->msg_magic == __swab32(SRPC_MSG_MAGIC));
171
172                 __swab32s(&reqstmsg->msg_type);
173                 __swab32s(&req->pnr_seq);
174                 __swab32s(&req->pnr_magic);
175                 __swab64s(&req->pnr_time_sec);
176                 __swab64s(&req->pnr_time_usec);
177         }
178         LASSERT (reqstmsg->msg_type == srpc_service2request(sv->sv_id));
179
180         if (req->pnr_magic != LST_PING_TEST_MAGIC) {
181                 CERROR ("Unexpect magic %08x from %s\n",
182                         req->pnr_magic, libcfs_id2str(rpc->srpc_peer));
183                 return -EINVAL;
184         }
185
186         rep->pnr_seq   = req->pnr_seq;
187         rep->pnr_magic = LST_PING_TEST_MAGIC;
188
189         CDEBUG (D_NET, "Get ping %d from %s\n",
190                 req->pnr_seq, libcfs_id2str(rpc->srpc_peer));
191         return 0;
192 }
193
194 sfw_test_client_ops_t ping_test_client;
195 void ping_init_test_client(void)
196 {
197         ping_test_client.tso_init     = ping_client_init;
198         ping_test_client.tso_fini     = ping_client_fini;
199         ping_test_client.tso_prep_rpc = ping_client_prep_rpc;
200         ping_test_client.tso_done_rpc = ping_client_done_rpc;
201 }
202
203 srpc_service_t ping_test_service;
204 void ping_init_test_service(void)
205 {
206         ping_test_service.sv_id       = SRPC_SERVICE_PING;
207         ping_test_service.sv_name     = "ping_test";
208         ping_test_service.sv_handler  = ping_server_handle;
209 }