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