0
votes

I'm using python socket to receive UDP packets sent by FPGA with a self-defined frame counter to indicate packet loss. My problem is the received data have 4 packets lost every 65536 packets received. Does windows socket has a 65536 packet limit? I'm not able to embed picture in my post. I will type the running result below:

start: 395070354
end: 395344794
Number of packets: 274441
Missing 4 packet from: 395115893 to: 395115898 distance from start: 45539
Missing 4 packet from: 395181429 to: 395181434 distance from start: 45539
Missing 4 packet from: 395246965 to: 395246970 distance from start: 45539
Missing 4 packet from: 395312501 to: 395312506 distance from start: 45539
Number of missing packets: 16

As you can see from the result, it's quite regular. 395181434-395115898=65536. 4 packets are lost every 65536 packets.

I've tried slow the transmitting speed. I assume it maybe the buffer size problem. I used the setsockopt function to change the RECVBUF size, but it's not working.

import socket
import time
import threading

def receive():
    global data
    while not kill.is_set():
        data.append(fpga.recvfrom(512)[0])

fpga = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
fpga.bind(('192.168.253.8', 34))
miss=0
data=[]
kill=threading.Event()
p=threading.Thread(target=receive)
p.start()
time.sleep(10)
kill.set()
count=len(data)
start=int.from_bytes(data[0][0:4], byteorder='big')
end=int.from_bytes(data[count-1][0:4], byteorder='big')
print("start: ",start)
print("end: ",end)
print("Number of packets: ",end-start+1)
for i in range(0,count-1):
    current=int.from_bytes(data[i][0:4], byteorder='big')
    next=int.from_bytes(data[i+1][0:4], byteorder='big')
    if next < current:
        print("out of order detected!!!")
        break
    step=next-current
    miss=miss+step-1
    if step > 1:
        print("Missing ",step-1," packet form: ",current," to: ",next,"distance form start: ",current-start)
print("Number of missing packets: ",miss)
1
First thing I noticed: range(0,count-1) will have count-1 items. So I wonder if your are handling the last item. - Klaus D.
@KlausD. OP is doing that so they can compare packets "pairwise" (i.e. the current and next variables). also the "missing packets" are in the middle, not just the "last" item. - Sam Mason
Can you absolutely confirm the transmitting end is sending all the packets/constructing them correctly? Are they received successfully/differently if you use a different UDP client? - balmy
Wireshark can correctly capture all the packets. So, I think this problem has nothing to do with my FPGA design. - Chenye Zhou
Network stack problems aren’t unknown - next step is to confirm that the packets are getting to your python code. Or can you use an alternative client and see if it shows the same gaps? - balmy

1 Answers

1
votes

I doubt it's anything to do with Windows, I'd expect it's deliberately dropping packets because they're malformed/out of spec (e.g. checksum incorrectly calculated, which is a 16bit number)

to figure out what's going on I'd suggest using a packet sniffer (e.g. Wireshark, tcpdump, Scapy) to record the same traffic your Python script is seeing. If you're lucky, filtering for "Malformed" packets will find the "missing" packets. otherwise, you could try and find the packets in the captured network traffic, having a sequence number in there should make this pretty easy. you could also alter your Python code to record timestamps, which might help narrow things down a bit