TCP Keepalive not working on Linux?

In the post Adding TCP Keepalive Support to DBD::Sybase perl module, I describe a patch that I submitted to Michael Peppler for his Perl DBD::Sybase driver.  While he implemented it in DBD::Sybase version 1.08, I didn’t mention that for Linux distributions, TCP Keepalive is disabled by default for processes. That means, any application that is to use TCP Keepalive, must be specially built for it or wrapper code must be used.

In order to modify the system settings for TCP Keepalive on Linux, you need to make a change to your /etc/sysctl.conf file and then run /sbin/sysctl -p:

# TCP Keepalive
net.ipv4.tcp_keepalive_probes = 20
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 60
Share Button

Adding TCP Keepalive Support to DBD::Sybase perl module

I’ve created a patch to enable support for CS_CON_KEEPALIVE (TCP keepalive packet SO_KEEPALIVE) in the DBD::Sybase v1.07 code. This will send an empty TCP keepalive ‘packet’ to the remote server.

"KeepAlive sets the value of the Sybase CT-Lib connection property CS_CON_KEEPALIVE to true or false. The default setting ensures that your connection is alive by sending packets to the database when the connection is idle. Set the value of this property to false for mobile clients that do not maintain constant connections." 

I’ve sent the patch to Michael Peppler.

diff dbdimp.h ../DBD-Sybase-1.07/dbdimp.h
< char keepalive[16];[/perl] [perl]diff dbdimp.c ../DBD-Sybase-1.07/dbdimp.c 1052d1051 < extractFromDsn("keepalive=", dsn, imp_dbh->keepalive, 10);
< < if (imp_dbh->keepalive[0]) {
< int keepalive = atoi(imp_dbh->keepalive);
< < if (keepalive != 1) { < keepalive = 0; < } < < if(DBIc_DBISTATE(imp_dbh)->debug >= 3)
< PerlIO_printf(DBIc_LOGPIO(imp_dbh), " syb_db_login() -> ct_config(CS_CON_KEEPALIVE,%d)\n”, keepalive);
< < if((retcode = ct_config(context, CS_SET, CS_CON_KEEPALIVE, &keepalive, CS_UNUSED, NULL)) != CS_SUCCEED) < warn("ct_config(CS_SET, CS_CON_KEEPALIVE) failed"); < } <[/perl]

Share Button