Skip to content

Commit

Permalink
Merge pull request #5 from hadriansecurity/fix_queue_count
Browse files Browse the repository at this point in the history
src/net_info: Fix queue count
  • Loading branch information
kalmjasper authored Aug 2, 2024
2 parents 597bbca + e6278cd commit c9c7dfc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
3 changes: 2 additions & 1 deletion include/net_info.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <linux/ethtool.h>
#include <net/if.h>
#include <fixed_name.hpp>
#include <network_types.h>
Expand All @@ -18,6 +19,6 @@ struct RouteInfo {

tl::expected<RouteInfo, std::string> get_route_info(std::optional<FixedName<IFNAMSIZ>> if_name = std::nullopt);
tl::expected<EtherAddr, std::string> get_mac_address(FixedName<IFNAMSIZ> iface, const InAddr& in_addr);
tl::expected<uint32_t, std::string> get_combined_channel_count(FixedName<IFNAMSIZ> iface);
tl::expected<ethtool_channels, std::string> get_channel_count(FixedName<IFNAMSIZ> iface);

}
43 changes: 29 additions & 14 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <expected_helpers.h>
#include <fcntl.h>
#include <ncurses.h>
#include <net/if.h>
#include <rte_cycles.h>
#include <rte_errno.h>
#include <rte_ethdev.h>
Expand Down Expand Up @@ -456,6 +457,30 @@ std::optional<EthernetConfig> GetEthernetConfig(const UserConfig& user_config) {
return to_ret;
}

tl::expected<void, std::string> VerifyQueues(FixedName<IFNAMSIZ> dev_name, const uint16_t num_cores,
const uint16_t total_queues) {
auto channel_count = net_info::get_channel_count(dev_name);
if (!channel_count.has_value()) {
return tl::unexpected(
fmt::format("{}, channel_count error: {}\n", error_str, channel_count.error()));
}

uint32_t combined_channels = channel_count.value().combined_count;

// Seems to be the DPDK logic
if (combined_channels == 0)
combined_channels = 1;

if (total_queues != combined_channels) {
return tl::unexpected(fmt::format(
"{} Running sanicdns with '-w {}' requires {} queues (current {}). "
"Configure using 'sudo ethtool -L {} combined {}'\n",
error_str, num_cores, total_queues, combined_channels, dev_name, total_queues));
}

return {};
}

rte_malloc_socket_stats GetMemoryUsage() {
rte_malloc_socket_stats sock_stats{};

Expand Down Expand Up @@ -567,21 +592,11 @@ int main(int argc, char** argv) {
const uint16_t total_queues = num_workers + (NIC_OPTS::queue_for_main_thread ? 1 : 0);

if constexpr (NIC_OPTS::make_af_xdp_socket) {
const auto dev_name = UNWRAP_OR_RETURN_VAL(
auto dev_name = UNWRAP_OR_RETURN_VAL(
FixedName<IFNAMSIZ>::init(ethernet_config.device_name), -1);

auto channel_count = net_info::get_combined_channel_count(dev_name);
if (!channel_count.has_value()) {
fmt::print("{}, channel_count error: {}\n", error_str,
channel_count.error());
return -1;
}

if (total_queues != channel_count.value()) {
fmt::print("{} Running sanicdns with '-w {}' requires {} combined queues. "
"Configure using 'sudo ethtool -L {} combined {}'\n",
error_str, user_config.cores, total_queues, ethernet_config.device_name,
total_queues);
auto res = VerifyQueues(dev_name, user_config.cores, total_queues);
if (!res) {
fmt::print("{} {}", error_str, res.error());
return -1;
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/net_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ tl::expected<net_info::RouteInfo, std::string> net_info::get_route_info(
return routes_map[iface];
}

tl::expected<uint32_t, std::string> net_info::get_combined_channel_count(
FixedName<IFNAMSIZ> iface) {
tl::expected<ethtool_channels, std::string> net_info::get_channel_count(FixedName<IFNAMSIZ> iface) {
int if_idx = if_nametoindex(iface.c_str());
if (if_idx < 0) {
return tl::unexpected(fmt::format("Cannot get index for {}", iface));
Expand All @@ -237,5 +236,5 @@ tl::expected<uint32_t, std::string> net_info::get_combined_channel_count(
}

close(fd);
return ethchannels.combined_count;
return ethchannels;
}

0 comments on commit c9c7dfc

Please sign in to comment.