Whamcloud - gitweb
LU-4423 libcfs: remove IS_PO2 and __is_po2
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lnet/selftest/conctl.c
33  *
34  * Test client & Server
35  *
36  * Author: Liang Zhen <liangzhen@clusterfs.com>
37  */
38
39 #include "selftest.h"
40
41 #define LST_PING_TEST_MAGIC     0xbabeface
42
43 static int ping_srv_workitems = SFW_TEST_WI_MAX;
44 module_param(ping_srv_workitems, int, 0644);
45 MODULE_PARM_DESC(ping_srv_workitems, "# PING server workitems");
46
47 typedef struct {
48         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         sfw_session_t *sn = tsi->tsi_batch->bat_session;
58
59         LASSERT(tsi->tsi_is_client);
60         LASSERT(sn != NULL && (sn->sn_features & ~LST_FEATS_MASK) == 0);
61
62         spin_lock_init(&lst_ping_data.pnd_lock);
63         lst_ping_data.pnd_counter = 0;
64
65         return 0;
66 }
67
68 static void
69 ping_client_fini (sfw_test_instance_t *tsi)
70 {
71         sfw_session_t *sn = tsi->tsi_batch->bat_session;
72         int            errors;
73
74         LASSERT (sn != NULL);
75         LASSERT (tsi->tsi_is_client);
76
77         errors = atomic_read(&sn->sn_ping_errors);
78         if (errors)
79                 CWARN ("%d pings have failed.\n", errors);
80         else
81                 CDEBUG (D_NET, "Ping test finished OK.\n");
82 }
83
84 static int
85 ping_client_prep_rpc(sfw_test_unit_t *tsu,
86                      lnet_process_id_t dest, srpc_client_rpc_t **rpc)
87 {
88         srpc_ping_reqst_t   *req;
89         sfw_test_instance_t *tsi = tsu->tsu_instance;
90         sfw_session_t       *sn  = tsi->tsi_batch->bat_session;
91         struct timespec64 ts;
92         int                  rc;
93
94         LASSERT(sn != NULL);
95         LASSERT((sn->sn_features & ~LST_FEATS_MASK) == 0);
96
97         rc = sfw_create_test_rpc(tsu, dest, sn->sn_features, 0, 0, rpc);
98         if (rc != 0)
99                 return rc;
100
101         req = &(*rpc)->crpc_reqstmsg.msg_body.ping_reqst;
102
103         req->pnr_magic = LST_PING_TEST_MAGIC;
104
105         spin_lock(&lst_ping_data.pnd_lock);
106         req->pnr_seq = lst_ping_data.pnd_counter++;
107         spin_unlock(&lst_ping_data.pnd_lock);
108
109         ktime_get_real_ts64(&ts);
110         req->pnr_time_sec  = ts.tv_sec;
111         req->pnr_time_nsec = ts.tv_nsec;
112
113         return rc;
114 }
115
116 static void
117 ping_client_done_rpc (sfw_test_unit_t *tsu, srpc_client_rpc_t *rpc)
118 {
119         sfw_test_instance_t *tsi = tsu->tsu_instance;
120         sfw_session_t *sn = tsi->tsi_batch->bat_session;
121         srpc_ping_reqst_t *reqst = &rpc->crpc_reqstmsg.msg_body.ping_reqst;
122         srpc_ping_reply_t *reply = &rpc->crpc_replymsg.msg_body.ping_reply;
123         struct timespec64 ts;
124
125         LASSERT(sn != NULL);
126
127         if (rpc->crpc_status != 0) {
128                 if (!tsi->tsi_stopping) /* rpc could have been aborted */
129                         atomic_inc(&sn->sn_ping_errors);
130                 CERROR ("Unable to ping %s (%d): %d\n",
131                         libcfs_id2str(rpc->crpc_dest),
132                         reqst->pnr_seq, rpc->crpc_status);
133                 return;
134         }
135
136         if (rpc->crpc_replymsg.msg_magic != SRPC_MSG_MAGIC) {
137                 __swab32s(&reply->pnr_seq);
138                 __swab32s(&reply->pnr_magic);
139                 __swab32s(&reply->pnr_status);
140         }
141
142         if (reply->pnr_magic != LST_PING_TEST_MAGIC) {
143                 rpc->crpc_status = -EBADMSG;
144                 atomic_inc(&sn->sn_ping_errors);
145                 CERROR ("Bad magic %u from %s, %u expected.\n",
146                         reply->pnr_magic, libcfs_id2str(rpc->crpc_dest),
147                         LST_PING_TEST_MAGIC);
148                 return;
149         }
150
151         if (reply->pnr_seq != reqst->pnr_seq) {
152                 rpc->crpc_status = -EBADMSG;
153                 atomic_inc(&sn->sn_ping_errors);
154                 CERROR ("Bad seq %u from %s, %u expected.\n",
155                         reply->pnr_seq, libcfs_id2str(rpc->crpc_dest),
156                         reqst->pnr_seq);
157                 return;
158         }
159
160         ktime_get_real_ts64(&ts);
161         CDEBUG(D_NET, "%d reply in %llu nsec\n", reply->pnr_seq,
162                (u64)((ts.tv_sec - reqst->pnr_time_sec) * NSEC_PER_SEC +
163                     (ts.tv_nsec - reqst->pnr_time_nsec)));
164         return;
165 }
166
167 static int
168 ping_server_handle(struct srpc_server_rpc *rpc)
169 {
170         struct srpc_service     *sv  = rpc->srpc_scd->scd_svc;
171         srpc_msg_t        *reqstmsg = &rpc->srpc_reqstbuf->buf_msg;
172         srpc_msg_t        *replymsg = &rpc->srpc_replymsg;
173         srpc_ping_reqst_t *req = &reqstmsg->msg_body.ping_reqst;
174         srpc_ping_reply_t *rep = &rpc->srpc_replymsg.msg_body.ping_reply;
175
176         LASSERT (sv->sv_id == SRPC_SERVICE_PING);
177
178         if (reqstmsg->msg_magic != SRPC_MSG_MAGIC) {
179                 LASSERT (reqstmsg->msg_magic == __swab32(SRPC_MSG_MAGIC));
180
181                 __swab32s(&req->pnr_seq);
182                 __swab32s(&req->pnr_magic);
183                 __swab64s(&req->pnr_time_sec);
184                 __swab64s(&req->pnr_time_nsec);
185         }
186         LASSERT (reqstmsg->msg_type == srpc_service2request(sv->sv_id));
187
188         if (req->pnr_magic != LST_PING_TEST_MAGIC) {
189                 CERROR ("Unexpect magic %08x from %s\n",
190                         req->pnr_magic, libcfs_id2str(rpc->srpc_peer));
191                 return -EINVAL;
192         }
193
194         rep->pnr_seq   = req->pnr_seq;
195         rep->pnr_magic = LST_PING_TEST_MAGIC;
196
197         if ((reqstmsg->msg_ses_feats & ~LST_FEATS_MASK) != 0) {
198                 replymsg->msg_ses_feats = LST_FEATS_MASK;
199                 rep->pnr_status = EPROTO;
200                 return 0;
201         }
202
203         replymsg->msg_ses_feats = reqstmsg->msg_ses_feats;
204
205         CDEBUG(D_NET, "Get ping %d from %s\n",
206                req->pnr_seq, libcfs_id2str(rpc->srpc_peer));
207         return 0;
208 }
209
210 sfw_test_client_ops_t ping_test_client;
211 void ping_init_test_client(void)
212 {
213         ping_test_client.tso_init     = ping_client_init;
214         ping_test_client.tso_fini     = ping_client_fini;
215         ping_test_client.tso_prep_rpc = ping_client_prep_rpc;
216         ping_test_client.tso_done_rpc = ping_client_done_rpc;
217 }
218
219 srpc_service_t ping_test_service;
220 void ping_init_test_service(void)
221 {
222         ping_test_service.sv_id       = SRPC_SERVICE_PING;
223         ping_test_service.sv_name     = "ping_test";
224         ping_test_service.sv_handler  = ping_server_handle;
225         ping_test_service.sv_wi_total = ping_srv_workitems;
226 }