Open aichemzee opened 2 months ago
The ctx
is simply a helper for reusing intermediate variables required by the C library.
The most improtant thing to know is that the buf
is reused and rewrote everytime you call receive functions, and the returned MemorySegment of the received packet is a slice of the buf
.
So each ctx
should be bond to one sending||receiving
thread.
Furthermore, if you need to pass the packet to somewhere else, you will have to copy the memory of the packet.
In your case, you will need to:
newSeg.copyFrom(seg)
Let's see scenario 1:
Questions:
Let's see scenario 2:
Which of the two scenarios would you recommend?
I would recommend scenario 2, the model would be much simpler, and performance would be better since the buffers could be reused.
For scenario 1:
Also please note that the driver copies each packet to all matched windivert handles, so you would have to manually distribute packets to each threads.
Currently windivert does not support bitwise operators, so you would have to use tcp.DstPort > 0 and tcp.DstPort < 32768
, which would not be easy to evenly distribute the packets.
It would be much more simple to use and more performant if windivert could support bitwise operators or hash functions.
See also: https://github.com/basil00/Divert/issues/265
Maybe some PRs would be helpful, however I believe windivert is not meant for high performance packet processing, but a convenient tool for some lightweight network handling.
You would need dpdk if you want to process packets on an enterprise level workload.
Hi I'm trying to read a lot of packets to a queue and then sending them one by one based on certain conditions / modifications.
However we do require a context while sending the packet. What would be the best way to achieve this?
Should I create a new context for each recv call? This would preserve the context for sending but this would generate a lot of garbage.
Or should I re use the same context for all send / receive calls. Would this cause problems? Since I'll have a few consumer threads for reading and a separate one for dispatching.