Motr  M0
dispatcher.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2016-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 
32 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_HA
33 #include "lib/trace.h"
34 
35 #include "ha/dispatcher.h"
36 
37 #include "lib/memory.h" /* M0_ALLOC_PTR */
38 #include "lib/errno.h" /* ENOMEM */
39 #include "ha/note.h" /* m0_ha_note_handler_init */
40 #include "ha/failvec.h" /* m0_ha_fvec_handler_init */
41 #include "motr/keepalive.h" /* m0_ha_keepalive_handler_init */
42 #include "module/instance.h" /* m0_get */
43 
44 M0_TL_DESCR_DEFINE(ha_dispatcher_handlers, "m0_ha_dispatcher::hds_handlers",
45  static, struct m0_ha_handler, hh_link, hh_magic,
46  21, 22);
47 M0_TL_DEFINE(ha_dispatcher_handlers, static, struct m0_ha_handler);
48 
58 };
59 
60 static int ha_dispatcher_level_enter(struct m0_module *module)
61 {
62  enum ha_dispatcher_level level = module->m_cur + 1;
63  struct m0_ha_dispatcher *hd;
64 
65  hd = container_of(module, struct m0_ha_dispatcher, hds_module);
66  M0_ENTRY("hd=%p level=%d", hd, level);
67  switch (level) {
69  ha_dispatcher_handlers_tlist_init(&hd->hds_handlers);
70  return M0_RC(0);
72  if (!hd->hds_cfg.hdc_enable_note)
73  return M0_RC(0);
75  return hd->hds_note_handler == NULL ? M0_ERR(-ENOMEM) :
76  M0_RC(0);
78  if (!hd->hds_cfg.hdc_enable_note)
79  return M0_RC(0);
81  hd));
83  if (!hd->hds_cfg.hdc_enable_fvec)
84  return M0_RC(0);
86  return hd->hds_fvec_handler == NULL ? M0_ERR(-ENOMEM) :
87  M0_RC(0);
89  if (!hd->hds_cfg.hdc_enable_fvec)
90  return M0_RC(0);
92  hd));
95  return M0_RC(0);
97  return hd->hds_keepalive_handler == NULL ? M0_ERR(-ENOMEM) :
98  M0_RC(0);
100  if (!hd->hds_cfg.hdc_enable_keepalive)
101  return M0_RC(0);
103  hd->hds_keepalive_handler, hd));
105  M0_IMPOSSIBLE("can't be here");
106  }
107  return M0_ERR(-ENOSYS);
108 }
109 
110 static void ha_dispatcher_level_leave(struct m0_module *module)
111 {
112  enum ha_dispatcher_level level = module->m_cur;
113  struct m0_ha_dispatcher *hd;
114 
115  hd = container_of(module, struct m0_ha_dispatcher, hds_module);
116  M0_ENTRY("hd=%p level=%d", hd, level);
117  switch (level) {
119  ha_dispatcher_handlers_tlist_fini(&hd->hds_handlers);
120  break;
122  if (hd->hds_cfg.hdc_enable_note)
124  break;
126  if (hd->hds_cfg.hdc_enable_note)
128  break;
130  if (hd->hds_cfg.hdc_enable_fvec)
132  break;
134  if (hd->hds_cfg.hdc_enable_fvec)
136  break;
140  break;
144  break;
146  M0_IMPOSSIBLE("can't be here");
147  }
148  M0_LEAVE();
149 }
150 
151 static const struct m0_modlev ha_dispatcher_levels[] = {
153  .ml_name = "HA_DISPATCHER_LEVEL_TLIST",
154  .ml_enter = ha_dispatcher_level_enter,
155  .ml_leave = ha_dispatcher_level_leave,
156  },
158  .ml_name = "HA_DISPATCHER_LEVEL_NOTE_HANDLER_ALLOC",
159  .ml_enter = ha_dispatcher_level_enter,
160  .ml_leave = ha_dispatcher_level_leave,
161  },
163  .ml_name = "HA_DISPATCHER_LEVEL_NOTE_HANDLER_INIT",
164  .ml_enter = ha_dispatcher_level_enter,
165  .ml_leave = ha_dispatcher_level_leave,
166  },
168  .ml_name = "HA_DISPATCHER_LEVEL_FVEC_HANDLER_ALLOC",
169  .ml_enter = ha_dispatcher_level_enter,
170  .ml_leave = ha_dispatcher_level_leave,
171  },
173  .ml_name = "HA_DISPATCHER_LEVEL_FVEC_HANDLER_INIT",
174  .ml_enter = ha_dispatcher_level_enter,
175  .ml_leave = ha_dispatcher_level_leave,
176  },
178  .ml_name = "HA_DISPATCHER_LEVEL_KEEPALIVE_HANDLER_ALLOC",
179  .ml_enter = ha_dispatcher_level_enter,
180  .ml_leave = ha_dispatcher_level_leave,
181  },
183  .ml_name = "HA_DISPATCHER_LEVEL_KEEPALIVE_HANDLER_INIT",
184  .ml_enter = ha_dispatcher_level_enter,
185  .ml_leave = ha_dispatcher_level_leave,
186  },
188  .ml_name = "HA_DISPATCHER_LEVEL_READY",
189  },
190 };
191 
192 M0_INTERNAL int m0_ha_dispatcher_init(struct m0_ha_dispatcher *hd,
193  struct m0_ha_dispatcher_cfg *hd_cfg)
194 {
195  int rc;
196 
197  M0_ENTRY("hd=%p", hd);
198  M0_PRE(M0_IS0(hd));
199 
200  hd->hds_cfg = *hd_cfg;
201  m0_module_setup(&hd->hds_module, "m0_ha_dispatcher",
205  if (rc != 0) {
207  return M0_ERR(rc);
208  }
209  return M0_RC(0);
210 }
211 
212 M0_INTERNAL void m0_ha_dispatcher_fini(struct m0_ha_dispatcher *hd)
213 {
214  M0_ENTRY("hd=%p", hd);
216  M0_LEAVE();
217 }
218 
219 M0_INTERNAL void m0_ha_dispatcher_attach(struct m0_ha_dispatcher *hd,
220  struct m0_ha_handler *hh)
221 {
222  ha_dispatcher_handlers_tlink_init_at_tail(hh, &hd->hds_handlers);
223 }
224 
225 M0_INTERNAL void m0_ha_dispatcher_detach(struct m0_ha_dispatcher *hd,
226  struct m0_ha_handler *hh)
227 {
228  ha_dispatcher_handlers_tlink_del_fini(hh);
229 }
230 
231 M0_INTERNAL void m0_ha_dispatcher_handle(struct m0_ha_dispatcher *hd,
232  struct m0_ha *ha,
233  struct m0_ha_link *hl,
234  struct m0_ha_msg *msg,
235  uint64_t tag)
236 {
237  struct m0_ha_handler *hh;
238 
239  m0_tl_for(ha_dispatcher_handlers, &hd->hds_handlers, hh) {
240  hh->hh_msg_received_cb(hh, ha, hl, msg, tag, hh->hh_data);
241  } m0_tl_endfor;
242 }
243 
244 #undef M0_TRACE_SUBSYSTEM
245 
248 /*
249  * Local variables:
250  * c-indentation-style: "K&R"
251  * c-basic-offset: 8
252  * tab-width: 8
253  * fill-column: 80
254  * scroll-step: 1
255  * End:
256  */
257 /*
258  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
259  */
struct m0_tl hds_handlers
Definition: dispatcher.h:60
static int ha_dispatcher_level_enter(struct m0_module *module)
Definition: dispatcher.c:60
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
static uint64_t tag(uint8_t code, uint64_t id)
Definition: addb2.c:1047
M0_LEAVE()
enum m0_trace_level level
Definition: trace.c:111
ha_dispatcher_level
Definition: dispatcher.c:49
void(* hh_msg_received_cb)(struct m0_ha_handler *hh, struct m0_ha *ha, struct m0_ha_link *hl, struct m0_ha_msg *msg, uint64_t tag, void *data)
Definition: dispatcher.h:71
M0_INTERNAL void m0_ha_note_handler_fini(struct m0_ha_note_handler *hnh)
Definition: note.c:364
M0_INTERNAL int m0_ha_keepalive_handler_init(struct m0_ha_keepalive_handler *ka, struct m0_ha_dispatcher *hd)
Definition: keepalive.c:80
const char * ml_name
Definition: module.h:114
M0_INTERNAL struct m0 * m0_get(void)
Definition: instance.c:41
#define container_of(ptr, type, member)
Definition: misc.h:33
M0_TL_DESCR_DEFINE(ha_dispatcher_handlers, "m0_ha_dispatcher::hds_handlers", static, struct m0_ha_handler, hh_link, hh_magic, 21, 22)
M0_INTERNAL void m0_ha_dispatcher_handle(struct m0_ha_dispatcher *hd, struct m0_ha *ha, struct m0_ha_link *hl, struct m0_ha_msg *msg, uint64_t tag)
Definition: dispatcher.c:231
#define m0_tl_endfor
Definition: tlist.h:700
M0_INTERNAL int m0_ha_note_handler_init(struct m0_ha_note_handler *hnh, struct m0_ha_dispatcher *hd)
Definition: note.c:345
return M0_RC(rc)
#define M0_ENTRY(...)
Definition: trace.h:170
return M0_ERR(-EOPNOTSUPP)
struct m0_ha_keepalive_handler * hds_keepalive_handler
Definition: dispatcher.h:64
struct m0_module hds_module
Definition: dispatcher.h:53
M0_INTERNAL void m0_module_setup(struct m0_module *module, const char *name, const struct m0_modlev *level, int level_nr, struct m0 *instance)
Definition: module.c:193
struct m0_ha_dispatcher_cfg hds_cfg
Definition: dispatcher.h:52
Definition: msg.h:115
M0_INTERNAL void m0_ha_dispatcher_fini(struct m0_ha_dispatcher *hd)
Definition: dispatcher.c:212
M0_INTERNAL void m0_ha_dispatcher_attach(struct m0_ha_dispatcher *hd, struct m0_ha_handler *hh)
Definition: dispatcher.c:219
static void ha_dispatcher_level_leave(struct m0_module *module)
Definition: dispatcher.c:110
Definition: ha.h:289
M0_INTERNAL int m0_ha_dispatcher_init(struct m0_ha_dispatcher *hd, struct m0_ha_dispatcher_cfg *hd_cfg)
Definition: dispatcher.c:192
static const struct m0_modlev ha_dispatcher_levels[]
Definition: dispatcher.c:151
M0_INTERNAL void m0_ha_dispatcher_detach(struct m0_ha_dispatcher *hd, struct m0_ha_handler *hh)
Definition: dispatcher.c:225
#define M0_IS0(obj)
Definition: misc.h:70
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
M0_TL_DEFINE(ha_dispatcher_handlers, static, struct m0_ha_handler)
int m_cur
Definition: module.h:160
struct m0_ha_fvec_handler * hds_fvec_handler
Definition: dispatcher.h:63
M0_INTERNAL int m0_ha_fvec_handler_init(struct m0_ha_fvec_handler *hfh, struct m0_ha_dispatcher *hd)
Definition: failvec.c:108
#define m0_tl_for(name, head, obj)
Definition: tlist.h:695
void m0_free(void *data)
Definition: memory.c:146
struct m0_ha_note_handler * hds_note_handler
Definition: dispatcher.h:62
M0_INTERNAL void m0_ha_keepalive_handler_fini(struct m0_ha_keepalive_handler *ka)
Definition: keepalive.c:96
void * hh_data
Definition: dispatcher.h:70
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
M0_INTERNAL void m0_ha_fvec_handler_fini(struct m0_ha_fvec_handler *hfh)
Definition: failvec.c:126
M0_INTERNAL void m0_module_fini(struct m0_module *module, int level)
Definition: module.c:142
M0_INTERNAL int m0_module_init(struct m0_module *module, int level)
Definition: module.c:131
#define M0_IMPOSSIBLE(fmt,...)