Skip to content

Commit

Permalink
Make a writemGSO API with just iovecs [2/n]
Browse files Browse the repository at this point in the history
Summary:
**Background for the diff stack**: We'd like to excise the usage of folly from a library that we maintain (quic), which is why we'd like to have a version of `writemGSO` that takes in iovecs instead of IOBufs.

**What this diff is doing**: I'd like to use `fillMsgVec` within the version of `writemGSO` that takes in iovecs instead of IOBufs. I'm changing the signature of `fillMsgVec` so that it doesn't need to take in any IOBufs. Instead, it gets the lengths of the buffers from the passed in array `messageIovLens`, which is filled in by `fillIoVec`.

Reviewed By: jbeshay

Differential Revision: D67873321

fbshipit-source-id: 93181cc69f51bce84e985c17b5f0d660e12c620e
  • Loading branch information
Aman Sharma authored and facebook-github-bot committed Jan 22, 2025
1 parent aa229d8 commit e969dc6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
31 changes: 24 additions & 7 deletions folly/io/async/AsyncUDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,11 +868,13 @@ int AsyncUDPSocket::writemGSO(
void AsyncUDPSocket::fillIoVec(
const std::unique_ptr<folly::IOBuf>* bufs,
struct iovec* iov,
size_t* messageIovLens,
size_t count,
size_t iov_count) {
size_t remaining = iov_count;
size_t iov_pos = 0;
for (size_t i = 0; i < count; i++) {
messageIovLens[i] = bufs[i]->countChainElements();
auto ret = bufs[i]->fillIov(&iov[iov_pos], remaining);
size_t iovec_len = ret.numIovecs;
remaining -= iovec_len;
Expand All @@ -882,7 +884,7 @@ void AsyncUDPSocket::fillIoVec(

void AsyncUDPSocket::fillMsgVec(
Range<full_sockaddr_storage*> addrs,
const std::unique_ptr<folly::IOBuf>* bufs,
size_t* messageIovLens,
size_t count,
struct mmsghdr* msgvec,
struct iovec* iov,
Expand All @@ -905,7 +907,7 @@ void AsyncUDPSocket::fillMsgVec(
msg.msg_namelen = addrs[addr_count - 1].len;
}
msg.msg_iov = &iov[iov_pos];
msg.msg_iovlen = bufs[i]->countChainElements();
msg.msg_iovlen = messageIovLens[i];
#ifdef FOLLY_HAVE_MSG_ERRQUEUE
size_t controlBufSize = 1 +
cmsgs_->size() *
Expand Down Expand Up @@ -982,7 +984,7 @@ void AsyncUDPSocket::fillMsgVec(

msgvec[i].msg_len = 0;

iov_pos += bufs[i]->countChainElements();
iov_pos += messageIovLens[i];
}
}

Expand Down Expand Up @@ -1015,15 +1017,30 @@ int AsyncUDPSocket::writeImpl(
FOLLY_PUSH_WARNING
FOLLY_GNU_DISABLE_WARNING("-Wvla")
iovec iov[BOOST_PP_IF(FOLLY_HAVE_VLA_01, iov_count, kSmallSizeMax)];
size_t messageIovLens[BOOST_PP_IF(FOLLY_HAVE_VLA_01, count, kSmallSizeMax)];
FOLLY_POP_WARNING
fillIoVec(bufs, iov, count, iov_count);
fillMsgVec(range(addrStorage), bufs, count, msgvec, iov, options, control);
fillIoVec(bufs, iov, messageIovLens, count, iov_count);
fillMsgVec(
range(addrStorage),
messageIovLens,
count,
msgvec,
iov,
options,
control);
ret = sendmmsg(fd_, msgvec, count, 0);
} else {
std::unique_ptr<iovec[]> iov(new iovec[iov_count]);
fillIoVec(bufs, iov.get(), count, iov_count);
std::unique_ptr<size_t[]> messageIovLens(new size_t[count]);
fillIoVec(bufs, iov.get(), messageIovLens.get(), count, iov_count);
fillMsgVec(
range(addrStorage), bufs, count, msgvec, iov.get(), options, control);
range(addrStorage),
messageIovLens.get(),
count,
msgvec,
iov.get(),
options,
control);
ret = sendmmsg(fd_, msgvec, count, 0);
}

Expand Down
3 changes: 2 additions & 1 deletion folly/io/async/AsyncUDPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,13 @@ class AsyncUDPSocket : public EventHandler {
void fillIoVec(
const std::unique_ptr<folly::IOBuf>* bufs,
struct iovec* iov,
size_t* messageIovLens,
size_t count,
size_t iov_count);

void fillMsgVec(
Range<full_sockaddr_storage*> addrs,
const std::unique_ptr<folly::IOBuf>* bufs,
size_t* messageIovLens,
size_t count,
struct mmsghdr* msgvec,
struct iovec* iov,
Expand Down

0 comments on commit e969dc6

Please sign in to comment.