The keepalive mechanism of using TCP in Linux environment

Source: Internet
Author: User
Tags sin htons

Linux built-in support keepalive mechanism, in order to use it, you need to enable TCP/IP network, in order to be able to configure the kernel at runtime parameters, you also need PROCFS and sysctl support.

This process involves three user-driven variables used by keepalive:

Tcp_keepalive_time: Represents the time interval between the most recent packet (a simple ACKs packet without data) and the first KeepAlive probe sent, and the counter is no longer used when the connection is marked as keepalive.

TCP_KEEPALIVE_INTVL: Represents the time interval between the concurrent keepalive probes.

Tcp_keepalive_probes: The number of probes that were sent without a reply before the connection was determined to be disconnected and notified to the application layer.

For these three parameters, you can view and modify their default values in the terminal of a Linux system:

View the values of three parameters:

[Email protected] ~]# cat/proc/sys/net/ipv4/tcp_keepalive_time7200[[email protected] ~]# cat/proc/sys/net/ipv4/tcp_ Keepalive_intvl75[[email protected] ~]# CAT/PROC/SYS/NET/IPV4/TCP_KEEPALIVE_PROBES9

These three parameter values are modified by command (three parameter values are set to: 600, 60, 20, respectively):

[[email protected] ~]# echo >/proc/sys/net/ipv4/tcp_keepalive_time[[email protected] ~]# echo >/proc/sys/ne T/ipv4/tcp_keepalive_intvl[[email protected] ~]# echo 6 >/proc/sys/net/ipv4/tcp_keepalive_probes

This way reset three parameter values, after the system restarts the value of the three parameters will revert to the default, how to let the system always remember the value of their settings, can refer to other materials, we are now related to how to use the KeepAlive mechanism in the program and set the values of the three parameters.

using the keepalive mechanism in your program

To use this mechanism in your program, you only need to use the setsockopt () function.

The setsockopt () function is used for setting option values of any type, any state socket interface. Although there are options on different protocol tiers, this function only defines the highest option on the "set interface" level. Options affect the operation of the socket, such as whether the expedited data is received in the normal data stream, whether the broadcast data can be sent from the socket, and so on.

The following is a prototype of the setsockopt () function:

#include <sys/types.h> #include <sys/socket.h>int setsockopt (int sock, int level, int optname, const void *opt Val, socklen_t Optlen);
Parameters:
Sock: The socket that will be set or get the option.
Level: The protocol layer where the option is located.
Optname: The name of the option that needs to be accessed.
Optval: For GetSockOpt (), point to the buffer that returns the value of the option. For setsockopt (), point to the buffer that contains the new option value.
Optlen: For GetSockOpt (), the maximum length of the option value when used as an ingress parameter. The actual length of the option value as an exit parameter. For setsockopt (), the length of the current option.

In order to use the function setsockopt () to open the keepalive mechanism of a particular socket, the parameter s is a socket file descriptor that must be created before this using the socket () function; The parameter level must be set to Sol_socket The third parameter must be set to the So_keepalive;optval parameter, which must be a Boolean integer variable, indicating that you want to enable this option; The last parameter represents the size of the fourth parameter.

Program realizes heartbeat packet detection mechanism

The first step is to establish a dedicated socket link between the backup machine and the source machine for heartbeat detection.

On the source side, before the data migration, a socket is established to listen to the connection of the backup machine, and the socket and corresponding processing function are placed in the IO processing list of the original software, the code is as follows:

int listenfd;    struct sockaddr_in server_sin;    /* Establish socket *    /Listenfd=socket (af_inet,sock_stream,0);    server_sin.sin_family=af_inet;    Server_sin.sin_addr.s_addr=htonl (inaddr_any);    Server_sin.sin_port=htons (port);    Bind (LISTENFD, (struct sockaddr *) &server_sin,sizeof (server_sin));    /* Establish end *    /Listen (listenfd,1024);    Qemu_set_fd_handler2 (LISTENFD, NULL, tcpkeepalive_server, NULL,                         (void *) (intptr_t) LISTENFD);

The socket corresponds to the following processing function:

static void Tcpkeepalive_server (void *opaque) {    int connfd;    struct sockaddr_in client_sin;    socklen_t client_len=sizeof (client_sin);    int listenfd = (intptr_t) opaque;    Connfd=accept (LISTENFD, (struct sockaddr *) &client_sin,&client_len);}

On the backup side, when it starts as a backup machine, it establishes the listener side of the socket connection source, sets the corresponding tcpkeepalive parameters, and then joins the socket and corresponding handler functions into the IO processing list.

We established the socket is a heartbeat detection dedicated link, there will be no data flow, there is only one case the backup machine end will receive data, that is, the source has failed, the tcpkeepalive mechanism will return an error message, so captured this information, the backup machine will jump to the corresponding processing function , the replacement source machine starts to run.

The corresponding code is as follows:

int sockfd;    struct sockaddr_in sin;    int optval;    socklen_t Optlen = sizeof (optval);    Sockfd=socket (af_inet,sock_stream,0);    sin.sin_family=af_inet;    sin.sin_addr.s_addr=addr.sin_addr.s_addr;    Sin.sin_port=htons (port);    Optval = 1;    SetSockOpt (SOCKFD, Sol_socket, So_keepalive, &optval, Optlen);    Optval = 5;    SetSockOpt (SOCKFD, Sol_tcp, tcp_keepcnt, &optval, Optlen);    Optval = 1;    SetSockOpt (SOCKFD, Sol_tcp, Tcp_keepidle, &optval, Optlen);    Optval = 1;    SetSockOpt (SOCKFD, Sol_tcp, TCP_KEEPINTVL, &optval, Optlen);    Connect (SOCKFD, (struct sockaddr *) &sin,sizeof (sin));    Qemu_set_fd_handler2 (SOCKFD, NULL, Tcpkeepalive_vm_start, NULL,                         (void *) (intptr_t) SOCKFD);

The handler for the socket is simple enough to get the backup machine running:

static void Tcpkeepalive_vm_start (void *opaque) {    vm_start ();}








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.