[Winpcap-users] Multithreaded programming with WinPCap

Prachyabrued Mores mxp4758 at louisiana.edu
Sat Apr 29 02:02:52 GMT 2006


Hello!,

I found out today that setting read timeout to -1 value allow my multithread 
program to surpass the single thread version. Now the achievable throughput 
is about 55 Mbps (out of 100Mbps). A large improvement from a previous 10 
Mbps.

Surprisingly, -1 value (no wait, return immediately) is not mentioned in 
current version of WinPCap help file. I have to browse through the archives.

Btw, i still need an answer to this questions,

1. Can i access the same descriptor in different thread without 
synchronization?

2. Since i am implementing (application level) router queue using WinPCap, i 
wonder if there is any underlying system queue used by WinPCap? Is it 
possible for packet to be dropped by WinPCap before reaching my app?

3. If there is someone who has enough patience to read my first question. A 
suggestion or comment to improve my program will be very welcome.!

Thank you,
Mark

On Fri, 28 Apr 2006 02:36:19 -0500, Prachyabrued Mores wrote
> Hello!,
> 
> I am experimenting with a queue management algorithm (BLUE) for a 
> router. I use a PC with two network interface cards to simulate a 
> router, write a program using WinPCap to capture packets from one 
> interface and forward to the other interface.
> 
> I tried first with a single thread version to test WinPCap features 
> (i am new to WinPCap). I tested with ftp and got about 50 Mbps using 
> my simulated router on 100 Mbps network. The code was something like;
> 
> (set read timeout to 1ms in pcap_open_live)
> for(;;)
>    if (pcap_next_ex(nic1,,packet) == 1) {
>       // if packet available at nic1, forward to nic2
>       pcap_sendpacket(nic2,packet,); 
>    }
>    if (pcap_next_ex(nic2,,packet) == 1) {
>       // if packet available at nic2, forward to nic1
>       pcap_sendpacket(nic1,packet,);
>    }
> }
> 
> At first, i didn't care about performance and go on to implement 
> multithread version to experiment with queue algorithm. The code is 
> something like
> 
> (again, set read timeout to 1ms in pcap_open_live)
> 
> Thread1: 
> for(;;)
>    if (pcap_next_ex(nic1, packet)==1) {
>       queue1.enqueue(packet);   
>    }
>    packet = queue2.dequeue();
>    if (packet != NULL) {
>       pcap_sendpacket(nic1,packet,);
>    }
> }
> 
> Thread2: 
> for(;;)
>    if (pcap_next_ex(nic2, packet)==1) {
>       queue2.enqueue(packet2);   
>    }
>    packet = queue1.dequeue();
>    if (packet != NULL) {
>       pcap_sendpacket(nic2,packe,);
>    }
> }
> 
> (queues are already synchronized by semaphore, packet is local var)
> 
> The idea is each thread reads/writes on its corresponding nic and 
> communicate with each other via queues. Surprisingly, i get only 
> about 9 Mbps using this multithread programming. Now, i seriously 
> seek how to improve the performance of my implementation.
> 
> I try to browse the WinPCap archive and found many related topics. 
> But admittedly, i couldn't understand them completely and decide to 
> ask a direct questions here.
> 
> One of the thing i can grab from the archive is about thread sleep,
>  read timeout of WinPCap and maybe more. I have 3 questions to ask here:
> 
> 1. According to my implementation, i may have to wait for 1ms(read 
> timeout) before i can dequeue. This maybe the cause?? Will it help 
> if i have 4 threads? (read nic1, write nic1, read nic2, write nic2)
>  I think that this will decouple dequeue operation from read 
> timeout. In other words, dequeue doesn't have to wait for pcap_next_ex.
> 
> 2. If i have 4 threads, can i read&write to the same descriptor at 
> the same time? Do i need to protect it with some synchronization construct?
> 
> 3. Could you suggest a way to improve my multithreaded 
> implementation?? I am sure that my implementation is very naive and 
> can be improved a lot. Alas, i am not good with WinPCap and 
> multithread programming and would like to seek some sage advice here.
> 
> Thank you for bearing with me and hope to get some response soon,
> Mark
> --
> 
> _______________________________________________
> Winpcap-users mailing list
> Winpcap-users at winpcap.org
> https://www.winpcap.org/mailman/listinfo/winpcap-users


--



More information about the Winpcap-users mailing list