Skip to content

Commit

Permalink
iox-eclipse-iceoryx#2185 Introduce 'ResourceType'
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Feb 16, 2024
1 parent 72f37a4 commit f024b06
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 44 deletions.
3 changes: 2 additions & 1 deletion iceoryx_binding_c/test/moduletests/test_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ TEST_F(BindingC_Runtime_test, RuntimeNameLengthIsMax)
::testing::Test::RecordProperty("TEST_ID", "854a471d-936e-4c98-b56e-ba8a7d83460e");

RuntimeName_t dummy{"a"};
auto prefixLength = runtime::ipcChannelNameToInterfaceName(dummy).value().size() - dummy.size();
auto prefixLength =
runtime::ipcChannelNameToInterfaceName(dummy, ResourceType::USER_DEFINED).value().size() - dummy.size();
std::string maxName(iox::MAX_RUNTIME_NAME_LENGTH - prefixLength, 's');

iox_runtime_init(maxName.c_str());
Expand Down
11 changes: 10 additions & 1 deletion iceoryx_posh/include/iceoryx_posh/iceoryx_posh_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,19 @@ struct DefaultChunkQueueConfig

constexpr const char ICEORYX_RESOURCE_PREFIX[] = "iox1";

/// @brief The resource type is used to customize the resource prefix by adding an 'i' or 'u' depending whether the
/// resource is defined by iceoryx, e.g. the roudi IPC channel, or by the user, e.g. the runtime name. This shall
/// prevent the system from being affected by users defining resource names which are intended to be used by iceoryx.
enum class ResourceType
{
ICEORYX_DEFINED,
USER_DEFINED,
};

using ResourcePrefix_t = string<13>; // 'iox1_' + MAX_UINT16_SIZE + '_' + optional 'x_'
/// @brief Returns the prefix string used for resources
/// @param[in] uniqueRouDiID to use for the prefix string
inline ResourcePrefix_t iceoryxResourcePrefix(uint16_t uniqueRouDiID, iox::string<1> customizer = "");
inline ResourcePrefix_t iceoryxResourcePrefix(uint16_t uniqueRouDiID, ResourceType resourceType);

// alias for string
using RuntimeName_t = string<MAX_RUNTIME_NAME_LENGTH>;
Expand Down
11 changes: 7 additions & 4 deletions iceoryx_posh/include/iceoryx_posh/iceoryx_posh_types.inl
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ log::LogStream& operator<<(log::LogStream& stream, ConnectionState value) noexce
return stream;
}

ResourcePrefix_t iceoryxResourcePrefix(uint16_t uniqueRouDiID, iox::string<1> customizer)
ResourcePrefix_t iceoryxResourcePrefix(uint16_t uniqueRouDiID, ResourceType resourceType)
{
static_assert(std::is_same_v<uint16_t, std::remove_const_t<decltype(uniqueRouDiID)>>);
static_assert(std::is_same_v<uint16_t, std::remove_const_t<decltype(uniqueRouDiID)>>,
"Please adjust 'MAX_UINT16_WIDTH' to the new fixed width type to have enough space for the "
"stringified unique RouDi ID");
constexpr auto MAX_UINT16_WIDTH{5};
iox::string<MAX_UINT16_WIDTH> uniqueRoudiIdString{TruncateToCapacity,
iox::convert::toString(uniqueRouDiID).c_str()};
auto end = customizer.empty() ? "_" : concatenate("_", customizer, "_");
return concatenate(ICEORYX_RESOURCE_PREFIX, "_", uniqueRoudiIdString, end);

iox::string<1> resourceTypeString{resourceType == ResourceType::ICEORYX_DEFINED ? "i" : "u"};
return concatenate(ICEORYX_RESOURCE_PREFIX, "_", uniqueRoudiIdString, "_", resourceTypeString, "_");
}

namespace roudi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,15 @@ inline SharedMemoryObjectType MePooSegment<SharedMemoryObjectType, MemoryManager
typename SharedMemoryObjectType::Builder()
.name([&writerGroup] {
using ShmName_t = detail::PosixSharedMemory::Name_t;
ShmName_t shmName = iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID,
"p"); // use an additional 'p' to prevent creating a payload
// segment with the same name as the management segment
ShmName_t shmName = iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID, ResourceType::USER_DEFINED);
if (shmName.size() + writerGroup.getName().size() > ShmName_t::capacity())
{
IOX_LOG(FATAL,
"The payload segment with the name '"
<< writerGroup.getName().size()
<< "' would exceed the maximum allowed size when used with the '" << shmName
<< "' prefix!");
IOX_PANIC("The shm name exceeds the max size with the 'iox1_#_p_' prefix");
IOX_PANIC("");
}
shmName.append(TruncateToCapacity, writerGroup.getName());
return shmName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ std::string IpcMessageErrorTypeToString(const IpcMessageErrorType msg) noexcept;

/// @brief Transforms an IPC channel name to a prefixed interface name
/// @param[in] channelName the name of the channel without the 'iox1_#_' prefix
/// @param[in] resourceType to be used for the resource prefix
/// @return the interface name with the 'iox1_#_' prefix or a 'nullopt' if the resulting name would be too long
iox::optional<RuntimeName_t> ipcChannelNameToInterfaceName(RuntimeName_t channelName);
iox::optional<RuntimeName_t> ipcChannelNameToInterfaceName(RuntimeName_t channelName, ResourceType resourceType);

class IpcInterfaceUser;
class IpcInterfaceCreator;
Expand Down Expand Up @@ -236,7 +237,10 @@ class IpcInterface
/// IPC channel needs a unique string to be identified with.
IpcInterface() = delete;

IpcInterface(const RuntimeName_t& runtimeName, const uint64_t maxMessages, const uint64_t messageSize) noexcept;
IpcInterface(const RuntimeName_t& runtimeName,
const ResourceType resourceType,
const uint64_t maxMessages,
const uint64_t messageSize) noexcept;

/// @brief delete copy and move ctor and assignment since they are not needed
IpcInterface(const IpcInterface&) = delete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ class IpcInterfaceCreator : public IpcInterfaceBase
/// If it fails isInitialized will return false. Therefore, isInitialized
/// should always be called before using this class.
/// @param[in] name Unique identifier of the IPC channel
/// @param[in] resourceType to be used for the resource prefix
/// @param[in] maxMessages maximum number of queued messages
/// @param[in] message size maximum message size
IpcInterfaceCreator(const RuntimeName_t& name,
const ResourceType resourceType,
const uint64_t maxMessages = ROUDI_MAX_MESSAGES,
const uint64_t messageSize = ROUDI_MESSAGE_SIZE) noexcept;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class IpcInterfaceUser : public IpcInterfaceBase
/// Therefore, isInitialized should always be called
/// before using this class.
/// @param[in] name Unique identifier of the IPC channel
/// @param[in] resourceType to be used for the resource prefix
/// @param[in] maxMessages maximum number of queued messages
/// @param[in] message size maximum message size
IpcInterfaceUser(const RuntimeName_t& name,
const ResourceType resourceType,
const uint64_t maxMessages = APP_MAX_MESSAGES,
const uint64_t messageSize = APP_MESSAGE_SIZE) noexcept;

Expand All @@ -52,4 +54,4 @@ class IpcInterfaceUser : public IpcInterfaceBase
} // namespace runtime
} // namespace iox

