Skip to content

Commit

Permalink
Move socket watch APIs into System::Layer
Browse files Browse the repository at this point in the history
#### Problem

Transitionally, `System::Layer` operations are provided by a
configuration-specific `WatchableEventManager`, but they are not yet
sufficiently aligned to convert to abstract and implementation classes.

Part of project-chip#7715 Virtualize System and Inet interfaces

#### Change overview

- Move socket event watch APIs into `System::Layer`.
- Remove `WatchableSocket` and its include-file hack that allowed
  implementation-specific state to be contained directly in EndPoints.
- Revise the select() implementation to use an internal pool for watch
  state in place of the EndPoint-embedded WatchableSocket.
- Revise the libevent sample implementation to use libevent timers
  directly instead of the System::Timer pool. The libevent
  implementation uses STL containers with heap-allocated state for the
  sake of being different from the select()-based implementation.

#### Testing

CI; no change to functionality should is expected.
  • Loading branch information
kpschoedel committed Aug 18, 2021
1 parent 98e5494 commit 61db091
Show file tree
Hide file tree
Showing 52 changed files with 1,011 additions and 1,165 deletions.
1 change: 0 additions & 1 deletion examples/minimal-mdns/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <support/CHIPArgParser.hpp>
#include <support/CHIPMem.h>
#include <system/SystemPacketBuffer.h>
#include <system/SystemTimer.h>

#include "AllInterfaceListener.h"
#include "PacketReporter.h"
Expand Down
1 change: 0 additions & 1 deletion examples/minimal-mdns/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <support/CHIPArgParser.hpp>
#include <support/CHIPMem.h>
#include <system/SystemPacketBuffer.h>
#include <system/SystemTimer.h>

#include "AllInterfaceListener.h"
#include "PacketReporter.h"
Expand Down
5 changes: 2 additions & 3 deletions examples/platform/nrfconnect/util/test/TestInetCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include <support/CHIPMem.h>
#include <support/ErrorStr.h>
#include <support/ScopedBuffer.h>
#include <system/SystemTimer.h>

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#include <arpa/inet.h>
Expand Down Expand Up @@ -114,7 +113,7 @@ void ServiceEvents(struct ::timeval & aSleepTime)
FD_ZERO(&exceptFDs);

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (gSystemLayer.State() == System::kLayerState_Initialized)
if (gSystemLayer.State() == System::LayerState::kInitialized)
gSystemLayer.PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, aSleepTime);
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

Expand All @@ -131,7 +130,7 @@ void ServiceEvents(struct ::timeval & aSleepTime)
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

if (gSystemLayer.State() == System::kLayerState_Initialized)
if (gSystemLayer.State() == System::LayerState::kInitialized)
{

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Expand Down
1 change: 0 additions & 1 deletion src/app/EventManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>
#include <support/logging/CHIPLogging.h>
#include <system/SystemTimer.h>

using namespace chip::TLV;

Expand Down
1 change: 0 additions & 1 deletion src/app/tests/TestReadInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include <support/ErrorStr.h>
#include <support/UnitTestRegistration.h>
#include <system/SystemPacketBuffer.h>
#include <system/SystemTimer.h>
#include <system/TLVPacketBufferBackingStore.h>
#include <transport/SecureSessionMgr.h>
#include <transport/raw/UDP.h>
Expand Down
1 change: 0 additions & 1 deletion src/app/tests/integration/MockEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <protocols/secure_channel/PASESession.h>
#include <support/ErrorStr.h>
#include <system/SystemPacketBuffer.h>
#include <system/SystemTimer.h>
#include <transport/SecureSessionMgr.h>

static uint64_t kLivenessDeviceStatus = chip::TLV::ContextTag(1);
Expand Down
1 change: 0 additions & 1 deletion src/app/util/af-event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <app/util/attribute-storage.h>

#include <platform/CHIPDeviceLayer.h>
#include <system/SystemTimer.h>

#define EMBER_MAX_EVENT_CONTROL_DELAY_MS (UINT32_MAX / 2)
#define EMBER_MAX_EVENT_CONTROL_DELAY_QS (EMBER_MAX_EVENT_CONTROL_DELAY_MS >> 8)
Expand Down
2 changes: 1 addition & 1 deletion src/inet/EndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState)
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
(void) mSocket.Init(aInetLayer.SystemLayer()->WatchableEventsManager());
mSocket = INET_INVALID_SOCKET_FD;
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
}

