2001-02-01 09:13:28

by Nicholas Daley

[permalink] [raw]
Subject: PROBLEM: small socket send/receive buffers on TCP stream result in data not being transferred

#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
typedef double sk_time;

int openCon(const char* hostname, int port) {
int fd=-1;
sockaddr_in sa;
hostent *he;

fd=socket(PF_INET, SOCK_STREAM, 0);
if(fd<0) return -1;

sa.sin_family=AF_INET;
sa.sin_port=htons(port);
he=gethostbyname(hostname);
if(he==NULL) return fd=-1;
sa.sin_addr=*(struct in_addr*) he->h_addr;
if(connect(fd,(struct sockaddr*) &sa, sizeof(sa))<0) return fd=-1;

return fd;

}
int main(int argc, char** argv) {
int tracestream=openCon("localhost",argc>1?atoi(argv[1]):8001);
size_t s;
s=argc>2?atoi(argv[2]):3000;
setsockopt(tracestream,SOL_SOCKET,SO_RCVBUF,&s,sizeof(s));
setsockopt(tracestream,SOL_SOCKET,SO_SNDBUF,&s,sizeof(s));

while(1) {
char c;
if(read(tracestream,&c,1)<=0) break;
cerr<<c;
}
close(tracestream);
};


Attachments:
fakefeed.cc (1.40 kB)
fakesim.cc (1.02 kB)
Download all attachments

2001-02-01 18:54:11

by Alexey Kuznetsov

[permalink] [raw]
Subject: Re: PROBLEM: small socket send/receive buffers on TCP stream result in data not being transferred

Hello!

> 1. small socket send/receive buffers result in data not being transferred

I know why your test does not work in 2.4 (sort of bug, fix is appended).

But I have no idea, why it does not work with 2.2. Please, make
tcpdump in this case.


> and SO_RCVBUF (see small attached programs), I found that given small
> enough buffers, data would stop being sent, although more was available.

No doubts. If you think that tcp must work like gun no matter
what silly things application makes, you are mistaken.
TCP works like gun when application does not try to interlope. 8)

You have made two capital mistakes:

1. RCVBUF/SNDBUF are not arbibrary numbers. Of course, kernel tries
to adjust them to something reasonable, when user asks something
wicked (like your case), but it cannot do this in all the cases.
2. Playing with buffer sizes after connection is established
is very bad idea, tcp is defenceless in this case.

> This behaviour may depend on the speed of the system, on mine I can

Surely, it is not.


> usually get it to happen with buffer sizes around 100

Advice: do not play with these sockopts. Kernel does this better,
than any application.

Application may set them, when it wants to set large values.
Values, selected by kernel by default, are minimal ones in fact.

Alexey


--- ../vger3-010130/linux/include/net/tcp.h Wed Dec 13 22:37:49 2000
+++ linux/include/net/tcp.h Thu Feb 1 21:08:45 2001
@@ -1304,7 +1305,8 @@
struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;

if (tp->ucopy.task) {
- if ((tp->ucopy.memory += skb->truesize) <= (sk->rcvbuf<<1)) {
+ if (tp->ucopy.memory < (sk->rcvbuf<<1)) {
+ tp->ucopy.memory += skb->truesize;
__skb_queue_tail(&tp->ucopy.prequeue, skb);
if (skb_queue_len(&tp->ucopy.prequeue) == 1) {
wake_up_interruptible(sk->sleep);
@@ -1313,7 +1315,6 @@
}
} else {
NET_INC_STATS_BH(TCPPrequeueDropped);
- tp->ucopy.memory -= skb->truesize;
__kfree_skb(skb);
}
return 1;