Skip to content

Commit

Permalink
Folly SocketOptionMap with strings - separate cmsgs
Browse files Browse the repository at this point in the history
Summary: Splitting cmgs from socket options.

Reviewed By: mjoras

Differential Revision: D49557451

fbshipit-source-id: fa3005170d049557d1c845fd14b0ada73b58b377
  • Loading branch information
avasylev authored and facebook-github-bot committed Oct 1, 2023
1 parent d8bef8b commit e363724
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 53 deletions.
3 changes: 3 additions & 0 deletions folly/io/SocketOptionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ using SocketNontrivialOptionMap = std::map<SocketOptionKey, std::string>;
extern const SocketOptionMap emptySocketOptionMap;
extern const SocketNontrivialOptionMap emptySocketNontrivialOptionMap;

using SocketCmsgMap = std::map<SocketOptionKey, int>;
using SocketNontrivialCmsgMap = std::map<SocketOptionKey, std::string>;

int applySocketOptions(
NetworkSocket fd,
const SocketOptionMap& options,
Expand Down
8 changes: 4 additions & 4 deletions folly/io/async/AsyncUDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1556,16 +1556,16 @@ void AsyncUDPSocket::attachEventBase(folly::EventBase* evb) {
updateRegistration();
}

void AsyncUDPSocket::setCmsgs(const SocketOptionMap& cmsgs) {
void AsyncUDPSocket::setCmsgs(const SocketCmsgMap& cmsgs) {
defaultCmsgs_ = cmsgs;
}

void AsyncUDPSocket::setNontrivialCmsgs(
const SocketNontrivialOptionMap& nontrivialCmsgs) {
const SocketNontrivialCmsgMap& nontrivialCmsgs) {
nontrivialCmsgs_ = nontrivialCmsgs;
}

void AsyncUDPSocket::appendCmsgs(const SocketOptionMap& cmsgs) {
void AsyncUDPSocket::appendCmsgs(const SocketCmsgMap& cmsgs) {
for (auto itr = cmsgs.begin(); itr != cmsgs.end(); ++itr) {
defaultCmsgs_[itr->first] = itr->second;
}
Expand All @@ -1586,7 +1586,7 @@ void AsyncUDPSocket::maybeUpdateDynamicCmsgs() noexcept {
}

void AsyncUDPSocket::appendNontrivialCmsgs(
const SocketNontrivialOptionMap& nontrivialCmsgs) {
const SocketNontrivialCmsgMap& nontrivialCmsgs) {
for (auto itr = nontrivialCmsgs.begin(); itr != nontrivialCmsgs.end();
++itr) {
nontrivialCmsgs_[itr->first] = itr->second;
Expand Down
19 changes: 9 additions & 10 deletions folly/io/async/AsyncUDPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ class AsyncUDPSocket : public EventHandler {

using IOBufFreeFunc = folly::Function<void(std::unique_ptr<folly::IOBuf>&&)>;

using AdditionalCmsgsFunc =
folly::Function<folly::Optional<SocketOptionMap>()>;
using AdditionalCmsgsFunc = folly::Function<folly::Optional<SocketCmsgMap>()>;

struct WriteOptions {
WriteOptions() = default;
Expand Down Expand Up @@ -239,13 +238,13 @@ class AsyncUDPSocket : public EventHandler {
/**
* Set extra control messages to send
*/
virtual void setCmsgs(const SocketOptionMap& cmsgs);
virtual void setCmsgs(const SocketCmsgMap& cmsgs);
virtual void setNontrivialCmsgs(
const SocketNontrivialOptionMap& nontrivialCmsgs);
const SocketNontrivialCmsgMap& nontrivialCmsgs);

virtual void appendCmsgs(const SocketOptionMap& cmsgs);
virtual void appendCmsgs(const SocketCmsgMap& cmsgs);
virtual void appendNontrivialCmsgs(
const SocketNontrivialOptionMap& nontrivialCmsgs);
const SocketNontrivialCmsgMap& nontrivialCmsgs);
virtual void setAdditionalCmsgsFunc(
AdditionalCmsgsFunc&& additionalCmsgsFunc) {
additionalCmsgsFunc_ = std::move(additionalCmsgsFunc);
Expand Down Expand Up @@ -637,11 +636,11 @@ class AsyncUDPSocket : public EventHandler {

IOBufFreeFunc ioBufFreeFunc_;

SocketOptionMap defaultCmsgs_;
SocketOptionMap dynamicCmsgs_;
SocketOptionMap* cmsgs_{&defaultCmsgs_};
SocketCmsgMap defaultCmsgs_;
SocketCmsgMap dynamicCmsgs_;
SocketCmsgMap* cmsgs_{&defaultCmsgs_};

SocketNontrivialOptionMap nontrivialCmsgs_;
SocketNontrivialCmsgMap nontrivialCmsgs_;

AdditionalCmsgsFunc additionalCmsgsFunc_;

Expand Down
70 changes: 35 additions & 35 deletions folly/io/async/test/AsyncUDPSocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ MATCHER_P(HasCmsgs, cmsgs, "") {
return false;
}
#ifdef FOLLY_HAVE_MSG_ERRQUEUE
folly::SocketOptionMap sentCmsgs;
folly::SocketCmsgMap sentCmsgs;

struct cmsghdr* cmsg;
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != nullptr;
Expand Down Expand Up @@ -997,7 +997,7 @@ MATCHER_P(HasNontrivialCmsgs, cmsgs, "") {
return false;
}
#ifdef FOLLY_HAVE_MSG_ERRQUEUE
folly::SocketNontrivialOptionMap sentCmsgs;
folly::SocketNontrivialCmsgMap sentCmsgs;

struct cmsghdr* cmsg;
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != nullptr;
Expand Down Expand Up @@ -1029,13 +1029,13 @@ TEST_F(AsyncUDPSocketTest, TestWriteCmsg) {
#ifdef FOLLY_HAVE_MSG_ERRQUEUE
// empty
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
EXPECT_CALL(*netOpsDispatcher, sendmsg(_, HasCmsgs(cmsgs), _));
socket_->write(addr, folly::IOBuf::copyBuffer("hey"));
}
// writeGSO
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
cmsgs[{SOL_UDP, UDP_SEGMENT}] = 1;
EXPECT_CALL(*netOpsDispatcher, sendmsg(_, HasCmsgs(cmsgs), _));
socket_->writeGSO(
Expand All @@ -1046,29 +1046,29 @@ TEST_F(AsyncUDPSocketTest, TestWriteCmsg) {
}
// SO_MARK
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
cmsgs[{SOL_SOCKET, SO_MARK}] = 123;
socket_->setCmsgs(cmsgs);
EXPECT_CALL(*netOpsDispatcher, sendmsg(_, HasCmsgs(cmsgs), _));
socket_->write(addr, folly::IOBuf::copyBuffer("hey"));
}
// append IP_TOS
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
cmsgs[{IPPROTO_IP, IP_TOS}] = 456;
socket_->appendCmsgs(cmsgs);
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TOS}] = 456;
expectedCmsgs[{SOL_SOCKET, SO_MARK}] = 123;
EXPECT_CALL(*netOpsDispatcher, sendmsg(_, HasCmsgs(expectedCmsgs), _));
socket_->write(addr, folly::IOBuf::copyBuffer("hey"));
}
// append IP_TOS with a different value
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
cmsgs[{IPPROTO_IP, IP_TOS}] = 789;
socket_->appendCmsgs(cmsgs);
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TOS}] = 789;
expectedCmsgs[{SOL_SOCKET, SO_MARK}] = 123;
socket_->setCmsgs(expectedCmsgs);
Expand All @@ -1077,7 +1077,7 @@ TEST_F(AsyncUDPSocketTest, TestWriteCmsg) {
}
// writeGSO with IP_TOS and SO_MARK
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TOS}] = 789;
expectedCmsgs[{SOL_SOCKET, SO_MARK}] = 123;
expectedCmsgs[{SOL_UDP, UDP_SEGMENT}] = 1;
Expand All @@ -1101,22 +1101,22 @@ TEST_F(AsyncUDPSocketTest, TestWriteDynamicCmsg) {
MockFunction<AsyncUDPSocket::AdditionalCmsgsFunc> mockAdditionalCmsgs;
int mockCallCount = 1;
socket_->setAdditionalCmsgsFunc([&mockCallCount]() {
folly::SocketOptionMap additionalCmsgs;
folly::SocketCmsgMap additionalCmsgs;
additionalCmsgs[{IPPROTO_IP, IP_TTL}] = mockCallCount++;
return additionalCmsgs;
});

#ifdef FOLLY_HAVE_MSG_ERRQUEUE
// Dynamic cmsgs only (IP_TTL)
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TTL}] = mockCallCount;
EXPECT_CALL(*netOpsDispatcher, sendmsg(_, HasCmsgs(expectedCmsgs), _));
socket_->write(addr, folly::IOBuf::copyBuffer("hey"));
}
// writeGSO with dynamic cmsgs (IP_TTL)
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{SOL_UDP, UDP_SEGMENT}] = 1;
expectedCmsgs[{IPPROTO_IP, IP_TTL}] = mockCallCount;
EXPECT_CALL(*netOpsDispatcher, sendmsg(_, HasCmsgs(expectedCmsgs), _));
Expand All @@ -1128,22 +1128,22 @@ TEST_F(AsyncUDPSocketTest, TestWriteDynamicCmsg) {
}
// SO_MARK with dynamic cmsgs (IP_TTL)
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
cmsgs[{SOL_SOCKET, SO_MARK}] = 123;
socket_->setCmsgs(cmsgs);
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{SOL_SOCKET, SO_MARK}] = 123;
expectedCmsgs[{IPPROTO_IP, IP_TTL}] = mockCallCount;
EXPECT_CALL(*netOpsDispatcher, sendmsg(_, HasCmsgs(expectedCmsgs), _));
socket_->write(addr, folly::IOBuf::copyBuffer("hey"));
}
// append IP_TOS + IP_TTL + overwrite with dynamic cmsgs (IP_TTL)
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
cmsgs[{IPPROTO_IP, IP_TOS}] = 456;
cmsgs[{IPPROTO_IP, IP_TTL}] = 9999;
socket_->appendCmsgs(cmsgs);
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TOS}] = 456;
expectedCmsgs[{SOL_SOCKET, SO_MARK}] = 123;
expectedCmsgs[{IPPROTO_IP, IP_TTL}] = mockCallCount;
Expand All @@ -1152,10 +1152,10 @@ TEST_F(AsyncUDPSocketTest, TestWriteDynamicCmsg) {
}
// append IP_TOS with a different value + dynamic cmsgs (IP_TTL)
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
cmsgs[{IPPROTO_IP, IP_TOS}] = 789;
socket_->appendCmsgs(cmsgs);
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TOS}] = 789;
expectedCmsgs[{SOL_SOCKET, SO_MARK}] = 123;
expectedCmsgs[{IPPROTO_IP, IP_TTL}] = mockCallCount;
Expand All @@ -1164,7 +1164,7 @@ TEST_F(AsyncUDPSocketTest, TestWriteDynamicCmsg) {
}
// writeGSO with IP_TOS and SO_MARK + dynamic cmsgs (IP_TTL)
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TOS}] = 789;
expectedCmsgs[{SOL_SOCKET, SO_MARK}] = 123;
expectedCmsgs[{SOL_UDP, UDP_SEGMENT}] = 1;
Expand All @@ -1179,7 +1179,7 @@ TEST_F(AsyncUDPSocketTest, TestWriteDynamicCmsg) {
// Empty dynamic cmsgs should revert to default cmsgs
socket_->setAdditionalCmsgsFunc([]() { return folly::none; });
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TOS}] = 789;
expectedCmsgs[{SOL_SOCKET, SO_MARK}] = 123;
expectedCmsgs[{IPPROTO_IP, IP_TTL}] = 9999; // This won't be overwritten
Expand Down Expand Up @@ -1256,7 +1256,7 @@ TEST(MatcherTest, AllHaveCmsgsTest) {
cm->cmsg_len = CMSG_LEN(sizeof(val));
memcpy(CMSG_DATA(cm), &val, sizeof(val));
}
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
cmsgs[{SOL_SOCKET, SO_MARK}] = 20;
cmsgs[{IPPROTO_IP, IP_TOS}] = 30;
struct mmsghdr* msgvecPtr = nullptr;
Expand All @@ -1278,14 +1278,14 @@ TEST_F(AsyncUDPSocketTest, TestWritemCmsg) {
#ifdef FOLLY_HAVE_MSG_ERRQUEUE
// empty
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
EXPECT_CALL(
*netOpsDispatcher, sendmmsg(_, AllHaveCmsgs(cmsgs, bufs.size()), _, _));
socket_->writem(folly::range(&addr, &addr + 1), bufs.data(), bufs.size());
}
// set IP_TOS & SO_MARK
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
cmsgs[{IPPROTO_IP, IP_TOS}] = 456;
cmsgs[{SOL_SOCKET, SO_MARK}] = 123;
socket_->setCmsgs(cmsgs);
Expand All @@ -1295,7 +1295,7 @@ TEST_F(AsyncUDPSocketTest, TestWritemCmsg) {
}
// writemGSO
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TOS}] = 456;
expectedCmsgs[{SOL_SOCKET, SO_MARK}] = 123;
expectedCmsgs[{SOL_UDP, UDP_SEGMENT}] = 1;
Expand Down Expand Up @@ -1326,15 +1326,15 @@ TEST_F(AsyncUDPSocketTest, TestWritemDynamicCmsg) {
MockFunction<AsyncUDPSocket::AdditionalCmsgsFunc> mockAdditionalCmsgs;
int mockCallCount = 1;
socket_->setAdditionalCmsgsFunc([&mockCallCount]() {
folly::SocketOptionMap additionalCmsgs;
folly::SocketCmsgMap additionalCmsgs;
additionalCmsgs[{IPPROTO_IP, IP_TTL}] = mockCallCount++;
return additionalCmsgs;
});

#ifdef FOLLY_HAVE_MSG_ERRQUEUE
// Dynamic cmsgs only (IP_TTL)
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TTL}] = mockCallCount;
EXPECT_CALL(
*netOpsDispatcher,
Expand All @@ -1343,7 +1343,7 @@ TEST_F(AsyncUDPSocketTest, TestWritemDynamicCmsg) {
}
// set IP_TOS & SO_MARK & IP_TTL + overwrite from dynamic cmsgs (IP_TTL)
{
folly::SocketOptionMap cmsgs;
folly::SocketCmsgMap cmsgs;
cmsgs[{IPPROTO_IP, IP_TOS}] = 456;
cmsgs[{SOL_SOCKET, SO_MARK}] = 123;
cmsgs[{IPPROTO_IP, IP_TTL}] = 9999;
Expand All @@ -1357,7 +1357,7 @@ TEST_F(AsyncUDPSocketTest, TestWritemDynamicCmsg) {
}
// writemGSO + dynamic cmsgs (IP_TTL)
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketCmsgMap expectedCmsgs;
expectedCmsgs[{IPPROTO_IP, IP_TOS}] = 456;
expectedCmsgs[{SOL_SOCKET, SO_MARK}] = 123;
expectedCmsgs[{SOL_UDP, UDP_SEGMENT}] = 1;
Expand Down Expand Up @@ -1414,8 +1414,8 @@ TEST_F(AsyncUDPSocketTest, TestWritemNontrivialCmsgs) {
#ifdef FOLLY_HAVE_MSG_ERRQUEUE
// empty
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketNontrivialOptionMap expectedNontrivialCmsgs;
folly::SocketCmsgMap expectedCmsgs;
folly::SocketNontrivialCmsgMap expectedNontrivialCmsgs;
EXPECT_CALL(
*netOpsDispatcher,
sendmmsg(
Expand All @@ -1428,8 +1428,8 @@ TEST_F(AsyncUDPSocketTest, TestWritemNontrivialCmsgs) {
}
// set IP_TOS & SO_MARK
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketNontrivialOptionMap expectedNontrivialCmsgs;
folly::SocketCmsgMap expectedCmsgs;
folly::SocketNontrivialCmsgMap expectedNontrivialCmsgs;
struct linger sl {
.l_onoff = 1, .l_linger = 123,
};
Expand All @@ -1451,8 +1451,8 @@ TEST_F(AsyncUDPSocketTest, TestWritemNontrivialCmsgs) {
}
// writemGSO
{
folly::SocketOptionMap expectedCmsgs;
folly::SocketNontrivialOptionMap expectedNontrivialCmsgs;
folly::SocketCmsgMap expectedCmsgs;
folly::SocketNontrivialCmsgMap expectedNontrivialCmsgs;
struct linger sl {
.l_onoff = 1, .l_linger = 123,
};
Expand Down
8 changes: 4 additions & 4 deletions folly/io/async/test/MockAsyncUDPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ struct MockAsyncUDPSocketT : public Base {
int,
recvmmsg,
(struct mmsghdr*, unsigned int, unsigned int, struct timespec*));
MOCK_METHOD(void, setCmsgs, (const SocketOptionMap&));
MOCK_METHOD(void, setNontrivialCmsgs, (const SocketNontrivialOptionMap&));
MOCK_METHOD(void, appendCmsgs, (const SocketOptionMap&));
MOCK_METHOD(void, appendNontrivialCmsgs, (const SocketNontrivialOptionMap&));
MOCK_METHOD(void, setCmsgs, (const SocketCmsgMap&));
MOCK_METHOD(void, setNontrivialCmsgs, (const SocketNontrivialCmsgMap&));
MOCK_METHOD(void, appendCmsgs, (const SocketCmsgMap&));
MOCK_METHOD(void, appendNontrivialCmsgs, (const SocketNontrivialCmsgMap&));
MOCK_METHOD(
void, applyOptions, (const SocketOptionMap&, SocketOptionKey::ApplyPos));
MOCK_METHOD(
Expand Down

0 comments on commit e363724

Please sign in to comment.