3.3.30. tcp_wmem
This variable takes 3 different values which holds information on how much TCP sendbuffer memory space each TCP socket has to use. Every TCP socket has this much buffer space to use before the buffer is filled up. Each of the three values are used under different conditions.
The first value in this variable tells the minimum TCP send buffer space available for a single TCP socket. This space is always allocated for a specific TCP socket opened by a program as soon as it is opened. This value is normally set to 4096 bytes, or 4 kilobytes.
The second value in the variable tells us the default buffer space allowed for a single TCP socket to use. If the buffer tries to grow larger than this, it may get hampered if the system is currently under heavy load and don't have a lot of memory space available. It may even have to drop packets if the system is so heavily loaded that it can not give more memory than this limit. The default value set here is 16384 bytes, or 16 kilobytes of memory. It is not very wise to raise this value since the system is most probably already under heavy memory load and usage, and this would hence lead to even more problems for the rest of the system. This value overrides the /proc/sys/net/core/wmem_default value that is used by other protocols, and is usually set to a lower value than the core value.
The third value tells the kernel the maximum TCP send buffer space. This defines the maximum amount of memory a single TCP socket may use. Per default this value is set to 131072, or 128 kilobytes. This should be a reasonable value for most circumstances, and you will most probably never need to change these values. However, if you ever do need to change it, you should keep in mind that the /proc/sys/net/core/wmem_max value overrides this value, and hence this value should always be smaller than that value.
Tip
This variable may give tremenduous increase in throughput on high bandwidth networks, if used properly together with the tcp_mem and tcp_rmem variable. The tcp_wmem variable is the variable of the three which may give the most gain from this kind of tweaking. Do note that you will see almost no gain on slower networks than giga ethernet networks. For more information about this, look at the TCP Tuning Guide.
http://wwwx.cs.unc.edu/~sparkst/howto/network_tuning.php
sk → sndbuf is initialized to sysctl_tcp_wmem[1] (16K). This is the maximum
memory that can be allocated for the send buffer at any point of time, and
this value can be changed by setsockopts() .
sk → rcvbuf is initialized to sysctl_tcp_rmem[1] (87,380 bytes). This is the
maximum memory that can be allocated for the receive buffer at any point
of time, and this value can be changed by setsockopts() .
sk → state is set to TCP_CLOSE as there is still no connection open for this
socket.
http://wwwx.cs.unc.edu/~sparkst/howto/network_tuning.php
Type: sysctl -w net.ipv4.tcp_wmem='4096 65536 8388608'
TCP Autotuning setting. "This variable takes 3 different values which holds information on how much TCP sendbuffer memory space each TCP socket has to use. Every TCP socket has this much buffer space to use before the buffer is filled up. Each of the three values are used under different conditions. ... The first value in this variable tells the minimum TCP send buffer space available for a single TCP socket. ... The second value in the variable tells us the default buffer space allowed for a single TCP socket to use. ... The third value tells the kernel the maximum TCP send buffer space."
http://lxr.linux.no/linux+v2.6.29/net/core/sock.c
http://www.unixresources.org/linux/clf/linuxK/archive/00/00/46/71/467130.html
sk->sk_allocation = GFP_KERNEL;
sk->sk_rcvbuf = sysctl_rmem_default;
sk->sk_sndbuf = sysctl_wmem_default;
sk->sk_state = TCP_CLOSE;
allocation : This fi eld contains the policy using which memory for sk_buff for
this socket needs to be allocated. For this case, this fi eld is initialized to
GFP_KERNEL .
rcvbuf : This fi elds contains the number indicating a maximum limit for the
receive buffer at any point in time. This is initialized to sysctl_rmem_default
and can be changed using setsockopts() . This value is checked whenever we
are want to allocate memory for an incoming packet. If the limit has been
reached, a new buffer is not allocated until the receive_queue is consumed.
This restricts the socket from consuming the entire system memory when the
packets are fl ooding in for a given socket.
sndbuf : Same as recvbuf , but it is used to limit the send buffer size. The value
is initialized to sysctl_wmem_default , which can be changed using
setsockopts() .
state : This is the state of the socket for a protocol — in this case the socket state
for the TCP connection. This is initialized to TCP_CLOSE since there is no
connection on this socket at this point in time.
Current values : net.ipv4.tcp_rmem = 4096 87380 174760
tcp_rmem
--------------------------------------------------------------------------------
min: Minimal size of receive buffer used by TCP sockets.
It is guaranteed to each TCP socket, even under moderate memory
pressure.
Default: 8K
default: default size of receive buffer used by TCP sockets.
This value overrides net.core.rmem_default used by other protocols.
Default: 87380 bytes. This value results in window of 65535 with
default setting of tcp_adv_win_scale and tcp_app_win:0 and a bit
less for default tcp_app_win. See below about these variables.
max: maximal size of receive buffer allowed for automatically
selected receiver buffers for TCP socket. This value does not override
net.core.rmem_max, "static" selection via SO_RCVBUF does not use this.
Default: 87380*2 bytes.
adsl2:~# cat /proc/sys/net/ipv4/tcp_fin_timeout
60
% echo "10" > /proc/sys/net/ipv4/tcp_fin_timeout
% /etc/rc.d/init.d/network restart
It would set the TCP/FIN timeout to 10 seconds instead of its default 60 seconds.
You can also adjust the parameters in /etc/sysctl.conf ; in our example, the parameter is net.ipv4.tcp_fin_timeout.
http://www.ibiblio.org/pub/Linux/docs/HOWTO/TCP-Keepalive-HOWTO
sndtimeo : The same as rcvtimeo, but in the opposite direction. It may be a
timeout value when we are blocked to send TCP data (waiting for memory
to be available for sending data when send ‘ Q ’ is full and there is no memory
available to accommodate more send data) or when we are blocked to make
TCP connections (client is waiting for acknowledgment of connect request).
Initialized to MAX_SCHEDULE_TIMEOUT .
connect in gönderdiği syn e 5 saniye içinde syn - ack gelmezse
error - Operation now in progress
connect in gönderdiği syn e reset gelirse
error - Connection refused
connect in sadece 5 saniye block lanmasını istiyorsak ;
struct timeval tv;
// Create socket
soc = socket(AF_INET, SOCK_STREAM, 0);
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(soc, SOL_SOCKET, SO_SNDTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
örnek kod;
aşağıdaki kod 23 nolu porta tcp bağlantısı yapiyor 5 saniyede cevap gelmezse error dönüyor ve istediğimiz komutu çalıştırıyor.5 saniyelik süreyi SO_SNDTIMEO i 5 saniye yaparak belirliyoruz.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void benimlog(char *);
int main(void)
{
int res, valopt,soc, pid, logfd;
struct sockaddr_in addr;
struct timeval tv;
// Create socket
soc = socket(AF_INET, SOCK_STREAM, 0);
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(soc, SOL_SOCKET, SO_SNDTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
// Trying to connect with timeout
addr.sin_family = PF_INET;
addr.sin_port = htons(23);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
res = connect(soc, (struct sockaddr *)&addr, sizeof(addr));
if (res != 0) {
fprintf(stderr, "Error in connection() %d - %s\n", res, strerror(errno));
benimlog("A");
execlp("/bin/ls", "ls", (char*)NULL);
exit(0);
}
else {
fprintf(stdout, "syn ack received\n");
exit(0);
}
return 0;
}
void benimlog (char *p){
char timebuffer[100]={0};
time_t unixtime;
int logfd;
logfd = open("/root/ozan/ozanreboot.log", O_WRONLY | O_APPEND);
unixtime = time(NULL);
sprintf(timebuffer,"%d\t%s\n", unixtime, p);
write(logfd, timebuffer, strlen(timebuffer));
}
connect in gönderdiği syn e 5 saniye içinde syn - ack gelmezse
error - Operation now in progress
connect in gönderdiği syn e reset gelirse
error - Connection refused
örnek SO_RCVTIMEO
timeout.tv_sec = 4;
timeout.tv_usec = 0;
setsockopt(listenSocket, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout))
The do limit time the process can block on the socket. So, if you
do read() from this socket and it blocks, it will resume after 4 seconds
even if no input is present.
rcvtimeo : This fi eld keeps the value of the maximum timeout for any blocking
event on the IP protocol socket. It may be a timeout value when we are
blocked to receiving TCP data or when we are blocked to accept TCP connections.
Initialized to MAX_SCHEDULE_TIMEOUT
No comments:
Post a Comment