Motr  M0
rm_service.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-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 "rm/rm_service.h"
24 #include "rm/ut/rmut.h" /* rm_ctx */
25 #include "rpc/rpclib.h" /* m0_rpc_server_ctx */
26 #include "ut/misc.h" /* M0_UT_PATH */
27 #include "ut/ut.h"
28 
29 #define SERVER_ENDPOINT_ADDR "0@lo:12345:34:1"
30 #define SERVER_ENDPOINT M0_NET_XPRT_PREFIX_DEFAULT":"SERVER_ENDPOINT_ADDR
31 #define SERVER_DB "server_db"
32 #define SERVER_STOB "server_stob"
33 #define SERVER_ADDB_STOB "linuxstob:server_addb_stob"
34 #define SERVER_LOG "rmserver.log"
35 
36 static char *server_argv[] = {
37  "rm-ut", "-T", "linux", "-D", SERVER_DB,
38  "-S", SERVER_STOB, "-A", SERVER_ADDB_STOB,
39  "-w", "10", "-e", SERVER_ENDPOINT, "-H", SERVER_ENDPOINT_ADDR,
40  "-f", M0_UT_CONF_PROCESS,
41  "-c", M0_UT_PATH("conf.xc")
42 };
43 
44 extern struct m0_reqh_service_type m0_rms_type;
45 
46 static struct rm_ctx *server_ctx = &rm_ctxs[SERVER_1];
47 static struct rm_ctx *client_ctx = &rm_ctxs[SERVER_2];
48 static struct m0_clink tests_clink[TEST_NR];
49 extern void flock_client_utdata_ops_set(struct rm_ut_data *data);
50 
51 static struct m0_rpc_server_ctx sctx = {
52  .rsx_xprts_nr = 1,
53  .rsx_argv = server_argv,
54  .rsx_argc = ARRAY_SIZE(server_argv),
55  .rsx_log_file_name = SERVER_LOG,
56 };
57 
59 {
60  int result;
61 
62  result = m0_rpc_server_start(sctx);
63  M0_UT_ASSERT(result == 0);
64 }
65 
67 {
69 }
70 
71 static void rm_svc_server(const int tid)
72 {
74 
78 
79  /* Signal client that server is now up and running */
81  /* Stay alive till client runs its test cases */
83 
85  /* Tell client that I am done */
87 }
88 
89 static void test_flock(struct m0_rm_owner *owner, struct m0_file *file,
90  struct m0_fid *fid, struct m0_rm_remote *creditor,
91  bool unwind)
92 {
93  int rc;
94  struct m0_rm_incoming in;
95 
96  M0_SET0(owner);
99  m0_file_lock(owner, &in);
100  m0_rm_owner_lock(owner);
101  rc = m0_sm_timedwait(&in.rin_sm,
103  M0_TIME_NEVER);
104  M0_UT_ASSERT(rc == 0);
105  M0_UT_ASSERT(in.rin_rc == 0);
107 
108  m0_rm_owner_unlock(owner);
109  m0_file_unlock(&in);
110  if (unwind) {
111  m0_rm_owner_windup(owner);
113  M0_TIME_NEVER);
114  M0_UT_ASSERT(rc == 0);
115  m0_file_owner_fini(owner);
117  }
118 }
119 
120 static void rm_client(const int tid)
121 {
122  int rc;
123  struct m0_rm_resource *resource;
124  struct m0_rm_remote *creditor;
125  struct m0_rm_owner owner;
126  struct m0_file file1;
127  struct m0_file file2;
128  struct m0_fid fids[] = {{0, 1}, {0, 2}};
129  struct m0_reqh_service *rmservice;
130 
134  NULL, NULL);
135  M0_UT_ASSERT(rc == 0);
136 
140 
141  /* Start the server */
143  "rm_svc_%d", 0);
144  M0_UT_ASSERT(rc == 0);
145 
146  /* Wait till server starts */
148 
149  /* Connect to end point of SERVER_1 */
151 
152  M0_SET0(&file1);
153  M0_SET0(&file2);
156  M0_ALLOC_PTR(resource);
157  M0_UT_ASSERT(resource != NULL);
158 
161 
162  resource->r_type = rm_test_data.rd_rt;
163 
164  m0_rm_remote_init(creditor, resource);
167 
168  /*
169  * Test for Cancel
170  * We perform resource owner_finalisation on debtor before rm-service
171  * shuts down. This results in sending cancel request to rm-service.
172  */
173  test_flock(&owner, &file1, &fids[0], creditor, true);
174 
175  /*
176  * Test the request again for same resource
177  * This test checks for caching of credits on creditor side.
178  * When we ask for the same credit second time, creditor grants
179  * this request from cached credits instead of creating a new
180  * resource (compared to above case where a new resource is
181  * created and creditor is granted a self loan)
182  */
183  test_flock(&owner, &file1, &fids[0], creditor, true);
184 
185  /*
186  * Test for Revoke
187  * We disconnect from server and stop the rm-service before
188  * performing resource owner finalisation on debtor. In this case
189  * server sends revoke request for file resource.
190  */
191  test_flock(&owner, &file2, &fids[1], creditor, false);
192 
194 
195  /* Tell server to stop */
197  /* Wait for server to stop */
199 
200  m0_rm_owner_windup(&owner);
202  M0_UT_ASSERT(rc == 0);
203  m0_file_owner_fini(&owner);
204  m0_file_fini(&file2);
206  m0_free(resource);
207  m0_free(creditor);
213 }
214 
215 /*
216  * Two threads are started; One server, which runs rm-service and one client
217  * which requests resource credits to server.
218  *
219  * First server is started by using m0_rpc_server_start. rm-service option is
220  * provided in rpc server context.
221  *
222  * Client asks for credit request of rings resource type. A dummy creditor is
223  * created which points to session established with rm-service.
224  *
225  * When borrow request FOP reaches server, server checks that creditor cookie is
226  * NULL; It now creates an owner for given resource type and grants this request
227  * to client.
228  */
229 void rmsvc(void)
230 {
231  int rc;
232 
235 
236  for (rc = 0; rc < 2; ++rc) {
237  M0_SET0(&rm_ctxs[rc]);
238  rm_ctxs[rc].rc_id = rc;
243  }
244 
245  /* Start client */
247  "rm_cli_%d", 0);
248  M0_UT_ASSERT(rc == 0);
249 
254 
255  for (rc = 0; rc <= 1; ++rc) {
258  }
259 
262 }
263 
264 /*
265  * Local variables:
266  * c-indentation-style: "K&R"
267  * c-basic-offset: 8
268  * tab-width: 8
269  * fill-column: 80
270  * scroll-step: 1
271  * End:
272  */
273 /*
274  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
275  */
Definition: rmut.h:47
uint64_t id
Definition: cob.h:240
struct m0_rm_resource_type * r_type
Definition: rm.h:304
M0_INTERNAL void m0_chan_wait(struct m0_clink *link)
Definition: chan.c:336
struct m0_reqh_service * rmservice[SERVER_NR]
Definition: rmut.c:162
Definition: rmut.h:59
M0_INTERNAL void m0_ut_rpc_mach_init_and_add(struct m0_ut_rpc_mach_ctx *ctx)
enum m0_rm_remote_state rem_state
Definition: rm.h:748
static struct rm_ctx * server_ctx
Definition: rm_service.c:46
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_clink_init(struct m0_clink *link, m0_chan_cb_t cb)
Definition: chan.c:201
static struct m0_rm_remote creditor
Definition: file.c:95
struct rm_ctx rm_ctxs[SERVER_NR]
Definition: rmut.c:54
M0_INTERNAL void m0_clink_del_lock(struct m0_clink *link)
Definition: chan.c:293
int m0_thread_join(struct m0_thread *q)
Definition: kthread.c:169
struct rm_ut_data rm_test_data
Definition: rmut.c:53
const m0_time_t M0_TIME_NEVER
Definition: time.c:108
struct m0_file file
Definition: di.c:36
static struct m0_rpc_server_ctx sctx
Definition: rm_service.c:51
int m0_rpc_server_start(struct m0_rpc_server_ctx *sctx)
Definition: rpclib.c:50
struct m0_ut_rpc_mach_ctx rc_rmach_ctx
Definition: rmut.h:129
const char * serv_addr[]
Definition: rmut.c:43
struct m0_bufvec data
Definition: di.c:40
struct m0_cob_domain_id rmc_cob_id
M0_INTERNAL void m0_file_lock(struct m0_rm_owner *owner, struct m0_rm_incoming *req)
Definition: file.c:522
struct m0_rm_resource_type * rd_rt
Definition: rmut.h:110
void flock_client_utdata_ops_set(struct rm_ut_data *data)
Definition: file.c:157
#define M0_BITS(...)
Definition: misc.h:236
M0_INTERNAL void m0_rm_remote_init(struct m0_rm_remote *rem, struct m0_rm_resource *res)
Definition: rm.c:1411
#define M0_THREAD_INIT(thread, TYPE, init, func, arg, namefmt,...)
Definition: thread.h:139
#define SERVER_DB
Definition: rm_service.c:31
#define M0_SET0(obj)
Definition: misc.h:64
struct m0_rpc_session * rem_session
Definition: rm.h:755
#define SERVER_STOB
Definition: rm_service.c:32
M0_INTERNAL int m0_rm_owner_timedwait(struct m0_rm_owner *owner, uint64_t state, const m0_time_t abs_timeout)
Definition: rm.c:892
struct m0_clink rc_clink
Definition: rmut.h:127
M0_INTERNAL int m0_sm_timedwait(struct m0_sm *mach, uint64_t states, m0_time_t deadline)
Definition: sm.c:387
struct m0_fid fid
Definition: di.c:46
void rmsvc(void)
Definition: rm_service.c:229
struct m0_sm rin_sm
Definition: rm.h:1436
Definition: rmut.h:123
struct m0_chan rm_ut_tests_chan
Definition: rmut.c:55
Definition: rmut.h:46
static void test_flock(struct m0_rm_owner *owner, struct m0_file *file, struct m0_fid *fid, struct m0_rm_remote *creditor, bool unwind)
Definition: rm_service.c:89
const struct m0_uint128 m0_rm_no_group
Definition: rm.c:211
M0_INTERNAL void m0_chan_init(struct m0_chan *chan, struct m0_mutex *ch_guard)
Definition: chan.c:96
static enum m0_rm_incoming_state incoming_state(const struct m0_rm_incoming *in)
Definition: rm_internal.h:285
static void rm_client(const int tid)
Definition: rm_service.c:120
#define SERVER_ENDPOINT_ADDR
Definition: rm_service.c:29
void m0_thread_fini(struct m0_thread *q)
Definition: thread.c:92
int32_t rin_rc
Definition: rm.h:1446
int m0_net_xprt_nr(void)
Definition: net.c:168
M0_INTERNAL void m0_file_owner_init(struct m0_rm_owner *owner, const struct m0_uint128 *grp_id, struct m0_file *file, struct m0_rm_remote *creditor)
Definition: file.c:507
struct m0_net_xprt ** rsx_xprts
Definition: rpclib.h:69
enum rm_server rc_id
Definition: rmut.h:124
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
static void rm_svc_server(const int tid)
Definition: rm_service.c:71
struct m0_reqh rmc_reqh
static const struct m0_fid fids[]
Definition: diter.c:76
void rm_utdata_init(struct rm_ut_data *data, enum obj_type type)
Definition: rmut.c:96
struct m0_rm_domain rd_dom
Definition: rmut.h:109
M0_INTERNAL int m0_reqh_service_setup(struct m0_reqh_service **out, struct m0_reqh_service_type *stype, struct m0_reqh *reqh, struct m0_reqh_context *rctx, const struct m0_fid *fid)
Definition: reqh_service.c:565
M0_INTERNAL void m0_chan_signal_lock(struct m0_chan *chan)
Definition: chan.c:165
static struct rm_ctx * client_ctx
Definition: rm_service.c:47
const int cob_ids[]
Definition: rmut.c:49
void rm_ctx_disconnect(struct rm_ctx *src, const struct rm_ctx *dest)
Definition: rmut.c:211
void m0_clink_add_lock(struct m0_chan *chan, struct m0_clink *link)
Definition: chan.c:255
struct m0_mutex rc_mutex
Definition: rmut.h:128
struct m0_reqh_service_type m0_rms_type
Definition: rm_service.c:69
struct m0_thread rc_thr
Definition: rmut.h:125
static void rm_service_start(struct m0_rpc_server_ctx *sctx)
Definition: rm_service.c:58
#define SERVER_LOG
Definition: rm_service.c:34
#define SERVER_ADDB_STOB
Definition: rm_service.c:33
int rsx_xprts_nr
Definition: rpclib.h:71
M0_INTERNAL void m0_file_unlock(struct m0_rm_incoming *req)
Definition: file.c:540
Definition: fid.h:38
M0_INTERNAL void m0_rm_remote_fini(struct m0_rm_remote *rem)
Definition: rm.c:1431
struct m0_chan rc_chan
Definition: rmut.h:126
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
#define M0_UT_CONF_PROCESS
Definition: misc.h:45
const char * rmc_ep_addr
struct m0_net_xprt ** m0_net_all_xprt_get(void)
Definition: net.c:161
static void rm_service_stop(struct m0_rpc_server_ctx *sctx)
Definition: rm_service.c:66
Definition: rm.h:863
M0_INTERNAL void m0_ut_rpc_mach_fini(struct m0_ut_rpc_mach_ctx *ctx)
M0_INTERNAL void m0_rm_owner_windup(struct m0_rm_owner *owner)
Definition: rm.c:930
Definition: rmut.h:38
static char * server_argv[]
Definition: rm_service.c:36
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
M0_INTERNAL void m0_clink_fini(struct m0_clink *link)
Definition: chan.c:208
#define SERVER_ENDPOINT
Definition: rm_service.c:30
M0_INTERNAL void m0_rm_owner_unlock(struct m0_rm_owner *owner)
Definition: rm.c:603
M0_INTERNAL void m0_file_fini(struct m0_file *file)
Definition: file.c:498
void m0_rpc_server_stop(struct m0_rpc_server_ctx *sctx)
Definition: rpclib.c:85
#define M0_UT_PATH(name)
Definition: misc.h:41
Definition: file.h:81
M0_INTERNAL void m0_rm_owner_lock(struct m0_rm_owner *owner)
Definition: rm.c:592
M0_INTERNAL void m0_file_owner_fini(struct m0_rm_owner *owner)
Definition: file.c:516
struct m0_rpc_session rc_sess[SERVER_NR]
Definition: rmut.h:132
M0_INTERNAL void m0_chan_fini_lock(struct m0_chan *chan)
Definition: chan.c:112
void m0_free(void *data)
Definition: memory.c:146
int32_t rc
Definition: trigger_fop.h:47
static struct m0_clink tests_clink[TEST_NR]
Definition: rm_service.c:48
#define ARRAY_SIZE(a)
Definition: misc.h:45
struct m0_mutex rm_ut_tests_chan_mutex
Definition: rmut.c:56
#define M0_UT_ASSERT(a)
Definition: ut.h:46
struct m0_motr rsx_motr_ctx
Definition: rpclib.h:84
void rm_ctx_connect(struct rm_ctx *src, const struct rm_ctx *dest)
Definition: rmut.c:197
M0_INTERNAL void m0_file_init(struct m0_file *file, const struct m0_fid *fid, struct m0_rm_domain *dom, enum m0_di_types di_type)
Definition: file.c:477
void rm_utdata_fini(struct rm_ut_data *data, enum obj_type type)
Definition: rmut.c:128