Motr  M0
sd_send_not.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2017-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 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_FDMI
24 #include "lib/trace.h"
25 
26 #include "lib/memory.h"
27 #include "lib/types.h"
28 #include "rpc/conn_pool_internal.h"
29 #include "rpc/packet_internal.h" /* packet_item_tlist_head */
31 #include "ut/ut.h"
32 #include "fdmi/fdmi.h"
33 #include "fdmi/fops.h"
34 #include "fdmi/service.h" /* m0_reqh_fdmi_service */
36 
37 #include "fdmi/ut/sd_common.h"
38 
39 static struct m0_semaphore g_sem1;
40 static struct m0_semaphore g_sem2;
41 static struct m0_semaphore g_sem3;
42 static char g_fdmi_data[] = "hello, FDMI";
43 static struct m0_fdmi_src_rec g_src_rec;
44 static struct test_rpc_env g_rpc_env;
46 
47 static bool inc_ref_passed;
48 static int dec_ref_count;
49 static bool first_filter;
50 static int refcount;
51 
52 static const struct m0_fid g_fid = M0_FID_INIT(0xFA11, 0x11AF);
53 
54 static int send_notif_packet_ready(struct m0_rpc_packet *p);
55 
56 static const struct m0_rpc_frm_ops send_notif_frm_ops = {
58 };
59 
60 /*********** FilterC stub ***********/
61 
63 static char *g_var_str;
64 
66  struct m0_reqh *reqh);
67 
68 static void filterc_send_notif_stop(struct m0_filterc_ctx *ctx);
69 
70 static int filterc_send_notif_open(struct m0_filterc_ctx *ctx,
71  enum m0_fdmi_rec_type_id rec_type_id,
72  struct m0_filterc_iter *iter);
73 
74 static int filterc_send_notif_get_next(struct m0_filterc_iter *iter,
75  struct m0_conf_fdmi_filter **out);
76 
77 static void filterc_send_notif_close(struct m0_filterc_iter *iter);
78 
81  .fco_stop = filterc_send_notif_stop,
82  .fco_open = filterc_send_notif_open,
83  .fco_get_next = filterc_send_notif_get_next,
84  .fco_close = filterc_send_notif_close
85 };
86 
88  struct m0_reqh *reqh)
89 {
90  return 0;
91 }
92 
94 {
95 }
96 
98  enum m0_fdmi_rec_type_id rec_type_id,
99  struct m0_filterc_iter *iter)
100 {
101  return 0;
102 }
103 
105  struct m0_conf_fdmi_filter **out)
106 {
107  int rc;
108  struct m0_conf_fdmi_filter *conf_flt = &g_conf_filter;
109  struct m0_fdmi_filter *flt = &conf_flt->ff_filter;
110  struct m0_fdmi_flt_node *root;
111  struct m0_buf var = M0_BUF_INITS(g_var_str);
112 
113  if (first_filter) {
115  M0_FFO_OR,
118 
119  m0_fdmi_filter_init(flt);
120 
122 
123  M0_ALLOC_ARR(conf_flt->ff_endpoints, 1);
124  conf_flt->ff_type = M0_FDMI_FILTER_TYPE_TREE;
125  conf_flt->ff_endpoints[0] = g_rpc_env.ep_addr_remote;
126  conf_flt->ff_filter_id = g_fid;
127  *out = conf_flt;
128  rc = 1;
129  first_filter = false;
130  } else {
131  *out = NULL;
132  rc = 0;
133  }
134  return rc;
135 }
136 
137 static void filterc_send_notif_close(struct m0_filterc_iter *iter)
138 {
140 }
141 
142 /*********** Source definition ***********/
143 static int test_fs_node_eval(struct m0_fdmi_src_rec *src_rec,
144  struct m0_fdmi_flt_var_node *value_desc,
145  struct m0_fdmi_flt_operand *value)
146 {
147  M0_UT_ASSERT(src_rec == &g_src_rec);
148  M0_UT_ASSERT(src_rec->fsr_data == &g_fdmi_data);
149  M0_UT_ASSERT(value_desc->ffvn_data.b_nob == strlen(g_var_str));
150  M0_UT_ASSERT(value_desc->ffvn_data.b_addr == g_var_str);
151 
153  return 0;
154 }
155 
156 static int test_fs_encode(struct m0_fdmi_src_rec *src_rec,
157  struct m0_buf *buf)
158 {
159  M0_UT_ASSERT(src_rec == &g_src_rec);
160  M0_UT_ASSERT(src_rec->fsr_data == &g_fdmi_data);
161 
163 
164  return 0;
165 }
166 
167 static void test_fs_get(struct m0_fdmi_src_rec *src_rec)
168 {
169  M0_UT_ASSERT(src_rec != NULL);
170  M0_UT_ASSERT(src_rec == &g_src_rec);
171  inc_ref_passed = true;
172  ++refcount;
173 }
174 
175 static void test_fs_put(struct m0_fdmi_src_rec *src_rec)
176 {
177  M0_UT_ASSERT(src_rec != NULL);
178  M0_UT_ASSERT(src_rec == &g_src_rec);
179  ++dec_ref_count;
180  --refcount;
181  if (refcount == 0)
183 }
184 
185 static void test_fs_begin(struct m0_fdmi_src_rec *src_rec)
186 {
191  M0_ENTRY("* src_rec %p, assigned %p", src_rec, &g_src_rec);
192  M0_UT_ASSERT(src_rec == &g_src_rec);
193  M0_UT_ASSERT(src_rec->fsr_data == &g_fdmi_data);
194  M0_LEAVE();
195 }
196 
197 static void test_fs_end(struct m0_fdmi_src_rec *src_rec)
198 {
199  M0_UT_ASSERT(src_rec == &g_src_rec);
200  M0_UT_ASSERT(src_rec->fsr_data == &g_fdmi_data);
203 }
204 
205 static struct m0_fdmi_src *src_alloc()
206 {
207  struct m0_fdmi_src *src;
208  int rc;
209 
211  M0_UT_ASSERT(rc == 0);
212 
213  src->fs_node_eval = test_fs_node_eval;
214  src->fs_get = test_fs_get;
215  src->fs_put = test_fs_put;
216  src->fs_begin = test_fs_begin;
217  src->fs_end = test_fs_end;
218  src->fs_encode = test_fs_encode;
219  return src;
220 }
221 
222 static void check_fop_content(struct m0_rpc_item *item)
223 {
224  struct m0_fop_fdmi_record *fdmi_rec;
226 
227  fdmi_rec = m0_fop_data(m0_rpc_item_to_fop(item));
228 
229  M0_UT_ASSERT((void *)fdmi_rec->fr_rec_id.u_lo == &g_src_rec);
231  M0_UT_ASSERT(m0_buf_eq(&fdmi_rec->fr_payload, &buf));
232  M0_UT_ASSERT(fdmi_rec->fr_matched_flts.fmf_count == 1);
234  &g_fid));
235 }
236 
238 {
239  check_fop_content(packet_item_tlist_head(&p->rp_items));
241 
243  return 0;
244 }
245 
247 {
248  struct m0_fdmi_src_dock *src_dock;
249  struct m0_fdmi_src *src = src_alloc();
250  struct fdmi_sd_fom *sd_fom;
251  struct m0_rpc_conn_pool *conn_pool;
252  struct m0_rpc_conn_pool_item *pool_item;
253  int rc;
254 
256  g_var_str = strdup("test");
257  M0_SET0(&g_sem1);
258  M0_SET0(&g_sem2);
259  M0_SET0(&g_sem3);
260  M0_SET0(&g_rpc_env);
262  inc_ref_passed = false;
263  dec_ref_count = 0;
264  first_filter = true;
265 
267  src_dock = m0_fdmi_src_dock_get();
268  sd_fom = &src_dock->fsdc_sd_fom;
269  conn_pool = &sd_fom->fsf_conn_pool;
270  M0_UT_ASSERT(rpc_conn_pool_items_tlist_is_empty(&conn_pool->cp_items));
271  M0_ALLOC_PTR(pool_item);
272  M0_UT_ASSERT(pool_item != NULL);
273  rpc_conn_pool_items_tlink_init_at_tail(pool_item, &conn_pool->cp_items);
275  &send_notif_frm_ops, true,
276  &pool_item->cpi_rpc_link.rlk_conn,
277  &pool_item->cpi_rpc_link.rlk_sess);
282  M0_UT_ASSERT(rc == 0);
283 
284  g_src_rec = (struct m0_fdmi_src_rec) {
285  .fsr_src = src,
286  .fsr_data = g_fdmi_data,
287  };
288 
290  /* Wait until record is sent over RPC */
294  /* Wait until record is released */
300  M0_UT_ASSERT(rpc_conn_pool_items_tlist_head(&conn_pool->cp_items) ==
301  rpc_conn_pool_items_tlist_tail(&conn_pool->cp_items));
303 
304  /*
305  * At this moment, the pool_item might have been removed from the list
306  * and been freed in FDMI notification failure handling.
307  */
308  if (rpc_conn_pool_items_tlink_is_in(pool_item)) {
309  rpc_conn_pool_items_tlink_del_fini(pool_item);
310  m0_free(pool_item);
311  pool_item = NULL;
312  }
316 }
317 
318 #undef M0_TRACE_SUBSYSTEM
319 
320 /*
321  * Local variables:
322  * c-indentation-style: "K&R"
323  * c-basic-offset: 8
324  * tab-width: 8
325  * fill-column: 80
326  * scroll-step: 1
327  * End:
328  */
329 /*
330  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
331  */
struct m0_motr motr
Definition: sd_common.h:41
static struct m0_addb2_philter p
Definition: consumer.c:40
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
struct m0_fdmi_flt_id_arr fr_matched_flts
Definition: fops.h:78
struct m0_fid ff_filter_id
Definition: obj.h:730
static void test_fs_begin(struct m0_fdmi_src_rec *src_rec)
Definition: sd_send_not.c:185
static const struct m0_fid g_fid
Definition: sd_send_not.c:52
struct m0_rpc_link cpi_rpc_link
Definition: conn_pool.h:36
#define NULL
Definition: misc.h:38
struct fdmi_sd_fom fsdc_sd_fom
M0_INTERNAL void m0_fdmi_filter_init(struct m0_fdmi_filter *flt)
Definition: filter.c:39
void * b_addr
Definition: buf.h:39
struct fdmi_sd_ut_ctx g_sd_ut
Definition: sd_common.c:41
M0_INTERNAL bool m0_buf_eq(const struct m0_buf *x, const struct m0_buf *y)
Definition: buf.c:90
M0_INTERNAL void m0_fdmi_flt_bool_opnd_fill(struct m0_fdmi_flt_operand *opnd, bool value)
Definition: filter.c:125
M0_LEAVE()
#define M0_FID_INIT(container, key)
Definition: fid.h:84
static char * g_var_str
Definition: sd_send_not.c:63
struct m0_rpc_machine tre_rpc_machine
Definition: sd_common.h:56
static struct m0_conf_fdmi_filter g_conf_filter
Definition: sd_send_not.c:62
static void filterc_send_notif_stop(struct m0_filterc_ctx *ctx)
Definition: sd_send_not.c:93
int const char const void * value
Definition: dir.c:325
void * m0_fop_data(const struct m0_fop *fop)
Definition: fop.c:220
static int refcount
Definition: sd_send_not.c:50
M0_INTERNAL void m0_fdmi_source_free(struct m0_fdmi_src *src)
Definition: source_dock.c:301
struct m0_fdmi_filter ff_filter
Definition: obj.h:731
#define M0_SET0(obj)
Definition: misc.h:64
const char ** ff_endpoints
Definition: obj.h:760
static struct m0_rpc_item * item
Definition: item.c:56
static int send_notif_packet_ready(struct m0_rpc_packet *p)
Definition: sd_send_not.c:237
static struct m0_semaphore g_sem1
Definition: sd_send_not.c:39
Definition: sock.c:887
static struct m0_semaphore g_sem2
Definition: sd_send_not.c:40
struct m0_fdmi_src * fsr_src
Definition: src_rec.h:72
void fdmi_serv_start_ut(const struct m0_filterc_ops *filterc_ops)
Definition: sd_common.c:66
static struct m0_fdmi_src * src_alloc()
Definition: sd_send_not.c:205
struct m0_fdmi_flt_node * m0_fdmi_flt_bool_node_create(bool value)
Definition: filter.c:136
#define M0_ENTRY(...)
Definition: trace.h:170
Definition: buf.h:37
struct m0_buf fr_payload
Definition: fops.h:75
M0_INTERNAL int m0_fdmi_source_alloc(enum m0_fdmi_rec_type_id type_id, struct m0_fdmi_src **src)
Definition: source_dock.c:285
static void test_fs_end(struct m0_fdmi_src_rec *src_rec)
Definition: sd_send_not.c:197
struct m0_conf_root * root
Definition: note.c:50
static void test_fs_put(struct m0_fdmi_src_rec *src_rec)
Definition: sd_send_not.c:175
struct m0_fdmi_flt_node * m0_fdmi_flt_op_node_create(enum m0_fdmi_flt_op_code op_code, struct m0_fdmi_flt_node *left, struct m0_fdmi_flt_node *right)
Definition: filter.c:227
static int test_fs_node_eval(struct m0_fdmi_src_rec *src_rec, struct m0_fdmi_flt_var_node *value_desc, struct m0_fdmi_flt_operand *value)
Definition: sd_send_not.c:143
m0_bcount_t b_nob
Definition: buf.h:38
static int test_fs_encode(struct m0_fdmi_src_rec *src_rec, struct m0_buf *buf)
Definition: sd_send_not.c:156
struct m0_fdmi_flt_node * m0_fdmi_flt_var_node_create(struct m0_buf *data)
Definition: filter.c:209
M0_INTERNAL void m0_fdmi_source_deregister(struct m0_fdmi_src *src)
Definition: source_dock.c:337
static struct test_rpc_env g_rpc_env
Definition: sd_send_not.c:44
void * fsr_data
Definition: src_rec.h:75
static void test_fs_get(struct m0_fdmi_src_rec *src_rec)
Definition: sd_send_not.c:167
struct m0_reqh rc_reqh
Definition: setup.h:312
M0_INTERNAL struct m0_fdmi_src_dock * m0_fdmi_src_dock_get(void)
Definition: fdmi.c:890
struct m0_tl cp_items
Definition: conn_pool.h:47
M0_INTERNAL int m0_fdmi_source_register(struct m0_fdmi_src *src)
Definition: source_dock.c:311
M0_INTERNAL int m0_semaphore_init(struct m0_semaphore *semaphore, unsigned value)
Definition: semaphore.c:38
uint32_t fr_rec_type
Definition: fops.h:72
struct m0_buf ffvn_data
Definition: filter.h:193
#define M0_BUF_INITS(str)
Definition: buf.h:70
static const struct m0_rpc_frm_ops send_notif_frm_ops
Definition: sd_send_not.c:56
int(* fo_packet_ready)(struct m0_rpc_packet *p)
Definition: reqh.h:94
M0_INTERNAL void m0_fdmi_filter_fini(struct m0_fdmi_filter *flt)
Definition: filter.c:113
static char g_fdmi_data[]
Definition: sd_send_not.c:42
m0_fdmi_rec_type_id
Definition: fdmi.h:234
void fdmi_serv_stop_ut(void)
Definition: sd_common.c:121
static void check_fop_content(struct m0_rpc_item *item)
Definition: sd_send_not.c:222
void fdmi_sd_send_notif(void)
Definition: sd_send_not.c:246
static int filterc_send_notif_get_next(struct m0_filterc_iter *iter, struct m0_conf_fdmi_filter **out)
Definition: sd_send_not.c:104
static int dec_ref_count
Definition: sd_send_not.c:48
char * ep_addr_remote
Definition: sd_common.h:61
M0_INTERNAL void m0_fdmi_filter_root_set(struct m0_fdmi_filter *flt, struct m0_fdmi_flt_node *root)
Definition: filter.c:49
M0_INTERNAL bool m0_fid_eq(const struct m0_fid *fid0, const struct m0_fid *fid1)
Definition: fid.c:164
static bool first_filter
Definition: sd_send_not.c:49
struct m0_reqh reqh
Definition: rm_foms.c:48
struct m0_rpc_conn_pool fsf_conn_pool
void prepare_rpc_env(struct test_rpc_env *env, struct m0_reqh *reqh, const struct m0_rpc_frm_ops *frm_ops, bool sender, struct m0_rpc_conn *rpc_conn, struct m0_rpc_session *rpc_session)
Definition: sd_common.c:171
struct m0_reqh_context cc_reqh_ctx
Definition: setup.h:361
Definition: fid.h:38
const struct m0_filterc_ops filterc_send_notif_ops
Definition: sd_send_not.c:79
static int filterc_send_notif_open(struct m0_filterc_ctx *ctx, enum m0_fdmi_rec_type_id rec_type_id, struct m0_filterc_iter *iter)
Definition: sd_send_not.c:97
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
M0_INTERNAL void m0_semaphore_fini(struct m0_semaphore *semaphore)
Definition: semaphore.c:45
uint32_t fmf_count
Definition: fops.h:58
#define M0_FDMI_SOURCE_POST_RECORD(_src_rec_ptr)
Definition: source_dock.h:148
static bool inc_ref_passed
Definition: sd_send_not.c:47
void unprepare_rpc_env(struct test_rpc_env *env)
Definition: sd_common.c:241
static struct m0_fdmi_src_rec g_src_rec
Definition: sd_send_not.c:43
struct m0_fid * fmf_flt_id
Definition: fops.h:61
static void filterc_send_notif_close(struct m0_filterc_iter *iter)
Definition: sd_send_not.c:137
struct m0_uint128 fr_rec_id
Definition: fops.h:69
void fdmi_ut_packet_send_failed(struct m0_rpc_machine *mach, struct m0_rpc_packet *p)
Definition: sd_common.c:154
struct m0_fop * m0_rpc_item_to_fop(const struct m0_rpc_item *item)
Definition: fop.c:346
int(* fco_start)(struct m0_filterc_ctx *ctx, struct m0_reqh *reqh)
Definition: filterc.h:56
M0_INTERNAL void m0_semaphore_down(struct m0_semaphore *semaphore)
Definition: semaphore.c:49
Definition: nucleus.c:42
#define out(...)
Definition: gen.c:41
M0_INTERNAL void m0_semaphore_up(struct m0_semaphore *semaphore)
Definition: semaphore.c:65
static struct m0_rpc_packet * g_sent_rpc_packet
Definition: sd_send_not.c:45
uint64_t u_lo
Definition: types.h:37
static struct m0_semaphore g_sem3
Definition: sd_send_not.c:41
static int filterc_send_notif_start(struct m0_filterc_ctx *ctx, struct m0_reqh *reqh)
Definition: sd_send_not.c:87
void m0_free(void *data)
Definition: memory.c:146
enum m0_fdmi_filter_type_id ff_type
Definition: obj.h:729
struct m0_pdclust_src_addr src
Definition: fd.c:108
int32_t rc
Definition: trigger_fop.h:47
#define M0_UT_ASSERT(a)
Definition: ut.h:46