Skip to content

Commit

Permalink
[nathelper] Added clustering support
Browse files Browse the repository at this point in the history
Only the nodes owning the active tag will be able to do the pinging to the contacts.
  • Loading branch information
bogdan-iancu committed Jan 3, 2019
1 parent 6c28166 commit 14c3570
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 4 deletions.
74 changes: 74 additions & 0 deletions modules/nathelper/doc/nathelper_admin.xml
Expand Up @@ -93,6 +93,12 @@
contacts are to be pinged.
</para>
</listitem>
<listitem>
<para>
<emphasis>clustere</emphasis> - only if "cluster_id"
option is enabled.
</para>
</listitem>
</itemizedlist>
</para>
</section>
Expand Down Expand Up @@ -513,6 +519,74 @@ modparam("nathelper", "ping_threshold", 10)
...
modparam("nathelper", "max_pings_lost", 5)
...
</programlisting>
</example>
</section>

<section id="param_cluster_id" xreflabel="cluster_id">
<title><varname>cluster_id</varname> (integer)</title>
<para>
The ID of the cluster the module is part of. The clustering support is
used by the nathelper module for controlling the pinging process. When
part of a cluster of multiple nodes, the nodes can agree upon which node
is the one responsible for pinging.
</para>
<para>
The clustering with sharing tag support may be used to control which
node in the cluster will perform the pinging/probing to the
contacts. See the
<xref linkend="param_cluster_sharing_tag"/> option.
</para>
<para>
For more info on how to define and populate a cluster (with OpenSIPS
nodes) see the "clusterer" module.
</para>
<para>
<emphasis>
Default value is <quote>0 (none)</quote>.
</emphasis>
</para>
<example>
<title>Set <varname>cluster_id</varname> parameter</title>
<programlisting format="linespecific">
...
# Be part of cluster ID 9
modparam("nathelper", "cluster_id", 9)
...
</programlisting>
</example>
</section>

<section id="param_cluster_sharing_tag" xreflabel="cluster_sharing_tag">
<title><varname>cluster_sharing_tag</varname> (string)</title>
<para>
The name of the sharing tag (as defined per clusterer modules) to
control which node is responsible for perform pinging of the
contacts.
If defined, only the node with active status of this tag will
perform the pinging.
</para>
<para>
The <xref linkend="param_cluster_id"/> must be defined for this option
to work.
</para>
<para>
This is an optional parameter. If not set, all the nodes in the cluster
will individually do the pinging.
</para>
<para>
<emphasis>
Default value is <quote>empty (none)</quote>.
</emphasis>
</para>
<example>
<title>Set <varname>cluster_sharing_tag</varname> parameter</title>
<programlisting format="linespecific">
...
# only the node with the active "vip" sharing tag will perform pinging
modparam("nathelper", "cluster_id", 9)
modparam("nathelper", "cluster_sharing_tag", "vip")
...
</programlisting>
</example>
</section>
Expand Down
18 changes: 14 additions & 4 deletions modules/nathelper/nathelper.c
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2003-2008 Sippy Software, Inc., http://www.sippysoft.com
* Copyright (C) 2005-2019 OpenSIPS Project
*
* This file is part of opensips, a free SIP server.
*
Expand Down Expand Up @@ -81,6 +82,8 @@ static int sipping_latency_flag = -1; /* by the code imported by sip_pinger*/

#include "nh_table.h"

#include "nh_clustering.h"


/* NAT UAC test constants */
#define NAT_UAC_TEST_C_1918 0x01
Expand Down Expand Up @@ -273,9 +276,11 @@ static param_export_t params[] = {
{"natping_partitions", INT_PARAM, &natping_partitions },
{"natping_socket", STR_PARAM, &natping_socket },
{"oldip_skip", STR_PARAM|USE_FUNC_PARAM,
(void*)get_oldip_fields_value},
{"ping_threshold", INT_PARAM, &ping_threshold },
{"max_pings_lost", INT_PARAM, &max_pings_lost },
(void*)get_oldip_fields_value },
{"ping_threshold", INT_PARAM, &ping_threshold },
{"max_pings_lost", INT_PARAM, &max_pings_lost },
{"cluster_id", INT_PARAM, &nh_cluster_id },
{"cluster_sharing_tag", STR_PARAM, &nh_cluster_shtag },
{0, 0, 0}
};

Expand Down Expand Up @@ -647,6 +652,10 @@ mod_init(void)
nets_1918[i].netaddr = ntohl(addr.s_addr) & nets_1918[i].mask;
}

if (nh_cluster_id>0 && nh_init_cluster()<0) {
LM_ERR("failed to initialized the clustering support\n");
return -1;
}

return 0;
}
Expand Down Expand Up @@ -1378,7 +1387,8 @@ nh_timer(unsigned int ticks, void *timer_idx)

udomain_t *d;

if ((*natping_state) == 0)
if ( (*natping_state) == 0
|| nh_cluster_shtag_is_active()!=0 )
goto done;

if (cblen > 0) {
Expand Down
68 changes: 68 additions & 0 deletions modules/nathelper/nh_clustering.c
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2019 OpenSIPS Project
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#include "nh_clustering.h"

#define BIN_VERSION 1

/* the cluster ID */
int nh_cluster_id = 0;

str nh_cluster_shtag = {NULL,0};

static struct clusterer_binds c_api;


int nh_cluster_shtag_is_active(void)
{
if ( nh_cluster_id<=0 || (nh_cluster_shtag.s &&
c_api.shtag_get(&nh_cluster_shtag,nh_cluster_id)!=SHTAG_STATE_ACTIVE) )
/* no clustering support or sharing tag found on not-active */
return 0;

return 1;
}


int nh_init_cluster(void)
{
if (load_clusterer_api(&c_api)!=0) {
LM_ERR("failed to find clusterer API - is clusterer "
"module loaded?\n");
return -1;
}

/* "register" the sharing tag */
if (nh_cluster_shtag.s) {
nh_cluster_shtag.len = strlen(nh_cluster_shtag.s);
if (c_api.shtag_get( &nh_cluster_shtag, nh_cluster_id)<0) {
LM_ERR("failed to initialized the sharing tag <%.*s>\n",
nh_cluster_shtag.len, nh_cluster_shtag.s);
return -1;
}
} else {
nh_cluster_shtag.len = 0;
}

return 0;
}


37 changes: 37 additions & 0 deletions modules/nathelper/nh_clustering.h
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2019 OpenSIPS Project
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#ifndef _NATHELPER_CLUSTERING_H_
#define _NATHELPER_CLUSTERING_H_

#include "../../sr_module.h"
#include "../../bin_interface.h"
#include "../clusterer/api.h"

extern int nh_cluster_id;
extern str nh_cluster_shtag;

int nh_init_cluster(void);

/* checks if the sharing tag is on active */
int nh_cluster_shtag_is_active(void);

#endif

0 comments on commit 14c3570

Please sign in to comment.