views:

40

answers:

3

Not sure if this is the right place to ask, but here goes....

I implemented a UDP client/server using normal sockets in linux.

(Assuming there is nothing on the network side between these 2 hosts that interferes with the packet in any way, but just pass it on as is)

Does the kernel do any kind of caching/optimization of these UDP packets (it is the exact same data packet I send each time)

I'm doing some testing which "ping pongs" different size packets between client/server and want to know if I need to pad the packet with random data every time or can I just send this "fixed content" packet all the time? (packet size stays the same for each test).

+2  A: 

Unless a BPF (Berkeley Packet Filter) or equivalent (i.e. firewall) has been set up to examine traffic and possibly alter it, the stack should be blissfully unaware of the payloads in your datagrams. You should be able to use the same packets over again.

Amardeep
Ok, thanks, didn't think of that...but even if I do have a firewall inspecting packets they shouldn't interfere if the packet is allowed to pass through. I'm mainly concerned about some kind of caching going on in the kernel without me knowing about it.
NomadAlien
A: 

As armardeep said, the stack is unaware of the payload. Firewalls don't usually modify datagrams, they let them though or block them completely.

The kernel doesn't 'cache' packages but will buffer them. There will be at least an output buffer where the packages end up until the network line is free, and an input buffer where received packages are stored until your code calls the recv method.

In practice there might be some more buffers in the pipeline, but from the applications point of view the kernel will behave as if it only has those two buffers. Both can overflow by the way.

Lawnmower
Thanks for that!
NomadAlien
A: 

There is a kernel buffer associated with the socket where the packets are stored until you call recv. If the kernel receives data faster than you remove data from the socket buffer, then an overflow will occur and the kernel will silently discard your packets. You can increase the size of the receive buffer for that particular socket using a call to setsockopt.

int rcvbuflen = 0x20000; // 128kb
setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuflen, sizeof(rcvbuflen));

You can use getsockopt to check the current size of the buffer.

Robert S. Barnes