| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include <corosync/cpg.h> |
| 36 |
#include <corosync/cfg.h> |
| 37 |
|
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/logger.h" |
| 40 |
#include "asterisk/poll-compat.h" |
| 41 |
#include "asterisk/config.h" |
| 42 |
#include "asterisk/event.h" |
| 43 |
#include "asterisk/cli.h" |
| 44 |
#include "asterisk/devicestate.h" |
| 45 |
#include "asterisk/mwi.h" |
| 46 |
#include "asterisk/stasis.h" |
| 47 |
#include "asterisk/stasis_message_router.h" |
| 48 |
#include "asterisk/stasis_system.h" |
| 49 |
#include "asterisk/taskprocessor.h" |
| 50 |
|
| 51 |
AST_RWLOCK_DEFINE_STATIC(event_types_lock); |
| 52 |
AST_RWLOCK_DEFINE_STATIC(init_cpg_lock); |
| 53 |
|
| 54 |
|
| 55 |
#define COROSYNC_POLL_TIMEOUT (10 * 1000) |
| 56 |
|
| 57 |
static void publish_mwi_to_stasis(struct ast_event *event); |
| 58 |
static void publish_device_state_to_stasis(struct ast_event *event); |
| 59 |
static void publish_cluster_discovery_to_stasis(struct ast_event *event); |
| 60 |
|
| 61 |
|
| 62 |
static int corosync_node_joined = 0; |
| 63 |
|
| 64 |
|
| 65 |
static struct ao2_container *nodes; |
| 66 |
|
| 67 |
|
| 68 |
static struct stasis_topic *corosync_aggregate_topic; |
| 69 |
|
| 70 |
|
| 71 |
static struct stasis_message_router *stasis_router; |
| 72 |
|
| 73 |
|
| 74 |
static struct stasis_topic *corosync_topic(void) |
| 75 |
{ |
| 76 |
return corosync_aggregate_topic; |
| 77 |
} |
| 78 |
|
| 79 |
struct corosync_node { |
| 80 |
|
| 81 |
int id; |
| 82 |
|
| 83 |
struct ast_eid eid; |
| 84 |
|
| 85 |
struct ast_sockaddr addr; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
#define COROSYNC_IPC_BUFFER_SIZE (8192 * 128) |
| 90 |
|
| 91 |
|
| 92 |
#define corosync_pthread_create_background(a, b, c, d) \ |
| 93 |
ast_pthread_create_stack(a, b, c, d, \ |
| 94 |
(AST_BACKGROUND_STACKSIZE + (3 * COROSYNC_IPC_BUFFER_SIZE)), \ |
| 95 |
__FILE__, __FUNCTION__, __LINE__, #c) |
| 96 |
|
| 97 |
static struct corosync_node *corosync_node_alloc(struct ast_event *event) |
| 98 |
{ |
| 99 |
struct corosync_node *node; |
| 100 |
|
| 101 |
node = ao2_alloc_options(sizeof(*node), NULL, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 102 |
if (!node) { |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
memcpy(&node->eid, (struct ast_eid *)ast_event_get_ie_raw(event, AST_EVENT_IE_EID), sizeof(node->eid)); |
| 107 |
node->id = ast_event_get_ie_uint(event, AST_EVENT_IE_NODE_ID); |
| 108 |
ast_sockaddr_parse(&node->addr, ast_event_get_ie_str(event, AST_EVENT_IE_LOCAL_ADDR), PARSE_PORT_IGNORE); |
| 109 |
|
| 110 |
return node; |
| 111 |
} |
| 112 |
|
| 113 |
static int corosync_node_hash_fn(const void *obj, const int flags) |
| 114 |
{ |
| 115 |
const struct corosync_node *node; |
| 116 |
const int *id; |
| 117 |
|
| 118 |
switch (flags & OBJ_SEARCH_MASK) { |
| 119 |
case OBJ_SEARCH_KEY: |
| 120 |
id = obj; |
| 121 |
break; |
| 122 |
case OBJ_SEARCH_OBJECT: |
| 123 |
node = obj; |
| 124 |
id = &node->id; |
| 125 |
break; |
| 126 |
default: |
| 127 |
ast_assert(0); |
| 128 |
return 0; |
| 129 |
} |
| 130 |
return *id; |
| 131 |
} |
| 132 |
|
| 133 |
static int corosync_node_cmp_fn(void *obj, void *arg, int flags) |
| 134 |
{ |
| 135 |
struct corosync_node *left = obj; |
| 136 |
struct corosync_node *right = arg; |
| 137 |
const int *id = arg; |
| 138 |
int cmp; |
| 139 |
|
| 140 |
switch (flags & OBJ_SEARCH_MASK) { |
| 141 |
case OBJ_SEARCH_OBJECT: |
| 142 |
id = &right->id; |
| 143 |
|
| 144 |
case OBJ_SEARCH_KEY: |
| 145 |
cmp = (left->id == *id); |
| 146 |
break; |
| 147 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 148 |
cmp = (left->id == right->id); |
| 149 |
break; |
| 150 |
default: |
| 151 |
|
| 152 |
ast_assert(0); |
| 153 |
cmp = 1; |
| 154 |
break; |
| 155 |
} |
| 156 |
return cmp ? CMP_MATCH : 0; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
struct corosync_ping_payload { |
| 162 |
|
| 163 |
struct ast_event *event; |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
static void corosync_ping_payload_dtor(void *obj) |
| 168 |
{ |
| 169 |
struct corosync_ping_payload *payload = obj; |
| 170 |
|
| 171 |
ast_free(payload->event); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
static struct ast_event *corosync_ping_to_event(struct stasis_message *message) |
| 176 |
{ |
| 177 |
struct corosync_ping_payload *payload; |
| 178 |
struct ast_event *event; |
| 179 |
struct ast_eid *event_eid; |
| 180 |
|
| 181 |
if (!message) { |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
payload = stasis_message_data(message); |
| 186 |
|
| 187 |
if (!payload->event) { |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
|
| 191 |
event_eid = (struct ast_eid *)ast_event_get_ie_raw(payload->event, AST_EVENT_IE_EID); |
| 192 |
|
| 193 |
event = ast_event_new(AST_EVENT_PING, |
| 194 |
AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW, event_eid, sizeof(*event_eid), |
| 195 |
AST_EVENT_IE_END); |
| 196 |
|
| 197 |
return event; |
| 198 |
} |
| 199 |
|
| 200 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(corosync_ping_message_type, |
| 201 |
.to_event = corosync_ping_to_event, ); |
| 202 |
|
| 203 |
|
| 204 |
static void publish_corosync_ping_to_stasis(struct ast_event *event) |
| 205 |
{ |
| 206 |
struct corosync_ping_payload *payload; |
| 207 |
struct stasis_message *message; |
| 208 |
struct ast_eid *event_eid; |
| 209 |
|
| 210 |
ast_assert(ast_event_get_type(event) == AST_EVENT_PING); |
| 211 |
ast_assert(event != NULL); |
| 212 |
|
| 213 |
if (!corosync_ping_message_type()) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
payload = ao2_t_alloc(sizeof(*payload), corosync_ping_payload_dtor, "Create ping payload"); |
| 218 |
if (!payload) { |
| 219 |
return; |
| 220 |
} |
| 221 |
event_eid = (struct ast_eid *)ast_event_get_ie_raw(event, AST_EVENT_IE_EID); |
| 222 |
payload->event = ast_event_new(AST_EVENT_PING, |
| 223 |
AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW, event_eid, sizeof(*event_eid), |
| 224 |
AST_EVENT_IE_END); |
| 225 |
|
| 226 |
message = stasis_message_create(corosync_ping_message_type(), payload); |
| 227 |
if (!message) { |
| 228 |
ao2_t_ref(payload, -1, "Destroy payload on off nominal"); |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
stasis_publish(corosync_topic(), message); |
| 233 |
|
| 234 |
ao2_t_ref(payload, -1, "Hand ref to stasis"); |
| 235 |
ao2_t_ref(message, -1, "Hand ref to stasis"); |
| 236 |
} |
| 237 |
|
| 238 |
static struct { |
| 239 |
const char *name; |
| 240 |
struct stasis_forward *sub; |
| 241 |
unsigned char publish; |
| 242 |
unsigned char publish_default; |
| 243 |
unsigned char subscribe; |
| 244 |
unsigned char subscribe_default; |
| 245 |
struct stasis_topic *(* topic_fn)(void); |
| 246 |
struct stasis_cache *(* cache_fn)(void); |
| 247 |
struct stasis_message_type *(* message_type_fn)(void); |
| 248 |
void (* publish_to_stasis)(struct ast_event *); |
| 249 |
} event_types[] = { |
| 250 |
[AST_EVENT_MWI] = { .name = "mwi", |
| 251 |
.topic_fn = ast_mwi_topic_all, |
| 252 |
.cache_fn = ast_mwi_state_cache, |
| 253 |
.message_type_fn = ast_mwi_state_type, |
| 254 |
.publish_to_stasis = publish_mwi_to_stasis, }, |
| 255 |
[AST_EVENT_DEVICE_STATE_CHANGE] = { .name = "device_state", |
| 256 |
.topic_fn = ast_device_state_topic_all, |
| 257 |
.cache_fn = ast_device_state_cache, |
| 258 |
.message_type_fn = ast_device_state_message_type, |
| 259 |
.publish_to_stasis = publish_device_state_to_stasis, }, |
| 260 |
[AST_EVENT_PING] = { .name = "ping", |
| 261 |
.publish_default = 1, |
| 262 |
.subscribe_default = 1, |
| 263 |
.topic_fn = corosync_topic, |
| 264 |
.message_type_fn = corosync_ping_message_type, |
| 265 |
.publish_to_stasis = publish_corosync_ping_to_stasis, }, |
| 266 |
[AST_EVENT_CLUSTER_DISCOVERY] = { .name = "cluster_discovery", |
| 267 |
.publish_default = 1, |
| 268 |
.subscribe_default = 1, |
| 269 |
.topic_fn = ast_system_topic, |
| 270 |
.message_type_fn = ast_cluster_discovery_type, |
| 271 |
.publish_to_stasis = publish_cluster_discovery_to_stasis, }, |
| 272 |
}; |
| 273 |
|
| 274 |
static struct { |
| 275 |
pthread_t id; |
| 276 |
int alert_pipe[2]; |
| 277 |
unsigned int stop:1; |
| 278 |
} dispatch_thread = { |
| 279 |
.id = AST_PTHREADT_NULL, |
| 280 |
.alert_pipe = { -1, -1 }, |
| 281 |
}; |
| 282 |
|
| 283 |
static cpg_handle_t cpg_handle; |
| 284 |
static corosync_cfg_handle_t cfg_handle; |
| 285 |
|
| 286 |
#ifdef HAVE_COROSYNC_CFG_STATE_TRACK |
| 287 |
static void cfg_state_track_cb( |
| 288 |
corosync_cfg_state_notification_buffer_t *notification_buffer, |
| 289 |
cs_error_t error); |
| 290 |
#endif |
| 291 |
|
| 292 |
static void cfg_shutdown_cb(corosync_cfg_handle_t cfg_handle, |
| 293 |
corosync_cfg_shutdown_flags_t flags); |
| 294 |
|
| 295 |
static corosync_cfg_callbacks_t cfg_callbacks = { |
| 296 |
#ifdef HAVE_COROSYNC_CFG_STATE_TRACK |
| 297 |
.corosync_cfg_state_track_callback = cfg_state_track_cb, |
| 298 |
#endif |
| 299 |
.corosync_cfg_shutdown_callback = cfg_shutdown_cb, |
| 300 |
}; |
| 301 |
|
| 302 |
|
| 303 |
static void publish_cluster_discovery_to_stasis_full(struct corosync_node *node, int joined) |
| 304 |
{ |
| 305 |
struct ast_json *json; |
| 306 |
struct ast_json_payload *payload; |
| 307 |
struct stasis_message *message; |
| 308 |
char eid[18]; |
| 309 |
const char *addr; |
| 310 |
|
| 311 |
ast_eid_to_str(eid, sizeof(eid), &node->eid); |
| 312 |
addr = ast_sockaddr_stringify_addr(&node->addr); |
| 313 |
|
| 314 |
ast_log(AST_LOG_NOTICE, "Node %u (%s) at %s %s the cluster\n", |
| 315 |
node->id, |
| 316 |
eid, |
| 317 |
addr, |
| 318 |
joined ? "joined" : "left"); |
| 319 |
|
| 320 |
json = ast_json_pack("{s: s, s: i, s: s, s: i}", |
| 321 |
"address", addr, |
| 322 |
"node_id", node->id, |
| 323 |
"eid", eid, |
| 324 |
"joined", joined); |
| 325 |
if (!json) { |
| 326 |
return; |
| 327 |
} |
| 328 |
|
| 329 |
payload = ast_json_payload_create(json); |
| 330 |
if (!payload) { |
| 331 |
ast_json_unref(json); |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
message = stasis_message_create(ast_cluster_discovery_type(), payload); |
| 336 |
if (!message) { |
| 337 |
ast_json_unref(json); |
| 338 |
ao2_ref(payload, -1); |
| 339 |
return; |
| 340 |
} |
| 341 |
|
| 342 |
stasis_publish(ast_system_topic(), message); |
| 343 |
ast_json_unref(json); |
| 344 |
ao2_ref(payload, -1); |
| 345 |
ao2_ref(message, -1); |
| 346 |
} |
| 347 |
|
| 348 |
static void send_cluster_notify(void); |
| 349 |
|
| 350 |
|
| 351 |
static void publish_cluster_discovery_to_stasis(struct ast_event *event) |
| 352 |
{ |
| 353 |
struct corosync_node *node; |
| 354 |
int id = ast_event_get_ie_uint(event, AST_EVENT_IE_NODE_ID); |
| 355 |
struct ast_eid *event_eid; |
| 356 |
|
| 357 |
ast_assert(ast_event_get_type(event) == AST_EVENT_CLUSTER_DISCOVERY); |
| 358 |
|
| 359 |
event_eid = (struct ast_eid *)ast_event_get_ie_raw(event, AST_EVENT_IE_EID); |
| 360 |
if (!event_eid || !ast_eid_cmp(&ast_eid_default, event_eid)) { |
| 361 |
|
| 362 |
return; |
| 363 |
} |
| 364 |
|
| 365 |
ao2_lock(nodes); |
| 366 |
node = ao2_find(nodes, &id, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 367 |
if (node) { |
| 368 |
|
| 369 |
ao2_unlock(nodes); |
| 370 |
ao2_ref(node, -1); |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
node = corosync_node_alloc(event); |
| 375 |
if (!node) { |
| 376 |
ao2_unlock(nodes); |
| 377 |
return; |
| 378 |
} |
| 379 |
ao2_link_flags(nodes, node, OBJ_NOLOCK); |
| 380 |
ao2_unlock(nodes); |
| 381 |
|
| 382 |
publish_cluster_discovery_to_stasis_full(node, 1); |
| 383 |
|
| 384 |
ao2_ref(node, -1); |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
send_cluster_notify(); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
static void publish_mwi_to_stasis(struct ast_event *event) |
| 395 |
{ |
| 396 |
const char *mailbox; |
| 397 |
const char *context; |
| 398 |
unsigned int new_msgs; |
| 399 |
unsigned int old_msgs; |
| 400 |
struct ast_eid *event_eid; |
| 401 |
|
| 402 |
ast_assert(ast_event_get_type(event) == AST_EVENT_MWI); |
| 403 |
|
| 404 |
mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX); |
| 405 |
context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT); |
| 406 |
new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS); |
| 407 |
old_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS); |
| 408 |
event_eid = (struct ast_eid *)ast_event_get_ie_raw(event, AST_EVENT_IE_EID); |
| 409 |
|
| 410 |
if (ast_strlen_zero(mailbox) || ast_strlen_zero(context)) { |
| 411 |
return; |
| 412 |
} |
| 413 |
|
| 414 |
if (new_msgs > INT_MAX) { |
| 415 |
new_msgs = INT_MAX; |
| 416 |
} |
| 417 |
|
| 418 |
if (old_msgs > INT_MAX) { |
| 419 |
old_msgs = INT_MAX; |
| 420 |
} |
| 421 |
|
| 422 |
if (ast_publish_mwi_state_full(mailbox, context, (int)new_msgs, |
| 423 |
(int)old_msgs, NULL, event_eid)) { |
| 424 |
char eid[18]; |
| 425 |
ast_eid_to_str(eid, sizeof(eid), event_eid); |
| 426 |
ast_log(LOG_WARNING, "Failed to publish MWI message for %s@%s from %s\n", |
| 427 |
mailbox, context, eid); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
static void publish_device_state_to_stasis(struct ast_event *event) |
| 433 |
{ |
| 434 |
const char *device; |
| 435 |
enum ast_device_state state; |
| 436 |
unsigned int cachable; |
| 437 |
struct ast_eid *event_eid; |
| 438 |
|
| 439 |
ast_assert(ast_event_get_type(event) == AST_EVENT_DEVICE_STATE_CHANGE); |
| 440 |
|
| 441 |
device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE); |
| 442 |
state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE); |
| 443 |
cachable = ast_event_get_ie_uint(event, AST_EVENT_IE_CACHABLE); |
| 444 |
event_eid = (struct ast_eid *)ast_event_get_ie_raw(event, AST_EVENT_IE_EID); |
| 445 |
|
| 446 |
if (ast_strlen_zero(device)) { |
| 447 |
return; |
| 448 |
} |
| 449 |
|
| 450 |
if (ast_publish_device_state_full(device, state, cachable, event_eid)) { |
| 451 |
char eid[18]; |
| 452 |
ast_eid_to_str(eid, sizeof(eid), event_eid); |
| 453 |
ast_log(LOG_WARNING, "Failed to publish device state message for %s from %s\n", |
| 454 |
device, eid); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
static void cpg_deliver_cb(cpg_handle_t handle, const struct cpg_name *group_name, |
| 459 |
uint32_t nodeid, uint32_t pid, void *msg, size_t msg_len); |
| 460 |
|
| 461 |
static void cpg_confchg_cb(cpg_handle_t handle, const struct cpg_name *group_name, |
| 462 |
const struct cpg_address *member_list, size_t member_list_entries, |
| 463 |
const struct cpg_address *left_list, size_t left_list_entries, |
| 464 |
const struct cpg_address *joined_list, size_t joined_list_entries); |
| 465 |
|
| 466 |
static cpg_callbacks_t cpg_callbacks = { |
| 467 |
.cpg_deliver_fn = cpg_deliver_cb, |
| 468 |
.cpg_confchg_fn = cpg_confchg_cb, |
| 469 |
}; |
| 470 |
|
| 471 |
#ifdef HAVE_COROSYNC_CFG_STATE_TRACK |
| 472 |
static void cfg_state_track_cb( |
| 473 |
corosync_cfg_state_notification_buffer_t *notification_buffer, |
| 474 |
cs_error_t error) |
| 475 |
{ |
| 476 |
} |
| 477 |
#endif |
| 478 |
|
| 479 |
static void cfg_shutdown_cb(corosync_cfg_handle_t cfg_handle, |
| 480 |
corosync_cfg_shutdown_flags_t flags) |
| 481 |
{ |
| 482 |
} |
| 483 |
|
| 484 |
static void cpg_deliver_cb(cpg_handle_t handle, const struct cpg_name *group_name, |
| 485 |
uint32_t nodeid, uint32_t pid, void *msg, size_t msg_len) |
| 486 |
{ |
| 487 |
struct ast_event *event; |
| 488 |
void (*publish_handler)(struct ast_event *) = NULL; |
| 489 |
enum ast_event_type event_type; |
| 490 |
struct ast_eid *event_eid; |
| 491 |
|
| 492 |
if (msg_len < ast_event_minimum_length()) { |
| 493 |
ast_debug(1, "Ignoring event that's too small. %u < %u\n", |
| 494 |
(unsigned int) msg_len, |
| 495 |
(unsigned int) ast_event_minimum_length()); |
| 496 |
return; |
| 497 |
} |
| 498 |
|
| 499 |
event_eid = (struct ast_eid *)ast_event_get_ie_raw(msg, AST_EVENT_IE_EID); |
| 500 |
if (!event_eid || !ast_eid_cmp(&ast_eid_default, event_eid)) { |
| 501 |
|
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
event_type = ast_event_get_type(msg); |
| 506 |
if (event_type > AST_EVENT_TOTAL) { |
| 507 |
|
| 508 |
return; |
| 509 |
} |
| 510 |
|
| 511 |
ast_rwlock_rdlock(&event_types_lock); |
| 512 |
ast_debug(5, "cpg_deliver_cb rdlock\n"); |
| 513 |
publish_handler = event_types[event_type].publish_to_stasis; |
| 514 |
if (!event_types[event_type].subscribe || !publish_handler) { |
| 515 |
|
| 516 |
|
| 517 |
ast_rwlock_unlock(&event_types_lock); |
| 518 |
ast_debug(5, "cpg_deliver_cb unlock\n"); |
| 519 |
return; |
| 520 |
} |
| 521 |
ast_rwlock_unlock(&event_types_lock); |
| 522 |
ast_debug(5, "cpg_deliver_cb unlock\n"); |
| 523 |
|
| 524 |
if (!(event = ast_malloc(msg_len))) { |
| 525 |
return; |
| 526 |
} |
| 527 |
|
| 528 |
memcpy(event, msg, msg_len); |
| 529 |
|
| 530 |
if (event_type == AST_EVENT_PING) { |
| 531 |
const struct ast_eid *eid; |
| 532 |
char buf[128] = ""; |
| 533 |
|
| 534 |
eid = (struct ast_eid *)ast_event_get_ie_raw(event, AST_EVENT_IE_EID); |
| 535 |
ast_eid_to_str(buf, sizeof(buf), (struct ast_eid *) eid); |
| 536 |
ast_log(LOG_NOTICE, "Got event PING from server with EID: '%s'\n", buf); |
| 537 |
} |
| 538 |
ast_debug(5, "Publishing event %s (%u) to stasis\n", |
| 539 |
ast_event_get_type_name(event), event_type); |
| 540 |
publish_handler(event); |
| 541 |
ast_free(event); |
| 542 |
} |
| 543 |
|
| 544 |
static void publish_event_to_corosync(struct ast_event *event) |
| 545 |
{ |
| 546 |
cs_error_t cs_err; |
| 547 |
struct iovec iov; |
| 548 |
|
| 549 |
iov.iov_base = (void *)event; |
| 550 |
iov.iov_len = ast_event_get_size(event); |
| 551 |
|
| 552 |
ast_debug(5, "Publishing event %s (%u) to corosync\n", |
| 553 |
ast_event_get_type_name(event), ast_event_get_type(event)); |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
if (corosync_node_joined && !ast_rwlock_tryrdlock(&init_cpg_lock)) { |
| 558 |
ast_debug(5, "publish_event_to_corosync rdlock\n"); |
| 559 |
if ((cs_err = cpg_mcast_joined(cpg_handle, CPG_TYPE_FIFO, &iov, 1)) != CS_OK) { |
| 560 |
ast_log(LOG_WARNING, "CPG mcast failed (%u) for event %s (%u)\n", |
| 561 |
cs_err, ast_event_get_type_name(event), ast_event_get_type(event)); |
| 562 |
} |
| 563 |
ast_rwlock_unlock(&init_cpg_lock); |
| 564 |
ast_debug(5, "publish_event_to_corosync unlock\n"); |
| 565 |
} else { |
| 566 |
ast_log(LOG_WARNING, "CPG mcast not executed for event %s (%u): initializing CPG.\n", |
| 567 |
ast_event_get_type_name(event), ast_event_get_type(event)); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
static void publish_to_corosync(struct stasis_message *message) |
| 572 |
{ |
| 573 |
struct ast_event *event; |
| 574 |
struct ast_eid *event_eid; |
| 575 |
|
| 576 |
event = stasis_message_to_event(message); |
| 577 |
if (!event) { |
| 578 |
return; |
| 579 |
} |
| 580 |
|
| 581 |
event_eid = (struct ast_eid *)ast_event_get_ie_raw(event, AST_EVENT_IE_EID); |
| 582 |
if (!event_eid || ast_eid_cmp(&ast_eid_default, event_eid)) { |
| 583 |
|
| 584 |
ast_event_destroy(event); |
| 585 |
return; |
| 586 |
} |
| 587 |
|
| 588 |
if (ast_event_get_type(event) == AST_EVENT_PING) { |
| 589 |
const struct ast_eid *eid; |
| 590 |
char buf[128] = ""; |
| 591 |
|
| 592 |
eid = (struct ast_eid *)ast_event_get_ie_raw(event, AST_EVENT_IE_EID); |
| 593 |
ast_eid_to_str(buf, sizeof(buf), (struct ast_eid *) eid); |
| 594 |
ast_log(LOG_NOTICE, "Sending event PING from this server with EID: '%s'\n", buf); |
| 595 |
} |
| 596 |
|
| 597 |
publish_event_to_corosync(event); |
| 598 |
ast_event_destroy(event); |
| 599 |
} |
| 600 |
|
| 601 |
static void stasis_message_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 602 |
{ |
| 603 |
if (!message) { |
| 604 |
return; |
| 605 |
} |
| 606 |
|
| 607 |
publish_to_corosync(message); |
| 608 |
} |
| 609 |
|
| 610 |
static int dump_cache_cb(void *obj, void *arg, int flags) |
| 611 |
{ |
| 612 |
struct stasis_message *message = obj; |
| 613 |
|
| 614 |
if (!message) { |
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
publish_to_corosync(message); |
| 619 |
|
| 620 |
return 0; |
| 621 |
} |
| 622 |
|
| 623 |
static int clear_node_cache(void *obj, void *arg, int flags) |
| 624 |
{ |
| 625 |
struct stasis_message *cached_msg = obj; |
| 626 |
struct stasis_topic *topic = arg; |
| 627 |
struct stasis_message *msg; |
| 628 |
struct ast_eid *msg_eid; |
| 629 |
|
| 630 |
if (!cached_msg) { |
| 631 |
return 0; |
| 632 |
} |
| 633 |
|
| 634 |
msg_eid = (struct ast_eid *)stasis_message_eid(cached_msg); |
| 635 |
if(msg_eid && ast_eid_cmp(&ast_eid_default, msg_eid)) |
| 636 |
{ |
| 637 |
msg = stasis_cache_clear_create(cached_msg); |
| 638 |
if (msg) { |
| 639 |
stasis_publish(topic, msg); |
| 640 |
ao2_cleanup(msg); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
return 0; |
| 645 |
} |
| 646 |
|
| 647 |
static void cpg_confchg_cb(cpg_handle_t handle, const struct cpg_name *group_name, |
| 648 |
const struct cpg_address *member_list, size_t member_list_entries, |
| 649 |
const struct cpg_address *left_list, size_t left_list_entries, |
| 650 |
const struct cpg_address *joined_list, size_t joined_list_entries) |
| 651 |
{ |
| 652 |
unsigned int i; |
| 653 |
|
| 654 |
|
| 655 |
for (i = 0; i < left_list_entries; i++) { |
| 656 |
const struct cpg_address *cpg_node = &left_list[i]; |
| 657 |
struct corosync_node* node; |
| 658 |
unsigned int j; |
| 659 |
|
| 660 |
node = ao2_find(nodes, &cpg_node->nodeid, OBJ_UNLINK | OBJ_SEARCH_KEY); |
| 661 |
if (!node) { |
| 662 |
continue; |
| 663 |
} |
| 664 |
|
| 665 |
for (j = 0; j < ARRAY_LEN(event_types); j++) { |
| 666 |
struct ao2_container *messages; |
| 667 |
int messages_count; |
| 668 |
|
| 669 |
ast_rwlock_rdlock(&event_types_lock); |
| 670 |
ast_debug(5, "cpg_confchg_cb rdlock\n"); |
| 671 |
if (!event_types[j].subscribe) { |
| 672 |
ast_rwlock_unlock(&event_types_lock); |
| 673 |
ast_debug(5, "cpg_confchg_cb unlock\n"); |
| 674 |
continue; |
| 675 |
} |
| 676 |
|
| 677 |
if (!event_types[j].cache_fn || !event_types[j].message_type_fn) { |
| 678 |
ast_rwlock_unlock(&event_types_lock); |
| 679 |
ast_debug(5, "cpg_confchg_cb unlock\n"); |
| 680 |
continue; |
| 681 |
} |
| 682 |
ast_rwlock_unlock(&event_types_lock); |
| 683 |
ast_debug(5, "cpg_confchg_cb unlock\n"); |
| 684 |
|
| 685 |
messages = stasis_cache_dump_by_eid(event_types[j].cache_fn(), event_types[j].message_type_fn(), &node->eid); |
| 686 |
|
| 687 |
messages_count = ao2_container_count(messages); |
| 688 |
ast_log(LOG_NOTICE, "Clearing %i events of type %s of node %i from stasis cache.\n", messages_count, event_types[j].name, node->id); |
| 689 |
ao2_callback(messages, OBJ_NODATA, clear_node_cache, event_types[j].topic_fn()); |
| 690 |
ast_log(LOG_NOTICE, "Cleared events of type %s from stasis cache.\n", event_types[j].name); |
| 691 |
|
| 692 |
ao2_t_ref(messages, -1, "Dispose of flushed cache"); |
| 693 |
} |
| 694 |
|
| 695 |
publish_cluster_discovery_to_stasis_full(node, 0); |
| 696 |
ao2_ref(node, -1); |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
if (!joined_list_entries) { |
| 702 |
return; |
| 703 |
} |
| 704 |
|
| 705 |
for (i = 0; i < ARRAY_LEN(event_types); i++) { |
| 706 |
struct ao2_container *messages; |
| 707 |
int messages_count; |
| 708 |
|
| 709 |
ast_rwlock_rdlock(&event_types_lock); |
| 710 |
ast_debug(5, "cpg_confchg_cb rdlock\n"); |
| 711 |
if (!event_types[i].publish) { |
| 712 |
ast_rwlock_unlock(&event_types_lock); |
| 713 |
ast_debug(5, "cpg_confchg_cb unlock\n"); |
| 714 |
continue; |
| 715 |
} |
| 716 |
|
| 717 |
if (!event_types[i].cache_fn || !event_types[i].message_type_fn) { |
| 718 |
ast_rwlock_unlock(&event_types_lock); |
| 719 |
ast_debug(5, "cpg_confchg_cb unlock\n"); |
| 720 |
continue; |
| 721 |
} |
| 722 |
ast_rwlock_unlock(&event_types_lock); |
| 723 |
ast_debug(5, "cpg_confchg_cb unlock\n"); |
| 724 |
|
| 725 |
messages = stasis_cache_dump_by_eid(event_types[i].cache_fn(), event_types[i].message_type_fn(), &ast_eid_default); |
| 726 |
|
| 727 |
messages_count = ao2_container_count(messages); |
| 728 |
ast_log(LOG_NOTICE, "Sending %i events of type %s to corosync.\n", messages_count, event_types[i].name); |
| 729 |
ao2_callback(messages, OBJ_NODATA, dump_cache_cb, NULL); |
| 730 |
ast_log(LOG_NOTICE, "Sent events of type %s to corosync.\n", event_types[i].name); |
| 731 |
|
| 732 |
ao2_t_ref(messages, -1, "Dispose of dumped cache"); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
static void send_cluster_notify(void) |
| 738 |
{ |
| 739 |
struct ast_event *event; |
| 740 |
unsigned int node_id; |
| 741 |
cs_error_t cs_err; |
| 742 |
corosync_cfg_node_address_t corosync_addr; |
| 743 |
int num_addrs = 0; |
| 744 |
struct sockaddr *sa; |
| 745 |
size_t sa_len; |
| 746 |
char buf[128]; |
| 747 |
int res; |
| 748 |
|
| 749 |
if (!ast_rwlock_tryrdlock(&init_cpg_lock)) { |
| 750 |
ast_debug(5, "send_cluster_notify rdlock\n"); |
| 751 |
|
| 752 |
if ((cs_err = corosync_cfg_local_get(cfg_handle, &node_id)) != CS_OK) { |
| 753 |
ast_log(LOG_WARNING, "Failed to extract Corosync node ID for this node. Not informing cluster of existance.\n"); |
| 754 |
return; |
| 755 |
} |
| 756 |
|
| 757 |
if (((cs_err = corosync_cfg_get_node_addrs(cfg_handle, node_id, 1, &num_addrs, &corosync_addr)) != CS_OK) || (num_addrs < 1)) { |
| 758 |
ast_log(LOG_WARNING, "Failed to get local Corosync address. Not informing cluster of existance.\n"); |
| 759 |
return; |
| 760 |
} |
| 761 |
|
| 762 |
ast_rwlock_unlock(&init_cpg_lock); |
| 763 |
ast_debug(5, "send_cluster_notify unlock\n"); |
| 764 |
} |
| 765 |
|
| 766 |
sa = (struct sockaddr *)corosync_addr.address; |
| 767 |
sa_len = (size_t)corosync_addr.address_length; |
| 768 |
if ((res = getnameinfo(sa, sa_len, buf, sizeof(buf), NULL, 0, NI_NUMERICHOST))) { |
| 769 |
ast_log(LOG_WARNING, "Failed to determine name of local Corosync address: %s (%d). Not informing cluster of existance.\n", |
| 770 |
gai_strerror(res), res); |
| 771 |
return; |
| 772 |
} |
| 773 |
|
| 774 |
event = ast_event_new(AST_EVENT_CLUSTER_DISCOVERY, |
| 775 |
AST_EVENT_IE_NODE_ID, AST_EVENT_IE_PLTYPE_UINT, node_id, |
| 776 |
AST_EVENT_IE_LOCAL_ADDR, AST_EVENT_IE_PLTYPE_STR, buf, |
| 777 |
AST_EVENT_IE_END); |
| 778 |
publish_event_to_corosync(event); |
| 779 |
ast_event_destroy(event); |
| 780 |
} |
| 781 |
|
| 782 |
static void *dispatch_thread_handler(void *data) |
| 783 |
{ |
| 784 |
cs_error_t cs_err; |
| 785 |
struct pollfd pfd[3] = { |
| 786 |
{ .events = POLLIN, }, |
| 787 |
{ .events = POLLIN, }, |
| 788 |
{ .events = POLLIN, }, |
| 789 |
}; |
| 790 |
|
| 791 |
if (!ast_rwlock_tryrdlock(&init_cpg_lock)) { |
| 792 |
ast_debug(5, "dispatch_thread_handler rdlock\n"); |
| 793 |
if ((cs_err = cpg_fd_get(cpg_handle, &pfd[0].fd)) != CS_OK) { |
| 794 |
ast_log(LOG_ERROR, "Failed to get CPG fd. This module is now broken.\n"); |
| 795 |
ast_rwlock_unlock(&init_cpg_lock); |
| 796 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 797 |
return NULL; |
| 798 |
} |
| 799 |
|
| 800 |
if ((cs_err = corosync_cfg_fd_get(cfg_handle, &pfd[1].fd)) != CS_OK) { |
| 801 |
ast_log(LOG_ERROR, "Failed to get CFG fd. This module is now broken.\n"); |
| 802 |
ast_rwlock_unlock(&init_cpg_lock); |
| 803 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 804 |
return NULL; |
| 805 |
} |
| 806 |
|
| 807 |
pfd[2].fd = dispatch_thread.alert_pipe[0]; |
| 808 |
ast_rwlock_unlock(&init_cpg_lock); |
| 809 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 810 |
} else { |
| 811 |
ast_log(LOG_ERROR, "Failed to get fd: initializing CPG. This module is now broken.\n"); |
| 812 |
return NULL; |
| 813 |
} |
| 814 |
send_cluster_notify(); |
| 815 |
while (!dispatch_thread.stop) { |
| 816 |
int res; |
| 817 |
|
| 818 |
cs_err = CS_OK; |
| 819 |
|
| 820 |
pfd[0].revents = 0; |
| 821 |
pfd[1].revents = 0; |
| 822 |
pfd[2].revents = 0; |
| 823 |
|
| 824 |
res = ast_poll(pfd, ARRAY_LEN(pfd), COROSYNC_POLL_TIMEOUT); |
| 825 |
if (res == -1 && errno != EINTR && errno != EAGAIN) { |
| 826 |
ast_log(LOG_ERROR, "poll() error: %s (%d)\n", strerror(errno), errno); |
| 827 |
cs_err = CS_ERR_BAD_HANDLE; |
| 828 |
} else if (res == 0) { |
| 829 |
unsigned int local_nodeid; |
| 830 |
|
| 831 |
if (!ast_rwlock_tryrdlock(&init_cpg_lock)) { |
| 832 |
ast_debug(5, "dispatch_thread_handler rdlock\n"); |
| 833 |
if ((cs_err = cpg_local_get(cpg_handle, &local_nodeid)) == CS_OK) { |
| 834 |
struct cpg_name name; |
| 835 |
struct cpg_address address[CPG_MEMBERS_MAX]; |
| 836 |
int entries = CPG_MEMBERS_MAX; |
| 837 |
|
| 838 |
ast_copy_string(name.value, "asterisk", sizeof(name.value)); |
| 839 |
name.length = strlen(name.value); |
| 840 |
if ((cs_err = cpg_membership_get(cpg_handle, &name, address, &entries)) == CS_OK) { |
| 841 |
int i; |
| 842 |
int found = 0; |
| 843 |
|
| 844 |
ast_debug(1, "CPG group has %i node membership\n", entries); |
| 845 |
for (i = 0; (i < entries) && !found; i++) { |
| 846 |
if (address[i].nodeid == local_nodeid) |
| 847 |
found = 1; |
| 848 |
} |
| 849 |
if (!found) { |
| 850 |
ast_log(LOG_WARNING, "Failed to check CPG node membership\n"); |
| 851 |
corosync_node_joined = 0; |
| 852 |
cs_err = CS_ERR_BAD_HANDLE; |
| 853 |
} |
| 854 |
} else { |
| 855 |
ast_log(LOG_WARNING, "Failed to get CPG node membership: %u\n", cs_err); |
| 856 |
corosync_node_joined = 0; |
| 857 |
cs_err = CS_ERR_BAD_HANDLE; |
| 858 |
} |
| 859 |
} else { |
| 860 |
ast_log(LOG_WARNING, "Failed to get CPG local node id: %u\n", cs_err); |
| 861 |
corosync_node_joined = 0; |
| 862 |
cs_err = CS_ERR_BAD_HANDLE; |
| 863 |
} |
| 864 |
ast_rwlock_unlock(&init_cpg_lock); |
| 865 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 866 |
} else { |
| 867 |
ast_log(LOG_WARNING, "Failed to check CPG node membership: initializing CPG.\n"); |
| 868 |
corosync_node_joined = 0; |
| 869 |
cs_err = CS_ERR_BAD_HANDLE; |
| 870 |
} |
| 871 |
} else { |
| 872 |
if (!ast_rwlock_tryrdlock(&init_cpg_lock)) { |
| 873 |
ast_debug(5, "dispatch_thread_handler rdlock\n"); |
| 874 |
if (pfd[0].revents & POLLIN) { |
| 875 |
if ((cs_err = cpg_dispatch(cpg_handle, CS_DISPATCH_ALL)) != CS_OK) { |
| 876 |
ast_log(LOG_WARNING, "Failed CPG dispatch: %u\n", cs_err); |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
if (pfd[1].revents & POLLIN) { |
| 881 |
if ((cs_err = corosync_cfg_dispatch(cfg_handle, CS_DISPATCH_ALL)) != CS_OK) { |
| 882 |
ast_log(LOG_WARNING, "Failed CFG dispatch: %u\n", cs_err); |
| 883 |
} |
| 884 |
} |
| 885 |
ast_rwlock_unlock(&init_cpg_lock); |
| 886 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 887 |
} else { |
| 888 |
ast_log(LOG_WARNING, "Failed to dispatch: initializing CPG.\n"); |
| 889 |
} |
| 890 |
} |
| 891 |
if (cs_err == CS_ERR_LIBRARY || cs_err == CS_ERR_BAD_HANDLE) { |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
ast_log(LOG_NOTICE, "Attempting to recover from corosync failure.\n"); |
| 896 |
|
| 897 |
if (!ast_rwlock_trywrlock(&init_cpg_lock)) { |
| 898 |
struct cpg_name name; |
| 899 |
ast_debug(5, "dispatch_thread_handler wrlock\n"); |
| 900 |
|
| 901 |
corosync_node_joined = 0; |
| 902 |
if (cpg_handle && (cs_err = cpg_finalize(cpg_handle)) != CS_OK) { |
| 903 |
ast_log(LOG_ERROR, "Failed to finalize cpg (%d)\n", (int) cs_err); |
| 904 |
} |
| 905 |
|
| 906 |
if (cfg_handle && (cs_err = corosync_cfg_finalize(cfg_handle)) != CS_OK) { |
| 907 |
ast_log(LOG_ERROR, "Failed to finalize cfg (%d)\n", (int) cs_err); |
| 908 |
} |
| 909 |
|
| 910 |
if ((cs_err = corosync_cfg_initialize(&cfg_handle, &cfg_callbacks)) != CS_OK) { |
| 911 |
ast_log(LOG_ERROR, "Failed to initialize cfg (%d)\n", (int) cs_err); |
| 912 |
ast_rwlock_unlock(&init_cpg_lock); |
| 913 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 914 |
sleep(5); |
| 915 |
continue; |
| 916 |
} |
| 917 |
|
| 918 |
if ((cs_err = cpg_initialize(&cpg_handle, &cpg_callbacks) != CS_OK)) { |
| 919 |
ast_log(LOG_ERROR, "Failed to initialize cpg (%d)\n", (int) cs_err); |
| 920 |
ast_rwlock_unlock(&init_cpg_lock); |
| 921 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 922 |
sleep(5); |
| 923 |
continue; |
| 924 |
} |
| 925 |
|
| 926 |
if ((cs_err = cpg_fd_get(cpg_handle, &pfd[0].fd)) != CS_OK) { |
| 927 |
ast_log(LOG_ERROR, "Failed to get CPG fd.\n"); |
| 928 |
ast_rwlock_unlock(&init_cpg_lock); |
| 929 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 930 |
sleep(5); |
| 931 |
continue; |
| 932 |
} |
| 933 |
|
| 934 |
if ((cs_err = corosync_cfg_fd_get(cfg_handle, &pfd[1].fd)) != CS_OK) { |
| 935 |
ast_log(LOG_ERROR, "Failed to get CFG fd.\n"); |
| 936 |
ast_rwlock_unlock(&init_cpg_lock); |
| 937 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 938 |
sleep(5); |
| 939 |
continue; |
| 940 |
} |
| 941 |
|
| 942 |
ast_copy_string(name.value, "asterisk", sizeof(name.value)); |
| 943 |
name.length = strlen(name.value); |
| 944 |
if ((cs_err = cpg_join(cpg_handle, &name)) != CS_OK) { |
| 945 |
ast_log(LOG_ERROR, "Failed to join cpg (%d)\n", (int) cs_err); |
| 946 |
ast_rwlock_unlock(&init_cpg_lock); |
| 947 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 948 |
sleep(5); |
| 949 |
continue; |
| 950 |
} |
| 951 |
corosync_node_joined = 1; |
| 952 |
ast_rwlock_unlock(&init_cpg_lock); |
| 953 |
ast_debug(5, "dispatch_thread_handler unlock\n"); |
| 954 |
ast_log(LOG_NOTICE, "Corosync recovery complete.\n"); |
| 955 |
send_cluster_notify(); |
| 956 |
} else { |
| 957 |
ast_log(LOG_NOTICE, "Failed to recover from corosync failure: initializing CPG.\n"); |
| 958 |
} |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
return NULL; |
| 963 |
} |
| 964 |
|
| 965 |
static char *corosync_show_members(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 966 |
{ |
| 967 |
cs_error_t cs_err; |
| 968 |
cpg_iteration_handle_t cpg_iter; |
| 969 |
struct cpg_iteration_description_t cpg_desc; |
| 970 |
unsigned int i; |
| 971 |
|
| 972 |
switch (cmd) { |
| 973 |
case CLI_INIT: |
| 974 |
e->command = "corosync show members"; |
| 975 |
e->usage = |
| 976 |
"Usage: corosync show members\n" |
| 977 |
" Show corosync cluster members\n"; |
| 978 |
return NULL; |
| 979 |
|
| 980 |
case CLI_GENERATE: |
| 981 |
return NULL; |
| 982 |
} |
| 983 |
|
| 984 |
if (a->argc != e->args) { |
| 985 |
return CLI_SHOWUSAGE; |
| 986 |
} |
| 987 |
|
| 988 |
if (!ast_rwlock_tryrdlock(&init_cpg_lock)) { |
| 989 |
ast_debug(5, "corosync_show_members rdlock\n"); |
| 990 |
cs_err = cpg_iteration_initialize(cpg_handle, CPG_ITERATION_ALL, NULL, &cpg_iter); |
| 991 |
|
| 992 |
if (cs_err != CS_OK) { |
| 993 |
ast_cli(a->fd, "Failed to initialize CPG iterator: %u.\n", cs_err); |
| 994 |
cpg_iteration_finalize(cpg_iter); |
| 995 |
ast_rwlock_unlock(&init_cpg_lock); |
| 996 |
ast_debug(5, "corosync_show_members unlock\n"); |
| 997 |
return CLI_FAILURE; |
| 998 |
} |
| 999 |
|
| 1000 |
ast_cli(a->fd, "\n" |
| 1001 |
"=============================================================\n" |
| 1002 |
"=== Cluster members =========================================\n" |
| 1003 |
"=============================================================\n" |
| 1004 |
"===\n"); |
| 1005 |
|
| 1006 |
for (i = 1, cs_err = cpg_iteration_next(cpg_iter, &cpg_desc); |
| 1007 |
cs_err == CS_OK; |
| 1008 |
cs_err = cpg_iteration_next(cpg_iter, &cpg_desc), i++) { |
| 1009 |
#ifdef HAVE_COROSYNC_CFG_STATE_TRACK |
| 1010 |
corosync_cfg_node_address_t addrs[8]; |
| 1011 |
int num_addrs = 0; |
| 1012 |
unsigned int j; |
| 1013 |
#endif |
| 1014 |
|
| 1015 |
ast_cli(a->fd, "=== Node %u\n", i); |
| 1016 |
ast_cli(a->fd, "=== --> Group: %s\n", cpg_desc.group.value); |
| 1017 |
|
| 1018 |
#ifdef HAVE_COROSYNC_CFG_STATE_TRACK |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
cs_err = corosync_cfg_get_node_addrs(cfg_handle, cpg_desc.nodeid, |
| 1025 |
ARRAY_LEN(addrs), &num_addrs, addrs); |
| 1026 |
if (cs_err != CS_OK) { |
| 1027 |
ast_log(LOG_WARNING, "Failed to get node addresses\n"); |
| 1028 |
continue; |
| 1029 |
} |
| 1030 |
|
| 1031 |
for (j = 0; j < num_addrs; j++) { |
| 1032 |
struct sockaddr *sa = (struct sockaddr *) addrs[j].address; |
| 1033 |
size_t sa_len = (size_t) addrs[j].address_length; |
| 1034 |
char buf[128]; |
| 1035 |
|
| 1036 |
getnameinfo(sa, sa_len, buf, sizeof(buf), NULL, 0, NI_NUMERICHOST); |
| 1037 |
|
| 1038 |
ast_cli(a->fd, "=== --> Address %u: %s\n", j + 1, buf); |
| 1039 |
} |
| 1040 |
#else |
| 1041 |
ast_cli(a->fd, "=== --> Nodeid: %"PRIu32"\n", cpg_desc.nodeid); |
| 1042 |
#endif |
| 1043 |
} |
| 1044 |
|
| 1045 |
ast_cli(a->fd, "===\n" |
| 1046 |
"=============================================================\n" |
| 1047 |
"\n"); |
| 1048 |
|
| 1049 |
cpg_iteration_finalize(cpg_iter); |
| 1050 |
ast_rwlock_unlock(&init_cpg_lock); |
| 1051 |
ast_debug(5, "corosync_show_members unlock\n"); |
| 1052 |
} else { |
| 1053 |
ast_cli(a->fd, "Failed to initialize CPG iterator: initializing CPG.\n"); |
| 1054 |
} |
| 1055 |
|
| 1056 |
return CLI_SUCCESS; |
| 1057 |
} |
| 1058 |
|
| 1059 |
static char *corosync_ping(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1060 |
{ |
| 1061 |
struct ast_event *event; |
| 1062 |
|
| 1063 |
switch (cmd) { |
| 1064 |
case CLI_INIT: |
| 1065 |
e->command = "corosync ping"; |
| 1066 |
e->usage = |
| 1067 |
"Usage: corosync ping\n" |
| 1068 |
" Send a test ping to the cluster.\n" |
| 1069 |
"A NOTICE will be in the log for every ping received\n" |
| 1070 |
"on a server.\n If you send a ping, you should see a NOTICE\n" |
| 1071 |
"in the log for every server in the cluster.\n"; |
| 1072 |
return NULL; |
| 1073 |
|
| 1074 |
case CLI_GENERATE: |
| 1075 |
return NULL; |
| 1076 |
} |
| 1077 |
|
| 1078 |
if (a->argc != e->args) { |
| 1079 |
return CLI_SHOWUSAGE; |
| 1080 |
} |
| 1081 |
|
| 1082 |
event = ast_event_new(AST_EVENT_PING, AST_EVENT_IE_END); |
| 1083 |
|
| 1084 |
if (!event) { |
| 1085 |
return CLI_FAILURE; |
| 1086 |
} |
| 1087 |
|
| 1088 |
event_types[AST_EVENT_PING].publish_to_stasis(event); |
| 1089 |
|
| 1090 |
ast_event_destroy(event); |
| 1091 |
return CLI_SUCCESS; |
| 1092 |
} |
| 1093 |
|
| 1094 |
static char *corosync_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1095 |
{ |
| 1096 |
unsigned int i; |
| 1097 |
|
| 1098 |
switch (cmd) { |
| 1099 |
case CLI_INIT: |
| 1100 |
e->command = "corosync show config"; |
| 1101 |
e->usage = |
| 1102 |
"Usage: corosync show config\n" |
| 1103 |
" Show configuration loaded from res_corosync.conf\n"; |
| 1104 |
return NULL; |
| 1105 |
|
| 1106 |
case CLI_GENERATE: |
| 1107 |
return NULL; |
| 1108 |
} |
| 1109 |
|
| 1110 |
if (a->argc != e->args) { |
| 1111 |
return CLI_SHOWUSAGE; |
| 1112 |
} |
| 1113 |
|
| 1114 |
ast_cli(a->fd, "\n" |
| 1115 |
"=============================================================\n" |
| 1116 |
"=== res_corosync config =====================================\n" |
| 1117 |
"=============================================================\n" |
| 1118 |
"===\n"); |
| 1119 |
|
| 1120 |
ast_rwlock_rdlock(&event_types_lock); |
| 1121 |
ast_debug(5, "corosync_show_config rdlock\n"); |
| 1122 |
for (i = 0; i < ARRAY_LEN(event_types); i++) { |
| 1123 |
if (event_types[i].publish) { |
| 1124 |
ast_cli(a->fd, "=== ==> Publishing Event Type: %s\n", |
| 1125 |
event_types[i].name); |
| 1126 |
} |
| 1127 |
if (event_types[i].subscribe) { |
| 1128 |
ast_cli(a->fd, "=== ==> Subscribing to Event Type: %s\n", |
| 1129 |
event_types[i].name); |
| 1130 |
} |
| 1131 |
} |
| 1132 |
ast_rwlock_unlock(&event_types_lock); |
| 1133 |
ast_debug(5, "corosync_show_config unlock\n"); |
| 1134 |
|
| 1135 |
ast_cli(a->fd, "===\n" |
| 1136 |
"=============================================================\n" |
| 1137 |
"\n"); |
| 1138 |
|
| 1139 |
return CLI_SUCCESS; |
| 1140 |
} |
| 1141 |
|
| 1142 |
static struct ast_cli_entry corosync_cli[] = { |
| 1143 |
AST_CLI_DEFINE(corosync_show_config, "Show configuration"), |
| 1144 |
AST_CLI_DEFINE(corosync_show_members, "Show cluster members"), |
| 1145 |
AST_CLI_DEFINE(corosync_ping, "Send a test ping to the cluster"), |
| 1146 |
}; |
| 1147 |
|
| 1148 |
enum { |
| 1149 |
PUBLISH, |
| 1150 |
SUBSCRIBE, |
| 1151 |
}; |
| 1152 |
|
| 1153 |
static int set_event(const char *event_type, int pubsub) |
| 1154 |
{ |
| 1155 |
unsigned int i; |
| 1156 |
|
| 1157 |
for (i = 0; i < ARRAY_LEN(event_types); i++) { |
| 1158 |
if (!event_types[i].name || strcasecmp(event_type, event_types[i].name)) { |
| 1159 |
continue; |
| 1160 |
} |
| 1161 |
|
| 1162 |
switch (pubsub) { |
| 1163 |
case PUBLISH: |
| 1164 |
event_types[i].publish = 1; |
| 1165 |
break; |
| 1166 |
case SUBSCRIBE: |
| 1167 |
event_types[i].subscribe = 1; |
| 1168 |
break; |
| 1169 |
} |
| 1170 |
|
| 1171 |
break; |
| 1172 |
} |
| 1173 |
|
| 1174 |
return (i == ARRAY_LEN(event_types)) ? -1 : 0; |
| 1175 |
} |
| 1176 |
|
| 1177 |
static int load_general_config(struct ast_config *cfg) |
| 1178 |
{ |
| 1179 |
struct ast_variable *v; |
| 1180 |
int res = 0; |
| 1181 |
unsigned int i; |
| 1182 |
|
| 1183 |
ast_rwlock_wrlock(&event_types_lock); |
| 1184 |
ast_debug(5, "load_general_config wrlock\n"); |
| 1185 |
|
| 1186 |
for (i = 0; i < ARRAY_LEN(event_types); i++) { |
| 1187 |
event_types[i].publish = event_types[i].publish_default; |
| 1188 |
event_types[i].subscribe = event_types[i].subscribe_default; |
| 1189 |
} |
| 1190 |
|
| 1191 |
for (v = ast_variable_browse(cfg, "general"); v && !res; v = v->next) { |
| 1192 |
if (!strcasecmp(v->name, "publish_event")) { |
| 1193 |
res = set_event(v->value, PUBLISH); |
| 1194 |
} else if (!strcasecmp(v->name, "subscribe_event")) { |
| 1195 |
res = set_event(v->value, SUBSCRIBE); |
| 1196 |
} else { |
| 1197 |
ast_log(LOG_WARNING, "Unknown option '%s'\n", v->name); |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
for (i = 0; i < ARRAY_LEN(event_types); i++) { |
| 1202 |
if (event_types[i].publish && !event_types[i].sub) { |
| 1203 |
event_types[i].sub = stasis_forward_all(event_types[i].topic_fn(), |
| 1204 |
corosync_topic()); |
| 1205 |
stasis_message_router_add(stasis_router, |
| 1206 |
event_types[i].message_type_fn(), |
| 1207 |
stasis_message_cb, |
| 1208 |
NULL); |
| 1209 |
} else if (!event_types[i].publish && event_types[i].sub) { |
| 1210 |
event_types[i].sub = stasis_forward_cancel(event_types[i].sub); |
| 1211 |
stasis_message_router_remove(stasis_router, |
| 1212 |
event_types[i].message_type_fn()); |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
ast_rwlock_unlock(&event_types_lock); |
| 1217 |
ast_debug(5, "load_general_config unlock\n"); |
| 1218 |
|
| 1219 |
return res; |
| 1220 |
} |
| 1221 |
|
| 1222 |
static int load_config(unsigned int reload) |
| 1223 |
{ |
| 1224 |
static const char filename[] = "res_corosync.conf"; |
| 1225 |
struct ast_config *cfg; |
| 1226 |
const char *cat = NULL; |
| 1227 |
struct ast_flags config_flags = { 0 }; |
| 1228 |
int res = 0; |
| 1229 |
|
| 1230 |
cfg = ast_config_load(filename, config_flags); |
| 1231 |
|
| 1232 |
if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) { |
| 1233 |
return -1; |
| 1234 |
} |
| 1235 |
|
| 1236 |
while ((cat = ast_category_browse(cfg, cat))) { |
| 1237 |
if (!strcasecmp(cat, "general")) { |
| 1238 |
res = load_general_config(cfg); |
| 1239 |
} else { |
| 1240 |
ast_log(LOG_WARNING, "Unknown configuration section '%s'\n", cat); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
ast_config_destroy(cfg); |
| 1245 |
|
| 1246 |
return res; |
| 1247 |
} |
| 1248 |
|
| 1249 |
static void cleanup_module(void) |
| 1250 |
{ |
| 1251 |
cs_error_t cs_err; |
| 1252 |
unsigned int i; |
| 1253 |
|
| 1254 |
if (stasis_router) { |
| 1255 |
|
| 1256 |
|
| 1257 |
for (i = 0; i < ARRAY_LEN(event_types); i++) { |
| 1258 |
struct ao2_container *messages = NULL; |
| 1259 |
int messages_count; |
| 1260 |
unsigned char subscribe = 0; |
| 1261 |
|
| 1262 |
ast_rwlock_wrlock(&event_types_lock); |
| 1263 |
ast_debug(5, "cleanup_module wrlock\n"); |
| 1264 |
subscribe = event_types[i].subscribe; |
| 1265 |
|
| 1266 |
if (event_types[i].sub) { |
| 1267 |
event_types[i].sub = stasis_forward_cancel(event_types[i].sub); |
| 1268 |
stasis_message_router_remove(stasis_router, event_types[i].message_type_fn()); |
| 1269 |
} |
| 1270 |
event_types[i].publish = 0; |
| 1271 |
event_types[i].subscribe = 0; |
| 1272 |
ast_rwlock_unlock(&event_types_lock); |
| 1273 |
ast_debug(5, "cleanup_module unlock\n"); |
| 1274 |
|
| 1275 |
if (subscribe && event_types[i].cache_fn && event_types[i].message_type_fn) { |
| 1276 |
messages = stasis_cache_dump_all(event_types[i].cache_fn(), event_types[i].message_type_fn()); |
| 1277 |
messages_count = ao2_container_count(messages); |
| 1278 |
ast_log(LOG_NOTICE, "Clearing %i events of type %s of other nodes from stasis cache.\n", messages_count, event_types[i].name); |
| 1279 |
ao2_callback(messages, OBJ_NODATA, clear_node_cache, event_types[i].topic_fn()); |
| 1280 |
ast_log(LOG_NOTICE, "Cleared events of type %s from stasis cache.\n", event_types[i].name); |
| 1281 |
ao2_t_ref(messages, -1, "Dispose of flushed cache"); |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
stasis_message_router_unsubscribe_and_join(stasis_router); |
| 1286 |
stasis_router = NULL; |
| 1287 |
} |
| 1288 |
|
| 1289 |
if (corosync_aggregate_topic) { |
| 1290 |
ao2_t_ref(corosync_aggregate_topic, -1, "Dispose of topic on cleanup"); |
| 1291 |
corosync_aggregate_topic = NULL; |
| 1292 |
} |
| 1293 |
|
| 1294 |
STASIS_MESSAGE_TYPE_CLEANUP(corosync_ping_message_type); |
| 1295 |
|
| 1296 |
if (dispatch_thread.id != AST_PTHREADT_NULL) { |
| 1297 |
char meepmeep = 'x'; |
| 1298 |
dispatch_thread.stop = 1; |
| 1299 |
if (ast_carefulwrite(dispatch_thread.alert_pipe[1], &meepmeep, 1, |
| 1300 |
5000) == -1) { |
| 1301 |
ast_log(LOG_ERROR, "Failed to write to pipe: %s (%d)\n", |
| 1302 |
strerror(errno), errno); |
| 1303 |
} |
| 1304 |
pthread_join(dispatch_thread.id, NULL); |
| 1305 |
} |
| 1306 |
|
| 1307 |
if (dispatch_thread.alert_pipe[0] != -1) { |
| 1308 |
close(dispatch_thread.alert_pipe[0]); |
| 1309 |
dispatch_thread.alert_pipe[0] = -1; |
| 1310 |
} |
| 1311 |
|
| 1312 |
if (dispatch_thread.alert_pipe[1] != -1) { |
| 1313 |
close(dispatch_thread.alert_pipe[1]); |
| 1314 |
dispatch_thread.alert_pipe[1] = -1; |
| 1315 |
} |
| 1316 |
|
| 1317 |
if (!ast_rwlock_trywrlock(&init_cpg_lock)) { |
| 1318 |
ast_debug(5, "cleanup_module wrlock\n"); |
| 1319 |
if (cpg_handle && (cs_err = cpg_finalize(cpg_handle)) != CS_OK) { |
| 1320 |
ast_log(LOG_ERROR, "Failed to finalize cpg (%d)\n", (int) cs_err); |
| 1321 |
} |
| 1322 |
cpg_handle = 0; |
| 1323 |
|
| 1324 |
if (cfg_handle && (cs_err = corosync_cfg_finalize(cfg_handle)) != CS_OK) { |
| 1325 |
ast_log(LOG_ERROR, "Failed to finalize cfg (%d)\n", (int) cs_err); |
| 1326 |
} |
| 1327 |
cfg_handle = 0; |
| 1328 |
corosync_node_joined = 0; |
| 1329 |
ast_rwlock_unlock(&init_cpg_lock); |
| 1330 |
ast_debug(5, "cleanup_module unlock\n"); |
| 1331 |
} |
| 1332 |
ao2_cleanup(nodes); |
| 1333 |
nodes = NULL; |
| 1334 |
} |
| 1335 |
|
| 1336 |
static int load_module(void) |
| 1337 |
{ |
| 1338 |
cs_error_t cs_err; |
| 1339 |
struct cpg_name name; |
| 1340 |
|
| 1341 |
if (ast_eid_is_empty(&ast_eid_default)) { |
| 1342 |
ast_log(LOG_ERROR, "Entity ID is not set.\n"); |
| 1343 |
return AST_MODULE_LOAD_DECLINE; |
| 1344 |
} |
| 1345 |
|
| 1346 |
nodes = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 23, |
| 1347 |
corosync_node_hash_fn, NULL, corosync_node_cmp_fn); |
| 1348 |
if (!nodes) { |
| 1349 |
goto failed; |
| 1350 |
} |
| 1351 |
|
| 1352 |
corosync_aggregate_topic = stasis_topic_create("corosync:aggregator"); |
| 1353 |
if (!corosync_aggregate_topic) { |
| 1354 |
ast_log(AST_LOG_ERROR, "Failed to create stasis topic for corosync\n"); |
| 1355 |
goto failed; |
| 1356 |
} |
| 1357 |
|
| 1358 |
stasis_router = stasis_message_router_create(corosync_aggregate_topic); |
| 1359 |
if (!stasis_router) { |
| 1360 |
ast_log(AST_LOG_ERROR, "Failed to create message router for corosync topic\n"); |
| 1361 |
goto failed; |
| 1362 |
} |
| 1363 |
stasis_message_router_set_congestion_limits(stasis_router, -1, |
| 1364 |
10 * AST_TASKPROCESSOR_HIGH_WATER_LEVEL); |
| 1365 |
|
| 1366 |
if (STASIS_MESSAGE_TYPE_INIT(corosync_ping_message_type) != 0) { |
| 1367 |
ast_log(AST_LOG_ERROR, "Failed to initialize corosync ping message type\n"); |
| 1368 |
goto failed; |
| 1369 |
} |
| 1370 |
|
| 1371 |
if (load_config(0)) { |
| 1372 |
|
| 1373 |
goto failed; |
| 1374 |
} |
| 1375 |
|
| 1376 |
if (!ast_rwlock_trywrlock(&init_cpg_lock)) { |
| 1377 |
corosync_node_joined = 0; |
| 1378 |
ast_debug(5, "load_module wrlock\n"); |
| 1379 |
if ((cs_err = corosync_cfg_initialize(&cfg_handle, &cfg_callbacks)) != CS_OK) { |
| 1380 |
ast_log(LOG_ERROR, "Failed to initialize cfg: (%d)\n", (int) cs_err); |
| 1381 |
ast_rwlock_unlock(&init_cpg_lock); |
| 1382 |
ast_debug(5, "load_module unlock\n"); |
| 1383 |
goto failed; |
| 1384 |
} |
| 1385 |
|
| 1386 |
if ((cs_err = cpg_initialize(&cpg_handle, &cpg_callbacks)) != CS_OK) { |
| 1387 |
ast_log(LOG_ERROR, "Failed to initialize cpg: (%d)\n", (int) cs_err); |
| 1388 |
ast_rwlock_unlock(&init_cpg_lock); |
| 1389 |
ast_debug(5, "load_module unlock\n"); |
| 1390 |
goto failed; |
| 1391 |
} |
| 1392 |
|
| 1393 |
ast_copy_string(name.value, "asterisk", sizeof(name.value)); |
| 1394 |
name.length = strlen(name.value); |
| 1395 |
|
| 1396 |
if ((cs_err = cpg_join(cpg_handle, &name)) != CS_OK) { |
| 1397 |
ast_log(LOG_ERROR, "Failed to join: (%d)\n", (int) cs_err); |
| 1398 |
ast_rwlock_unlock(&init_cpg_lock); |
| 1399 |
ast_debug(5, "load_module unlock\n"); |
| 1400 |
goto failed; |
| 1401 |
} |
| 1402 |
|
| 1403 |
if (pipe(dispatch_thread.alert_pipe) == -1) { |
| 1404 |
ast_log(LOG_ERROR, "Failed to create alert pipe: %s (%d)\n", |
| 1405 |
strerror(errno), errno); |
| 1406 |
ast_rwlock_unlock(&init_cpg_lock); |
| 1407 |
ast_debug(5, "load_module unlock\n"); |
| 1408 |
goto failed; |
| 1409 |
} |
| 1410 |
corosync_node_joined = 1; |
| 1411 |
|
| 1412 |
ast_rwlock_unlock(&init_cpg_lock); |
| 1413 |
ast_debug(5, "load_module unlock\n"); |
| 1414 |
if (corosync_pthread_create_background(&dispatch_thread.id, NULL, |
| 1415 |
dispatch_thread_handler, NULL)) { |
| 1416 |
ast_log(LOG_ERROR, "Error starting CPG dispatch thread.\n"); |
| 1417 |
goto failed; |
| 1418 |
} |
| 1419 |
|
| 1420 |
ast_cli_register_multiple(corosync_cli, ARRAY_LEN(corosync_cli)); |
| 1421 |
} else { |
| 1422 |
goto failed; |
| 1423 |
} |
| 1424 |
|
| 1425 |
return AST_MODULE_LOAD_SUCCESS; |
| 1426 |
|
| 1427 |
failed: |
| 1428 |
cleanup_module(); |
| 1429 |
|
| 1430 |
return AST_MODULE_LOAD_DECLINE; |
| 1431 |
} |
| 1432 |
|
| 1433 |
static int unload_module(void) |
| 1434 |
{ |
| 1435 |
ast_cli_unregister_multiple(corosync_cli, ARRAY_LEN(corosync_cli)); |
| 1436 |
|
| 1437 |
cleanup_module(); |
| 1438 |
|
| 1439 |
return 0; |
| 1440 |
} |
| 1441 |
|
| 1442 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Corosync"); |