[M] Change in libosmocore[master]: Refactor iofd_msghdr_alloc()
pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/44141?usp=email ) Change subject: Refactor iofd_msghdr_alloc() ...................................................................... Refactor iofd_msghdr_alloc() * flags can be passed during iofd_msghdr_alloc() as a parameter easily rather than later on assigning it to the already created object, do so. * msghdr has to be filled in differently mainly based on whether it is to be used for read or write related operations. Hence, split the allocation into 2 functions and fill as much as possible inside the allocation function (eg. internal pointers from the iofd_msghdr to the (sub) struct msghdr one. This way all users of the iofd_msghdr alloction are simplified a lot. Change-Id: I6552db468aaad7f638dade748f94a30255307f03 --- M src/core/osmo_io.c M src/core/osmo_io_internal.h M src/core/osmo_io_poll.c M src/core/osmo_io_uring.c 4 files changed, 101 insertions(+), 111 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/41/44141/1 diff --git a/src/core/osmo_io.c b/src/core/osmo_io.c index 83f3c05..a341b5c 100644 --- a/src/core/osmo_io.c +++ b/src/core/osmo_io.c @@ -136,15 +136,13 @@ /*! Allocate the msghdr. * \param[in] iofd the osmo_io file structure * \param[in] action the action this msg(hdr) is for (read, write, ..) - * \param[in] msg the msg buffer to use. Will allocate a new one if NULL + * \param[in] flags Flags to set on the msghdr and be passed on its fd operations. * \param[in] cmsg_size size (in bytes) of iofd_msghdr.cmsg buffer. Can be 0 if cmsg is not used. * \returns the newly allocated msghdr or NULL in case of error */ -struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg, - size_t cmsg_size) +static struct iofd_msghdr *_iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, + int flags, size_t cmsg_size) { struct iofd_msghdr *msghdr; - uint8_t idx, io_len; - msghdr = talloc_zero_size(iofd, sizeof(struct iofd_msghdr) + cmsg_size); if (!msghdr) @@ -152,20 +150,42 @@ msghdr->action = action; msghdr->iofd = iofd; + msghdr->flags = flags; + + msghdr->hdr.msg_iov = &msghdr->iov[0]; + + if (cmsg_size > 0) { + msghdr->hdr.msg_control = msghdr->cmsg; + msghdr->hdr.msg_controllen = cmsg_size; + } + return msghdr; +} + +/*! Allocate the msghdr. + * \param[in] iofd the osmo_io file structure + * \param[in] action the action this msg(hdr) is for (read, write, ..) + * \param[in] flags Flags to set on the msghdr and be passed on its fd operations. + * \param[in] cmsg_size size (in bytes) of iofd_msghdr.cmsg buffer. Can be 0 if cmsg is not used. + * \returns the newly allocated msghdr or NULL in case of error */ +struct iofd_msghdr *iofd_msghdr_alloc_for_reading(struct osmo_io_fd *iofd, enum iofd_msg_action action, + int flags, size_t cmsg_size) +{ + struct iofd_msghdr *msghdr; + uint8_t idx, io_len; + + msghdr = _iofd_msghdr_alloc(iofd, action, flags, cmsg_size); + if (!msghdr) + return NULL; /* Allocate the number of read buffers, configured by the user. Use msg as first buffer, if not NULL. * Only READ may have multiple buffers, because packets will only be written to the first buffer. */ io_len = (action == IOFD_ACT_READ) ? iofd->io_read_buffers : 1; for (idx = 0; idx < io_len; idx++) { - if (msg) { - talloc_steal(iofd, msg); - msghdr->msg[idx] = msg; - msg = NULL; - } else { - msghdr->msg[idx] = iofd_msgb_alloc(iofd); - if (!msghdr->msg[idx]) - break; - } + msghdr->msg[idx] = iofd_msgb_alloc(iofd); + if (!msghdr->msg[idx]) + break; + msghdr->iov[idx].iov_base = msghdr->msg[idx]->tail; + msghdr->iov[idx].iov_len = msgb_tailroom(msghdr->msg[idx]); } /* If at least one msgb is allocated, we can continue with only one msgb, instead of completely failing. */ if (idx == 0) { @@ -174,6 +194,47 @@ } msghdr->io_len = idx; + msghdr->hdr.msg_iovlen = idx; + + if (action != IOFD_ACT_READ) { + msghdr->hdr.msg_name = &msghdr->osa.u.sa; + msghdr->hdr.msg_namelen = sizeof(msghdr->osa.u.sas); + } + + return msghdr; +} + +/*! Allocate the msghdr. + * \param[in] iofd the osmo_io file structure + * \param[in] action the action this msg(hdr) is for (read, write, ..) + * \param[in] flags Flags to set on the msghdr and be passed on its fd operations. + * \param[in] cmsg_size size (in bytes) of iofd_msghdr.cmsg buffer. Can be 0 if cmsg is not used. + * \param[in] msg the msg buffer to use. Will allocate a new one if NULL + * \returns the newly allocated msghdr or NULL in case of error */ +struct iofd_msghdr *iofd_msghdr_alloc_for_writing(struct osmo_io_fd *iofd, enum iofd_msg_action action, + int flags, ssize_t cmsg_size, struct msgb *msg) +{ + struct iofd_msghdr *msghdr; + + OSMO_ASSERT(msg); + + msghdr = _iofd_msghdr_alloc(iofd, action, flags, cmsg_size); + if (!msghdr) + return NULL; + + talloc_steal(iofd, msg); + msghdr->msg[0] = msg; + + msghdr->iov[0].iov_base = msgb_data(msghdr->msg[0]); + msghdr->iov[0].iov_len = msgb_length(msghdr->msg[0]); + + msghdr->io_len = 1; + msghdr->hdr.msg_iovlen = 1; + + if (action != IOFD_ACT_WRITE) { + msghdr->hdr.msg_name = &msghdr->osa.u.sa; + /* msghdr->hdr.msg_namelen to be filled in by user when setting address! */ + } return msghdr; } @@ -640,7 +701,6 @@ { int rc; struct iofd_msghdr *msghdr; - int idx; if (OSMO_UNLIKELY(msgb_length(msg) == 0)) { LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n"); @@ -654,30 +714,25 @@ msghdr = llist_last_entry_or_null(&iofd->tx_queue.msg_queue, struct iofd_msghdr, list); if (msghdr && msghdr->io_len < iofd->io_write_buffers) { /* Add msg to existing msghdr. */ - msghdr->msg[msghdr->io_len++] = msg; - } else { - /* Create new msghdr with msg. */ - msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg, 0); - if (!msghdr) - return -ENOMEM; + msghdr->msg[msghdr->io_len] = msg; + msghdr->iov[msghdr->io_len].iov_base = msgb_data(msg); + msghdr->iov[msghdr->io_len].iov_len = msgb_length(msg); + msghdr->io_len++; + msghdr->hdr.msg_iovlen = msghdr->io_len; msghdr->hdr.msg_iov = &msghdr->iov[0]; - msghdr->flags = MSG_NOSIGNAL; + return 0; } - /* Add set IO vector to msg. */ - idx = msghdr->io_len - 1; - msghdr->iov[idx].iov_base = msgb_data(msg); - msghdr->iov[idx].iov_len = msgb_length(msg); - msghdr->hdr.msg_iovlen = msghdr->io_len; + /* Create new msghdr with msg and enqueue it. */ + msghdr = iofd_msghdr_alloc_for_writing(iofd, IOFD_ACT_WRITE, MSG_NOSIGNAL, 0, msg); + if (!msghdr) + return -ENOMEM; - /* Only new msghdr will be enqueued. */ - if (msghdr->io_len == 1) { - rc = iofd_txqueue_enqueue(iofd, msghdr); - if (rc < 0) { - iofd_msghdr_free(msghdr); - LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc); - return rc; - } + rc = iofd_txqueue_enqueue(iofd, msghdr); + if (rc < 0) { + iofd_msghdr_free(msghdr); + LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc); + return rc; } return 0; @@ -710,20 +765,14 @@ OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO); - struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg, 0); + struct iofd_msghdr *msghdr = iofd_msghdr_alloc_for_writing(iofd, IOFD_ACT_SENDTO, sendto_flags, 0, msg); if (!msghdr) return -ENOMEM; if (dest) { msghdr->osa = *dest; - msghdr->hdr.msg_name = &msghdr->osa.u.sa; msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa); } - msghdr->flags = sendto_flags; - msghdr->iov[0].iov_base = msgb_data(msghdr->msg[0]); - msghdr->iov[0].iov_len = msgb_length(msghdr->msg[0]); - msghdr->hdr.msg_iov = &msghdr->iov[0]; - msghdr->hdr.msg_iovlen = 1; rc = iofd_txqueue_enqueue(iofd, msghdr); if (rc < 0) { @@ -773,32 +822,19 @@ return -EINVAL; } - msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDMSG, msg, msgh->msg_controllen); + msghdr = iofd_msghdr_alloc_for_writing(iofd, IOFD_ACT_SENDMSG, sendmsg_flags, msgh->msg_controllen, msg); if (!msghdr) return -ENOMEM; /* copy over optional address */ if (msgh->msg_name) { memcpy(&msghdr->osa, msgh->msg_name, msgh->msg_namelen); - msghdr->hdr.msg_name = &msghdr->osa.u.sa; - msghdr->hdr.msg_namelen = msgh->msg_namelen; + msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa); } - /* build iov from msgb */ - msghdr->iov[0].iov_base = msgb_data(msghdr->msg[0]); - msghdr->iov[0].iov_len = msgb_length(msghdr->msg[0]); - msghdr->hdr.msg_iov = &msghdr->iov[0]; - msghdr->hdr.msg_iovlen = 1; - /* copy over the cmsg from the msghdr */ - if (msgh->msg_control && msgh->msg_controllen) { - msghdr->hdr.msg_control = msghdr->cmsg; - msghdr->hdr.msg_controllen = msgh->msg_controllen; + if (msgh->msg_control && msgh->msg_controllen) memcpy(msghdr->cmsg, msgh->msg_control, msgh->msg_controllen); - } - - /* copy over msg_flags */ - msghdr->hdr.msg_flags = sendmsg_flags; rc = iofd_txqueue_enqueue(iofd, msghdr); if (rc < 0) { diff --git a/src/core/osmo_io_internal.h b/src/core/osmo_io_internal.h index 81a70f3..6ad9313 100644 --- a/src/core/osmo_io_internal.h +++ b/src/core/osmo_io_internal.h @@ -185,7 +185,10 @@ IOFD_SEG_ACT_DEFER, }; -struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg, size_t cmsg_size); +struct iofd_msghdr *iofd_msghdr_alloc_for_reading(struct osmo_io_fd *iofd, enum iofd_msg_action action, + int flags, size_t cmsg_size); +struct iofd_msghdr *iofd_msghdr_alloc_for_writing(struct osmo_io_fd *iofd, enum iofd_msg_action action, + int flags, ssize_t cmsg_size, struct msgb *msg); void iofd_msghdr_free(struct iofd_msghdr *msghdr); struct msgb *iofd_msgb_alloc2(struct osmo_io_fd *iofd, size_t size); diff --git a/src/core/osmo_io_poll.c b/src/core/osmo_io_poll.c index b861675..60f2cfd 100644 --- a/src/core/osmo_io_poll.c +++ b/src/core/osmo_io_poll.c @@ -87,7 +87,6 @@ enum iofd_msg_action action; struct iofd_msghdr *msghdr; int rc; - uint8_t idx; if (what & OSMO_FD_READ) { @@ -105,34 +104,12 @@ OSMO_ASSERT(0); } - msghdr = iofd_msghdr_alloc(iofd, action, NULL, iofd->cmsg_size); + msghdr = iofd_msghdr_alloc_for_reading(iofd, action, 0, iofd->cmsg_size); if (!msghdr) { LOGPIO(iofd, LOGL_ERROR, "Could not allocate msghdr for reading\n"); OSMO_ASSERT(0); } - for (idx = 0; idx < msghdr->io_len; idx++) { - msghdr->iov[idx].iov_base = msghdr->msg[idx]->tail; - msghdr->iov[idx].iov_len = msgb_tailroom(msghdr->msg[idx]); - } - - switch (action) { - case IOFD_ACT_RECVMSG: - msghdr->hdr.msg_control = msghdr->cmsg; - msghdr->hdr.msg_controllen = iofd->cmsg_size; - /* fall-through */ - case IOFD_ACT_RECVFROM: - msghdr->hdr.msg_name = &msghdr->osa.u.sa; - msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa); - /* fall-through */ - case IOFD_ACT_READ: - msghdr->hdr.msg_iov = &msghdr->iov[0]; - msghdr->hdr.msg_iovlen = msghdr->io_len; - break; - default: - OSMO_ASSERT(0); - } - switch (action) { case IOFD_ACT_READ: rc = readv(ofd->fd, msghdr->hdr.msg_iov, msghdr->hdr.msg_iovlen); diff --git a/src/core/osmo_io_uring.c b/src/core/osmo_io_uring.c index 400b65b..8c1c7af 100644 --- a/src/core/osmo_io_uring.c +++ b/src/core/osmo_io_uring.c @@ -248,7 +248,6 @@ { struct iofd_msghdr *msghdr; struct io_uring_sqe *sqe; - uint8_t idx; /* Tell iofd_uring_get_sqe() not to allocate a new ring, if we want to enqueue multiple read SQEs. */ sqe = iofd_uring_get_sqe(iofd, true); @@ -259,34 +258,12 @@ OSMO_ASSERT(0); } - msghdr = iofd_msghdr_alloc(iofd, action, NULL, iofd->cmsg_size); + msghdr = iofd_msghdr_alloc_for_reading(iofd, action, 0, iofd->cmsg_size); if (!msghdr) { LOGPIO(iofd, LOGL_ERROR, "Could not allocate msghdr for reading\n"); OSMO_ASSERT(0); } - for (idx = 0; idx < msghdr->io_len; idx++) { - msghdr->iov[idx].iov_base = msghdr->msg[idx]->tail; - msghdr->iov[idx].iov_len = msgb_tailroom(msghdr->msg[idx]); - } - - switch (action) { - case IOFD_ACT_RECVMSG: - msghdr->hdr.msg_control = msghdr->cmsg; - msghdr->hdr.msg_controllen = iofd->cmsg_size; - /* fall-through */ - case IOFD_ACT_RECVFROM: - msghdr->hdr.msg_name = &msghdr->osa.u.sa; - msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa); - /* fall-through */ - case IOFD_ACT_READ: - msghdr->hdr.msg_iov = &msghdr->iov[0]; - msghdr->hdr.msg_iovlen = msghdr->io_len; - break; - default: - OSMO_ASSERT(0); - } - switch (action) { case IOFD_ACT_READ: io_uring_prep_readv(sqe, iofd->fd, msghdr->iov, msghdr->hdr.msg_iovlen, -1); @@ -713,15 +690,12 @@ LOGPIO(iofd, LOGL_ERROR, "Could not allocate msgb for writing\n"); OSMO_ASSERT(0); } - msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg, 0); + msghdr = iofd_msghdr_alloc_for_writing(iofd, IOFD_ACT_WRITE, 0, 0, msg); if (!msghdr) { LOGPIO(iofd, LOGL_ERROR, "Could not allocate msghdr for writing\n"); OSMO_ASSERT(0); } - msghdr->iov[0].iov_base = msgb_data(msg); - msghdr->iov[0].iov_len = msgb_length(msg); - sqe = iofd_uring_get_sqe(iofd, false); if (!sqe) { LOGPIO(iofd, LOGL_ERROR, "Could not get io_uring_sqe\n"); -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/44141?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: newchange Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I6552db468aaad7f638dade748f94a30255307f03 Gerrit-Change-Number: 44141 Gerrit-PatchSet: 1 Gerrit-Owner: pespin <pespin@sysmocom.de>
participants (1)
-
pespin