Expand Down
11 changes: 6 additions & 5 deletions src/inet/EndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <support/DLLUtil.h>

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#include <system/WatchableSocket.h>
#include <system/SocketEvents.h>
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
Expand Down Expand Up @@ -97,9 +97,10 @@ class DLL_EXPORT EndPointBasis : public InetLayerBasis
#endif

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
System::WatchableSocket mSocket; /**< Encapsulated socket descriptor. */
IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
int mSocket; /**< Encapsulated socket descriptor. */
IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */
System::SocketWatchToken mWatch; /**< Socket event watcher */
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

#if CHIP_SYSTEM_CONFIG_USE_LWIP
/** Encapsulated LwIP protocol control block */
Expand Down Expand Up @@ -145,7 +146,7 @@ inline bool EndPointBasis::IsNetworkFrameworkEndPoint(void) const
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
inline bool EndPointBasis::IsSocketsEndPoint() const
{
return mSocket.HasFD();
return mSocket != INET_INVALID_SOCKET_FD;
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

Expand Down
52 changes: 26 additions & 26 deletions src/inet/IPEndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ CHIP_ERROR IPEndPointBasis::SetMulticastLoopback(IPVersion aIPVersion, bool aLoo
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
lRetval = SocketsSetMulticastLoopback(mSocket.GetFD(), aIPVersion, aLoopback);
lRetval = SocketsSetMulticastLoopback(mSocket, aIPVersion, aLoopback);
SuccessOrExit(lRetval);

exit:
Expand Down Expand Up @@ -449,7 +449,7 @@ CHIP_ERROR IPEndPointBasis::JoinMulticastGroup(InterfaceId aInterfaceId, const I
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
lRetval = SocketsIPv4JoinLeaveMulticastGroup(mSocket.GetFD(), aInterfaceId, aAddress, IP_ADD_MEMBERSHIP);
lRetval = SocketsIPv4JoinLeaveMulticastGroup(mSocket, aInterfaceId, aAddress, IP_ADD_MEMBERSHIP);
SuccessOrExit(lRetval);
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
}
Expand All @@ -474,7 +474,7 @@ CHIP_ERROR IPEndPointBasis::JoinMulticastGroup(InterfaceId aInterfaceId, const I
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
lRetval = SocketsIPv6JoinMulticastGroup(mSocket.GetFD(), aInterfaceId, aAddress);
lRetval = SocketsIPv6JoinMulticastGroup(mSocket, aInterfaceId, aAddress);
SuccessOrExit(lRetval);
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
}
Expand Down Expand Up @@ -541,7 +541,7 @@ CHIP_ERROR IPEndPointBasis::LeaveMulticastGroup(InterfaceId aInterfaceId, const
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
lRetval = SocketsIPv4JoinLeaveMulticastGroup(mSocket.GetFD(), aInterfaceId, aAddress, IP_DROP_MEMBERSHIP);
lRetval = SocketsIPv4JoinLeaveMulticastGroup(mSocket, aInterfaceId, aAddress, IP_DROP_MEMBERSHIP);
SuccessOrExit(lRetval);
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
}
Expand All @@ -566,7 +566,7 @@ CHIP_ERROR IPEndPointBasis::LeaveMulticastGroup(InterfaceId aInterfaceId, const
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
lRetval = SocketsIPv6LeaveMulticastGroup(mSocket.GetFD(), aInterfaceId, aAddress);
lRetval = SocketsIPv6LeaveMulticastGroup(mSocket, aInterfaceId, aAddress);
SuccessOrExit(lRetval);
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
}
Expand Down Expand Up @@ -693,21 +693,21 @@ CHIP_ERROR IPEndPointBasis::Bind(IPAddressType aAddressType, const IPAddress & a
}
sa.sin6_scope_id = static_cast<decltype(sa.sin6_scope_id)>(aInterfaceId);

if (bind(mSocket.GetFD(), reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
if (bind(mSocket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
lRetval = chip::System::MapErrorPOSIX(errno);

// Instruct the kernel that any messages to multicast destinations should be
// sent down the interface specified by the caller.
#ifdef IPV6_MULTICAST_IF
if (lRetval == CHIP_NO_ERROR)
setsockopt(mSocket.GetFD(), IPPROTO_IPV6, IPV6_MULTICAST_IF, &aInterfaceId, sizeof(aInterfaceId));
setsockopt(mSocket, IPPROTO_IPV6, IPV6_MULTICAST_IF, &aInterfaceId, sizeof(aInterfaceId));
#endif // defined(IPV6_MULTICAST_IF)

// Instruct the kernel that any messages to multicast destinations should be
// set with the configured hop limit value.
#ifdef IPV6_MULTICAST_HOPS
int hops = INET_CONFIG_IP_MULTICAST_HOP_LIMIT;
setsockopt(mSocket.GetFD(), IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, sizeof(hops));
setsockopt(mSocket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, sizeof(hops));
#endif // defined(IPV6_MULTICAST_HOPS)
}
#if INET_CONFIG_ENABLE_IPV4
Expand All @@ -722,26 +722,26 @@ CHIP_ERROR IPEndPointBasis::Bind(IPAddressType aAddressType, const IPAddress & a
sa.sin_port = htons(aPort);
sa.sin_addr = aAddress.ToIPv4();

if (bind(mSocket.GetFD(), reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
if (bind(mSocket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
lRetval = chip::System::MapErrorPOSIX(errno);

// Instruct the kernel that any messages to multicast destinations should be
// sent down the interface to which the specified IPv4 address is bound.
#ifdef IP_MULTICAST_IF
if (lRetval == CHIP_NO_ERROR)
setsockopt(mSocket.GetFD(), IPPROTO_IP, IP_MULTICAST_IF, &sa, sizeof(sa));
setsockopt(mSocket, IPPROTO_IP, IP_MULTICAST_IF, &sa, sizeof(sa));
#endif // defined(IP_MULTICAST_IF)

// Instruct the kernel that any messages to multicast destinations should be
// set with the configured hop limit value.
#ifdef IP_MULTICAST_TTL
int ttl = INET_CONFIG_IP_MULTICAST_HOP_LIMIT;
setsockopt(mSocket.GetFD(), IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
setsockopt(mSocket, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
#endif // defined(IP_MULTICAST_TTL)

// Allow socket transmitting broadcast packets.
if (lRetval == CHIP_NO_ERROR)
setsockopt(mSocket.GetFD(), SOL_SOCKET, SO_BROADCAST, &enable, sizeof(enable));
setsockopt(mSocket, SOL_SOCKET, SO_BROADCAST, &enable, sizeof(enable));
}
#endif // INET_CONFIG_ENABLE_IPV4
else
Expand All @@ -758,7 +758,7 @@ CHIP_ERROR IPEndPointBasis::BindInterface(IPAddressType aAddressType, InterfaceI
if (aInterfaceId == INET_NULL_INTERFACEID)
{
// Stop interface-based filtering.
if (setsockopt(mSocket.GetFD(), SOL_SOCKET, SO_BINDTODEVICE, "", 0) == -1)
if (setsockopt(mSocket, SOL_SOCKET, SO_BINDTODEVICE, "", 0) == -1)
{
lRetval = chip::System::MapErrorPOSIX(errno);
}
Expand All @@ -774,7 +774,7 @@ CHIP_ERROR IPEndPointBasis::BindInterface(IPAddressType aAddressType, InterfaceI
}

if (lRetval == CHIP_NO_ERROR &&
setsockopt(mSocket.GetFD(), SOL_SOCKET, SO_BINDTODEVICE, lInterfaceName, socklen_t(strlen(lInterfaceName))) == -1)
setsockopt(mSocket, SOL_SOCKET, SO_BINDTODEVICE, lInterfaceName, socklen_t(strlen(lInterfaceName))) == -1)
{
lRetval = chip::System::MapErrorPOSIX(errno);
}
Expand Down Expand Up @@ -907,7 +907,7 @@ CHIP_ERROR IPEndPointBasis::SendMsg(const IPPacketInfo * aPktInfo, chip::System:
}

// Send IP packet.
const ssize_t lenSent = sendmsg(mSocket.GetFD(), &msgHeader, 0);
const ssize_t lenSent = sendmsg(mSocket, &msgHeader, 0);
if (lenSent == -1)
return chip::System::MapErrorPOSIX(errno);
if (lenSent != aBuffer->DataLength())
Expand All @@ -917,7 +917,7 @@ CHIP_ERROR IPEndPointBasis::SendMsg(const IPPacketInfo * aPktInfo, chip::System:

CHIP_ERROR IPEndPointBasis::GetSocket(IPAddressType aAddressType, int aType, int aProtocol)
{
if (!mSocket.HasFD())
if (mSocket == INET_INVALID_SOCKET_FD)
{
const int one = 1;
int family;
Expand All @@ -938,10 +938,10 @@ CHIP_ERROR IPEndPointBasis::GetSocket(IPAddressType aAddressType, int aType, int
return INET_ERROR_WRONG_ADDRESS_TYPE;
}

const int fd = ::socket(family, aType, aProtocol);
if (fd == -1)
mSocket = ::socket(family, aType, aProtocol);
if (mSocket == -1)
return chip::System::MapErrorPOSIX(errno);
ReturnErrorOnFailure(mSocket.Attach(fd));
ReturnErrorOnFailure(SystemLayer().StartWatchingSocket(mSocket, &mWatch));

mAddrType = aAddressType;

Expand All @@ -954,11 +954,11 @@ CHIP_ERROR IPEndPointBasis::GetSocket(IPAddressType aAddressType, int aType, int
// logic up to check for implementations of these options and
// to provide appropriate HAVE_xxxxx definitions accordingly.

int res = setsockopt(mSocket.GetFD(), SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
int res = setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
static_cast<void>(res);

#ifdef SO_REUSEPORT
res = setsockopt(mSocket.GetFD(), SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
res = setsockopt(mSocket, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
if (res != 0)
{
ChipLogError(Inet, "SO_REUSEPORT failed: %d", errno);
Expand All @@ -972,7 +972,7 @@ CHIP_ERROR IPEndPointBasis::GetSocket(IPAddressType aAddressType, int aType, int
#ifdef IPV6_V6ONLY
if (aAddressType == kIPAddressType_IPv6)
{
res = setsockopt(mSocket.GetFD(), IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
res = setsockopt(mSocket, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
if (res != 0)
{
ChipLogError(Inet, "IPV6_V6ONLY failed: %d", errno);
Expand All @@ -984,7 +984,7 @@ CHIP_ERROR IPEndPointBasis::GetSocket(IPAddressType aAddressType, int aType, int
#ifdef IP_PKTINFO
if (aAddressType == kIPAddressType_IPv4)
{
res = setsockopt(mSocket.GetFD(), IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
res = setsockopt(mSocket, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
if (res != 0)
{
ChipLogError(Inet, "IP_PKTINFO failed: %d", errno);
Expand All @@ -996,7 +996,7 @@ CHIP_ERROR IPEndPointBasis::GetSocket(IPAddressType aAddressType, int aType, int
#ifdef IPV6_RECVPKTINFO
if (aAddressType == kIPAddressType_IPv6)
{
res = setsockopt(mSocket.GetFD(), IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
res = setsockopt(mSocket, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
if (res != 0)
{
ChipLogError(Inet, "IPV6_PKTINFO failed: %d", errno);
Expand All @@ -1010,7 +1010,7 @@ CHIP_ERROR IPEndPointBasis::GetSocket(IPAddressType aAddressType, int aType, int
// SIGPIPEs on unconnected UDP sockets.
#ifdef SO_NOSIGPIPE
{
res = setsockopt(mSocket.GetFD(), SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one));
res = setsockopt(mSocket, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one));
if (res != 0)
{
ChipLogError(Inet, "SO_NOSIGPIPE failed: %d", errno);
Expand Down Expand Up @@ -1058,7 +1058,7 @@ void IPEndPointBasis::HandlePendingIO(uint16_t aPort)
msgHeader.msg_control = controlData;
msgHeader.msg_controllen = sizeof(controlData);

ssize_t rcvLen = recvmsg(mSocket.GetFD(), &msgHeader, MSG_DONTWAIT);
ssize_t rcvLen = recvmsg(mSocket, &msgHeader, MSG_DONTWAIT);

if (rcvLen < 0)
{
Expand Down
4 changes: 1 addition & 3 deletions src/inet/InetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* * Raw network transport
*
* For BSD/POSIX Sockets (CHIP_SYSTEM_CONFIG_USE_SOCKETS), event readiness
* notification is handled via file descriptors, using System::WatchableSocket.
* notification is handled via file descriptors, using a System::Layer API.
*
* For LwIP (CHIP_SYSTEM_CONFIG_USE_LWIP), event readiness notification is handled
* via events / messages and platform- and system-specific hooks for the event
Expand All @@ -49,8 +49,6 @@

#include <platform/LockTracker.h>

#include <system/SystemTimer.h>

#include <support/CodeUtils.h>
#include <support/logging/CHIPLogging.h>

Expand Down
Loading

0 comments on commit 61db091

Please sign in to comment.