Skip to content

Commit

Permalink
#387. Add 'keepalive_timeout' option
Browse files Browse the repository at this point in the history
  • Loading branch information
avbelov23 committed Dec 29, 2015
1 parent 9225fc0 commit 102cdc9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
14 changes: 13 additions & 1 deletion etc/tempesta_fw.conf
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,24 @@
# There is a special group called "default". All "server" entries defined
# outside an "srv_group" added to the default group implicitly.

# TAG: keepalive_timeout
#
# Syntax:
# keepalive_timeout TIMEOUT
#
# TIMEOUT is a timeout in secounds during which a keep-alive client connection
# will stay open on the server side for each 'listen' entry. When not specified
# explicitly or set the zero value, keep-alive client connections disables.
#
# Default:
# keepalive_timeout 75;

# TAG: listen
#
# Tempesta FW listening address.
#
# Syntax:
# listen PORT | IPADDR[:PORT]
# listen PORT | IPADDR[:PORT] [keepalive_timeout=TIMEOUT]
#
# IPADDR may be either an IPv4 or IPv6 address, no host names allowed.
# IPv6 address must be enclosed in square brackets (e.g. "[::0]" but not "::0").
Expand Down
58 changes: 53 additions & 5 deletions tempesta_fw/sock_clnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,14 @@ static const SsHooks tfw_sock_clnt_ss_hooks = {
* @sk - The underlying networking representation.
* @list - An entry in the tfw_listen_socks list.
* @addr - The IP address specified in the configuration.
* @ka_timeout - The keep-alive timeout.
*/
typedef struct {
SsProto proto;
struct sock *sk;
struct list_head list;
TfwAddr addr;
int ka_timeout;
} TfwListenSock;

/**
Expand All @@ -219,7 +221,7 @@ static LIST_HEAD(tfw_listen_socks);
* @type is the SsProto->type.
*/
static int
tfw_listen_sock_add(const TfwAddr *addr, int type)
tfw_listen_sock_add(const TfwAddr *addr, int ka_timeout, int type)
{
TfwListenSock *ls;

Expand All @@ -235,6 +237,7 @@ tfw_listen_sock_add(const TfwAddr *addr, int type)
return -EINVAL;
list_add(&ls->list, &tfw_listen_socks);
ls->addr = *addr;
ls->ka_timeout = ka_timeout;

/* Port is placed at the same offset in sockaddr_in and sockaddr_in6. */
tfw_classifier_add_inport(addr->v4.sin_port);
Expand Down Expand Up @@ -375,11 +378,13 @@ tfw_sock_check_listeners(void)
* ------------------------------------------------------------------------
*/

static const char *tfw_cli_cfg_dflt_ka_timeout;

static int
tfw_sock_clnt_cfg_handle_listen(TfwCfgSpec *cs, TfwCfgEntry *ce)
{
int r;
int port;
int port, ka_timeout;
TfwAddr addr;
const char *in_str = NULL;

Expand Down Expand Up @@ -413,17 +418,25 @@ tfw_sock_clnt_cfg_handle_listen(TfwCfgSpec *cs, TfwCfgEntry *ce)
if (r)
goto parse_err;

in_str = tfw_cfg_get_attr(ce, "keepalive_timeout",
tfw_cli_cfg_dflt_ka_timeout
? tfw_cli_cfg_dflt_ka_timeout
: "75");
r = tfw_cfg_parse_int(in_str, &ka_timeout);
if (r)
goto parse_err;

if (!ce->attr_n)
return tfw_listen_sock_add(&addr, TFW_FSM_HTTP);
return tfw_listen_sock_add(&addr, ka_timeout, TFW_FSM_HTTP);

in_str = tfw_cfg_get_attr(ce, "proto", NULL);
if (!in_str)
goto parse_err;

if (!strcasecmp(in_str, "http"))
return tfw_listen_sock_add(&addr, TFW_FSM_HTTP);
return tfw_listen_sock_add(&addr, ka_timeout, TFW_FSM_HTTP);
else if (!strcasecmp(in_str, "https"))
return tfw_listen_sock_add(&addr, TFW_FSM_HTTPS);
return tfw_listen_sock_add(&addr, ka_timeout, TFW_FSM_HTTPS);
else
goto parse_err;

Expand All @@ -433,6 +446,34 @@ tfw_sock_clnt_cfg_handle_listen(TfwCfgSpec *cs, TfwCfgEntry *ce)
return -EINVAL;
}

static int
tfw_sock_clnt_cfg_handle_keepalive(TfwCfgSpec *cs, TfwCfgEntry *ce)
{
int r, ka_timeout;

r = tfw_cfg_check_val_n(ce, 1);
if (r)
return -EINVAL;

tfw_cli_cfg_dflt_ka_timeout = ce->vals[0];
r = tfw_cfg_parse_int(tfw_cli_cfg_dflt_ka_timeout, &ka_timeout);
if (r) {
TFW_ERR("Unable to parse 'keepalive_timeout' value: '%s'\n",
tfw_cli_cfg_dflt_ka_timeout
? tfw_cli_cfg_dflt_ka_timeout
: "No value specified");
return -EINVAL;
}

if (ka_timeout < 0) {
TFW_ERR("Unable to parse 'keepalive_timeout' value: '%s'\n",
"Value less the zero");
return -EINVAL;
}

return 0;
}

static void
tfw_sock_clnt_cfg_cleanup_listen(TfwCfgSpec *cs)
{
Expand All @@ -451,6 +492,13 @@ TfwCfgMod tfw_sock_clnt_cfg_mod = {
.allow_repeat = true,
.cleanup = tfw_sock_clnt_cfg_cleanup_listen
},
{
"keepalive_timeout",
"75",
tfw_sock_clnt_cfg_handle_keepalive,
.allow_repeat = true,
.cleanup = tfw_sock_clnt_cfg_cleanup_listen
},
{}
}
};
Expand Down

0 comments on commit 102cdc9

Please sign in to comment.