提交 a1b8d5b5 authored 作者: Mitch Capper's avatar Mitch Capper
...@@ -537,6 +537,8 @@ switch_status_t config_hb(hac_hb_t** hb, switch_xml_t root) ...@@ -537,6 +537,8 @@ switch_status_t config_hb(hac_hb_t** hb, switch_xml_t root)
cur->lport = atoi(port); cur->lport = atoi(port);
cur->ttl = ttl; cur->ttl = ttl;
cur->next = NULL; cur->next = NULL;
switch_thread_rwlock_create(&cur->recv_lock, config.pool);
switch_thread_rwlock_create(&cur->send_lock, config.pool);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
fail: fail:
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
...@@ -611,9 +613,7 @@ switch_status_t config_general(switch_xml_t root) ...@@ -611,9 +613,7 @@ switch_status_t config_general(switch_xml_t root)
value = switch_xml_attr(x_list, "value"); value = switch_xml_attr(x_list, "value");
if(!switch_strlen_zero(value)) { if(!switch_strlen_zero(value)) {
config.general.nodeid = value; config.general.nodeid = value;
switch_thread_rwlock_wrlock(hac_globals.lock); switch_atomic_set(&hac_globals.nodeid, (switch_strlen_zero(value) ? -1 : atoi(value)));
hac_globals.nodeid = (switch_strlen_zero(value) ? -1 : atoi(value));
switch_thread_rwlock_unlock(hac_globals.lock);
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing nodeid value in general section.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing nodeid value in general section.\n");
goto fail; goto fail;
...@@ -623,31 +623,12 @@ switch_status_t config_general(switch_xml_t root) ...@@ -623,31 +623,12 @@ switch_status_t config_general(switch_xml_t root)
goto fail; goto fail;
} }
if((x_list = switch_xml_child(root, "master-slave-ratio"))) {
const char* value = NULL;
value = switch_xml_attr(x_list, "value");
if(!switch_strlen_zero(value)) {
config.general.msr = value;
switch_thread_rwlock_wrlock(hac_globals.lock);
hac_globals.msr = (switch_strlen_zero(value) ? "4:1" : value);
switch_thread_rwlock_unlock(hac_globals.lock);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing master-slave-ratio value in general section.\n");
goto fail;
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing master-slave-ratio element in general section.\n");
goto fail;
}
if((x_list = switch_xml_child(root, "timer-a"))) { if((x_list = switch_xml_child(root, "timer-a"))) {
const char* value = NULL; const char* value = NULL;
value = switch_xml_attr(x_list, "value"); value = switch_xml_attr(x_list, "value");
if(!switch_strlen_zero(value)) { if(!switch_strlen_zero(value)) {
config.general.timer_a = value; config.general.timer_a = value;
switch_thread_rwlock_wrlock(hac_globals.lock); switch_atomic_set(&hac_globals.timer_a_dur, (switch_strlen_zero(value) ? 60 : atoi(value)));
hac_globals.timer_a_dur = (switch_strlen_zero(value) ? 60 : atoi(value));
switch_thread_rwlock_unlock(hac_globals.lock);
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing timer-a value in general section.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing timer-a value in general section.\n");
goto fail; goto fail;
...@@ -662,9 +643,7 @@ switch_status_t config_general(switch_xml_t root) ...@@ -662,9 +643,7 @@ switch_status_t config_general(switch_xml_t root)
value = switch_xml_attr(x_list, "value"); value = switch_xml_attr(x_list, "value");
if(!switch_strlen_zero(value)) { if(!switch_strlen_zero(value)) {
config.general.timer_b = value; config.general.timer_b = value;
switch_thread_rwlock_wrlock(hac_globals.lock); switch_atomic_set(&hac_globals.timer_b_dur, (switch_strlen_zero(value) ? 60 : atoi(value)));
hac_globals.timer_b_dur = (switch_strlen_zero(value) ? 60 : atoi(value));
switch_thread_rwlock_unlock(hac_globals.lock);
} }
} }
...@@ -673,17 +652,13 @@ switch_status_t config_general(switch_xml_t root) ...@@ -673,17 +652,13 @@ switch_status_t config_general(switch_xml_t root)
value = switch_xml_attr(x_list, "value"); value = switch_xml_attr(x_list, "value");
if(!switch_strlen_zero(value)) { if(!switch_strlen_zero(value)) {
config.general.failure_threshold = value; config.general.failure_threshold = value;
switch_thread_rwlock_wrlock(hac_globals.lock); switch_atomic_set(&hac_globals.failure_threshold, (switch_strlen_zero(value) ? 5000 : atoi(value)));
hac_globals.failure_threshold = (switch_strlen_zero(value) ? 5000 : atoi(value));
switch_thread_rwlock_unlock(hac_globals.lock);
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "HAC: Missing failure-threshold value in general section; defaulted to 5000ms.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "HAC: Missing failure-threshold value in general section; defaulted to 5000ms.\n");
} }
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "HAC: Missing failure-threshold element in general section; defaulting to 5000ms.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "HAC: Missing failure-threshold element in general section; defaulting to 5000ms.\n");
switch_thread_rwlock_wrlock(hac_globals.lock); switch_atomic_set(&hac_globals.failure_threshold, 5000);
hac_globals.failure_threshold = 5000;
switch_thread_rwlock_unlock(hac_globals.lock);
} }
config.general.heartbeat = switch_core_alloc(config.pool, sizeof(hac_hb_t)); config.general.heartbeat = switch_core_alloc(config.pool, sizeof(hac_hb_t));
...@@ -692,9 +667,9 @@ switch_status_t config_general(switch_xml_t root) ...@@ -692,9 +667,9 @@ switch_status_t config_general(switch_xml_t root)
if(config_hb(&cur, x_list) != SWITCH_STATUS_SUCCESS) { if(config_hb(&cur, x_list) != SWITCH_STATUS_SUCCESS) {
goto fail; goto fail;
} }
switch_thread_rwlock_wrlock(hac_globals.lock); switch_thread_rwlock_wrlock(hac_globals.hb_lock);
hac_globals.hb = config.general.heartbeat; hac_globals.hb = config.general.heartbeat;
switch_thread_rwlock_unlock(hac_globals.lock); switch_thread_rwlock_unlock(hac_globals.hb_lock);
} }
if(switch_strlen_zero(config.general.heartbeat->address)) { if(switch_strlen_zero(config.general.heartbeat->address)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing heartbeat element in general section.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing heartbeat element in general section.\n");
...@@ -743,9 +718,7 @@ switch_status_t do_config(switch_bool_t reload) ...@@ -743,9 +718,7 @@ switch_status_t do_config(switch_bool_t reload)
} }
switch_xml_free(xml); switch_xml_free(xml);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "HAC: Configuration loaded.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "HAC: Configuration loaded.\n");
switch_thread_rwlock_wrlock(hac_globals.lock); switch_atomic_set(&hac_globals.state, HAC_PEER_STARTING);
hac_globals.state = HAC_PEER_STARTING;
switch_thread_rwlock_unlock(hac_globals.lock);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
failconfig: failconfig:
......
...@@ -50,8 +50,10 @@ switch_status_t initialize_interfaces() ...@@ -50,8 +50,10 @@ switch_status_t initialize_interfaces()
/* First, make sure all interfaces exist. */ /* First, make sure all interfaces exist. */
cur_int = config.network.interfaces; cur_int = config.network.interfaces;
while(cur_int != NULL) { while(cur_int != NULL) {
/* We cannot list a vlan if it is not up */
if(!strcasestr(cur_int->name, "vlan") && !strchr(cur_int->name, '.')) {
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ip link list %s", cur_int->name); snprintf(cmd, 256, "ip link list %s", cur_int->name);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result) {
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
...@@ -62,11 +64,43 @@ switch_status_t initialize_interfaces() ...@@ -62,11 +64,43 @@ switch_status_t initialize_interfaces()
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
switch_safe_free(cmd_result); switch_safe_free(cmd_result);
}
cur_int = cur_int->next; cur_int = cur_int->next;
} }
/* After we know they exist, then take controll over them. */ /* After we know they exist, then take control over them. */
cur_int = config.network.interfaces; cur_int = config.network.interfaces;
while(cur_int != NULL) { while(cur_int != NULL) {
switch_bool_t bridge_device = SWITCH_FALSE;
switch_bool_t vlan_device = SWITCH_FALSE;
/* Check to see if it is a VLAN device */
FILE * fp = NULL;
memset(cmd, 0, 256);
snprintf(cmd, 256, "/proc/net/vlan/%s", cur_int->name);
if((fp = fopen(cmd, "r")) != NULL) {
fclose(fp);
vlan_device = SWITCH_TRUE;
} else if(strcasestr(cur_int->name, "vlan")) {
vlan_device = SWITCH_TRUE;
} else if(strchr(cur_int->name, '.')) {
vlan_device = SWITCH_TRUE;
}
/* Cannot get driver of vlan interface which is down, and
regardless, it will always match underlying device. */
if(vlan_device == SWITCH_FALSE) {
/* Get the driver of the device to see if it is a bridge */
memset(cmd, 0, 256);
snprintf(cmd, 256, "ethtool -i %s 2>&1", cur_int->name);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "does not exist"))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to determine driver of interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: unable to determine driver of interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
switch_safe_free(cmd_result);
return SWITCH_STATUS_FALSE;
}
if(strcasestr(cmd_result, "bridge")) {
bridge_device = SWITCH_TRUE;
}
switch_safe_free(cmd_result);
}
/* This clears routes and puts the interface in a DOWN state /* This clears routes and puts the interface in a DOWN state
which then requires us to bring it back up. This helps to which then requires us to bring it back up. This helps to
check for issues to make sure we can actually bring it back check for issues to make sure we can actually bring it back
...@@ -74,18 +108,23 @@ switch_status_t initialize_interfaces() ...@@ -74,18 +108,23 @@ switch_status_t initialize_interfaces()
is wrong, but not bring it back up. This helps us check is wrong, but not bring it back up. This helps us check
that condition. */ that condition. */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ifdown %s 2>&1", cur_int->name); snprintf(cmd, 256, "ifdown %s 2>&1", cur_int->name);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "does not exist"))) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "does not exist"))) {
if(vlan_device == SWITCH_FALSE) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); fprintf(stderr, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
switch_safe_free(cmd_result); switch_safe_free(cmd_result);
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
}
switch_safe_free(cmd_result); switch_safe_free(cmd_result);
if(vlan_device == SWITCH_FALSE) {
/* We do not want to have an IP address conflict while starting /* We do not want to have an IP address conflict while starting
this node, so we flush the addresses. */ this node, so we flush the addresses. If this is a vlan, we
will not be able to reference the dev if it is down. We
expect the act of taking it down to remove the IPs. */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ip addr flush %s 2>&1", cur_int->name); snprintf(cmd, 256, "ip addr flush %s 2>&1", cur_int->name);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "does not exist"))) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "does not exist"))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); fprintf(stderr, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
...@@ -93,10 +132,12 @@ switch_status_t initialize_interfaces() ...@@ -93,10 +132,12 @@ switch_status_t initialize_interfaces()
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
switch_safe_free(cmd_result); switch_safe_free(cmd_result);
}
if(vlan_device == SWITCH_FALSE && bridge_device == SWITCH_FALSE) {
/* If speed and duplex are specified, set them and disable autoneg */ /* If speed and duplex are specified, set them and disable autoneg */
if(!switch_strlen_zero(cur_int->speed) && !switch_strlen_zero(cur_int->duplex)) { if(!switch_strlen_zero(cur_int->speed) && !switch_strlen_zero(cur_int->duplex)) {
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ethtool -s %s speed %s duplex %s autoneg off 2>&1", cur_int->name, cur_int->speed, cur_int->duplex); snprintf(cmd, 256, "ethtool -s %s speed %s duplex %s autoneg off 2>&1", cur_int->name, cur_int->speed, cur_int->duplex);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "Cannot set new settings"))) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "Cannot set new settings"))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to set speed and duplex on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to set speed and duplex on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: unable to set speed and duplex on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); fprintf(stderr, "HAC: unable to set speed and duplex on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
...@@ -108,7 +149,7 @@ switch_status_t initialize_interfaces() ...@@ -108,7 +149,7 @@ switch_status_t initialize_interfaces()
if(!switch_strlen_zero(cur_int->duplex)) { if(!switch_strlen_zero(cur_int->duplex)) {
/* Set the duplex on the interface if specified */ /* Set the duplex on the interface if specified */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ethtool -s %s duplex %s 2>&1", cur_int->name, cur_int->duplex); snprintf(cmd, 256, "ethtool -s %s duplex %s 2>&1", cur_int->name, cur_int->duplex);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "Cannot set new settings"))) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "Cannot set new settings"))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to set duplex on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to set duplex on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: unable to set duplex on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); fprintf(stderr, "HAC: unable to set duplex on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
...@@ -120,7 +161,7 @@ switch_status_t initialize_interfaces() ...@@ -120,7 +161,7 @@ switch_status_t initialize_interfaces()
if(!switch_strlen_zero(cur_int->speed)) { if(!switch_strlen_zero(cur_int->speed)) {
/* Set the speed on the interface if specified */ /* Set the speed on the interface if specified */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ethtool -s %s speed %s 2>&1", cur_int->name, cur_int->speed); snprintf(cmd, 256, "ethtool -s %s speed %s 2>&1", cur_int->name, cur_int->speed);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "Cannot set new settings"))) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "Cannot set new settings"))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to set speed on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to set speed on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: unable to set speed on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); fprintf(stderr, "HAC: unable to set speed on interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
...@@ -130,11 +171,16 @@ switch_status_t initialize_interfaces() ...@@ -130,11 +171,16 @@ switch_status_t initialize_interfaces()
switch_safe_free(cmd_result); switch_safe_free(cmd_result);
} }
} }
}
/* Now we bring the interface back up to ensure we *can* bring /* Now we bring the interface back up to ensure we *can* bring
it up. We turn off ARP so that when we assign IPs to it, we it up. We turn off ARP so that when we assign IPs to it, we
do not immediately start talking over it. */ do not immediately start talking over it. */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ip link set %s up arp off 2>&1", cur_int->name); if(vlan_device == SWITCH_TRUE) {
snprintf(cmd, 256, "ifup %s && sleep 1 && ip link set %s arp off 2>&1", cur_int->name, cur_int->name);
} else {
snprintf(cmd, 256, "ip link set %s up arp off 2>&1", cur_int->name);
}
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "does not exist"))) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && strstr(cmd_result, "does not exist"))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); fprintf(stderr, "HAC: unable to control interface '%s': %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
...@@ -142,10 +188,11 @@ switch_status_t initialize_interfaces() ...@@ -142,10 +188,11 @@ switch_status_t initialize_interfaces()
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
switch_safe_free(cmd_result); switch_safe_free(cmd_result);
if(bridge_device == SWITCH_FALSE && vlan_device == SWITCH_FALSE) {
/* Run an internal test on the interface to help check to see /* Run an internal test on the interface to help check to see
if it is good. */ if it is good. */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "sleep 1 && ethtool -t %s online 2>&1", cur_int->name); snprintf(cmd, 256, "sleep 1 && ethtool -t %s online 2>&1", cur_int->name);
count = 0; count = 0;
while(((status = execute_command(cmd, &cmd_result)) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && !strstr(cmd_result, "PASS"))) && count < 30) { while(((status = execute_command(cmd, &cmd_result)) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && !strstr(cmd_result, "PASS"))) && count < 30) {
switch_yield(1000); switch_yield(1000);
...@@ -158,9 +205,10 @@ switch_status_t initialize_interfaces() ...@@ -158,9 +205,10 @@ switch_status_t initialize_interfaces()
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
switch_safe_free(cmd_result); switch_safe_free(cmd_result);
}
/* Check for carrier signal on interface */ /* Check for carrier signal on interface */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ip link list dev %s 2>&1", cur_int->name); snprintf(cmd, 256, "ip link list dev %s 2>&1", cur_int->name);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && (strstr(cmd_result, "NO-CARRIER") || strstr(cmd_result, "DOWN")))) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && (strstr(cmd_result, "NO-CARRIER") || strstr(cmd_result, "DOWN")))) {
if(cmd_result && strstr(cmd_result, "NO-CARRIER")) { if(cmd_result && strstr(cmd_result, "NO-CARRIER")) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: interface '%s' loss of carrier detected\n", cur_int->name); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: interface '%s' loss of carrier detected\n", cur_int->name);
...@@ -176,9 +224,10 @@ switch_status_t initialize_interfaces() ...@@ -176,9 +224,10 @@ switch_status_t initialize_interfaces()
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
switch_safe_free(cmd_result); switch_safe_free(cmd_result);
/* Set the duplex on the interface if specified */ if(vlan_device == SWITCH_FALSE && bridge_device == SWITCH_FALSE) {
/* Check the duplex and speed on the interface if specified */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ethtool %s 2>&1", cur_int->name); snprintf(cmd, 256, "ethtool %s 2>&1", cur_int->name);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to query interface '%s' for duplex and speed: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: unable to query interface '%s' for duplex and speed: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: unable to query interface '%s' for duplex and speed: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); fprintf(stderr, "HAC: unable to query interface '%s' for duplex and speed: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
...@@ -186,7 +235,7 @@ switch_status_t initialize_interfaces() ...@@ -186,7 +235,7 @@ switch_status_t initialize_interfaces()
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
if(!switch_strlen_zero(cur_int->duplex)) { if(!switch_strlen_zero(cur_int->duplex)) {
sprintf(expected_duplex, "Duplex: %s", cur_int->duplex); snprintf(expected_duplex, 16, "Duplex: %s", cur_int->duplex);
if(!strcasestr(cmd_result, expected_duplex)) { if(!strcasestr(cmd_result, expected_duplex)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: interface '%s' duplex not set correctly: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: interface '%s' duplex not set correctly: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: interface '%s' duplex not set correctly: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); fprintf(stderr, "HAC: interface '%s' duplex not set correctly: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
...@@ -195,7 +244,7 @@ switch_status_t initialize_interfaces() ...@@ -195,7 +244,7 @@ switch_status_t initialize_interfaces()
} }
} }
if(!switch_strlen_zero(cur_int->speed)) { if(!switch_strlen_zero(cur_int->speed)) {
sprintf(expected_speed, "Speed: %sMb/s", cur_int->speed); snprintf(expected_speed, 16, "Speed: %sMb/s", cur_int->speed);
if(!strcasestr(cmd_result, expected_speed)) { if(!strcasestr(cmd_result, expected_speed)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: interface '%s' speed not set correctly: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: interface '%s' speed not set correctly: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: interface '%s' speed not set correctly: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); fprintf(stderr, "HAC: interface '%s' speed not set correctly: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
...@@ -204,6 +253,7 @@ switch_status_t initialize_interfaces() ...@@ -204,6 +253,7 @@ switch_status_t initialize_interfaces()
} }
} }
switch_safe_free(cmd_result); switch_safe_free(cmd_result);
}
cur_int = cur_int->next; cur_int = cur_int->next;
} }
/* If we get here, we are fairly confident all the interfaces are /* If we get here, we are fairly confident all the interfaces are
...@@ -336,7 +386,7 @@ switch_status_t verify_interfaces() ...@@ -336,7 +386,7 @@ switch_status_t verify_interfaces()
while(cur_int != NULL) { while(cur_int != NULL) {
/* Check for carrier signal on interface */ /* Check for carrier signal on interface */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ip link list dev %s 2>&1", cur_int->name); snprintf(cmd, 256, "ip link list dev %s 2>&1", cur_int->name);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && (strstr(cmd_result, "NO-CARRIER") || strstr(cmd_result, "DOWN")))) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && (strstr(cmd_result, "NO-CARRIER") || strstr(cmd_result, "DOWN")))) {
if(cmd_result && strstr(cmd_result, "NO-CARRIER")) { if(cmd_result && strstr(cmd_result, "NO-CARRIER")) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: interface '%s' loss of carrier detected\n", cur_int->name); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: interface '%s' loss of carrier detected\n", cur_int->name);
...@@ -355,7 +405,7 @@ switch_status_t verify_interfaces() ...@@ -355,7 +405,7 @@ switch_status_t verify_interfaces()
/* Run an internal test on the interface to help check to see /* Run an internal test on the interface to help check to see
if it is good. */ if it is good. */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ethtool -t %s 2>&1", cur_int->name); snprintf(cmd, 256, "ethtool -t %s 2>&1", cur_int->name);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && !strstr(cmd_result, "PASS"))) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS || !cmd_result || (cmd_result && !strstr(cmd_result, "PASS"))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: interface '%s' failed test: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: interface '%s' failed test: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
fprintf(stderr, "HAC: interface '%s' failed test: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result)); fprintf(stderr, "HAC: interface '%s' failed test: %s\n", cur_int->name, (switch_strlen_zero(cmd_result) ? "unkown error" : cmd_result));
...@@ -377,7 +427,7 @@ switch_status_t routes_up(const hac_route_t *routes) ...@@ -377,7 +427,7 @@ switch_status_t routes_up(const hac_route_t *routes)
while(cur != NULL) { while(cur != NULL) {
/* Check for carrier signal on interface */ /* Check for carrier signal on interface */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ip route add %s via %s dev %s metric %s 2>&1", cur->destination, cur->gateway, cur->dev, (switch_strlen_zero(cur->metric) ? "0" : cur->metric)); snprintf(cmd, 256, "ip route add %s via %s dev %s metric %s 2>&1", cur->destination, cur->gateway, cur->dev, (switch_strlen_zero(cur->metric) ? "0" : cur->metric));
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: error bringing up route '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: error bringing up route '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result));
fprintf(stderr, "HAC: error bringing up route '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result)); fprintf(stderr, "HAC: error bringing up route '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result));
...@@ -399,7 +449,7 @@ switch_status_t routes_down(const hac_route_t *routes) ...@@ -399,7 +449,7 @@ switch_status_t routes_down(const hac_route_t *routes)
while(cur != NULL) { while(cur != NULL) {
/* Check for carrier signal on interface */ /* Check for carrier signal on interface */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ip route del %s via %s dev %s metric %s 2>&1", cur->destination, cur->gateway, cur->dev, (switch_strlen_zero(cur->metric) ? "0" : cur->metric)); snprintf(cmd, 256, "ip route del %s via %s dev %s metric %s 2>&1", cur->destination, cur->gateway, cur->dev, (switch_strlen_zero(cur->metric) ? "0" : cur->metric));
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: error bringing down route '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: error bringing down route '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result));
fprintf(stderr, "HAC: error bringing down route '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result)); fprintf(stderr, "HAC: error bringing down route '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result));
...@@ -421,7 +471,7 @@ switch_status_t ips_up(const hac_ip_t *ips) ...@@ -421,7 +471,7 @@ switch_status_t ips_up(const hac_ip_t *ips)
while(cur != NULL) { while(cur != NULL) {
/* Check for carrier signal on interface */ /* Check for carrier signal on interface */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ip addr add %s dev %s 2>&1", cur->cidr, cur->dev); snprintf(cmd, 256, "ip addr add %s dev %s 2>&1", cur->cidr, cur->dev);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: error adding IP address '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: error adding IP address '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result));
fprintf(stderr, "HAC: error adding IP address '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result)); fprintf(stderr, "HAC: error adding IP address '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result));
...@@ -443,7 +493,7 @@ switch_status_t ips_down(const hac_ip_t *ips) ...@@ -443,7 +493,7 @@ switch_status_t ips_down(const hac_ip_t *ips)
while(cur != NULL) { while(cur != NULL) {
/* Check for carrier signal on interface */ /* Check for carrier signal on interface */
memset(cmd, 0, 256); memset(cmd, 0, 256);
sprintf(cmd, "ip addr del %s dev %s 2>&1", cur->cidr, cur->dev); snprintf(cmd, 256, "ip addr del %s dev %s 2>&1", cur->cidr, cur->dev);
if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS) { if(execute_command(cmd, &cmd_result) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: error removing IP address '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: error removing IP address '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result));
fprintf(stderr, "HAC: error removing IP address '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result)); fprintf(stderr, "HAC: error removing IP address '%s': %s\n", cmd, (switch_strlen_zero(cmd_result) ? "unknown error" : cmd_result));
......
...@@ -114,9 +114,10 @@ static switch_status_t send_event(switch_event_t* event, switch_bool_t hb) ...@@ -114,9 +114,10 @@ static switch_status_t send_event(switch_event_t* event, switch_bool_t hb)
/* Only one thread can write to the socket at one time, and we don't want /* Only one thread can write to the socket at one time, and we don't want
them changing in the middle of writes */ them changing in the middle of writes */
switch_thread_rwlock_wrlock(hac_globals.lock); switch_thread_rwlock_rdlock(hac_globals.hb_lock);
cur_hb = hac_globals.hb; cur_hb = hac_globals.hb;
while(cur_hb != NULL) { while(cur_hb != NULL) {
switch_thread_rwlock_wrlock(cur_hb->send_lock);
if(hb == SWITCH_TRUE) { if(hb == SWITCH_TRUE) {
tstamp_hb = cur_hb->last_hb.tv_sec * 1000; tstamp_hb = cur_hb->last_hb.tv_sec * 1000;
tstamp_hb += cur_hb->last_hb.tv_usec / 1000; tstamp_hb += cur_hb->last_hb.tv_usec / 1000;
...@@ -128,9 +129,10 @@ static switch_status_t send_event(switch_event_t* event, switch_bool_t hb) ...@@ -128,9 +129,10 @@ static switch_status_t send_event(switch_event_t* event, switch_bool_t hb)
} else { } else {
switch_socket_sendto(cur_hb->udp_sock, cur_hb->addr, 0, buf, &len); switch_socket_sendto(cur_hb->udp_sock, cur_hb->addr, 0, buf, &len);
} }
switch_thread_rwlock_unlock(cur_hb->send_lock);
cur_hb = cur_hb->next; cur_hb = cur_hb->next;
} }
switch_thread_rwlock_unlock(hac_globals.lock); switch_thread_rwlock_unlock(hac_globals.hb_lock);
switch_safe_free(packet); switch_safe_free(packet);
switch_safe_free(buf); switch_safe_free(buf);
...@@ -139,32 +141,58 @@ static switch_status_t send_event(switch_event_t* event, switch_bool_t hb) ...@@ -139,32 +141,58 @@ static switch_status_t send_event(switch_event_t* event, switch_bool_t hb)
failsend: failsend:
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
/*
static void clear_event_hash() /**
* Each call to clean_event_hash() will loop through all the events in the
* event hash table and remove events that have been processed which are
* older than max-latency * 10. This gives plenty of time for duplicate event
* elimination in case of temporary excessive latency.
*/
static void clean_event_hash()
{ {
switch_hash_index_t *hi = NULL; switch_hash_index_t *hi = NULL;
void *val; void *val;
hac_event_t *hevent; hac_event_t *hevent;
uint32_t max_latency = 0;
struct timeval cur_time;
max_latency = (uint32_t)switch_atomic_read(&hac_globals.max_latency);
gettimeofday(&cur_time, NULL);
switch_thread_rwlock_wrlock(hac_globals.event_hash_lock);
for(hi = switch_hash_first(NULL, hac_globals.event_hash); hi; hi = switch_hash_next(hi)) { for(hi = switch_hash_first(NULL, hac_globals.event_hash); hi; hi = switch_hash_next(hi)) {
switch_hash_this(hi, NULL, NULL, &val); switch_hash_this(hi, NULL, NULL, &val);
if(val) { if(val) {
uint32_t sec_diff = 0, diff_in_msec = 0;
hevent = (hac_event_t*)val; hevent = (hac_event_t*)val;
sec_diff = cur_time.tv_sec - hevent->arrived.tv_sec;
} diff_in_msec = (sec_diff * 1000) + ((cur_time.tv_usec - hevent->arrived.tv_usec) / 1000);
if(diff_in_msec > (10 * max_latency) && hevent->refcount == 0) {
switch_core_hash_delete(hac_globals.event_hash, hevent->uuid);
switch_safe_free(val); switch_safe_free(val);
} }
}
}
switch_thread_rwlock_unlock(hac_globals.event_hash_lock);
} }
/**
* Each call to receive_events() pulls up to 1000 events from each multicast
* group and stores them in a hash based on the event UUID. It also removes
* duplicates by dipping the hash table for the UUID to see if we already
* have the event. If no events are retrieved, it returns immediately.
*/
static void receive_events() static void receive_events()
{ {
//switch_event_t *event = NULL; //switch_event_t *event = NULL;
char *buf = NULL;//, *myaddr = NULL, *packet = NULL; char *buf = NULL, *packet = NULL;
switch_sockaddr_t *addr; switch_sockaddr_t *addr = NULL;
size_t len = HAC_BUFFSIZE; size_t len = HAC_BUFFSIZE;
switch_status_t status; switch_status_t status;
hac_hb_t *cur_hb = NULL; hac_hb_t *cur_hb = NULL;
switch_event_t *event = NULL;
char *m = NULL, *var = NULL, *val = NULL, *term = NULL, tmpname[128];
buf = (char*) malloc(HAC_BUFFSIZE); buf = (char*) malloc(HAC_BUFFSIZE);
switch_assert(buf); switch_assert(buf);
...@@ -172,17 +200,104 @@ static void receive_events() ...@@ -172,17 +200,104 @@ static void receive_events()
switch_sockaddr_info_get(&addr, NULL, SWITCH_UNSPEC, 0, 0, hac_globals.pool); switch_sockaddr_info_get(&addr, NULL, SWITCH_UNSPEC, 0, 0, hac_globals.pool);
switch_thread_rwlock_rdlock(hac_globals.hb_lock);
cur_hb = hac_globals.hb; cur_hb = hac_globals.hb;
while(cur_hb != NULL && hac_globals.mod_state == HAC_LOOPING) { while(cur_hb != NULL && switch_atomic_read(&hac_globals.mod_state) == HAC_LOOPING) {
/ * Read up to 1000 events from each multicast group at a time */ /* Read up to 1000 events from each multicast group at a time */
/* size_t count = 0; size_t count = 0;
switch_thread_rwlock_wrlock(cur_hb->recv_lock);
while((status = switch_socket_recvfrom(addr, cur_hb->udp_sock, 0, buf, &len)) == SWITCH_STATUS_SUCCESS && len && count < 1000) { while((status = switch_socket_recvfrom(addr, cur_hb->udp_sock, 0, buf, &len)) == SWITCH_STATUS_SUCCESS && len && count < 1000) {
count++; count++;
packet = buf;
if((m = strchr(packet, (int) MAGIC[0])) != 0) {
if(!strncmp((char*) MAGIC, m, strlen((char*)MAGIC))) {
*m = '\0';
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "HAC: Failed to find entire magic string\n");
continue;
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "HAC: Failed to find start of magic string\n");
continue;
}
m += 21;
status = SWITCH_STATUS_FALSE;
if(strcasestr(m, "HAC-Event-Type: HAC_HEARTBEAT")) {
status = switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, HAC_HEARTBEAT);
} else if(strcasestr(m, "HAC-Event-Type: HAC_CALL_UP")) {
} else if(strcasestr(m, "HAC-Event-Type: HAC_CALL_DOWN")) {
} else if(strcasestr(m, "HAC-Event-Type: HAC_LIMIT_UP")) {
} else if(strcasestr(m, "HAC-Event-Type: HAC_LIMIT_DOWN")) {
} else if(strcasestr(m, "HAC-Event-Type: HAC_REGISTRATION")) {
}
if(status != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "HAC: Unable to create event class for received event\n");
continue;
}
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Multicast", "yes");
var = packet;
while(*var) {
const char *uuid = NULL;
hac_event_t *hevent = NULL;
if((val = strchr(var, ':')) != 0) {
*val++ = '\0';
while (*val == ' ') {
val++;
}
if((term = strchr(val, '\r')) != 0 || (term = strchr(val, '\n')) != 0) {
*term = '\0';
while(*term == '\r' || *term == '\n') {
term++;
}
}
switch_url_decode(val);
switch_snprintf(tmpname, sizeof(tmpname), "Orig-%s", var);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, tmpname, val);
var = term + 1;
} else {
break;
}
if(var && strlen(var) > 1) {
switch_event_add_body(event, "%s", var);
}
uuid = switch_event_get_header(event, "HAC-Event-UUID");
if(!uuid) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "HAC: Event missing UUID\n");
continue;
}
/* If an event with the same UUID exists, then this is
* a duplicate event because of redundant links. */
switch_thread_rwlock_rdlock(hac_globals.event_hash_lock);
hevent = (hac_event_t*) switch_core_hash_find(hac_globals.event_hash, uuid);
switch_thread_rwlock_unlock(hac_globals.event_hash_lock);
if(hevent) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "HAC: Discarding duplicate event.\n");
} else {
hevent = malloc(sizeof(hevent));
switch_assert(hevent);
memset(hevent, 0, sizeof(hevent));
hevent->event = event;
hevent->processed = SWITCH_FALSE;
hevent->uuid = uuid;
gettimeofday(&hevent->arrived, NULL);
switch_thread_rwlock_wrlock(hac_globals.event_hash_lock);
switch_core_hash_insert(hac_globals.event_hash, uuid, hevent);
switch_thread_rwlock_unlock(hac_globals.event_hash_lock);
}
} }
}
switch_thread_rwlock_unlock(cur_hb->recv_lock);
cur_hb = cur_hb->next; cur_hb = cur_hb->next;
} }
switch_thread_rwlock_unlock(hac_globals.hb_lock);
} }
*/
static void send_hb() static void send_hb()
{ {
switch_event_t *local_event; switch_event_t *local_event;
...@@ -190,14 +305,16 @@ static void send_hb() ...@@ -190,14 +305,16 @@ static void send_hb()
char buf[256]; char buf[256];
memset(buf, 0, 256); memset(buf, 0, 256);
switch_event_add_header_string(local_event, SWITCH_STACK_TOP, "HAC-Event-Type", "HAC_HEARTBEAT");
switch_thread_rwlock_rdlock(hac_globals.lock); switch_thread_rwlock_rdlock(hac_globals.lock);
switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "HAC-Peer", hac_globals.hostname); switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "HAC-Peer", hac_globals.hostname);
sprintf(buf, "%d", hac_globals.nodeid); switch_thread_rwlock_unlock(hac_globals.lock);
sprintf(buf, "%d", switch_atomic_read(&hac_globals.nodeid));
switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "HAC-Node-ID", buf); switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "HAC-Node-ID", buf);
memset(buf, 0, 256); memset(buf, 0, 256);
sprintf(buf, "%ld", hac_globals.rand_val); sprintf(buf, "%d", switch_atomic_read(&hac_globals.rand_val));
switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "HAC-Priority", buf); switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "HAC-Priority", buf);
switch(hac_globals.state) { switch(switch_atomic_read(&hac_globals.state)) {
case HAC_PEER_STARTING: case HAC_PEER_STARTING:
switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "State", "STARTING"); switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "State", "STARTING");
break; break;
...@@ -224,11 +341,11 @@ static void send_hb() ...@@ -224,11 +341,11 @@ static void send_hb()
switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "State", "UNKNOWN"); switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "State", "UNKNOWN");
break; break;
} }
switch_thread_rwlock_unlock(hac_globals.lock);
if(send_event(local_event, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) { if(send_event(local_event, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) {
hac_notify_admin("Failed to send heartbeat."); hac_notify_admin("Failed to send heartbeat.");
} }
switch_event_destroy(&local_event);
} }
} }
...@@ -305,6 +422,31 @@ static void event_handler(switch_event_t *event) ...@@ -305,6 +422,31 @@ static void event_handler(switch_event_t *event)
} }
} }
static void listen_for_peers()
{
}
static void pick_master_slave_state()
{
}
static void enter_master_slave_state()
{
}
static void *SWITCH_THREAD_FUNC listen_for_events(switch_thread_t *thread, void *obj)
{
while(switch_atomic_read(&hac_globals.mod_state) == HAC_LOOPING) {
receive_events();
clean_event_hash();
}
return NULL;
}
SWITCH_STANDARD_API(ha_cluster_function) SWITCH_STANDARD_API(ha_cluster_function)
{ {
do_config(SWITCH_TRUE); do_config(SWITCH_TRUE);
...@@ -326,12 +468,23 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_ha_cluster_load) ...@@ -326,12 +468,23 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_ha_cluster_load)
sigprocmask(SIG_BLOCK, &set, &oset); sigprocmask(SIG_BLOCK, &set, &oset);
memset(&hac_globals, 0, sizeof(hac_globals)); memset(&hac_globals, 0, sizeof(hac_globals));
hac_globals.state = HAC_PEER_STARTING; if(switch_atomic_init(pool) != SWITCH_STATUS_SUCCESS) {
hac_globals.mod_state = HAC_LOOPING; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "HAC: Cannot initialize atomic data structures.\n");
switch_goto_status(SWITCH_STATUS_GENERR, fail);
}
switch_atomic_set(&hac_globals.state, HAC_PEER_STARTING);
switch_atomic_set(&hac_globals.mod_state, HAC_LOOPING);
hac_globals.pool = pool; hac_globals.pool = pool;
switch_atomic_set(&hac_globals.timer_a_dur, 5000); /* 5 second default */
switch_atomic_set(&hac_globals.timer_b_dur, 300000); /* 5 minute default */
switch_atomic_set(&hac_globals.failure_threshold, 3000); /* 3 second default */
switch_atomic_set(&hac_globals.max_latency, 500); /* 500ms default */
gethostname(hac_globals.hostname, sizeof(hac_globals.hostname)); gethostname(hac_globals.hostname, sizeof(hac_globals.hostname));
switch_thread_rwlock_create(&hac_globals.lock, pool); switch_thread_rwlock_create(&hac_globals.lock, pool);
switch_thread_rwlock_create(&hac_globals.hb_lock, pool);
switch_thread_rwlock_create(&hac_globals.event_hash_lock, pool);
switch_thread_rwlock_create(&hac_globals.peer_hash_lock, pool);
switch_core_new_memory_pool(&hac_globals.event_pool); switch_core_new_memory_pool(&hac_globals.event_pool);
switch_core_hash_init(&hac_globals.event_hash, pool); switch_core_hash_init(&hac_globals.event_hash, pool);
switch_core_hash_init(&hac_globals.peer_hash, pool); switch_core_hash_init(&hac_globals.peer_hash, pool);
...@@ -478,11 +631,14 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_ha_cluster_shutdown) ...@@ -478,11 +631,14 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_ha_cluster_shutdown)
/*switch_xml_config_cleanup(instructions);*/ /*switch_xml_config_cleanup(instructions);*/
hac_hb_t *cur_hb = NULL; hac_hb_t *cur_hb = NULL;
hac_globals.mod_state = HAC_SHUTDOWN_RUNTIME; switch_atomic_set(&hac_globals.mod_state, HAC_SHUTDOWN_RUNTIME);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Waiting for runtime thread...\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Waiting for runtime thread...\n");
while(hac_globals.mod_state == HAC_SHUTDOWN_RUNTIME) switch_yield(1000); while(switch_atomic_read(&hac_globals.mod_state) == HAC_SHUTDOWN_RUNTIME) switch_yield(1000);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Commencing module cleanup...\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Commencing module cleanup...\n");
/* TODO: Free event hash elements first */
/* TODO: Free peer hash elements first */
switch_core_hash_destroy(&hac_globals.event_hash); switch_core_hash_destroy(&hac_globals.event_hash);
switch_core_hash_destroy(&hac_globals.peer_hash); switch_core_hash_destroy(&hac_globals.peer_hash);
...@@ -519,23 +675,40 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_ha_cluster_shutdown) ...@@ -519,23 +675,40 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_ha_cluster_shutdown)
SWITCH_MODULE_RUNTIME_FUNCTION(mod_ha_cluster_runtime) SWITCH_MODULE_RUNTIME_FUNCTION(mod_ha_cluster_runtime)
{ {
int arg = 0; int arg = 0;
switch_yield(500000); switch_status_t status;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Starting processing...\n"); switch_threadattr_t *thd_attr = NULL;
hac_globals.mod_state = HAC_LOOPING; switch_thread_t *listener_thread = NULL; /*, *status_thread = NULL, *processor_thread = NULL; */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "HAC: Initializing...\n");
switch_atomic_set(&hac_globals.mod_state, HAC_LOOPING);
if(initialize_interfaces() != SWITCH_STATUS_SUCCESS) { if(initialize_interfaces() != SWITCH_STATUS_SUCCESS) {
switch_core_session_ctl(SCSC_SHUTDOWN_NOW, &arg); switch_core_session_ctl(SCSC_SHUTDOWN_NOW, &arg);
} }
if(initialize_firewall() != SWITCH_STATUS_SUCCESS) { if(initialize_firewall() != SWITCH_STATUS_SUCCESS) {
switch_core_session_ctl(SCSC_SHUTDOWN_NOW, &arg); switch_core_session_ctl(SCSC_SHUTDOWN_NOW, &arg);
} }
while(hac_globals.mod_state == HAC_LOOPING)
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "HAC: Launching listener thread...");
switch_threadattr_create(&thd_attr, hac_globals.pool);
switch_threadattr_detach_set(thd_attr, 1);
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
switch_thread_create(&listener_thread, thd_attr, listen_for_events, NULL, hac_globals.pool);
listen_for_peers();
pick_master_slave_state();
enter_master_slave_state();
while(switch_atomic_read(&hac_globals.mod_state) == HAC_LOOPING)
{ {
/*switch_cond_next();*/ /*switch_cond_next();*/
switch_yield(1000); switch_yield(1000);
send_hb(); send_hb();
} }
if(listener_thread) {
switch_thread_join(&status, listener_thread);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Shutting down...\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Shutting down...\n");
hac_globals.mod_state = HAC_UNLOAD; switch_atomic_set(&hac_globals.mod_state, HAC_UNLOAD);
return SWITCH_STATUS_TERM; return SWITCH_STATUS_TERM;
} }
......
...@@ -23,16 +23,22 @@ ...@@ -23,16 +23,22 @@
extern unsigned char MAGIC[]; extern unsigned char MAGIC[];
/*static char *MARKER = "1"; */ /*static char *MARKER = "1"; */
typedef enum { /* Atomic state type so we don't need rwlocks */
HAC_PEER_UNKNOWN = 0, #define HAC_PEER_UNKNOWN 0
HAC_PEER_STARTING = 1, #define HAC_PEER_STARTING 1
HAC_PEER_MASTER = 2, #define HAC_PEER_MASTER 2
HAC_PEER_SLAVE = 3, #define HAC_PEER_SLAVE 3
HAC_PEER_FAILED_MASTER = 4, #define HAC_PEER_FAILED_MASTER 4
HAC_PEER_FAILED_SLAVE = 5, #define HAC_PEER_FAILED_SLAVE 5
HAC_PEER_STOPPING_MASTER = 6, #define HAC_PEER_STOPPING_MASTER 6
HAC_PEER_STOPPING_SLAVE = 7 #define HAC_PEER_STOPPING_SLAVE 7
} hac_peer_state_t; typedef switch_atomic_t hac_peer_state_t;
/* Atomic state type so we don't need rwlocks */
#define HAC_LOOPING 0
#define HAC_SHUTDOWN_RUNTIME 1
#define HAC_UNLOAD 2
typedef switch_atomic_t hac_state_t;
struct s_hac_rule { struct s_hac_rule {
const char* name; const char* name;
...@@ -157,6 +163,8 @@ struct s_hac_hb { ...@@ -157,6 +163,8 @@ struct s_hac_hb {
switch_socket_t *udp_sock; switch_socket_t *udp_sock;
struct timeval last_hb; struct timeval last_hb;
struct s_hac_hb* next; struct s_hac_hb* next;
switch_thread_rwlock_t *recv_lock;
switch_thread_rwlock_t *send_lock;
}; };
typedef struct s_hac_hb hac_hb_t; typedef struct s_hac_hb hac_hb_t;
...@@ -193,18 +201,14 @@ struct s_hac_config { ...@@ -193,18 +201,14 @@ struct s_hac_config {
}; };
typedef struct s_hac_config hac_config_t; typedef struct s_hac_config hac_config_t;
typedef enum {
HAC_LOOPING = 0,
HAC_SHUTDOWN_RUNTIME = 1,
HAC_UNLOAD = 2
} hac_state_t;
extern hac_config_t config; extern hac_config_t config;
struct s_hac_event { struct s_hac_event {
switch_event_t *event; switch_event_t *event;
switch_bool_t processed; switch_bool_t processed;
switch_atomic_t refcount;
const char *uuid;
struct timeval arrived; struct timeval arrived;
}; };
typedef struct s_hac_event hac_event_t; typedef struct s_hac_event hac_event_t;
...@@ -227,31 +231,36 @@ struct s_hac_peer { ...@@ -227,31 +231,36 @@ struct s_hac_peer {
struct timeval last_seen; struct timeval last_seen;
hac_peer_state_t state; hac_peer_state_t state;
uint64_t missed_beats; uint64_t missed_beats;
switch_atomic_t refcount;
switch_thread_rwlock_t *lock;
}; };
typedef struct s_hac_peer hac_peer_t; typedef struct s_hac_peer hac_peer_t;
struct s_global { struct s_global {
switch_memory_pool_t* pool; switch_memory_pool_t* pool;
char hostname[256]; char hostname[256];
uint64_t host_hash; switch_atomic_t host_hash;
switch_memory_pool_t* event_pool; switch_memory_pool_t* event_pool;
switch_hash_t *event_hash; switch_hash_t *event_hash;
switch_hash_t *peer_hash; switch_hash_t *peer_hash;
hac_peer_state_t state; hac_peer_state_t state;
hac_state_t mod_state; hac_state_t mod_state;
hac_hb_t* hb; hac_hb_t* hb;
int nodeid; switch_atomic_t nodeid;
uint64_t rand_val; switch_atomic_t rand_val;
const char* msr; switch_atomic_t req_masters;
int req_masters; switch_atomic_t masters;
int masters; switch_atomic_t slaves;
int slaves; switch_atomic_t timer_a_dur;
int timer_a_dur; switch_atomic_t timer_b_dur;
int timer_b_dur;
struct timeval timer_a; struct timeval timer_a;
struct timeval timer_b; struct timeval timer_b;
int failure_threshold; switch_atomic_t failure_threshold;
switch_atomic_t max_latency;
switch_thread_rwlock_t *lock; switch_thread_rwlock_t *lock;
switch_thread_rwlock_t *hb_lock;
switch_thread_rwlock_t *event_hash_lock;
switch_thread_rwlock_t *peer_hash_lock;
}; };
extern struct s_global hac_globals; extern struct s_global hac_globals;
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论