io_uring_prep_sendmsg(3)

πŸ’Œ io_uring_prep_sendmsg β€” from the liburing manual, bestie edition

okay so welcome to the io_uring_prep_sendmsg(3) page, you're gonna eat this up πŸ’…


✨ NAME ✨

io_uring_prep_sendmsg β€” get a sendmsg request all prepped and ready to go, no cap


🌸 SYNOPSIS 🌸

#include <sys/types.h>
#include <sys/socket.h>
#include <liburing.h>

void io_uring_prep_sendmsg(struct io_uring_sqe *sqe,
                           int fd,
                           const struct msghdr *msg,
                           unsigned flags);

void io_uring_prep_sendmsg_zc(struct io_uring_sqe *sqe,
                              int fd,
                              const struct msghdr *msg,
                              unsigned flags);

πŸ¦‹ DESCRIPTION πŸ¦‹

So io_uring_prep_sendmsg(3) preps a sendmsg request for you πŸ’–. The submission queue entry sqe gets set up to use the file descriptor fd, and it starts sending the data that msg points at, using the sendmsg(2)-defined flags you dropped into the flags argument.

Then there's io_uring_prep_sendmsg_zc(3) β€” she takes the exact same parameters as io_uring_prep_sendmsg(3) but preps a zerocopy sendmsg request instead. iconique ✨

Pop over to io_uring_prep_send(3) for the rundown on flags you can set in the SQE ioprio field. On top of those, the zero-copy send also supports setting IORING_SEND_ZC_REPORT_USAGE. If you set it, the notification CQE res field will report the number of bytes that got copied rather than sent with zero copy. A value of 0 means success πŸŽ€. If the value is IORING_NOTIF_USAGE_ZC_COPIED, then yeah β€” data was copied.

Now here's the tea β˜•: unlike non-zerocopy send requests, a zerocopy send will usually generate two CQEs.

  1. The first CQE holds the result of the send operation itself. If that CQE has IORING_CQE_F_MORE set in the CQE flags field, then a second notification CQE will be posted for the operation.
  2. That second notification CQE tells the application that the memory associated with the send is safe to get reused. It'll have IORING_CQE_F_NOTIF set in the CQE flags field.

Also go read the io_uring_enter(2) man page for a fuller description of the notification CQE, iykyk.

⚠️ Heads up bestie: using IOSQE_IO_LINK with this request type requires you to set MSG_WAITALL in the flags argument, because a short send isn't considered an error condition without that being set. Don't skip this one πŸ™ˆ

And fr fr, this function prepares an async sendmsg(2) request β€” see that man page for the deets.


πŸ’« RETURN VALUE πŸ’«

None. Literally nothing, she's just prepping 🫢


😭 ERRORS 😭

The CQE res field will contain the result of the operation. Check out the related man page for possible values, bestie.

πŸ’€ Important: where synchronous system calls will return -1 on failure and set errno to the actual error value, io_uring never uses errno. Instead it returns the negated errno directly in the CQE res field.

Some common error cases are:

  • -ENOMEM β€” the ulimit(1) -l setting is too low to support the size of the attempted zero copy send. Increasing the limit may help.
  • -ENOMEM β€” the kernel ran out of memory.

πŸ‘‘ NOTES πŸ‘‘

As with any request that passes in data in a struct, that data must remain valid until the request has been successfully submitted. It does not need to remain valid until completion. Once a request has been submitted, the in-kernel state is stable. Very early kernels (5.4 and earlier) required state to be stable until the completion occurred. Applications can test for this behavior by inspecting the IORING_FEAT_SUBMIT_STABLE flag passed back from io_uring_queue_init_params(3). slay 🌈


πŸ”₯ SEE ALSO πŸ”₯

io_uring_get_sqe(3), io_uring_submit(3), io_uring_buf_ring_init(3), io_uring_buf_ring_add(3), sendmsg(2)


mwah πŸ’‹ that's the whole page, you're officially fluent in zerocopy sendmsg now. This was liburing-2.2, dated March 12, 2022, and it lives in the liburing manual under io_uring_prep_sendmsg(3). go forth and submit your SQEs, main character πŸ§šπŸ’œ

πŸ“¦ where this page comes from