lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] TCP Raw API questions about efficiency and threads


From: Pîrvu Mihai
Subject: Re: [lwip-users] TCP Raw API questions about efficiency and threads
Date: Tue, 7 Jun 2016 15:43:33 +0300

Alright, I've modified my code a little. What I've done is pretty much this:

- from recv callback, I just call tcp_recved on pbuf->tot_len and return ERR_OK
- also in recv callback, i save the pbuf in my client arg and process it in poll
- in accept callback, i set up the arg to my internal struct, and a poll function with argument 1 (as far as I understand that means polling every 500ms, can i change this? seems too slow for what I try to achieve)
- In poll callback, I process the packet(s) because there can be multiple packets arriving in my pbuf (packets that are concatenated together). I read all the data (pbuf->tot_len) in a separate buffer then loop the buffer for each individual inner packet. For each packet, I check the destination, if it's broadcast, I send it to all my active clients (note: that all connections are active until they disconnect from client-side, that means I don't have any keep-alive verification, if I get in recv callback with pbuf == NULL => client disconnected, otherwise I consider it active right now). If it's a unicast message, I send it just to the respective peer. I do my checking by MAC address (clients will send their MAC as the first message when they connect to server). I call tcp_write in order to send my messages (I saw something about tcp_output when I use tcp_write from outside recv callback, I'm not using that now).

After I've done this (before I processed the packet in recv callback, not poll callback), my ping between clients is even worse (700ms~) and only one client receives ICMP replies (so no concurrency).

I feel like I'm doing lots of stuff wrong, like copying the data from pbuf to my static buffer, then calling tcp_write with flag TCP_WRITE_FLAG_COPY seems like overkill when copying data.

Regarding of you saying stuff about examples online, I can't find any more "complex" example out there. All the examples have a really simple design, client sends message, receives message back and then the connection is closed. Is there any examples out there where connections are persistent and messages can come at any given time ?

Mihai

On Fri, Jun 3, 2016 at 10:21 AM, Noam Weissman <address@hidden> wrote:


In order to use RAW API efficiently you can use the poll call back to send more data. What do I 

mean by that ?

Lets assume you got some request for data and you are handling it inside the recv function.

If you send back all the data you are busy inside that recv instant and blocking other parallel

connections. One way to avoid this is to start sending (a portion) and get out of the recv. The rest of the send will be handled inside the poll function. the poll function is called periodically as long as the

current connection is a live... house keeping.


Once you no longer need that connection can gracefully close it from inside the poll function.


Another way to use the poll call back is to put some data in a queue (from main thread) while from

inside the poll call back (TCP own thread) read that data and send it to the open connection.

 

BR,

Noam.








From: lwip-users <lwip-users-bounces+noam=address@hidden> on behalf of Pîrvu Mihai <address@hidden>
Sent: Friday, June 3, 2016 12:15 AM
To: Mailing list for lwIP users
Subject: [lwip-users] TCP Raw API questions about efficiency and threads
 
Hello guys, I've just ported my application to tcp raw API and I was wondering a few things. 

I've noticed that the performance is a bit too bad (300-500 ms for pings, between local clients) and I was wondering if I'm doing some things wrong.

Firstly,  I'm doing all my packet processing + sending of replies in tcp_recv callback. Is this normal, or shall I save the data somehow and process it later. My idea is that this blocks the main thread and the next packets are delayed. But I don't know this for sure.

I don't understand how I can use multiple threads using this API. It's callback based, so where do I create the threads? Let's say I want to process the entire callback saved using tcp_recv on a separate thread. Is that possible or do I have to create a new thread inside the callback?

The application I'm using is a vpn server that has to inspect the packets (so I do that in tcp_recv), see where to forward one packet (from a list of connected clients to the server) and send that data to the client. I saw that each tcp_pcb has a unique memory address (obv.), so I save that in an array and if the client IP/MAC is the same as on in the array, I send my data to that pcb from the array. I can also send broadcasts (if mac is FF:FF:FF:FF:FF:FF). This is the "processing" that is done. 

One bad thing is that I copy the entire pbuf into a local buffer and free it (I saw that there's some idea to save it for later use, would love to see some examples on that).


_______________________________________________
lwip-users mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/lwip-users


reply via email to

[Prev in Thread] Current Thread [Next in Thread]