Whamcloud - gitweb
LU-3195 tests: Interop 2.3.0<->2.3.63 sanity-quota test_6
[fs/lustre-release.git] / lnet / selftest / ping_test.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
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 int ping_srv_workitems = SFW_TEST_WI_MAX;
48 CFS_MODULE_PARM(ping_srv_workitems, "i", int, 0644, "# PING server workitems");
49
50 typedef struct {
51         spinlock_t      pnd_lock;       /* serialize */
52         int             pnd_counter;    /* sequence counter */
53 } lst_ping_data_t;
54
55 static lst_ping_data_t  lst_ping_data;
56
57 static int
58 ping_client_init(sfw_test_instance_t *tsi)
59 {
60         sfw_session_t *sn = tsi->tsi_batch->bat_session;
61
62         LASSERT(tsi->tsi_is_client);
63         LASSERT(sn != NULL && (sn->sn_features & ~LST_FEATS_MASK) == 0);
64
65         spin_lock_init(&lst_ping_data.pnd_lock);
66         lst_ping_data.pnd_counter = 0;
67
68         return 0;
69 }
70
71 static void
72 ping_client_fini (sfw_test_instance_t *tsi)
73 {
74         sfw_session_t *sn = tsi->tsi_batch->bat_session;
75         int            errors;
76
77         LASSERT (sn != NULL);
78         LASSERT (tsi->tsi_is_client);
79
80         errors = cfs_atomic_read(&sn->sn_ping_errors);
81         if (errors)
82                 CWARN ("%d pings have failed.\n", errors);
83         else
84                 CDEBUG (D_NET, "Ping test finished OK.\n");
85 }
86
87 static int
88 ping_client_prep_rpc(sfw_test_unit_t *tsu,
89                      lnet_process_id_t dest, srpc_client_rpc_t **rpc)
90 {
91         srpc_ping_reqst_t   *req;
92         sfw_test_instance_t *tsi = tsu->tsu_instance;
93         sfw_session_t       *sn  = tsi->tsi_batch->bat_session;
94         struct timeval       tv;
95         int                  rc;
96
97         LASSERT(sn != NULL);
98         LASSERT((sn->sn_features & ~LST_FEATS_MASK) == 0);
99
100         rc = sfw_create_test_rpc(tsu, dest, sn->sn_features, 0, 0, rpc);
101         if (rc != 0)
102                 return rc;
103
104         req = &(*rpc)->crpc_reqstmsg.msg_body.ping_reqst;
105
106         req->pnr_magic = LST_PING_TEST_MAGIC;
107
108         spin_lock(&lst_ping_data.pnd_lock);
109         req->pnr_seq = lst_ping_data.pnd_counter++;
110         spin_unlock(&lst_ping_data.pnd_lock);
111
112         cfs_fs_timeval(&tv);
113         req->pnr_time_sec  = tv.tv_sec;
114         req->pnr_time_usec = tv.tv_usec;
115
116         return rc;
117 }
118
119 static void
120 ping_client_done_rpc (sfw_test_unit_t *tsu, srpc_client_rpc_t *rpc)
121 {
122         sfw_test_instance_t *tsi = tsu->tsu_instance;
123         sfw_session_t       *sn = tsi->tsi_batch->bat_session;
124         srpc_ping_reqst_t   *reqst = &rpc->crpc_reqstmsg.msg_body.ping_reqst;
125         srpc_ping_reply_t   *reply = &rpc->crpc_replymsg.msg_body.ping_reply;
126         struct timeval       tv;
127
128         LASSERT (sn != NULL);
129
130         if (rpc->crpc_status != 0) {
131                 if (!tsi->tsi_stopping) /* rpc could have been aborted */
132                         cfs_atomic_inc(&sn->sn_ping_errors);
133                 CERROR ("Unable to ping %s (%d): %d\n",
134                         libcfs_id2str(rpc->crpc_dest),
135                         reqst->pnr_seq, rpc->crpc_status);
136                 return;
137         }
138
139         if (rpc->crpc_replymsg.msg_magic != SRPC_MSG_MAGIC) {
140                 __swab32s(&reply->pnr_seq);
141                 __swab32s(&reply->pnr_magic);
142                 __swab32s(&reply->pnr_status);
143         }
144
145         if (reply->pnr_magic != LST_PING_TEST_MAGIC) {
146                 rpc->crpc_status = -EBADMSG;
147                 cfs_atomic_inc(&sn->sn_ping_errors);
148                 CERROR ("Bad magic %u from %s, %u expected.\n",
149                         reply->pnr_magic, libcfs_id2str(rpc->crpc_dest),
150                         LST_PING_TEST_MAGIC);
151                 return;
152         }
153
154         if (reply->pnr_seq != reqst->pnr_seq) {
155                 rpc->crpc_status = -EBADMSG;
156                 cfs_atomic_inc(&sn->sn_ping_errors);
157                 CERROR ("Bad seq %u from %s, %u expected.\n",
158                         reply->pnr_seq, libcfs_id2str(rpc->crpc_dest),
159                         reqst->pnr_seq);
160                 return;
161         }
162
163         cfs_fs_timeval(&tv);
164         CDEBUG (D_NET, "%d reply in %u usec\n", reply->pnr_seq,
165                 (unsigned)((tv.tv_sec - (unsigned)reqst->pnr_time_sec) * 1000000
166                            + (tv.tv_usec - reqst->pnr_time_usec)));
167         return;
168 }
169
170 static int
171 ping_server_handle(struct srpc_server_rpc *rpc)
172 {
173         struct srpc_service     *sv  = rpc->srpc_scd->scd_svc;
174         srpc_msg_t        *reqstmsg = &rpc->srpc_reqstbuf->buf_msg;
175         srpc_msg_t        *replymsg = &rpc->srpc_replymsg;
176         srpc_ping_reqst_t *req = &reqstmsg->msg_body.ping_reqst;
177         srpc_ping_reply_t *rep = &rpc->srpc_replymsg.msg_body.ping_reply;
178
179         LASSERT (sv->sv_id == SRPC_SERVICE_PING);
180
181         if (reqstmsg->msg_magic != SRPC_MSG_MAGIC) {
182                 LASSERT (reqstmsg->msg_magic == __swab32(SRPC_MSG_MAGIC));
183
184                 __swab32s(&req->pnr_seq);
185                 __swab32s(&req->pnr_magic);
186                 __swab64s(&req->pnr_time_sec);
187                 __swab64s(&req->pnr_time_usec);
188         }
189         LASSERT (reqstmsg->msg_type == srpc_service2request(sv->sv_id));
190
191         if (req->pnr_magic != LST_PING_TEST_MAGIC) {
192                 CERROR ("Unexpect magic %08x from %s\n",
193                         req->pnr_magic, libcfs_id2str(rpc->srpc_peer));
194                 return -EINVAL;
195         }
196
197         rep->pnr_seq   = req->pnr_seq;
198         rep->pnr_magic = LST_PING_TEST_MAGIC;
199
200         if ((reqstmsg->msg_ses_feats & ~LST_FEATS_MASK) != 0) {
201                 replymsg->msg_ses_feats = LST_FEATS_MASK;
202                 rep->pnr_status = EPROTO;
203                 return 0;
204         }
205
206         replymsg->msg_ses_feats = reqstmsg->msg_ses_feats;
207
208         CDEBUG(D_NET, "Get ping %d from %s\n",
209                req->pnr_seq, libcfs_id2str(rpc->srpc_peer));
210         return 0;
211 }
212
213 sfw_test_client_ops_t ping_test_client;
214 void ping_init_test_client(void)
215 {
216         ping_test_client.tso_init     = ping_client_init;
217         ping_test_client.tso_fini     = ping_client_fini;
218         ping_test_client.tso_prep_rpc = ping_client_prep_rpc;
219         ping_test_client.tso_done_rpc = ping_client_done_rpc;
220 }
221
222 srpc_service_t ping_test_service;
223 void ping_init_test_service(void)
224 {
225         ping_test_service.sv_id       = SRPC_SERVICE_PING;
226         ping_test_service.sv_name     = "ping_test";
227         ping_test_service.sv_handler  = ping_server_handle;
228         ping_test_service.sv_wi_total = ping_srv_workitems;
229 }