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