Whamcloud - gitweb
LU-10391 lnet: extend rspt_next_hop_nid in lnet_rsp_tracker
[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  *
31  * lnet/selftest/conctl.c
32  *
33  * Test client & Server
34  *
35  * Author: Liang Zhen <liangzhen@clusterfs.com>
36  */
37
38 #include "selftest.h"
39
40 #define LST_PING_TEST_MAGIC     0xbabeface
41
42 static int ping_srv_workitems = SFW_TEST_WI_MAX;
43 module_param(ping_srv_workitems, int, 0644);
44 MODULE_PARM_DESC(ping_srv_workitems, "# PING server workitems");
45
46 struct lst_ping_data {
47         spinlock_t      pnd_lock;       /* serialize */
48         int             pnd_counter;    /* sequence counter */
49 };
50
51 static struct lst_ping_data lst_ping_data;
52
53 static int
54 ping_client_init(struct sfw_test_instance *tsi)
55 {
56         struct sfw_session *sn = tsi->tsi_batch->bat_session;
57
58         LASSERT(tsi->tsi_is_client);
59         LASSERT(sn != NULL && (sn->sn_features & ~LST_FEATS_MASK) == 0);
60
61         spin_lock_init(&lst_ping_data.pnd_lock);
62         lst_ping_data.pnd_counter = 0;
63
64         return 0;
65 }
66
67 static void
68 ping_client_fini(struct sfw_test_instance *tsi)
69 {
70         struct sfw_session *sn = tsi->tsi_batch->bat_session;
71         int            errors;
72
73         LASSERT (sn != NULL);
74         LASSERT (tsi->tsi_is_client);
75
76         errors = atomic_read(&sn->sn_ping_errors);
77         if (errors)
78                 CWARN ("%d pings have failed.\n", errors);
79         else
80                 CDEBUG (D_NET, "Ping test finished OK.\n");
81 }
82
83 static int
84 ping_client_prep_rpc(struct sfw_test_unit *tsu, struct lnet_process_id dest,
85                      struct srpc_client_rpc **rpc)
86 {
87         struct srpc_ping_reqst *req;
88         struct sfw_test_instance *tsi = tsu->tsu_instance;
89         struct sfw_session *sn = tsi->tsi_batch->bat_session;
90         struct timespec64 ts;
91         int rc;
92
93         LASSERT(sn != NULL);
94         LASSERT((sn->sn_features & ~LST_FEATS_MASK) == 0);
95
96         rc = sfw_create_test_rpc(tsu, dest, sn->sn_features, 0, 0, rpc);
97         if (rc != 0)
98                 return rc;
99
100         req = &(*rpc)->crpc_reqstmsg.msg_body.ping_reqst;
101
102         req->pnr_magic = LST_PING_TEST_MAGIC;
103
104         spin_lock(&lst_ping_data.pnd_lock);
105         req->pnr_seq = lst_ping_data.pnd_counter++;
106         spin_unlock(&lst_ping_data.pnd_lock);
107
108         ktime_get_real_ts64(&ts);
109         req->pnr_time_sec  = ts.tv_sec;
110         req->pnr_time_nsec = ts.tv_nsec;
111
112         return rc;
113 }
114
115 static void
116 ping_client_done_rpc(struct sfw_test_unit *tsu, struct srpc_client_rpc *rpc)
117 {
118         struct sfw_test_instance *tsi = tsu->tsu_instance;
119         struct sfw_session *sn = tsi->tsi_batch->bat_session;
120         struct srpc_ping_reqst *reqst = &rpc->crpc_reqstmsg.msg_body.ping_reqst;
121         struct srpc_ping_reply *reply = &rpc->crpc_replymsg.msg_body.ping_reply;
122         struct timespec64 ts;
123
124         LASSERT(sn != NULL);
125
126         if (rpc->crpc_status != 0) {
127                 if (!tsi->tsi_stopping) /* rpc could have been aborted */
128                         atomic_inc(&sn->sn_ping_errors);
129                 CERROR ("Unable to ping %s (%d): %d\n",
130                         libcfs_id2str(rpc->crpc_dest),
131                         reqst->pnr_seq, rpc->crpc_status);
132                 return;
133         }
134
135         if (rpc->crpc_replymsg.msg_magic != SRPC_MSG_MAGIC) {
136                 __swab32s(&reply->pnr_seq);
137                 __swab32s(&reply->pnr_magic);
138                 __swab32s(&reply->pnr_status);
139         }
140
141         if (reply->pnr_magic != LST_PING_TEST_MAGIC) {
142                 rpc->crpc_status = -EBADMSG;
143                 atomic_inc(&sn->sn_ping_errors);
144                 CERROR ("Bad magic %u from %s, %u expected.\n",
145                         reply->pnr_magic, libcfs_id2str(rpc->crpc_dest),
146                         LST_PING_TEST_MAGIC);
147                 return;
148         }
149
150         if (reply->pnr_seq != reqst->pnr_seq) {
151                 rpc->crpc_status = -EBADMSG;
152                 atomic_inc(&sn->sn_ping_errors);
153                 CERROR ("Bad seq %u from %s, %u expected.\n",
154                         reply->pnr_seq, libcfs_id2str(rpc->crpc_dest),
155                         reqst->pnr_seq);
156                 return;
157         }
158
159         ktime_get_real_ts64(&ts);
160         CDEBUG(D_NET, "%d reply in %llu nsec\n", reply->pnr_seq,
161                (u64)((ts.tv_sec - reqst->pnr_time_sec) * NSEC_PER_SEC +
162                     (ts.tv_nsec - reqst->pnr_time_nsec)));
163 }
164
165 static int
166 ping_server_handle(struct srpc_server_rpc *rpc)
167 {
168         struct srpc_service *sv  = rpc->srpc_scd->scd_svc;
169         struct srpc_msg *reqstmsg = &rpc->srpc_reqstbuf->buf_msg;
170         struct srpc_msg *replymsg = &rpc->srpc_replymsg;
171         struct srpc_ping_reqst *req = &reqstmsg->msg_body.ping_reqst;
172         struct srpc_ping_reply *rep = &rpc->srpc_replymsg.msg_body.ping_reply;
173
174         LASSERT (sv->sv_id == SRPC_SERVICE_PING);
175
176         if (reqstmsg->msg_magic != SRPC_MSG_MAGIC) {
177                 LASSERT (reqstmsg->msg_magic == __swab32(SRPC_MSG_MAGIC));
178
179                 __swab32s(&req->pnr_seq);
180                 __swab32s(&req->pnr_magic);
181                 __swab64s(&req->pnr_time_sec);
182                 __swab64s(&req->pnr_time_nsec);
183         }
184         LASSERT (reqstmsg->msg_type == srpc_service2request(sv->sv_id));
185
186         if (req->pnr_magic != LST_PING_TEST_MAGIC) {
187                 CERROR ("Unexpect magic %08x from %s\n",
188                         req->pnr_magic, libcfs_id2str(rpc->srpc_peer));
189                 return -EINVAL;
190         }
191
192         rep->pnr_seq   = req->pnr_seq;
193         rep->pnr_magic = LST_PING_TEST_MAGIC;
194
195         if ((reqstmsg->msg_ses_feats & ~LST_FEATS_MASK) != 0) {
196                 replymsg->msg_ses_feats = LST_FEATS_MASK;
197                 rep->pnr_status = EPROTO;
198                 return 0;
199         }
200
201         replymsg->msg_ses_feats = reqstmsg->msg_ses_feats;
202
203         CDEBUG(D_NET, "Get ping %d from %s\n",
204                req->pnr_seq, libcfs_id2str(rpc->srpc_peer));
205         return 0;
206 }
207
208 struct sfw_test_client_ops ping_test_client;
209
210 void ping_init_test_client(void)
211 {
212         ping_test_client.tso_init     = ping_client_init;
213         ping_test_client.tso_fini     = ping_client_fini;
214         ping_test_client.tso_prep_rpc = ping_client_prep_rpc;
215         ping_test_client.tso_done_rpc = ping_client_done_rpc;
216 }
217
218 struct srpc_service ping_test_service;
219
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 }