Motr  M0
rpc_machine.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "ut/ut.h"
24 #include "lib/finject.h"
25 #include "rpc/rpc.h"
26 #include "rpc/rpc_internal.h"
27 #include "net/net.h"
28 #include "rpc/rpc.h"
29 #include "net/buffer_pool.h"
30 #include "net/lnet/lnet.h"
31 #include "rpc/ut/clnt_srv_ctx.c" /* sctx, cctx. NOTE: This is .c file */
32 
33 static struct m0_rpc_machine machine;
35 static const char *ep_addr = "0@lo:12345:34:2";
37 static struct m0_reqh reqh;
39 
40 static int rpc_mc_ut_init(void)
41 {
42  enum { NR_TMS = 1 };
43  int rc;
44  uint32_t bufs_nr;
46  M0_ASSERT(rc == 0);
47 
48  bufs_nr = m0_rpc_bufs_nr(tm_recv_queue_min_len, NR_TMS);
50  NR_TMS);
51  M0_ASSERT(rc == 0);
52  /*
53  * Initialise a rudimentary reqh, sufficient for m0_rcp_machine_init()
54  * to go through.
55  */
56  rc = M0_REQH_INIT(&reqh,
57  .rhia_dtm = (void *)1,
58  .rhia_db = NULL,
59  .rhia_mdstore = (void *)1,
60  .rhia_fid = &g_process_fid,
61  );
62  return rc;
63 }
64 
65 static int rpc_mc_ut_fini(void)
66 {
70  return 0;
71 }
72 
73 static void rpc_mc_init_fini_test(void)
74 {
75  int rc;
76 
77  /*
78  * Test - rpc_machine_init & rpc_machine_fini for success case
79  */
80 
84  M0_UT_ASSERT(rc == 0);
87 }
88 
89 static void rpc_mc_fini_race_test(void)
90 {
91  struct m0_rpc_conn conn;
92  struct m0_rpc_session session;
93  int rc;
94 
98  M0_UT_ASSERT(rc == 0);
100  &machine,
103  M0_TIME_NEVER);
104  M0_UT_ASSERT(rc == 0);
106  M0_UT_ASSERT(rc == 0);
107  /*
108  * The fault injection increases a time interval from the
109  * M0_RPC_CONN_TERMINATED state till M0_RPC_CONN_FINALISED. At this
110  * time, the connection isn't deleted yet from the the
111  * m0_rpc_machine::rm_incoming_conns and
112  * m0_rpc_machine_cleanup_incoming_connections()
113  * should work correctly in such case.
114  */
115  m0_fi_enable("buf_send_cb", "delay_callback");
117  M0_UT_ASSERT(rc == 0);
119  m0_fi_disable("buf_send_cb", "delay_callback");
120 }
121 
122 static void rpc_mc_init_fail_test(void)
123 {
124  int rc;
125 
126  /*
127  * Test - rpc_machine_init for failure cases
128  * Case 1 - m0_net_tm_init failed, should return -EINVAL
129  * Case 2 - m0_net_tm_start failed, should return -ENETUNREACH
130  * Case 3 - root_session_cob_create failed, should return -EINVAL
131  * Case 4 - m0_root_session_cob_create failed, should ret -EINVAL
132  * checks for db_tx_abort code path execution
133  */
134 
135  m0_fi_enable_once("m0_net_tm_init", "fake_error");
139  M0_UT_ASSERT(rc == -EINVAL);
140 
141  m0_fi_enable_once("m0_net_tm_start", "fake_error");
145  M0_UT_ASSERT(rc == -ENETUNREACH);
150 }
151 
152 #ifndef __KERNEL__
153 
154 static bool conn_added_called;
157 
158 static void conn_added(struct m0_rpc_machine_watch *watch,
159  struct m0_rpc_conn *conn)
160 {
162  M0_UT_ASSERT(rpc_conn_tlink_is_in(conn));
164  conn_added_called = true;
165 }
166 
167 static void session_added(struct m0_rpc_machine_watch *watch,
168  struct m0_rpc_session *session)
169 {
171  M0_UT_ASSERT(rpc_session_tlink_is_in(session));
173  session_added_called = true;
174 }
175 
176 static void mach_terminated(struct m0_rpc_machine_watch *watch)
177 {
178  M0_UT_ASSERT(!rmach_watch_tlink_is_in(watch));
179  mach_terminated_called = true;
180 }
181 
182 static void rpc_machine_watch_test(void)
183 {
184  struct m0_rpc_machine_watch watch;
185  struct m0_rpc_machine *rmach;
186  int rc;
187 
188  sctx_reset();
190  M0_UT_ASSERT(rc == 0);
191 
193  M0_UT_ASSERT(rmach != NULL);
194 
195  watch.mw_mach = rmach;
196  watch.mw_conn_added = conn_added;
198 
200 
202  M0_UT_ASSERT(rc == 0);
204 
206  /* It is safe to call detach if watch is already detached */
208 
209  /* If rpc machine is being terminated, while still having attached
210  watchers, then they are detached and mw_mach_terminated() callback
211  is called.
212  */
216  M0_UT_ASSERT(rc == 0);
219 }
220 #endif /* __KERNEL__ */
221 
223  .ts_name = "rpc-machine-ut",
224  .ts_init = rpc_mc_ut_init,
225  .ts_fini = rpc_mc_ut_fini,
226  .ts_tests = {
227  { "rpc_mc_init_fini", rpc_mc_init_fini_test },
228  { "rpc_mc_fini_race", rpc_mc_fini_race_test },
229  { "rpc_mc_init_fail", rpc_mc_init_fail_test },
230 #ifndef __KERNEL__
231  { "rpc_mc_watch", rpc_machine_watch_test},
232 #endif
233  { NULL, NULL}
234  }
235 };
236 M0_EXPORTED(rpc_mc_ut);
237 
238 /*
239  * Local variables:
240  * c-indentation-style: "K&R"
241  * c-basic-offset: 8
242  * tab-width: 8
243  * fill-column: 80
244  * scroll-step: 1
245  * End:
246  */
struct m0_rpc_machine * mw_mach
Definition: rpc_machine.h:234
void m0_rpc_machine_fini(struct m0_rpc_machine *machine)
Definition: rpc_machine.c:233
void m0_net_domain_fini(struct m0_net_domain *dom)
Definition: domain.c:71
#define NULL
Definition: misc.h:38
void(* mw_conn_added)(struct m0_rpc_machine_watch *w, struct m0_rpc_conn *conn)
Definition: rpc_machine.h:244
static const char * ep_addr
Definition: rpc_machine.c:35
#define M0_REQH_INIT(reqh,...)
Definition: reqh.h:262
const m0_time_t M0_TIME_NEVER
Definition: time.c:108
static void sctx_reset(void)
Definition: clnt_srv_ctx.c:68
static bool mach_terminated_called
Definition: rpc_machine.c:156
int m0_rpc_server_start(struct m0_rpc_server_ctx *sctx)
Definition: rpclib.c:50
int m0_rpc_session_destroy(struct m0_rpc_session *session, m0_time_t abs_timeout)
Definition: session.c:559
static struct m0_rpc_client_ctx cctx
Definition: rconfc.c:69
static void mach_terminated(struct m0_rpc_machine_watch *watch)
Definition: rpc_machine.c:176
const char * nep_addr
Definition: net.h:503
static struct m0_rpc_session session
Definition: formation2.c:38
Definition: ut.h:77
static void session_added(struct m0_rpc_machine_watch *watch, struct m0_rpc_session *session)
Definition: rpc_machine.c:167
M0_INTERNAL void m0_reqh_fini(struct m0_reqh *reqh)
Definition: reqh.c:320
struct m0_sm c_sm
Definition: conn.h:322
static void rpc_mc_init_fail_test(void)
Definition: rpc_machine.c:122
M0_INTERNAL bool m0_rpc_machine_is_locked(const struct m0_rpc_machine *machine)
Definition: rpc_machine.c:565
static void rpc_mc_fini_race_test(void)
Definition: rpc_machine.c:89
struct m0_ut_suite rpc_mc_ut
Definition: rpc_machine.c:222
void(* mw_session_added)(struct m0_rpc_machine_watch *w, struct m0_rpc_session *session)
Definition: rpc_machine.h:251
M0_INTERNAL int m0_rpc_client_connect(struct m0_rpc_conn *conn, struct m0_rpc_session *session, struct m0_rpc_machine *rpc_mach, const char *remote_addr, struct m0_fid *svc_fid, uint64_t max_rpcs_in_flight, m0_time_t abs_timeout)
Definition: rpclib.c:102
M0_INTERNAL void m0_fi_disable(const char *fp_func, const char *fp_tag)
Definition: finject.c:485
static void m0_fi_enable(const char *func, const char *tag)
Definition: finject.h:276
struct m0_net_transfer_mc rm_tm
Definition: rpc_machine.h:88
#define M0_ASSERT(cond)
M0_INTERNAL int m0_rpc_net_buffer_pool_setup(struct m0_net_domain *ndom, struct m0_net_buffer_pool *app_pool, uint32_t bufs_nr, uint32_t tm_nr)
Definition: rpc.c:229
M0_INTERNAL uint32_t m0_rpc_bufs_nr(uint32_t len, uint32_t tms_nr)
Definition: rpc.c:271
int m0_rpc_conn_destroy(struct m0_rpc_conn *conn, m0_time_t abs_timeout)
Definition: conn.c:974
int m0_rpc_client_stop(struct m0_rpc_client_ctx *cctx)
Definition: rpclib.c:217
M0_INTERNAL int m0_rpc_machine_init(struct m0_rpc_machine *machine, struct m0_net_domain *net_dom, const char *ep_addr, struct m0_reqh *reqh, struct m0_net_buffer_pool *receive_pool, uint32_t colour, m0_bcount_t msg_size, uint32_t queue_len)
Definition: rpc_machine.c:123
struct m0_net_xprt * m0_net_xprt_default_get(void)
Definition: net.c:151
int m0_rpc_client_start(struct m0_rpc_client_ctx *cctx)
Definition: rpclib.c:160
void m0_rpc_machine_watch_attach(struct m0_rpc_machine_watch *watch)
Definition: rpc_machine.c:979
static struct m0_rpc_server_ctx sctx
Definition: console.c:88
static struct m0_net_domain client_net_dom
Definition: rconfc.c:47
static int rpc_mc_ut_fini(void)
Definition: rpc_machine.c:65
Definition: reqh.h:94
struct m0_rpc_conn conn
Definition: fsync.c:96
void m0_rpc_machine_watch_detach(struct m0_rpc_machine_watch *watch)
Definition: rpc_machine.c:997
struct m0_sm s_sm
Definition: session.h:325
static uint32_t max_rpc_msg_size
Definition: rpc_machine.c:34
void(* mw_mach_terminated)(struct m0_rpc_machine_watch *w)
Definition: rpc_machine.h:260
const char * ts_name
Definition: ut.h:99
static void rpc_mc_init_fini_test(void)
Definition: rpc_machine.c:73
int m0_net_domain_init(struct m0_net_domain *dom, const struct m0_net_xprt *xprt)
Definition: domain.c:36
struct m0_net_end_point * ntm_ep
Definition: net.h:868
static bool session_added_called
Definition: rpc_machine.c:155
static struct m0_rpc_machine machine
Definition: rpc_machine.c:33
static bool conn_added_called
Definition: rpc_machine.c:154
static struct m0_net_buffer_pool buf_pool
Definition: rpc_machine.c:36
static void m0_fi_enable_once(const char *func, const char *tag)
Definition: finject.h:301
void m0_rpc_server_stop(struct m0_rpc_server_ctx *sctx)
Definition: rpclib.c:85
static void rpc_machine_watch_test(void)
Definition: rpc_machine.c:182
static uint32_t tm_recv_queue_min_len
Definition: rpc_machine.c:38
void m0_rpc_net_buffer_pool_cleanup(struct m0_net_buffer_pool *app_pool)
Definition: rpc.c:264
static struct m0_reqh reqh
Definition: rpc_machine.c:37
static int rpc_mc_ut_init(void)
Definition: rpc_machine.c:40
M0_INTERNAL struct m0_rpc_machine * m0_rpc_server_ctx_get_rmachine(struct m0_rpc_server_ctx *sctx)
Definition: rpclib.c:96
uint32_t sm_state
Definition: sm.h:307
int32_t rc
Definition: trigger_fop.h:47
struct m0_fid g_process_fid
Definition: ut.c:689
#define M0_UT_ASSERT(a)
Definition: ut.h:46
static void conn_added(struct m0_rpc_machine_watch *watch, struct m0_rpc_conn *conn)
Definition: rpc_machine.c:158