Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic allocation memory (part 1) #120

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/idataplane.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ class dataPlane
return get<common::idp::requestType::neighbor_stats, common::idp::neighbor_stats::response>();
}

auto memory_manager_update(const common::idp::memory_manager_update::request& request) const
{
return get<common::idp::requestType::memory_manager_update, eResult>(request);
}

auto memory_manager_stats() const
{
return get<common::idp::requestType::memory_manager_stats, common::idp::memory_manager_stats::response>();
}

protected:
void connectToDataPlane() const
{
Expand Down
24 changes: 22 additions & 2 deletions common/idp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "acl.h"
#include "balancer.h"
#include "config.h"
#include "memory_manager.h"
#include "neighbor.h"
#include "result.h"
#include "scheduler.h"
Expand Down Expand Up @@ -82,6 +83,8 @@ enum class requestType : uint32_t
neighbor_flush,
neighbor_update_interfaces,
neighbor_stats,
memory_manager_update,
memory_manager_stats,
size, // size should always be at the bottom of the list, this enum allows us to find out the size of the enum list
};

Expand Down Expand Up @@ -984,6 +987,21 @@ namespace neighbor_stats
using response = common::neighbor::stats;
}

namespace memory_manager_update
{
using request = memory_manager::memory_group;
}

namespace memory_manager_stats
{
using object = std::tuple<std::string, ///< name
tSocketId, ///< socket_id
uint64_t>; ///< size

using response = std::tuple<memory_manager::memory_group,
std::vector<object>>;
}

//

using request = std::tuple<requestType,
Expand All @@ -1005,7 +1023,8 @@ using request = std::tuple<requestType,
dump_physical_port::request,
neighbor_insert::request,
neighbor_remove::request,
neighbor_update_interfaces::request>>;
neighbor_update_interfaces::request,
memory_manager_update::request>>;

using response = std::variant<std::tuple<>,
updateGlobalBase::response, ///< + others which have eResult as response
Expand Down Expand Up @@ -1039,5 +1058,6 @@ using response = std::variant<std::tuple<>,
get_shm_info::response,
get_shm_tsc_info::response,
neighbor_show::response,
neighbor_stats::response>;
neighbor_stats::response,
memory_manager_stats::response>;
}
61 changes: 61 additions & 0 deletions common/memory_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include <memory>
#include <vector>

#include "stream.h"

namespace common::memory_manager
{

inline uint64_t convert_string_to_bytes(std::string string)
{
static std::map<char, uint64_t> multipliers =
{{'k', 1024ull},
{'K', 1024ull},
{'m', 1024ull * 1024ull},
{'M', 1024ull * 1024ull},
{'g', 1024ull * 1024ull * 1024ull},
{'G', 1024ull * 1024ull * 1024ull}};

if (string.empty())
{
return 0;
}

uint64_t multiplier = 1;

auto iter = multipliers.find(string.back());
if (iter != multipliers.end())
{
multiplier = iter->second;
string.pop_back();
}

return std::stoll(string) * multiplier;
}

class memory_group
{
public:
void pop(common::stream_in_t& stream)
{
stream.pop(name);
stream.pop(limit);
stream.pop(memory_groups);
}

void push(common::stream_out_t& stream) const
{
stream.push(name);
stream.push(limit);
stream.push(memory_groups);
}

public:
std::string name;
uint64_t limit;
std::vector<std::shared_ptr<memory_group>> memory_groups;
};

}
17 changes: 17 additions & 0 deletions common/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>

#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
Expand Down Expand Up @@ -61,6 +62,9 @@ class stream_in_t
template<typename TType>
inline void pop(std::optional<TType>& optional);

template<typename TType>
inline void pop(std::shared_ptr<TType>& pointer);

inline bool isFailed();

protected:
Expand Down Expand Up @@ -221,6 +225,12 @@ class stream_out_t
}
}

template<typename TType>
inline void push(const std::shared_ptr<TType>& pointer)
{
push(*pointer.get());
}

inline const std::vector<uint8_t>& getBuffer()
{
return outBuffer;
Expand Down Expand Up @@ -450,6 +460,13 @@ inline void stream_in_t::pop(std::optional<TType>& optional)
}
}

template<typename TType>
inline void stream_in_t::pop(std::shared_ptr<TType>& pointer)
{
pointer = std::make_shared<TType>();
pop(*pointer.get());
}

inline bool stream_in_t::isFailed()
{
return failed;
Expand Down
5 changes: 5 additions & 0 deletions common/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ class mac_address_t
return buffer;
}

uint8_t* data()
{
return address.data();
}

const uint8_t* data() const
{
return address.data();
Expand Down
2 changes: 2 additions & 0 deletions controlplane/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ class base_t
std::map<std::string, ///< vrf_name
std::vector<base_rib>>
rib;

common::memory_manager::memory_group root_memory_group;
};

//
Expand Down
31 changes: 31 additions & 0 deletions controlplane/configparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ controlplane::base_t config_parser_t::loadConfig(const std::string& rootFilePath
{
loadConfig_rib(baseNext, rootJson["rib"]);
}

if (exist(rootJson, "memory_groups"))
{
loadConfig_memory_group(baseNext.root_memory_group, rootJson["memory_groups"]);
}
}
catch (const error_result_t& err)
{
Expand Down Expand Up @@ -1901,3 +1906,29 @@ void config_parser_t::loadConfig_rib(controlplane::base_t& baseNext,
}
}
}

void config_parser_t::loadConfig_memory_group(common::memory_manager::memory_group& memory_group,
const nlohmann::json& json)
{
for (const auto& json_iter : json)
{
auto memory_group_next = std::make_shared<common::memory_manager::memory_group>();

std::string name = json_iter["name"].get<std::string>();
std::string limit = "0";
if (exist(json_iter, "limit"))
{
limit = json_iter["limit"].get<std::string>();
}

memory_group_next->name = name;
memory_group_next->limit = common::memory_manager::convert_string_to_bytes(std::move(limit));

if (exist(json_iter, "memory_groups"))
{
loadConfig_memory_group(*memory_group_next.get(), json_iter["memory_groups"]);
}

memory_group.memory_groups.emplace_back(memory_group_next);
}
}
1 change: 1 addition & 0 deletions controlplane/configparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class config_parser_t
void loadConfig_variables(controlplane::base_t& baseNext, const nlohmann::json& json);
void loadConfig_fqdns(controlplane::base_t& baseNext, const nlohmann::json& json, const std::string& rootFilePath, const std::map<std::string, nlohmann::json>& jsons);
void loadConfig_rib(controlplane::base_t& baseNext, const nlohmann::json& json);
void loadConfig_memory_group(common::memory_manager::memory_group& memory_group, const nlohmann::json& json);

private:
common::idp::getConfig::response dataPlaneConfig;
Expand Down
1 change: 1 addition & 0 deletions controlplane/controlplane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ eResult cControlPlane::init(const std::string& jsonFilePath)
modules.emplace_back(&durations);
modules.emplace_back(&nat64stateful);
modules.emplace_back(&nat46clat);
modules.emplace_back(&memory_manager);

for (auto* module : modules)
{
Expand Down
2 changes: 2 additions & 0 deletions controlplane/controlplane.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "durations.h"
#include "fqdn.h"
#include "isystem.h"
#include "memory_manager.h"
#include "module.h"
#include "nat46clat.h"
#include "nat64stateful.h"
Expand Down Expand Up @@ -172,6 +173,7 @@ class cControlPlane
durations_t durations;
nat64stateful_t nat64stateful;
nat46clat::manager nat46clat;
controlplane::memory_manager::memory_manager memory_manager;

counter_manager_t counter_manager;

Expand Down
12 changes: 12 additions & 0 deletions controlplane/memory_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "memory_manager.h"

using namespace controlplane::memory_manager;

void memory_manager::reload(const base_t& base_prev,
const base_t& base_next,
common::idp::updateGlobalBase::request& globalbase)
{
(void)base_prev;
(void)globalbase;
dataplane.memory_manager_update(base_next.root_memory_group);
}
21 changes: 21 additions & 0 deletions controlplane/memory_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "base.h"
#include "module.h"
#include "type.h"

#include "common/idataplane.h"

namespace controlplane::memory_manager
{

class memory_manager : public module_t
{
public:
void reload(const controlplane::base_t& base_prev, const controlplane::base_t& base_next, common::idp::updateGlobalBase::request& globalbase) override;

protected:
interface::dataPlane dataplane;
};

}
1 change: 1 addition & 0 deletions controlplane/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sources = files('acl_compiler.cpp',
'fqdn.cpp',
'isystem.cpp',
'main.cpp',
'memory_manager.cpp',
'module.cpp',
'nat46clat.cpp',
'nat64stateful.cpp',
Expand Down
4 changes: 4 additions & 0 deletions dataplane/bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ void cBus::clientThread(int clientSocket)
{
response = dataPlane->neighbor.neighbor_stats();
}
else if (type == common::idp::requestType::memory_manager_update)
{
response = dataPlane->memory_manager.memory_manager_update(std::get<common::idp::memory_manager_update::request>(std::get<1>(request)));
}
else
{
stats.errors[(uint32_t)common::idp::errorType::busParse]++;
Expand Down
10 changes: 10 additions & 0 deletions dataplane/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ inline uint32_t yanet_hash_crc(const void* data, uint32_t init)
//

static_assert(CONFIG_YADECAP_PORTS_SIZE <= 0xFF, "invalid CONFIG_YADECAP_PORTS_SIZE");

//

namespace dataplane
{

class memory_pointer;
class memory_manager;

}
12 changes: 5 additions & 7 deletions dataplane/controlplane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,14 +973,12 @@ common::idp::limits::response cControlPlane::limits()
YANET_CONFIG_ROUTE_TUNNEL_LPM6_EXTENDED_SIZE);

globalBase->updater.acl.network_table.limits(response, "acl.network.ht");
globalBase->updater.acl.transport_table.limits(response, "acl.transport.ht");
globalBase->updater.acl.total_table.limits(response, "acl.total.ht");
globalBase->updater.acl.network_ipv4_source.limits(response, "acl.network.v4.source.lpm");
globalBase->updater.acl.network_ipv4_destination.limits(response, "acl.network.v4.destination.lpm");
globalBase->updater.acl.transport_table->limits(response);
globalBase->updater.acl.total_table->limits(response);
globalBase->updater.acl.network_ipv4_source->limits(response);
globalBase->updater.acl.network_ipv4_destination->limits(response);
globalBase->updater.acl.network_ipv6_source.limits(response, "acl.network.v6.source.lpm");

/// globalBase->acl.network_ipv6_destination_ht is not critical

globalBase->updater.acl.network_ipv6_destination_ht->limits(response);
globalBase->updater.acl.network_ipv6_destination.limits(response, "acl.network.v6.destination.lpm");

limit_insert(response,
Expand Down
Loading
Loading