diff options
author | Donatas Abraitis <donatas@opensourcerouting.org> | 2022-10-10 14:46:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-10 14:46:30 +0200 |
commit | eb531283675924c3bfb5034e54d234679208a2b4 (patch) | |
tree | b01d54895bee560fb64af00d51dab5c42b380d5f /lib/sockopt.c | |
parent | Merge pull request #12082 from anlancs/fix/cleanup-21 (diff) | |
parent | bgpd: support TCP keepalive for BGP connection (diff) | |
download | frr-eb531283675924c3bfb5034e54d234679208a2b4.tar.xz frr-eb531283675924c3bfb5034e54d234679208a2b4.zip |
Merge pull request #9998 from pguibert6WIND/bgp_tcp_keepalive
Bgp tcp keepalive
Diffstat (limited to 'lib/sockopt.c')
-rw-r--r-- | lib/sockopt.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/sockopt.c b/lib/sockopt.c index 7a2b8a1c8..de11a9eab 100644 --- a/lib/sockopt.c +++ b/lib/sockopt.c @@ -693,3 +693,52 @@ int sockopt_tcp_mss_get(int sock) return tcp_maxseg; } + +int setsockopt_tcp_keepalive(int sock, uint16_t keepalive_idle, + uint16_t keepalive_intvl, + uint16_t keepalive_probes) +{ + int val = 1; + + if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) < 0) { + flog_err_sys(EC_LIB_SYSTEM_CALL, + "%s failed: setsockopt SO_KEEPALIVE (%d): %s", + __func__, sock, safe_strerror(errno)); + return -1; + } + +#if defined __OpenBSD__ + return 0; +#else + /* Send first probe after keepalive_idle seconds */ + val = keepalive_idle; + if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < + 0) { + flog_err_sys(EC_LIB_SYSTEM_CALL, + "%s failed: setsockopt TCP_KEEPIDLE (%d): %s", + __func__, sock, safe_strerror(errno)); + return -1; + } + + /* Set interval between two probes */ + val = keepalive_intvl; + if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < + 0) { + flog_err_sys(EC_LIB_SYSTEM_CALL, + "%s failed: setsockopt TCP_KEEPINTVL (%d): %s", + __func__, sock, safe_strerror(errno)); + return -1; + } + + /* Set maximum probes */ + val = keepalive_probes; + if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) { + flog_err_sys(EC_LIB_SYSTEM_CALL, + "%s failed: setsockopt TCP_KEEPCNT (%d): %s", + __func__, sock, safe_strerror(errno)); + return -1; + } + + return 0; +#endif +} |