Skip to content

Echoserver example - #2

Open
msaelices wants to merge 21 commits into
dmitry-salin:mainfrom
msaelices:echoserver-examples
Open

msaelices wants to merge 21 commits into
dmitry-salin:mainfrom
msaelices:echoserver-examples

Conversation

@msaelices

Copy link
Copy Markdown
Contributor

Echo server example that uses ring mapped buffers

Comment thread examples/echoserver.mojo Outdated

# Get buffer from the buffer ring
var buf_ring_ptr = buf_ring[]
var buffer = buf_ring_ptr.unsafe_buf(index=buffer_idx, len=UInt32(MAX_MESSAGE_LEN))

@dmitry-salin dmitry-salin Mar 10, 2025

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, this is the wrong way to use the buffer ring. The method is named unsafe_buf for a reason. The buffer ring has a head that is owned by the kernel and a tail that is shared between the kernel and user space. When a buffer ring is created, ownership of all buffers is transferred to the kernel. The kernel selects buffers from the ring, for example for a Recv operation, and increments the ring head, and after it completes, the kernel transfers ownership of the buffer to user-space. The buffer identifier will be passed using cqe. So it is safe for us to call unsafe_buf only when we have IoUringCqeFlags of the completed operation. When we are done with Buf, its destructor will be called and ownership will be transferred back to the kernel by incrementing the ring tail. We also have the option to prevent the ownership transfer by calling the into_index method (this could potentially be combined with converting to some new buffer type that can be used for any operation, such as Send). The "extracted" buffer (the one that has been converted to an index) can later be returned to the kernel by calling unsafe_recycle and passing the saved buffer index (this can only be done safely when we can guarantee that the buffer will no longer be used by user-space code and that there are no operations in progress that can use that buffer).

The reference C implementation is a good source of examples, io_uring-udp.c - is one example of using a buffer ring. buf-ring.c - another one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmitry-salin sorry, I'm far from being an expert on io_uring. I hugely appreciate any help from you and this was a good explanation. Unfortunately, the C example is not easily transferrable to the Mojo code, and, with my current expertise, I'm not sure the exact change I need to do.

However, I've tried and this change seems to work well, but not sure if it's correct: msaelices@8244e65

Could you please take a look?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance of taking a look to the example? Thanks in advance

@msaelices
msaelices force-pushed the echoserver-examples branch from 11314e0 to 62be884 Compare March 13, 2025 23:07
Disclaimer: Not sure if this change is right or not
@msaelices
msaelices force-pushed the echoserver-examples branch from 62be884 to 8244e65 Compare March 13, 2025 23:09
@msaelices
msaelices requested a review from dmitry-salin March 14, 2025 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants