Skip to content

Commit

Permalink
Migrate inet/endpoints to BitMapPool
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Nov 4, 2021
1 parent 8174144 commit 01f4929
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 99 deletions.
1 change: 1 addition & 0 deletions src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ int main(int argc, char * argv[])
chip::DeviceLayer::PlatformMgr().RunEventLoop();

chip::app::InteractionModelEngine::GetInstance()->Shutdown();
gTransportManager.Close();
ShutdownChip();
exit:
if (err != CHIP_NO_ERROR || (gCommandRespCount != kMaxCommandMessageCount + kTotalFailureCommandMessageCount))
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/integration/chip_im_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ int main(int argc, char * argv[])
}

chip::app::InteractionModelEngine::GetInstance()->Shutdown();

gTransportManager.Close();
ShutdownChip();

return EXIT_SUCCESS;
Expand Down
9 changes: 3 additions & 6 deletions src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ typedef void (*AsyncWorkFunct)(intptr_t arg);
#include <ble/BleConfig.h>
#include <inet/InetLayer.h>
#include <lib/support/LambdaBridge.h>
#include <system/SystemEvent.h>
#include <system/SystemLayer.h>
#include <system/SystemPacketBuffer.h>

namespace chip {
namespace DeviceLayer {
Expand All @@ -324,12 +327,6 @@ struct ChipDeviceEvent final
ChipDevicePlatformEvent Platform;
LambdaBridge LambdaEvent;
struct
{
::chip::System::EventType Type;
::chip::System::Object * Target;
uintptr_t Argument;
} ChipSystemLayerEvent;
struct
{
AsyncWorkFunct WorkFunct;
intptr_t Arg;
Expand Down
23 changes: 0 additions & 23 deletions src/include/platform/internal/GenericPlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,6 @@ void GenericPlatformManagerImpl<ImplClass>::_DispatchEvent(const ChipDeviceEvent
// Do nothing for no-op events.
break;

case DeviceEventType::kChipSystemLayerEvent:
// If the event is a CHIP System or Inet Layer event, deliver it to the System::Layer event handler.
Impl()->DispatchEventToSystemLayer(event);
break;

case DeviceEventType::kChipLambdaEvent:
event->LambdaEvent();
break;
Expand Down Expand Up @@ -259,24 +254,6 @@ void GenericPlatformManagerImpl<ImplClass>::_DispatchEvent(const ChipDeviceEvent
#endif // CHIP_PROGRESS_LOGGING
}

template <class ImplClass>
void GenericPlatformManagerImpl<ImplClass>::DispatchEventToSystemLayer(const ChipDeviceEvent * event)
{
// TODO(#788): remove ifdef LWIP once System::Layer event APIs are generally available
#if CHIP_SYSTEM_CONFIG_USE_LWIP
CHIP_ERROR err = CHIP_NO_ERROR;

// Invoke the System Layer's event handler function.
err = static_cast<System::LayerImplLwIP &>(SystemLayer())
.HandleEvent(*event->ChipSystemLayerEvent.Target, event->ChipSystemLayerEvent.Type,
event->ChipSystemLayerEvent.Argument);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Error handling CHIP System Layer event (type %d): %s", event->Type, ErrorStr(err));
}
#endif
}

template <class ImplClass>
void GenericPlatformManagerImpl<ImplClass>::DispatchEventToDeviceLayer(const ChipDeviceEvent * event)
{
Expand Down
1 change: 0 additions & 1 deletion src/include/platform/internal/GenericPlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class GenericPlatformManagerImpl

// ===== Support methods that can be overridden by the implementation subclass.

void DispatchEventToSystemLayer(const ChipDeviceEvent * event);
void DispatchEventToDeviceLayer(const ChipDeviceEvent * event);
void DispatchEventToApplication(const ChipDeviceEvent * event);
static void HandleMessageLayerActivityChanged(bool messageLayerIsActive);
Expand Down
22 changes: 0 additions & 22 deletions src/inet/EndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState)
mLwIPEndPointType = LwIPEndPointType::Unknown;
}

void EndPointBasis::DeferredFree(System::Object::ReleaseDeferralErrorTactic aTactic)
{
if (!CHIP_SYSTEM_CONFIG_USE_SOCKETS || (mVoid != nullptr))
{
DeferredRelease(static_cast<System::LayerLwIP *>(Layer().SystemLayer()), aTactic);
}
else
{
Release();
}
}

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Expand All @@ -62,11 +50,6 @@ void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState)
mSocket = kInvalidSocketFd;
}

void EndPointBasis::DeferredFree(System::Object::ReleaseDeferralErrorTactic aTactic)
{
Release();
}

#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
Expand All @@ -77,11 +60,6 @@ void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState)
mInetLayer = &aInetLayer;
}

void EndPointBasis::DeferredFree(System::Object::ReleaseDeferralErrorTactic aTactic)
{
Release();
}

#endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

} // namespace Inet
Expand Down
7 changes: 3 additions & 4 deletions src/inet/EndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#include <inet/IPAddress.h>
#include <lib/support/DLLUtil.h>
#include <system/SystemObject.h>

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#include <system/SocketEvents.h>
Expand All @@ -51,7 +50,7 @@ class InetLayer;
/**
* Basis of internet transport endpoint classes.
*/
class DLL_EXPORT EndPointBasis : public System::Object
class DLL_EXPORT EndPointBasis
{
public:
/**
Expand All @@ -68,12 +67,13 @@ class DLL_EXPORT EndPointBasis : public System::Object
*/
bool IsCreatedByInetLayer(const InetLayer & aInetLayer) const { return mInetLayer == &aInetLayer; }

void * AppState;

private:
InetLayer * mInetLayer; /**< Pointer to the InetLayer object that owns this object. */

protected:
void InitEndPointBasis(InetLayer & aInetLayer, void * aAppState = nullptr);
void DeferredFree(System::Object::ReleaseDeferralErrorTactic aTactic);

#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
nw_parameters_t mParameters;
Expand All @@ -91,7 +91,6 @@ class DLL_EXPORT EndPointBasis : public System::Object
/** Encapsulated LwIP protocol control block */
union
{
const void * mVoid; /**< An untyped protocol control buffer reference */
#if INET_CONFIG_ENABLE_UDP_ENDPOINT
udp_pcb * mUDP; /**< User datagram protocol (UDP) control */
#endif // INET_CONFIG_ENABLE_UDP_ENDPOINT
Expand Down
16 changes: 2 additions & 14 deletions src/inet/InetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,6 @@
namespace chip {
namespace Inet {

void InetLayer::UpdateSnapshot(chip::System::Stats::Snapshot & aSnapshot)
{
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
TCPEndPoint::sPool.GetStatistics(aSnapshot.mResourcesInUse[chip::System::Stats::kInetLayer_NumTCPEps],
aSnapshot.mHighWatermarks[chip::System::Stats::kInetLayer_NumTCPEps]);
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
#if INET_CONFIG_ENABLE_UDP_ENDPOINT
UDPEndPoint::sPool.GetStatistics(aSnapshot.mResourcesInUse[chip::System::Stats::kInetLayer_NumUDPEps],
aSnapshot.mHighWatermarks[chip::System::Stats::kInetLayer_NumUDPEps]);
#endif // INET_CONFIG_ENABLE_UDP_ENDPOINT
}

/**
* This is the InetLayer default constructor.
*
Expand Down Expand Up @@ -453,7 +441,7 @@ CHIP_ERROR InetLayer::NewTCPEndPoint(TCPEndPoint ** retEndPoint)

VerifyOrReturnError(mLayerState.IsInitialized(), CHIP_ERROR_INCORRECT_STATE);

*retEndPoint = TCPEndPoint::sPool.TryCreate();
*retEndPoint = TCPEndPoint::sPool.CreateObject();
if (*retEndPoint == nullptr)
{
ChipLogError(Inet, "%s endpoint pool FULL", "TCP");
Expand Down Expand Up @@ -493,7 +481,7 @@ CHIP_ERROR InetLayer::NewUDPEndPoint(UDPEndPoint ** retEndPoint)

VerifyOrReturnError(mLayerState.IsInitialized(), CHIP_ERROR_INCORRECT_STATE);

*retEndPoint = UDPEndPoint::sPool.TryCreate();
*retEndPoint = UDPEndPoint::sPool.CreateObject();
if (*retEndPoint == nullptr)
{
ChipLogError(Inet, "%s endpoint pool FULL", "UDP");
Expand Down
2 changes: 0 additions & 2 deletions src/inet/InetLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ class DLL_EXPORT InetLayer
CHIP_ERROR GetLinkLocalAddr(InterfaceId link, IPAddress * llAddr);
bool MatchLocalIPv6Subnet(const IPAddress & addr);

static void UpdateSnapshot(chip::System::Stats::Snapshot & aSnapshot);

void * GetPlatformData();
void SetPlatformData(void * aPlatformData);

Expand Down
29 changes: 25 additions & 4 deletions src/inet/TCPEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
namespace chip {
namespace Inet {

chip::System::ObjectPool<TCPEndPoint, INET_CONFIG_NUM_TCP_ENDPOINTS> TCPEndPoint::sPool;
BitMapObjectPool<TCPEndPoint, INET_CONFIG_NUM_TCP_ENDPOINTS> TCPEndPoint::sPool;

#if CHIP_SYSTEM_CONFIG_USE_LWIP
namespace {
Expand Down Expand Up @@ -2471,9 +2471,8 @@ void TCPEndPoint::Free()
Abort();
}

// Release the Retain() that happened when the end point was allocated
// [on LwIP, the object may still be alive if DoClose() used the
// EndPointBasis::DeferredFree() method.]
// Release the Retain() that happened when the end point was allocated [on LwIP, the object may still be alive if
// DoClose() used the DeferredFree() method.]
Release();
}

Expand Down Expand Up @@ -2723,6 +2722,28 @@ CHIP_ERROR TCPEndPoint::DoClose(CHIP_ERROR err, bool suppressCallback)
return err;
}

void TCPEndPoint::DeferredFree(ReleaseDeferralErrorTactic aTactic)
{
System::Layer * lSystemLayer = Layer().SystemLayer();
CHIP_ERROR err = lSystemLayer->ScheduleLambda([this] { this->Release(); });
if (err != CHIP_NO_ERROR)
{
switch (aTactic)
{
case kReleaseDeferralErrorTactic_Ignore:
break;

case kReleaseDeferralErrorTactic_Release:
this->Release();
break;

case kReleaseDeferralErrorTactic_Die:
VerifyOrDie(false);
break;
}
}
}

#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT

void TCPEndPoint::ScheduleNextTCPUserTimeoutPoll(uint32_t aTimeOut)
Expand Down
29 changes: 26 additions & 3 deletions src/inet/TCPEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
#include <inet/EndPointBasis.h>
#include <inet/IPAddress.h>
#include <inet/InetInterface.h>

#include <lib/core/ReferenceCounted.h>
#include <system/SystemPacketBuffer.h>
#include <lib/support/Pool.h>
#include <system/SystemLayer.h>

#include <utility>

Expand All @@ -50,6 +52,13 @@ namespace Inet {
class InetLayer;
class TCPTest;

class TCPEndPoint;
class TCPEndPointDeletor
{
public:
static void Release(TCPEndPoint * obj);
};

/**
* @brief Objects of this class represent TCP transport endpoints.
*
Expand All @@ -58,7 +67,7 @@ class TCPTest;
* endpoints (SOCK_STREAM sockets on Linux and BSD-derived systems) or LwIP
* TCP protocol control blocks, as the system is configured accordingly.
*/
class DLL_EXPORT TCPEndPoint : public EndPointBasis
class DLL_EXPORT TCPEndPoint : public EndPointBasis, public AtomicReferenceCounted<TCPEndPoint, TCPEndPointDeletor>
{
friend class InetLayer;
friend class ::chip::Transport::TCPTest;
Expand Down Expand Up @@ -350,6 +359,14 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis
*/
void Free();

enum ReleaseDeferralErrorTactic
{
kReleaseDeferralErrorTactic_Ignore, /**< No action. */
kReleaseDeferralErrorTactic_Release, /**< Release immediately. */
kReleaseDeferralErrorTactic_Die, /**< Die with message. */
};
void DeferredFree(ReleaseDeferralErrorTactic aTactic);

/**
* @brief Extract whether TCP connection is established.
*/
Expand Down Expand Up @@ -571,7 +588,8 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis
constexpr static size_t kMaxReceiveMessageSize = System::PacketBuffer::kMaxSizeWithoutReserve;

private:
static chip::System::ObjectPool<TCPEndPoint, INET_CONFIG_NUM_TCP_ENDPOINTS> sPool;
friend class TCPEndPointDeletor;
static BitMapObjectPool<TCPEndPoint, INET_CONFIG_NUM_TCP_ENDPOINTS> sPool;

/**
* Basic dynamic state of the underlying endpoint.
Expand Down Expand Up @@ -728,5 +746,10 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
};

inline void TCPEndPointDeletor::Release(TCPEndPoint * obj)
{
TCPEndPoint::sPool.ReleaseObject(obj);
}

} // namespace Inet
} // namespace chip
24 changes: 23 additions & 1 deletion src/inet/UDPEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
namespace chip {
namespace Inet {

chip::System::ObjectPool<UDPEndPoint, INET_CONFIG_NUM_UDP_ENDPOINTS> UDPEndPoint::sPool;
BitMapObjectPool<UDPEndPoint, INET_CONFIG_NUM_UDP_ENDPOINTS> UDPEndPoint::sPool;

#if CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_SOCKETS

Expand Down Expand Up @@ -431,6 +431,28 @@ void UDPEndPoint::Free()
DeferredFree(kReleaseDeferralErrorTactic_Die);
}

void UDPEndPoint::DeferredFree(ReleaseDeferralErrorTactic aTactic)
{
System::Layer * lSystemLayer = Layer().SystemLayer();
CHIP_ERROR err = lSystemLayer->ScheduleLambda([this] { UDPEndPoint::sPool.ReleaseObject(this); });
if (err != CHIP_NO_ERROR)
{
switch (aTactic)
{
case kReleaseDeferralErrorTactic_Ignore:
break;

case kReleaseDeferralErrorTactic_Release:
UDPEndPoint::sPool.ReleaseObject(this);
break;

case kReleaseDeferralErrorTactic_Die:
VerifyOrDie(false);
break;
}
}
}

void UDPEndPoint::HandleDataReceived(System::PacketBufferHandle && msg)
{
if ((mState == State::kListening) && (OnMessageReceived != nullptr))
Expand Down
Loading

0 comments on commit 01f4929

Please sign in to comment.