varnish-cache/bin/varnishd/mgt/mgt_param_tcp.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2015 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * Parameters related to TCP keepalives are not universally available
30
 * as socket options, and probing for system-wide defaults more appropriate
31
 * than our own involves slightly too much grunt-work to be neglible
32
 * so we sequestrate that code here.
33
 */
34
35
#include "config.h"
36
37
#include <sys/types.h>
38
#include <sys/socket.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <netinet/in.h>
42
#include <netinet/tcp.h>
43
#include <unistd.h>
44
45
#include "mgt/mgt.h"
46
#include "common/heritage.h"
47
48
#include "vtcp.h"
49
50
#ifdef HAVE_TCP_KEEP
51
52
static void
53 4212
tcp_probe(int sock, int nam, const char *param, unsigned def)
54
{
55
        int i;
56
        socklen_t l;
57
        unsigned u;
58
59 4212
        l = sizeof u;
60 4212
        i = getsockopt(sock, IPPROTO_TCP, nam, &u, &l);
61 4212
        if (i < 0 || u == 0)
62 0
                u = def;
63 4212
        MCF_ParamConf(MCF_DEFAULT, param, "%u", u);
64 4212
}
65
66
static void
67 1404
tcp_keep_probes(void)
68
{
69
        const char *err;
70
        int s;
71
72 1404
        s = VTCP_listen_on(":0", NULL, 10, &err);
73 1404
        if (err != NULL)
74 0
                ARGV_ERR("Could not probe TCP keepalives: %s", err);
75 1404
        assert(s > 0);
76 1404
        tcp_probe(s, TCP_KEEPIDLE, "tcp_keepalive_time",        600);
77 1404
        tcp_probe(s, TCP_KEEPCNT, "tcp_keepalive_probes",       5);
78 1404
        tcp_probe(s, TCP_KEEPINTVL, "tcp_keepalive_intvl",      5);
79 1404
        closefd(&s);
80 1404
}
81
#endif
82
83
void
84 1404
MCF_TcpParams(void)
85
{
86
#ifdef HAVE_TCP_KEEP
87 1404
        tcp_keep_probes();
88
#endif
89 1404
}