#endif // IOX_POSH_RUNTIME_IPC_INTERFACE_USER_HPP
#endif // IOX_POSH_RUNTIME_IPC_INTERFACE_USER_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace roudi
IceOryxRouDiMemoryManager::IceOryxRouDiMemoryManager(const RouDiConfig_t& roudiConfig) noexcept
: m_fileLock(
std::move(FileLockBuilder()
.name(concatenate(iceoryxResourcePrefix(DEFAULT_UNIQUE_ROUDI_ID), ROUDI_LOCK_NAME))
.name(concatenate(iceoryxResourcePrefix(DEFAULT_UNIQUE_ROUDI_ID, ResourceType::ICEORYX_DEFINED),
ROUDI_LOCK_NAME))
.permission(iox::perms::owner_read | iox::perms::owner_write)
.create()
.or_else([](auto& error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ expected<void*, MemoryProviderError> PosixShmMemoryProvider::createMemory(const
}

if (!PosixSharedMemoryObjectBuilder()
.name(concatenate(iceoryxResourcePrefix(DEFAULT_UNIQUE_ROUDI_ID), m_shmName))
.name(
concatenate(iceoryxResourcePrefix(DEFAULT_UNIQUE_ROUDI_ID, ResourceType::ICEORYX_DEFINED), m_shmName))
.memorySizeInBytes(size)
.accessMode(m_accessMode)
.openMode(m_openMode)
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_posh/source/roudi/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Process::Process(const RuntimeName_t& name,
const HeartbeatPoolIndexType heartbeatPoolIndex,
const uint64_t sessionId) noexcept
: m_pid(pid)
, m_ipcChannel(name)
, m_ipcChannel(name, ResourceType::USER_DEFINED)
, m_heartbeatPoolIndex(heartbeatPoolIndex)
, m_user(user)
, m_sessionId(sessionId)
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_posh/source/roudi/roudi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void RouDi::processRuntimeMessages() noexcept
{
setThreadName("IPC-msg-process");

runtime::IpcInterfaceCreator roudiIpcInterface{IPC_CHANNEL_ROUDI_NAME};
runtime::IpcInterfaceCreator roudiIpcInterface{IPC_CHANNEL_ROUDI_NAME, ResourceType::ICEORYX_DEFINED};

IOX_LOG(INFO, "RouDi is ready for clients");
fflush(stdout); // explicitly flush 'stdout' for 'launch_testing'
Expand Down
7 changes: 4 additions & 3 deletions iceoryx_posh/source/runtime/ipc_interface_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ std::string IpcMessageErrorTypeToString(const IpcMessageErrorType msg) noexcept
return convert::toString(static_cast<UnderlyingType>(msg));
}

iox::optional<RuntimeName_t> ipcChannelNameToInterfaceName(RuntimeName_t channelName)
iox::optional<RuntimeName_t> ipcChannelNameToInterfaceName(RuntimeName_t channelName, ResourceType resourceType)
{
RuntimeName_t interfaceName = iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID);
RuntimeName_t interfaceName = iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID, resourceType);
if (interfaceName.size() + channelName.size() > RuntimeName_t::capacity())
{
return nullopt;
Expand All @@ -91,6 +91,7 @@ iox::optional<RuntimeName_t> ipcChannelNameToInterfaceName(RuntimeName_t channel

template <typename IpcChannelType>
IpcInterface<IpcChannelType>::IpcInterface(const RuntimeName_t& runtimeName,
const ResourceType resourceType,
const uint64_t maxMessages,
const uint64_t messageSize) noexcept
{
Expand All @@ -109,7 +110,7 @@ IpcInterface<IpcChannelType>::IpcInterface(const RuntimeName_t& runtimeName,
}

m_interfaceName =
ipcChannelNameToInterfaceName(runtimeName)
ipcChannelNameToInterfaceName(runtimeName, resourceType)
.or_else([&runtimeName] {
IOX_LOG(FATAL,
"The runtime with the name '"
Expand Down
3 changes: 2 additions & 1 deletion iceoryx_posh/source/runtime/ipc_interface_creator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ namespace iox
namespace runtime
{
IpcInterfaceCreator::IpcInterfaceCreator(const RuntimeName_t& runtimeName,
const ResourceType resourceType,
const uint64_t maxMessages,
const uint64_t messageSize) noexcept
: IpcInterfaceBase(runtimeName, maxMessages, messageSize)
: IpcInterfaceBase(runtimeName, resourceType, maxMessages, messageSize)
, m_fileLock(std::move(FileLockBuilder()
.name(m_interfaceName)
.permission(iox::perms::owner_read | iox::perms::owner_write)
Expand Down
3 changes: 2 additions & 1 deletion iceoryx_posh/source/runtime/ipc_interface_user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ namespace iox
namespace runtime
{
IpcInterfaceUser::IpcInterfaceUser(const RuntimeName_t& name,
const ResourceType resourceType,
const uint64_t maxMessages,
const uint64_t messageSize) noexcept
: IpcInterfaceBase(name, maxMessages, messageSize)
: IpcInterfaceBase(name, resourceType, maxMessages, messageSize)
{
openIpcChannel(PosixIpcChannelSide::CLIENT);
}
Expand Down
4 changes: 2 additions & 2 deletions iceoryx_posh/source/runtime/ipc_runtime_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ IpcRuntimeInterface::IpcRuntimeInterface(const RuntimeName_t& roudiName,
const RuntimeName_t& runtimeName,
const units::Duration roudiWaitingTimeout) noexcept
: m_runtimeName(runtimeName)
, m_RoudiIpcInterface(roudiName)
, m_RoudiIpcInterface(roudiName, ResourceType::ICEORYX_DEFINED)
{
m_AppIpcInterface.emplace(runtimeName);
m_AppIpcInterface.emplace(runtimeName, ResourceType::USER_DEFINED);
if (!m_AppIpcInterface->isInitialized())
{
IOX_REPORT_FATAL(PoshError::IPC_INTERFACE__UNABLE_TO_CREATE_APPLICATION_CHANNEL);
Expand Down
7 changes: 3 additions & 4 deletions iceoryx_posh/source/runtime/shared_memory_user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ SharedMemoryUser::SharedMemoryUser(const size_t topicSize,
const UntypedRelativePointer::offset_t segmentManagerAddressOffset) noexcept
{
PosixSharedMemoryObjectBuilder()
.name(concatenate(iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID), roudi::SHM_NAME))
.name(concatenate(iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID, ResourceType::ICEORYX_DEFINED),
roudi::SHM_NAME))
.memorySizeInBytes(topicSize)
.accessMode(AccessMode::READ_WRITE)
.openMode(OpenMode::OPEN_EXISTING)
Expand Down Expand Up @@ -77,9 +78,7 @@ void SharedMemoryUser::openDataSegments(const uint64_t segmentId,
PosixSharedMemoryObjectBuilder()
.name([&segment] {
using ShmName_t = detail::PosixSharedMemory::Name_t;
ShmName_t shmName = iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID,
"p"); // use an additional 'p' to prevent creating a payload
// segment with the same name as the management segment
ShmName_t shmName = iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID, ResourceType::USER_DEFINED);
if (shmName.size() + segment.m_sharedMemoryName.size() > ShmName_t::capacity())
{
IOX_LOG(FATAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ class CMqInterfaceStartupRace_test : public Test
if (!m_appQueue.has_value())
{
platform::IoxIpcChannelType::Builder_t()
.name(runtime::ipcChannelNameToInterfaceName(MqAppName).expect("valid interface name"))
.name(runtime::ipcChannelNameToInterfaceName(MqAppName, ResourceType::USER_DEFINED)
.expect("valid interface name"))
.channelSide(PosixIpcChannelSide::CLIENT)
.create()
.and_then([this](auto& channel) { this->m_appQueue.emplace(std::move(channel)); });
Expand All @@ -121,7 +122,8 @@ class CMqInterfaceStartupRace_test : public Test
std::mutex m_appQueueMutex;
optional<platform::IoxIpcChannelType> m_appQueue;
RuntimeName_t m_roudiIpcChannelName{
runtime::ipcChannelNameToInterfaceName(roudi::IPC_CHANNEL_ROUDI_NAME).expect("valid interface name")};
runtime::ipcChannelNameToInterfaceName(roudi::IPC_CHANNEL_ROUDI_NAME, ResourceType::ICEORYX_DEFINED)
.expect("valid interface name")};
};

#if !defined(__APPLE__)
Expand Down
3 changes: 2 additions & 1 deletion iceoryx_posh/test/moduletests/test_mepoo_segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ TEST_F(MePooSegment_test, SharedMemoryCreationParameter)
const iox::access_rights) {
EXPECT_THAT(name,
Eq(detail::PosixSharedMemory::Name_t(
concatenate(iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID, "p"), "iox_roudi_test2"))));
concatenate(iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID, ResourceType::USER_DEFINED),
"iox_roudi_test2"))));
EXPECT_THAT(accessMode, Eq(iox::AccessMode::READ_WRITE));
EXPECT_THAT(openMode, Eq(iox::OpenMode::PURGE_AND_CREATE));
};
Expand Down
3 changes: 2 additions & 1 deletion iceoryx_posh/test/moduletests/test_posh_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ TEST_F(PoshRuntime_test, MaxAppNameLength)
{
::testing::Test::RecordProperty("TEST_ID", "dfdf3ce1-c7d4-4c57-94ea-6ed9479371e3");
RuntimeName_t dummy{"a"};
auto prefixLength = runtime::ipcChannelNameToInterfaceName(dummy).value().size() - dummy.size();
auto prefixLength =
runtime::ipcChannelNameToInterfaceName(dummy, ResourceType::USER_DEFINED).value().size() - dummy.size();
std::string maxValidName(iox::MAX_RUNTIME_NAME_LENGTH - prefixLength, 's');

auto& runtime = PoshRuntime::initRuntime(into<lossy<RuntimeName_t>>(maxValidName));
Expand Down
11 changes: 7 additions & 4 deletions iceoryx_posh/test/moduletests/test_posh_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ TEST(PoshTypes_test, IceoryxResourcePrefixWithDefaultRouDiIdWorks)
{
::testing::Test::RecordProperty("TEST_ID", "35f1d638-8efa-41dd-859b-bcc23450844f");

EXPECT_THAT(iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID).c_str(), StrEq("iox1_0_"));
EXPECT_THAT(iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID, ResourceType::ICEORYX_DEFINED).c_str(),
StrEq("iox1_0_i_"));
}

TEST(PoshTypes_test, IceoryxResourcePrefixWithMaxRouDiIdWorks)
{
::testing::Test::RecordProperty("TEST_ID", "049e79d7-d0ca-4951-8d44-c80aebab7a88");

EXPECT_THAT(iceoryxResourcePrefix(std::numeric_limits<uint16_t>::max()).c_str(), StrEq("iox1_65535_"));
EXPECT_THAT(iceoryxResourcePrefix(std::numeric_limits<uint16_t>::max(), ResourceType::ICEORYX_DEFINED).c_str(),
StrEq("iox1_65535_i_"));
}

TEST(PoshTypes_test, IceoryxResourcePrefixWithMaxRouDiIdAndCustomizerWorks)
TEST(PoshTypes_test, IceoryxResourcePrefixWithMaxRouDiIdAndUserDefinedResourceTypeWorks)
{
::testing::Test::RecordProperty("TEST_ID", "b63bbdca-ff19-41bc-9f8a-c657b0ee8009");

EXPECT_THAT(iceoryxResourcePrefix(std::numeric_limits<uint16_t>::max(), "a").c_str(), StrEq("iox1_65535_a_"));
EXPECT_THAT(iceoryxResourcePrefix(std::numeric_limits<uint16_t>::max(), ResourceType::USER_DEFINED).c_str(),
StrEq("iox1_65535_u_"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class PosixShmMemoryProvider_Test : public Test
bool shmExists()
{
return !iox::PosixSharedMemoryObjectBuilder()
.name(concatenate(iceoryxResourcePrefix(DEFAULT_UNIQUE_ROUDI_ID), TEST_SHM_NAME))
.name(concatenate(iceoryxResourcePrefix(DEFAULT_UNIQUE_ROUDI_ID, ResourceType::ICEORYX_DEFINED),
TEST_SHM_NAME))
.memorySizeInBytes(8)
.accessMode(iox::AccessMode::READ_ONLY)
.openMode(iox::OpenMode::OPEN_EXISTING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ProcessManager_test : public Test
const bool m_isMonitored{true};
VersionInfo m_versionInfo{42U, 42U, 42U, 42U, "Foo", "Bar"};

IpcInterfaceCreator m_processIpcInterface{m_processname};
IpcInterfaceCreator m_processIpcInterface{m_processname, ResourceType::USER_DEFINED};
ProcessIntrospectionType m_processIntrospection;

std::unique_ptr<IceOryxRouDiMemoryManager> m_roudiMemoryManager{nullptr};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class IpcInterface_test : public Test
SutType(const RuntimeName_t& runtimeName,
const uint64_t maxMessages = MaxMsgNumber,
const uint64_t messageSize = MaxMsgSize) noexcept
: IpcChannelType(runtimeName, maxMessages, messageSize)
: IpcChannelType(runtimeName, ResourceType::USER_DEFINED, maxMessages, messageSize)
{
}
using IpcChannelType::ipcChannelMapsToFile;
Expand Down
Loading

0 comments on commit f024b06

Please sign in to comment.