Motr  M0
service.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-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_service_xc */
27 #include "motr/magic.h" /* M0_CONF_SERVICE_MAGIC */
28 #include "lib/string.h" /* m0_strings_free */
29 
30 static bool service_check(const void *bob)
31 {
32  const struct m0_conf_service *self = bob;
33  const struct m0_conf_obj *self_obj = &self->cs_obj;
34 
36 
37  return m0_conf_obj_is_stub(self_obj) ||
38  _0C(m0_conf_service_type_is_valid(self->cs_type));
39 }
40 
42 M0_CONF__INVARIANT_DEFINE(service_invariant, m0_conf_service);
43 
44 #define XCAST(xobj) ((struct m0_confx_service *)(&(xobj)->xo_u))
46 
47 static int
49 {
50  int rc;
52  const struct m0_confx_service *s = XCAST(src);
53 
54  d->cs_type = s->xs_type;
55  rc = m0_bufs_to_strings(&d->cs_endpoints, &s->xs_endpoints);
56  if (rc != 0)
57  return M0_ERR(rc);
58  rc = m0_bufs_to_strings(&d->cs_params, &s->xs_params);
59  if (rc != 0) {
61  return M0_RC(rc);
62  }
63  rc = m0_conf_dir_new(dest, &M0_CONF_SERVICE_SDEVS_FID,
64  &M0_CONF_SDEV_TYPE, &s->xs_sdevs, &d->cs_sdevs);
65  if (rc != 0) {
68  }
69  return M0_RC(rc);
70 }
71 
72 static int
74 {
75  int rc;
77  struct m0_confx_service *d = XCAST(dest);
78 
80  d->xs_type = s->cs_type;
81 
82  rc = m0_bufs_from_strings(&d->xs_endpoints, s->cs_endpoints);
83  if (rc != 0)
84  return M0_ERR(-ENOMEM);
85 
86  rc = m0_bufs_from_strings(&d->xs_params, s->cs_params);
87  if (rc != 0) {
89  return M0_ERR(-ENOMEM);
90  }
91 
92  rc = arrfid_from_dir(&d->xs_sdevs, s->cs_sdevs);
93  if (rc != 0) {
96  }
97  return M0_RC(rc);
98 }
99 
100 static bool
101 service_match(const struct m0_conf_obj *cached, const struct m0_confx_obj *flat)
102 {
103  const struct m0_confx_service *xobj = XCAST(flat);
104  const struct m0_conf_service *obj = M0_CONF_CAST(cached,
106  M0_PRE(xobj->xs_endpoints.ab_count != 0);
107 
108  return obj->cs_type == xobj->xs_type &&
109  m0_bufs_streq(&xobj->xs_endpoints, obj->cs_endpoints) &&
110  m0_conf_dir_elems_match(obj->cs_sdevs, &xobj->xs_sdevs);
111 }
112 
113 static int service_lookup(const struct m0_conf_obj *parent,
114  const struct m0_fid *name, struct m0_conf_obj **out)
115 {
116  struct m0_conf_service *svc = M0_CONF_CAST(parent, m0_conf_service);
117  const struct conf_dir_relation dirs[] = {
118  { svc->cs_sdevs, &M0_CONF_SERVICE_SDEVS_FID }
119  };
120 
121  M0_PRE(parent->co_status == M0_CS_READY);
122  return M0_RC(conf_dirs_lookup(out, name, dirs, ARRAY_SIZE(dirs)));
123 }
124 
125 static const struct m0_fid **service_downlinks(const struct m0_conf_obj *obj)
126 {
127  static const struct m0_fid *rels[] = { &M0_CONF_SERVICE_SDEVS_FID,
128  NULL };
130  return rels;
131 }
132 
133 static void service_delete(struct m0_conf_obj *obj)
134 {
136 
137  m0_strings_free(x->cs_endpoints);
138  m0_strings_free(x->cs_params);
139  m0_conf_service_bob_fini(x);
140  m0_free(x);
141 }
142 
143 static const struct m0_conf_obj_ops service_ops = {
144  .coo_invariant = service_invariant,
145  .coo_decode = service_decode,
146  .coo_encode = service_encode,
147  .coo_match = service_match,
148  .coo_lookup = service_lookup,
149  .coo_readdir = NULL,
150  .coo_downlinks = service_downlinks,
151  .coo_delete = service_delete
152 };
153 
155 
157  .cot_ftype = {
158  .ft_id = M0_CONF__SERVICE_FT_ID,
159  .ft_name = "conf_service"
160  },
161  .cot_create = &service_create,
162  .cot_xt = &m0_confx_service_xc,
163  .cot_branch = "u_service",
164  .cot_xc_init = &m0_xc_m0_confx_service_struct_init,
165  .cot_magic = M0_CONF_SERVICE_MAGIC
166 };
167 
168 M0_INTERNAL const char *
170 {
171  static const char *names[] = {
172 #define X_CST(name) [name] = #name,
174 #undef X_CST
175  };
176 
178  return names[type];
179 }
180 
181 #undef XCAST
182 #undef M0_TRACE_SUBSYSTEM
183 
184 /*
185  * Local variables:
186  * c-indentation-style: "K&R"
187  * c-basic-offset: 8
188  * tab-width: 8
189  * fill-column: 80
190  * scroll-step: 1
191  * End:
192  */
193 /*
194  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
195  */
const struct m0_conf_obj_type * m0_conf_obj_type(const struct m0_conf_obj *obj)
Definition: obj.c:363
static int service_decode(struct m0_conf_obj *dest, const struct m0_confx_obj *src)
Definition: service.c:48
M0_INTERNAL int conf_dirs_lookup(struct m0_conf_obj **out, const struct m0_fid *name, const struct conf_dir_relation *rels, size_t nr_rels)
Definition: common.c:64
#define M0_PRE(cond)
struct m0_conf_dir * cs_sdevs
Definition: obj.h:596
#define NULL
Definition: misc.h:38
static bool service_match(const struct m0_conf_obj *cached, const struct m0_confx_obj *flat)
Definition: service.c:101
m0_conf_service_type
Definition: schema.h:194
M0_CONF__INVARIANT_DEFINE(service_invariant, m0_conf_service)
static bool x
Definition: sm.c:168
M0_CONF__BOB_DEFINE(m0_conf_service, M0_CONF_SERVICE_MAGIC, service_check)
uint8_t ft_id
Definition: fid.h:101
const struct m0_conf_obj_type M0_CONF_SERVICE_TYPE
Definition: service.c:156
bool(* coo_invariant)(const struct m0_conf_obj *obj)
Definition: obj_ops.h:79
uint32_t ab_count
Definition: buf.h:44
const struct m0_conf_obj_type M0_CONF_SDEV_TYPE
Definition: sdev.c:122
struct m0_bufs xs_endpoints
Definition: onwire.h:165
static const struct m0_fid ** service_downlinks(const struct m0_conf_obj *obj)
Definition: service.c:125
M0_INTERNAL bool m0_conf_obj_is_stub(const struct m0_conf_obj *obj)
Definition: obj.c:302
static struct foo * obj
Definition: tlist.c:302
static int service_encode(struct m0_confx_obj *dest, const struct m0_conf_obj *src)
Definition: service.c:73
return M0_RC(rc)
M0_INTERNAL int m0_conf_dir_new(struct m0_conf_obj *parent, const struct m0_fid *relfid, const struct m0_conf_obj_type *children_type, const struct m0_fid_arr *children_ids, struct m0_conf_dir **out)
Definition: dir.c:159
return M0_ERR(-EOPNOTSUPP)
const char ** cs_endpoints
Definition: obj.h:605
static bool m0_conf_service_type_is_valid(enum m0_conf_service_type t)
Definition: schema.h:204
const char * name
Definition: trace.c:110
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_CONF_SERVICE_TYPES
Definition: schema.h:169
M0_INTERNAL const char * m0_conf_service_type2str(enum m0_conf_service_type type)
Definition: service.c:169
M0_INTERNAL void m0_bufs_free(struct m0_bufs *bufs)
Definition: buf.c:229
M0_BASSERT(M0_CAS_GET_FOP_OPCODE==CO_GET+M0_CAS_GET_FOP_OPCODE)
static int service_lookup(const struct m0_conf_obj *parent, const struct m0_fid *name, struct m0_conf_obj **out)
Definition: service.c:113
M0_INTERNAL void confx_encode(struct m0_confx_obj *dest, const struct m0_conf_obj *src)
Definition: common.c:110
static void service_delete(struct m0_conf_obj *obj)
Definition: service.c:133
struct m0_fid_arr xs_sdevs
Definition: onwire.h:167
enum m0_conf_service_type cs_type
Definition: obj.h:598
#define M0_CONF_CAST(ptr, type)
Definition: obj.h:780
M0_INTERNAL int arrfid_from_dir(struct m0_fid_arr *dest, const struct m0_conf_dir *dir)
Definition: common.c:82
const char ** cs_params
Definition: obj.h:611
M0_INTERNAL void m0_strings_free(const char **arr)
Definition: string.c:45
M0_CONF__CTOR_DEFINE(service_create, m0_conf_service, &service_ops)
struct m0_bufs xs_params
Definition: onwire.h:166
#define XCAST(xobj)
Definition: service.c:44
Definition: fid.h:38
static struct m0_net_test_service svc
Definition: service.c:34
M0_INTERNAL int m0_bufs_to_strings(const char ***dest, const struct m0_bufs *src)
Definition: buf.c:188
#define _0C(exp)
Definition: assert.h:311
enum m0_conf_status co_status
Definition: obj.h:210
static const struct m0_conf_obj_ops service_ops
Definition: service.c:143
#define out(...)
Definition: gen.c:41
int type
Definition: dir.c:1031
void m0_free(void *data)
Definition: memory.c:146
static bool service_check(const void *bob)
Definition: service.c:30
static struct m0_addb2_source * s
Definition: consumer.c:39
M0_INTERNAL bool m0_conf_dir_elems_match(const struct m0_conf_dir *dir, const struct m0_fid_arr *fids)
Definition: dir.c:63
struct m0_confx_header xs_header
Definition: onwire.h:120
struct m0_pdclust_src_addr src
Definition: fd.c:108
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL int m0_bufs_from_strings(struct m0_bufs *dest, const char **src)
Definition: buf.c:157
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define offsetof(typ, memb)
Definition: misc.h:29