LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2009, 11:24 AM   #1
mibo
LQ Newbie
 
Registered: Aug 2009
Posts: 21

Rep: Reputation: 1
receiver side UDP packet loss - why?


Hi network experts,

I have a very simple test system running.
A microcontroller (mc) with 100Mbit/s ethernet sending 500bytes packets at a 2000Hz rate (so one packet every 500µs).
The mc is directly connected with an 1m ethernet cable to a dedicated intel ethernet adapter in the PC. The PC runs Ubuntu 9.10 (the same problems with 9.04) has 2GB ram, a quad core cpu and is idle.

My single threaded PC program just receives the packets with "recvfrom" and checks the packet number and writes an info to the screen - nothing more. The mc writes a 16 bit number n=n+1 to every packet, so the PC knows, which packet number to expect next.
The PC program reports no problem as long as I don't touch the PC. If I switch between the 4 desktops - the program receives some more packets but then quits with an error because it received packet#630 but expected packet#621 (just an example).
So it seems, a buffer is filling up and then dropping packets.

Why this is so strange to me:
- the PC has 4 cores, enough ram and cannot handle 1kbytes/s (the correct number is 1Mbytes/s) data???
- wireshark runs on the PC and sees ALL the packets - even the ones that my program did not receive. (wireshark is not the reason for the problem - the packets get lost even without running wireshark)

Can somebody explain to me what is happening here?

Thank you,
mibo

Last edited by mibo; 11-26-2009 at 01:28 AM.
 
Old 11-25-2009, 12:44 PM   #2
dckump
LQ Newbie
 
Registered: Nov 2009
Location: Golden, CO
Distribution: Centos 7
Posts: 1

Rep: Reputation: 0
If I understand this correctly you are generating 1 MBytes/seconds (500 * 2000) or about 8 mBits/sec. When you set up to read UDP packets from a given port, the system creates a little buffering for incoming packets. The TCP/IP stack receives UDP packets and stuffs them into the buffer for any processing listening for packets on that port.

Your reader task is probably being interrupted for a few milliseconds as part of your desktop switch and during that time it is not reading the UDP packets and enough new ones have arrived that some get thrown away. There is no flow control for UDP packets and you are getting them at a pretty high rate.

I have not done anything quite this high rate, but in Java you can get and set the receive buffer size for a DatagramSocket. In C I think this is called the SO_RCVBUF option and it is manipulated via equivalent library calls. I suggest to read the value from your socket and try to set it to a higher value. This will increase the amount of buffering to something that you are pretty sure cannot be filled up while your application is not running.

Good luck.
 
Old 11-25-2009, 12:55 PM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
In addition to the advice given by dckump, I'd add that your program should be able to cope with dropped UDP packets. This might be obvious, but I think it needs saying - if you don't design it to be able to cope with lost UDP packets, it *will* crash & burn sooner or later due to network "happenings", regardless of how big a buffer the receiver has and how quickly it can process packets.

If you need to guarantee that you see all the data coming in, TCP is a better option.
 
Old 11-25-2009, 08:09 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
One additional note: maybe some of these so-called "dropped packets" are actually be "out of order" packets. Your program appears to be failing to handle either case. UDP, of course, guarantees neither receipt nor ordering.

SUGGESTION:
1. Write a test program that simply receives the packets

2. Count the %/packets received out of order

3. Count the %/packets never received at all

4. Post back the results.

We're curious what you find

Last edited by paulsm4; 11-25-2009 at 08:11 PM.
 
Old 11-26-2009, 01:27 AM   #5
mibo
LQ Newbie
 
Registered: Aug 2009
Posts: 21

Original Poster
Rep: Reputation: 1
Thank you for the good ideas.

dckump, you are right - it's 1MByte/s.

I will check, if I can make the receive buffer bigger. I will also check if the packets are just in the wrong order. ATM my program quits as soon as the first packet with a wrong number arrives.

The mc sends "realtime" data and the PC needs to handle it with a low latency. So, there is no option to resend data. I have to build in a (non quitting) packet loss detection and a "data good" flag/counter.
In the end the receiving PC will run a realtime linux but its easier to start just with an Ubuntu PC in my office.
About the network traffic - it will never happen, as the PC will always directly be connected to the mc (just one network cable between them).

mibo
 
Old 12-01-2009, 01:41 AM   #6
mibo
LQ Newbie
 
Registered: Aug 2009
Posts: 21

Original Poster
Rep: Reputation: 1
Sorry for not responding lately. The mc-device has some problems, so atm I can not make the tests.
But, I tried the SO_RCVBUF option and found out that the buffer size was set to something about 100000 bytes. I set it to 1Mbytes but the kernel set it automatically to something more than 200000 bytes. Hopefully, next week I can test if the bigger buffer size helps.

Cheers,
mibo

PS: I found an interesting website discussing UDP buffers and packet loss
http://www.29west.com/docs/THPM/udp-buffer-sizing.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reduce packet loss in UDP pankaj_bhamare Linux - Networking 1 04-22-2009 01:09 PM
How to track TCP/UDP Packet loss for certain period ? neel.gurjar Linux - Networking 2 07-20-2008 01:05 AM
udp packet loss problem vlyamtse Linux - Networking 2 02-12-2008 08:59 AM
UDP broadcast receiver estratos Programming 1 03-14-2007 07:58 AM
How to reliably detect UDP packet loss. rikusg Linux - Networking 2 07-14-2006 03:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration