Skip to content

Commit

Permalink
Inet: Split InetLayer class (#12291)
Browse files Browse the repository at this point in the history
#### Problem

This is a step toward #7715 _Virtualize System and Inet interfaces_
for mockability, building on recent virtualization of Inet endpoints
and changes to object allocation pools.

The `InetLayer` class acts as a factory for `UDPEndPoint` and `TCPEndPoint`.
Merely making its methods virtual would result in an unacceptable code
size increase for applications that do not use TCP on platforms that
offer it, since the vtable would introduce a dependency that normal
linking doesn't remove.

#### Change overview

- Split the `EndPoint` factory/iteration code into its own class,
  `EndPointManager`. `InetLayer` remains as a stub wrapper for the pair
  of Managers.
- Move the code for TCP idle timeout from `InetLayer` to `TCPEndPoint`.
- Move `IPPacketInfo` from `InetLayer.h` to its own files.

**NOTE** that applications that use CHIP TCP must now explicitly initialize
it via `InetLayer::InitTCP()`.

#### Testing

CI; no changes to external functionality. Tests revised where necessary
to initialize TCP.
  • Loading branch information
kpschoedel authored and pull[bot] committed Apr 6, 2023
1 parent e68f644 commit 1053736
Show file tree
Hide file tree
Showing 38 changed files with 486 additions and 631 deletions.
1 change: 1 addition & 0 deletions src/credentials/FabricTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "FabricTable.h"

#include <lib/core/CHIPEncoding.h>
#include <lib/support/BufferWriter.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/SafeInt.h>
Expand Down
1 change: 0 additions & 1 deletion src/credentials/tests/TestFabricTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include <stdarg.h>

using namespace chip;
using namespace Transport;

static const uint8_t sTestRootCert[] = {
0x15, 0x30, 0x01, 0x08, 0x59, 0xea, 0xa6, 0x32, 0x94, 0x7f, 0x54, 0x1c, 0x24, 0x02, 0x01, 0x37, 0x03, 0x27, 0x14, 0x01, 0x00,
Expand Down
2 changes: 1 addition & 1 deletion src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ typedef void (*AsyncWorkFunct)(intptr_t arg);
#endif // defined(CHIP_DEVICE_LAYER_TARGET)

#include <ble/BleConfig.h>
#include <inet/InetLayer.h>
#include <inet/InetInterface.h>
#include <lib/support/LambdaBridge.h>
#include <system/SystemEvent.h>
#include <system/SystemLayer.h>
Expand Down
2 changes: 2 additions & 0 deletions src/include/platform/CHIPDeviceLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ void SetSystemLayerForTesting(System::LayerImpl * layer);

// These functions are defined in src/platform/Globals.cpp
chip::Inet::InetLayer & InetLayer();
chip::Inet::EndPointManager<Inet::UDPEndPoint> * UDPEndPointManager();
chip::Inet::EndPointManager<Inet::TCPEndPoint> * TCPEndPointManager();
chip::System::Layer & SystemLayer();

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack()
SuccessOrExit(err);

// Initialize the CHIP Inet layer.
err = InetLayer().Init(SystemLayer(), nullptr);
err = InetLayer().Init(SystemLayer(), UDPEndPointManager());
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "InetLayer initialization failed: %s", ErrorStr(err));
Expand Down
2 changes: 2 additions & 0 deletions src/inet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ static_library("inet") {
"IPAddress-StringFuncts.cpp",
"IPAddress.cpp",
"IPAddress.h",
"IPPacketInfo.cpp",
"IPPacketInfo.h",
"IPPrefix.cpp",
"IPPrefix.h",
"Inet.h",
Expand Down
37 changes: 31 additions & 6 deletions src/inet/EndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,55 @@
#pragma once

#include <inet/InetConfig.h>
#include <lib/core/ReferenceCounted.h>
#include <lib/support/DLLUtil.h>

namespace chip {

namespace System {
class Layer;
} // namespace System

namespace Inet {

class InetLayer;
template <typename EndPointType>
class EndPointManager;

template <typename EndPointType>
class EndPointDeletor;

/**
* Basis of internet transport endpoint classes.
*/
class DLL_EXPORT EndPointBase
template <typename EndPointType>
class DLL_EXPORT EndPointBasis : public ReferenceCounted<EndPointType, EndPointDeletor<EndPointType>>
{
public:
EndPointBase(InetLayer & aInetLayer, void * aAppState = nullptr) : mAppState(aAppState), mInetLayer(aInetLayer) {}
using EndPoint = EndPointType;

EndPointBasis(EndPointManager<EndPoint> & endPointManager) : mAppState(nullptr), mEndPointManager(endPointManager) {}

/**
* Returns a reference to the Inet layer object that owns this basis object.
* Returns a reference to the endpoint fatory that owns this basis object.
*/
InetLayer & Layer() const { return mInetLayer; }
EndPointManager<EndPoint> & GetEndPointManager() const { return mEndPointManager; }

/**
* Returns a reference to the System::Layer associated with this object.
*/
chip::System::Layer & GetSystemLayer() const { return mEndPointManager.SystemLayer(); }

void * mAppState;

private:
InetLayer & mInetLayer; /**< InetLayer object that owns this object. */
EndPointManager<EndPoint> & mEndPointManager; /**< Factory that owns this object. */
};

template <typename EndPointType>
class EndPointDeletor
{
public:
static void Release(EndPointType * obj) { obj->GetEndPointManager().DeleteEndPoint(obj); }
};

} // namespace Inet
Expand Down
2 changes: 1 addition & 1 deletion src/inet/IPAddress-StringFuncts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <stdint.h>
#include <string.h>

#include <inet/InetLayer.h>
#include <inet/IPAddress.h>
#include <lib/support/CodeUtils.h>

#if !CHIP_SYSTEM_CONFIG_USE_LWIP
Expand Down
34 changes: 34 additions & 0 deletions src/inet/IPPacketInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2013-2017 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "IPPacketInfo.h"

namespace chip {
namespace Inet {

void IPPacketInfo::Clear()
{
SrcAddress = IPAddress::Any;
DestAddress = IPAddress::Any;
Interface = InterfaceId::Null();
SrcPort = 0;
DestPort = 0;
}

} // namespace Inet
} // namespace chip
50 changes: 50 additions & 0 deletions src/inet/IPPacketInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2013-2017 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <inet/IPAddress.h>
#include <inet/InetInterface.h>

namespace chip {
namespace Inet {

/**
* Information about an incoming/outgoing message/connection.
*
* @warning
* Do not alter the contents of this class without first reading and understanding
* the code/comments in UDPEndPoint::GetPacketInfo().
*/
class IPPacketInfo
{
public:
IPAddress SrcAddress; /**< The source IPAddress in the packet. */
IPAddress DestAddress; /**< The destination IPAddress in the packet. */
InterfaceId Interface; /**< The interface identifier for the connection. */
uint16_t SrcPort; /**< The source port in the packet. */
uint16_t DestPort; /**< The destination port in the packet. */

/**
* Reset the members of the IPPacketInfo object.
*/
void Clear();
};

} // namespace Inet
} // namespace chip
5 changes: 2 additions & 3 deletions src/inet/InetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
#define __STDC_LIMIT_MACROS
#endif

#include "InetInterface.h"

#include "InetLayer.h"
#include <inet/InetInterface.h>

#include <inet/IPPrefix.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/DLLUtil.h>
Expand Down
Loading

0 comments on commit 1053736

Please sign in to comment.