Motr  M0
fdmi_filter.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_CONF
23 #include "lib/trace.h"
24 
25 #include "conf/objs/common.h"
26 #include "conf/onwire_xc.h" /* m0_confx_fdmi_filter_xc */
27 #include "fdmi/filter.h"
28 #include "motr/magic.h" /* M0_CONF_FDMI_FILTER_MAGIC */
29 #include "lib/memory.h" /* m0_free */
30 #include "lib/buf.h" /* m0_buf_strdup */
31 #include "lib/string.h" /* m0_strings_free */
32 
33 static bool fdmi_filter_check(const void *bob)
34 {
35  const struct m0_conf_fdmi_filter *self = bob;
36  const struct m0_conf_obj *self_obj = &self->ff_obj;
37 
39 
41  return true;
42 }
43 
46 
47 M0_CONF__INVARIANT_DEFINE(fdmi_filter_invariant, m0_conf_fdmi_filter);
48 
49 #define XCAST(xobj) ((struct m0_confx_fdmi_filter *)(&(xobj)->xo_u))
51 
52 static int
54 {
55  char *flt_root_str;
56  struct m0_fdmi_flt_node *flt_root;
57  struct m0_conf_obj *node;
58  struct m0_conf_obj *dix_pver;
59  struct m0_conf_fdmi_filter *d;
60  const struct m0_confx_fdmi_filter *s;
61  int rc;
62 
63  M0_ENTRY();
64 
66  s = XCAST(src);
67 
68  d->ff_type = s->xf_type;
69  d->ff_filter_id = s->xf_filter_id;
70  rc = m0_bufs_to_strings(&d->ff_endpoints, &s->xf_endpoints);
71  if (rc != 0)
72  return M0_ERR(rc);
73  switch (d->ff_type) {
75  rc = m0_conf_obj_find(dest->co_cache, &s->xf_node, &node);
76  if (rc != 0) {
78  return M0_ERR(rc);
79  }
81  flt_root_str = m0_buf_strdup(&s->xf_filter_root);
82  if (flt_root_str == NULL) {
84  return M0_ERR(-ENOMEM);
85  }
86  M0_ALLOC_PTR(flt_root);
87  if (flt_root == NULL) {
88  m0_free(flt_root_str);
90  return M0_ERR(-ENOMEM);
91  }
92  m0_fdmi_filter_root_set(&d->ff_filter, flt_root);
93  rc = m0_fdmi_flt_node_parse(flt_root_str, flt_root);
94  if (rc != 0) {
95  M0_ASSERT(d->ff_filter.ff_root == flt_root);
98  }
99  m0_free(flt_root_str);
100  break;
102  rc = m0_conf_obj_find(dest->co_cache, &s->xf_dix_pver,
103  &dix_pver);
104  if (rc != 0) {
106  return M0_ERR(rc);
107  }
108  d->ff_dix_pver = M0_CONF_CAST(dix_pver, m0_conf_pver);
109  rc = m0_bufs_to_strings(&d->ff_substrings, &s->xf_substrings);
110  if (rc != 0) {
112  return M0_ERR(rc);
113  }
114  break;
115  default:
116  M0_IMPOSSIBLE("unknown FDMI filter type %"PRIu32, d->ff_type);
117  }
118  return M0_RC(rc);
119 }
120 
121 static int
123 {
125  struct m0_confx_fdmi_filter *d = XCAST(dest);
126  char *str = NULL;
127  int rc;
128 
129  M0_ENTRY();
131  d->xf_type = s->ff_type;
132  d->xf_filter_id = s->ff_filter_id;
133  rc = m0_bufs_from_strings(&d->xf_endpoints, s->ff_endpoints);
134  if (rc != 0)
135  return M0_ERR(rc);
136  switch (s->ff_type) {
138  d->xf_node = s->ff_node->cn_obj.co_id;
139  rc = m0_fdmi_flt_node_print(s->ff_filter.ff_root, &str);
140  if (rc != 0) {
142  return M0_ERR(rc);
143  }
145  m0_free(str);
146  break;
148  d->xf_dix_pver = s->ff_dix_pver->pv_obj.co_id;
149  rc = m0_bufs_from_strings(&d->xf_substrings, s->ff_substrings);
150  break;
151  default:
152  M0_IMPOSSIBLE("unknown FDMI filter type %"PRIu32, s->ff_type);
153  }
154  if (rc != 0)
156  return M0_RC(rc);
157 }
158 
159 static bool fdmi_filter_match(const struct m0_conf_obj *cached,
160  const struct m0_confx_obj *flat)
161 {
162  const struct m0_confx_fdmi_filter *xobj = XCAST(flat);
163  const struct m0_conf_fdmi_filter *obj = M0_CONF_CAST(
164  cached, m0_conf_fdmi_filter);
165 
166  M0_PRE(xobj->xf_endpoints.ab_count != 0);
167 
168  return m0_bufs_streq(&xobj->xf_endpoints, obj->ff_endpoints) &&
169  m0_bufs_streq(&xobj->xf_substrings, obj->ff_substrings) &&
170  m0_fid_eq(&xobj->xf_node, &obj->ff_node->cn_obj.co_id) &&
171  m0_fid_eq(&xobj->xf_dix_pver, &obj->ff_dix_pver->pv_obj.co_id);
172 }
173 
174 static void fdmi_filter_delete(struct m0_conf_obj *obj)
175 {
177 
178  m0_strings_free(x->ff_endpoints);
179  m0_strings_free(x->ff_substrings);
180  m0_fdmi_filter_fini(&x->ff_filter);
181  m0_conf_fdmi_filter_bob_fini(x);
182  m0_free(x);
183 }
184 
185 static const struct m0_conf_obj_ops conf_fdmi_filter_ops = {
186  .coo_invariant = fdmi_filter_invariant,
187  .coo_decode = fdmi_filter_decode,
188  .coo_encode = fdmi_filter_encode,
189  .coo_match = fdmi_filter_match,
190  .coo_lookup = conf_obj_lookup_denied,
191  .coo_readdir = NULL,
192  .coo_downlinks = conf_obj_downlinks_none,
193  .coo_delete = fdmi_filter_delete
194 };
195 
196 M0_CONF__CTOR_DEFINE(fdmi_filter_create, m0_conf_fdmi_filter,
198 
200  .cot_ftype = {
201  .ft_id = M0_CONF__FDMI_FILTER_FT_ID,
202  .ft_name = "conf_fdmi_filter"
203  },
204  .cot_create = &fdmi_filter_create,
205  .cot_xt = &m0_confx_fdmi_filter_xc,
206  .cot_branch = "u_fdmi_filter",
207  .cot_xc_init = &m0_xc_m0_confx_fdmi_filter_struct_init,
208  .cot_magic = M0_CONF_FDMI_FILTER_MAGIC
209 };
210 
211 #undef XCAST
212 #undef M0_TRACE_SUBSYSTEM
213 
214 /*
215  * Local variables:
216  * c-indentation-style: "K&R"
217  * c-basic-offset: 8
218  * tab-width: 8
219  * fill-column: 80
220  * scroll-step: 1
221  * End:
222  */
223 /*
224  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
225  */
const struct m0_conf_obj_type * m0_conf_obj_type(const struct m0_conf_obj *obj)
Definition: obj.c:363
M0_BASSERT(offsetof(struct m0_confx_fdmi_filter, xf_header)==0)
M0_CONF__CTOR_DEFINE(fdmi_filter_create, m0_conf_fdmi_filter, &conf_fdmi_filter_ops)
M0_INTERNAL const struct m0_fid ** conf_obj_downlinks_none(const struct m0_conf_obj *obj)
Definition: common.c:226
#define M0_PRE(cond)
struct m0_fid ff_filter_id
Definition: obj.h:730
struct m0_fid xf_dix_pver
Definition: onwire.h:232
#define NULL
Definition: misc.h:38
static bool x
Definition: sm.c:168
uint8_t ft_id
Definition: fid.h:101
const char ** ff_substrings
Definition: obj.h:752
bool(* coo_invariant)(const struct m0_conf_obj *obj)
Definition: obj_ops.h:79
static struct net_test_cmd_node * node
Definition: commands.c:72
uint32_t ab_count
Definition: buf.h:44
M0_INTERNAL int conf_obj_lookup_denied(const struct m0_conf_obj *parent, const struct m0_fid *name, struct m0_conf_obj **out)
Definition: common.c:217
struct m0_fdmi_filter ff_filter
Definition: obj.h:731
static int fdmi_filter_decode(struct m0_conf_obj *dest, const struct m0_confx_obj *src)
Definition: fdmi_filter.c:53
const char ** ff_endpoints
Definition: obj.h:760
static struct foo * obj
Definition: tlist.c:302
return M0_RC(rc)
#define M0_ENTRY(...)
Definition: trace.h:170
static int fdmi_filter_encode(struct m0_confx_obj *dest, const struct m0_conf_obj *src)
Definition: fdmi_filter.c:122
return M0_ERR(-EOPNOTSUPP)
static const struct m0_conf_obj_ops conf_fdmi_filter_ops
Definition: fdmi_filter.c:185
struct m0_fid xf_filter_id
Definition: onwire.h:228
const struct m0_fid_type cot_ftype
Definition: obj.h:314
M0_INTERNAL bool m0_bufs_streq(const struct m0_bufs *bufs, const char **strs)
Definition: buf.c:217
#define m0_free0(pptr)
Definition: memory.h:77
#define M0_ASSERT(cond)
M0_INTERNAL void m0_bufs_free(struct m0_bufs *bufs)
Definition: buf.c:229
M0_INTERNAL int m0_fdmi_flt_node_parse(const char *str, struct m0_fdmi_flt_node *node)
Definition: filter.c:298
static bool fdmi_filter_match(const struct m0_conf_obj *cached, const struct m0_confx_obj *flat)
Definition: fdmi_filter.c:159
struct m0_conf_pver * ff_dix_pver
Definition: obj.h:740
struct m0_confx_header xf_header
Definition: onwire.h:120
M0_CONF__BOB_DEFINE(m0_conf_fdmi_filter, M0_CONF_FDMI_FILTER_MAGIC, fdmi_filter_check)
M0_INTERNAL void confx_encode(struct m0_confx_obj *dest, const struct m0_conf_obj *src)
Definition: common.c:110
#define M0_BUF_INITS(str)
Definition: buf.h:70
M0_INTERNAL int m0_conf_obj_find(struct m0_conf_cache *cache, const struct m0_fid *id, struct m0_conf_obj **out)
Definition: obj_ops.c:136
M0_INTERNAL void m0_fdmi_filter_fini(struct m0_fdmi_filter *flt)
Definition: filter.c:113
#define M0_CONF_CAST(ptr, type)
Definition: obj.h:780
M0_INTERNAL int m0_buf_copy(struct m0_buf *dest, const struct m0_buf *src)
Definition: buf.c:104
#define XCAST(xobj)
Definition: fdmi_filter.c:49
M0_INTERNAL int m0_fdmi_flt_node_print(struct m0_fdmi_flt_node *node, char **out)
Definition: filter.c:252
M0_INTERNAL void m0_fdmi_filter_root_set(struct m0_fdmi_filter *flt, struct m0_fdmi_flt_node *root)
Definition: filter.c:49
struct m0_fdmi_flt_node * ff_root
Definition: filter.h:120
M0_INTERNAL bool m0_fid_eq(const struct m0_fid *fid0, const struct m0_fid *fid1)
Definition: fid.c:164
static bool fdmi_filter_check(const void *bob)
Definition: fdmi_filter.c:33
#define PRIu32
Definition: types.h:66
struct m0_fid xf_node
Definition: onwire.h:231
M0_INTERNAL char * m0_buf_strdup(const struct m0_buf *buf)
Definition: buf.c:140
M0_INTERNAL void m0_strings_free(const char **arr)
Definition: string.c:45
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
M0_INTERNAL int m0_bufs_to_strings(const char ***dest, const struct m0_bufs *src)
Definition: buf.c:188
static void fdmi_filter_delete(struct m0_conf_obj *obj)
Definition: fdmi_filter.c:174
struct m0_bufs xf_endpoints
Definition: onwire.h:234
struct m0_buf xf_filter_root
Definition: onwire.h:230
struct m0_bufs xf_substrings
Definition: onwire.h:233
void m0_free(void *data)
Definition: memory.c:146
static struct m0_addb2_source * s
Definition: consumer.c:39
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
M0_CONF__INVARIANT_DEFINE(fdmi_filter_invariant, m0_conf_fdmi_filter)
M0_INTERNAL int m0_bufs_from_strings(struct m0_bufs *dest, const char **src)
Definition: buf.c:157
#define offsetof(typ, memb)
Definition: misc.h:29
struct m0_conf_node * ff_node
Definition: obj.h:733
const struct m0_conf_obj_type M0_CONF_FDMI_FILTER_TYPE
Definition: fdmi_filter.c:199
#define M0_IMPOSSIBLE(fmt,...)