From 10be792ffb0f8b2b4993367051c63bf4b37cd011 Mon Sep 17 00:00:00 2001 From: Bashar Abdelgafer Date: Sat, 14 Dec 2024 14:57:09 +0200 Subject: [PATCH] issue: 4209214 Read system control parameters from environment variables Refactored the logic to retrieve system parameters via environment variables instead of relying on system control (`sysctl`) parameters. Improved compatibility with containerized and runtime-configurable environments. Signed-off-by: Bashar Abdelgafer --- src/core/dev/src_addr_selector.cpp | 4 +- src/core/main.cpp | 84 ++++++++ src/core/proto/xlio_lwip.cpp | 8 +- src/core/sock/sockinfo.cpp | 12 +- src/core/sock/sockinfo_tcp.cpp | 18 +- src/core/sock/sockinfo_udp.cpp | 12 +- src/core/util/sys_vars.cpp | 114 +++++++++++ src/core/util/sys_vars.h | 61 ++++++ src/core/util/sysctl_reader.h | 298 +++++------------------------ 9 files changed, 335 insertions(+), 276 deletions(-) diff --git a/src/core/dev/src_addr_selector.cpp b/src/core/dev/src_addr_selector.cpp index 2418988da..834af9bd2 100644 --- a/src/core/dev/src_addr_selector.cpp +++ b/src/core/dev/src_addr_selector.cpp @@ -44,8 +44,8 @@ bool ipv6_source_addr_score::use_optimistic_addr() const { sysctl_reader_t &ctl = sysctl_reader_t::instance(); - return (ctl.get_ipv6_conf_all_optimistic_dad() || m_dev->ipv6_optimistic_dad()) && - (ctl.get_ipv6_conf_all_use_optimistic() || m_dev->ipv6_use_optimistic()); + return (ctl.ipv6_conf_all_optimistic_dad || m_dev->ipv6_optimistic_dad()) && + (ctl.ipv6_conf_all_use_optimistic || m_dev->ipv6_use_optimistic()); } int ipv6_source_addr_score::get_score(const ipv6_source_addr_score_dst &score_dst, int rule) diff --git a/src/core/main.cpp b/src/core/main.cpp index 5031b17d5..4f06b2a65 100644 --- a/src/core/main.cpp +++ b/src/core/main.cpp @@ -877,6 +877,90 @@ void print_xlio_global_settings() SYS_VAR_MULTILOCK, (safe_mce_sys().multilock == MULTILOCK_SPIN ? "Spin " : "Mutex")); + //Sysctl parameters + VLOG_PARAM_NUMBER("TCP max syn backlog", safe_mce_sys().sysctl_reader.tcp_max_syn_backlog, + MCE_DEFAULT_TCP_MAX_SYN_BACKLOG, SYS_VAR_TCP_MAX_SYN_BACKLOG); + + VLOG_PARAM_NUMBER("Listen max connections", safe_mce_sys().sysctl_reader.listen_maxconn, + MCE_DEFAULT_LISTEN_MAXCONN, SYS_VAR_LISTEN_MAXCONN); + + VLOG_PARAM_NUMBER("TCP window scaling", safe_mce_sys().sysctl_reader.tcp_window_scaling, + MCE_DEFAULT_TCP_WINDOW_SCALING, SYS_VAR_TCP_WINDOW_SCALING); + + VLOG_PARAM_NUMBER("TCP write memory (min)", safe_mce_sys().sysctl_reader.tcp_wmem.min_value, + MCE_DEFAULT_TCP_WMEM_MIN, SYS_VAR_TCP_WMEM_MIN); + + VLOG_PARAM_NUMBER("TCP write memory (default)", + safe_mce_sys().sysctl_reader.tcp_wmem.default_value, + MCE_DEFAULT_TCP_WMEM_DEFAULT, SYS_VAR_TCP_WMEM_DEFAULT); + + VLOG_PARAM_NUMBER("TCP write memory (max)", safe_mce_sys().sysctl_reader.tcp_wmem.max_value, + MCE_DEFAULT_TCP_WMEM_MAX, SYS_VAR_TCP_WMEM_MAX); + + VLOG_PARAM_NUMBER("TCP read memory (min)", safe_mce_sys().sysctl_reader.tcp_rmem.min_value, + MCE_DEFAULT_TCP_RMEM_MIN, SYS_VAR_TCP_RMEM_MIN); + + VLOG_PARAM_NUMBER("TCP read memory (default)", + safe_mce_sys().sysctl_reader.tcp_rmem.default_value, + MCE_DEFAULT_TCP_RMEM_DEFAULT, SYS_VAR_TCP_RMEM_DEFAULT); + + VLOG_PARAM_NUMBER("TCP read memory (max)", safe_mce_sys().sysctl_reader.tcp_rmem.max_value, + MCE_DEFAULT_TCP_RMEM_MAX, SYS_VAR_TCP_RMEM_MAX); + + VLOG_PARAM_NUMBER("TCP keepalive time", + safe_mce_sys().sysctl_reader.tcp_keepalive_infos.idle_secs, + MCE_DEFAULT_TCP_KEEPALIVE_TIME, SYS_VAR_TCP_KEEPALIVE_IDLE); + + VLOG_PARAM_NUMBER("TCP keepalive interval", + safe_mce_sys().sysctl_reader.tcp_keepalive_infos.interval_secs, + MCE_DEFAULT_TCP_KEEPALIVE_INTERVAL, SYS_VAR_TCP_KEEPALIVE_INTERVAL); + + VLOG_PARAM_NUMBER("TCP keepalive probes", + safe_mce_sys().sysctl_reader.tcp_keepalive_infos.num_probes, + MCE_DEFAULT_TCP_KEEPALIVE_PROBES, SYS_VAR_TCP_KEEPALIVE_PROBES); + + VLOG_PARAM_NUMBER("Net core receive memory max", safe_mce_sys().sysctl_reader.net_core_rmem_max, + MCE_DEFAULT_NET_CORE_RMEM_MAX, SYS_VAR_NET_CORE_RMEM_MAX); + + VLOG_PARAM_NUMBER("Net core write memory max", safe_mce_sys().sysctl_reader.net_core_wmem_max, + MCE_DEFAULT_NET_CORE_WMEM_MAX, SYS_VAR_NET_CORE_WMEM_MAX); + + VLOG_PARAM_NUMBER("Net IPv4 TCP timestamps", + safe_mce_sys().sysctl_reader.net_ipv4_tcp_timestamps, + MCE_DEFAULT_NET_IPV4_TCP_TIMESTAMPS, SYS_VAR_NET_IPV4_TCP_TIMESTAMPS); + + // behavior params + // VLOG_PARAM_NUMBER("Net IPv4 TTL", safe_mce_sys().sysctl_reader.net_ipv4_ttl, + // MCE_DEFAULT_NET_IPV4_TTL, SYS_VAR_NET_IPV4_TTL); + + // VLOG_PARAM_NUMBER("IGMP max membership", safe_mce_sys().sysctl_reader.igmp_max_membership, + // MCE_DEFAULT_IGMP_MAX_MEMBERSHIP, SYS_VAR_IGMP_MAX_MEMBERSHIP); + + // VLOG_PARAM_NUMBER("IGMP max source membership", + // safe_mce_sys().sysctl_reader.igmp_max_source_membership, + // MCE_DEFAULT_IGMP_MAX_SOURCE_MEMBERSHIP, + // SYS_VAR_IGMP_MAX_SOURCE_MEMBERSHIP); + + // VLOG_PARAM_NUMBER("MLD max source membership", + // safe_mce_sys().sysctl_reader.mld_max_source_membership, + // MCE_DEFAULT_MLD_MAX_SOURCE_MEMBERSHIP, SYS_VAR_MLD_MAX_SOURCE_MEMBERSHIP); + + // VLOG_PARAM_NUMBER("Net IPv6 hop limit", safe_mce_sys().sysctl_reader.net_ipv6_hop_limit, + // MCE_DEFAULT_NET_IPV6_HOP_LIMIT, SYS_VAR_NET_IPV6_HOP_LIMIT); + + // VLOG_PARAM_NUMBER("IPv6 bindv6only", safe_mce_sys().sysctl_reader.ipv6_bindv6only, + // MCE_DEFAULT_IPV6_BINDV6ONLY, SYS_VAR_IPV6_BINDV6ONLY); + + // VLOG_PARAM_NUMBER( + // "IPv6 conf all optimistic dad", + // safe_mce_sys().sysctl_reader.ipv6_conf_all_optimistic_dad, + // MCE_DEFAULT_IPV6_CONF_ALL_OPTIMISTIC_DAD, SYS_VAR_IPV6_CONF_ALL_OPTIMISTIC_DAD); + + // VLOG_PARAM_NUMBER( + // "IPv6 conf all use optimistic", + // safe_mce_sys().sysctl_reader.ipv6_conf_all_use_optimistic, + // MCE_DEFAULT_IPV6_CONF_ALL_USE_OPTIMISTIC, SYS_VAR_IPV6_CONF_ALL_USE_OPTIMISTIC); + vlog_printf(VLOG_INFO, "---------------------------------------------------------------------------\n"); } diff --git a/src/core/proto/xlio_lwip.cpp b/src/core/proto/xlio_lwip.cpp index f6edc0493..8d29d11c5 100644 --- a/src/core/proto/xlio_lwip.cpp +++ b/src/core/proto/xlio_lwip.cpp @@ -69,7 +69,7 @@ u32_t xlio_lwip::sys_now(void) u8_t xlio_lwip::read_tcp_timestamp_option(void) { u8_t res = (safe_mce_sys().tcp_ts_opt == TCP_TS_OPTION_FOLLOW_OS) - ? safe_mce_sys().sysctl_reader.get_net_ipv4_tcp_timestamps() + ? safe_mce_sys().sysctl_reader.net_ipv4_tcp_timestamps : (safe_mce_sys().tcp_ts_opt == TCP_TS_OPTION_ENABLE ? 1 : 0); if (res) { #if LWIP_TCP_TIMESTAMPS @@ -108,10 +108,10 @@ xlio_lwip::xlio_lwip() enable_push_flag = !!safe_mce_sys().tcp_push_flag; enable_ts_option = read_tcp_timestamp_option(); - int is_window_scaling_enabled = safe_mce_sys().sysctl_reader.get_tcp_window_scaling(); + int is_window_scaling_enabled = safe_mce_sys().sysctl_reader.tcp_window_scaling; if (is_window_scaling_enabled) { - int rmem_max_value = safe_mce_sys().sysctl_reader.get_tcp_rmem()->max_value; - int core_rmem_max = safe_mce_sys().sysctl_reader.get_net_core_rmem_max(); + int rmem_max_value = safe_mce_sys().sysctl_reader.tcp_rmem.max_value; + int core_rmem_max = safe_mce_sys().sysctl_reader.net_core_rmem_max; enable_wnd_scale = 1; rcv_wnd_scale = get_window_scaling_factor(rmem_max_value, core_rmem_max); } else { diff --git a/src/core/sock/sockinfo.cpp b/src/core/sock/sockinfo.cpp index bf935d126..58ea058e7 100644 --- a/src/core/sock/sockinfo.cpp +++ b/src/core/sock/sockinfo.cpp @@ -104,16 +104,15 @@ sockinfo::sockinfo(int fd, int domain, bool use_ring_locks) , m_family(domain) , m_fd(fd) , m_rx_num_buffs_reuse(safe_mce_sys().rx_bufs_batch) - , m_is_ipv6only(safe_mce_sys().sysctl_reader.get_ipv6_bindv6only()) + , m_is_ipv6only(safe_mce_sys().sysctl_reader.ipv6_bindv6only) , m_lock_rcv(MULTILOCK_RECURSIVE, MODULE_NAME "::m_lock_rcv") , m_lock_snd(MODULE_NAME "::m_lock_snd") , m_so_bindtodevice_ip(ip_address::any_addr(), domain) , m_rx_ring_map_lock(MODULE_NAME "::m_rx_ring_map_lock") , m_ring_alloc_log_rx(safe_mce_sys().ring_allocation_logic_rx, use_ring_locks) , m_ring_alloc_log_tx(safe_mce_sys().ring_allocation_logic_tx, use_ring_locks) - , m_n_uc_ttl_hop_lim(m_family == AF_INET - ? safe_mce_sys().sysctl_reader.get_net_ipv4_ttl() - : safe_mce_sys().sysctl_reader.get_net_ipv6_hop_limit()) + , m_n_uc_ttl_hop_lim(m_family == AF_INET ? safe_mce_sys().sysctl_reader.net_ipv4_ttl + : safe_mce_sys().sysctl_reader.net_ipv6_hop_limit) { m_rx_epfd = SYSCALL(epoll_create, 128); if (unlikely(m_rx_epfd == -1)) { @@ -601,9 +600,8 @@ int sockinfo::setsockopt(int __level, int __optname, const void *__optval, sockl ret = SOCKOPT_NO_XLIO_SUPPORT; errno = EINVAL; } else { - m_n_uc_ttl_hop_lim = (val == -1) - ? safe_mce_sys().sysctl_reader.get_net_ipv4_ttl() - : (uint8_t)val; + m_n_uc_ttl_hop_lim = + (val == -1) ? safe_mce_sys().sysctl_reader.net_ipv4_ttl : (uint8_t)val; header_ttl_hop_limit_updater du(m_n_uc_ttl_hop_lim, false); update_header_field(&du); si_logdbg("IPPROTO_IP, optname=IP_TTL (%d)", m_n_uc_ttl_hop_lim); diff --git a/src/core/sock/sockinfo_tcp.cpp b/src/core/sock/sockinfo_tcp.cpp index 77b472f71..c93175cb2 100644 --- a/src/core/sock/sockinfo_tcp.cpp +++ b/src/core/sock/sockinfo_tcp.cpp @@ -330,7 +330,7 @@ sockinfo_tcp::sockinfo_tcp(int fd, int domain) tcp_pcb_init(&m_pcb, TCP_PRIO_NORMAL, this); - const tcp_keepalive_info keepalive_info = safe_mce_sys().sysctl_reader.get_tcp_keepalive_info(); + const tcp_keepalive_info keepalive_info = safe_mce_sys().sysctl_reader.tcp_keepalive_infos; tcp_set_keepalive(&m_pcb, static_cast(1000U * keepalive_info.idle_secs), static_cast(1000U * keepalive_info.interval_secs), static_cast(keepalive_info.num_probes)); @@ -350,7 +350,7 @@ sockinfo_tcp::sockinfo_tcp(int fd, int domain) m_iomux_ready_fd_array = nullptr; /* RCVBUF accounting */ - m_rcvbuff_max = safe_mce_sys().sysctl_reader.get_tcp_rmem()->default_value; + m_rcvbuff_max = safe_mce_sys().sysctl_reader.tcp_rmem.default_value; m_rcvbuff_current = 0; m_rcvbuff_non_tcp_recved = 0; @@ -1716,7 +1716,7 @@ void sockinfo_tcp::process_my_ctl_packets() mem_buf_desc_t *desc = temp_list.get_and_pop_front(); static const unsigned int MAX_SYN_RCVD = tcp_ctl_thread_on(safe_mce_sys().tcp_ctl_thread) - ? safe_mce_sys().sysctl_reader.get_tcp_max_syn_backlog() + ? safe_mce_sys().sysctl_reader.tcp_max_syn_backlog : 0; // NOTE: currently, in case tcp_ctl_thread is disabled, only established backlog is // supported (no syn-rcvd backlog) @@ -2536,7 +2536,7 @@ bool sockinfo_tcp::rx_input_cb(mem_buf_desc_t *p_rx_pkt_mem_buf_desc_info, void /// distinguish between backlog of established sockets vs. backlog of syn-rcvd static const unsigned int MAX_SYN_RCVD = tcp_ctl_thread_on(safe_mce_sys().tcp_ctl_thread) - ? safe_mce_sys().sysctl_reader.get_tcp_max_syn_backlog() + ? safe_mce_sys().sysctl_reader.tcp_max_syn_backlog : 0; // NOTE: currently, in case tcp_ctl_thread is disabled, only established backlog is // supported (no syn-rcvd backlog) @@ -2974,7 +2974,7 @@ int sockinfo_tcp::listen(int backlog) * Note: backlog behavior depends on safe_mce_sys().tcp_ctl_thread status. */ if (backlog < 0) { - backlog = safe_mce_sys().sysctl_reader.get_listen_maxconn(); + backlog = safe_mce_sys().sysctl_reader.listen_maxconn; si_tcp_logdbg("changing listen backlog=%d to the maximum=%d", orig_backlog, backlog); } else if (backlog == 0) { backlog = 1; @@ -2986,8 +2986,8 @@ int sockinfo_tcp::listen(int backlog) /* If an application calls listen() with a backlog value larger than net.core.somaxconn, * then the backlog for that listener will be silently truncated to the somaxconn value. */ - if (backlog > safe_mce_sys().sysctl_reader.get_listen_maxconn()) { - backlog = safe_mce_sys().sysctl_reader.get_listen_maxconn(); + if (backlog > safe_mce_sys().sysctl_reader.listen_maxconn) { + backlog = safe_mce_sys().sysctl_reader.listen_maxconn; si_tcp_logdbg("truncating listen backlog=%d to the maximun=%d", orig_backlog, backlog); } } @@ -4478,7 +4478,7 @@ int sockinfo_tcp::tcp_setsockopt(int __level, int __optname, __const void *__opt si_tcp_logdbg("(SO_KEEPALIVE) val: %d", val); break; case SO_RCVBUF: - val = std::min(*(int *)__optval, safe_mce_sys().sysctl_reader.get_net_core_rmem_max()); + val = std::min(*(int *)__optval, safe_mce_sys().sysctl_reader.net_core_rmem_max); lock_tcp_con(); // OS allocates double the size of memory requested by the application - not sure we // need it. @@ -4489,7 +4489,7 @@ int sockinfo_tcp::tcp_setsockopt(int __level, int __optname, __const void *__opt si_tcp_logdbg("setsockopt SO_RCVBUF: %d", m_rcvbuff_max); break; case SO_SNDBUF: - val = std::min(*(int *)__optval, safe_mce_sys().sysctl_reader.get_net_core_wmem_max()); + val = std::min(*(int *)__optval, safe_mce_sys().sysctl_reader.net_core_wmem_max); lock_tcp_con(); // OS allocates double the size of memory requested by the application - not sure we // need it. diff --git a/src/core/sock/sockinfo_udp.cpp b/src/core/sock/sockinfo_udp.cpp index 76787ef23..4bcd15b38 100644 --- a/src/core/sock/sockinfo_udp.cpp +++ b/src/core/sock/sockinfo_udp.cpp @@ -2675,7 +2675,7 @@ int sockinfo_udp::mc_change_membership_start_helper_ip4(const ip_address &mc_grp case IP_ADD_MEMBERSHIP: if (m_mc_memberships_map.find(mc_grp) == m_mc_memberships_map.end() && m_mc_memberships_map.size() >= - (size_t)safe_mce_sys().sysctl_reader.get_igmp_max_membership()) { + (size_t)safe_mce_sys().sysctl_reader.igmp_max_membership) { errno = ENOBUFS; return -1; } @@ -2684,13 +2684,13 @@ int sockinfo_udp::mc_change_membership_start_helper_ip4(const ip_address &mc_grp if (m_mc_memberships_map.find(mc_grp) != m_mc_memberships_map.end()) { // This group is // exist if (m_mc_memberships_map[mc_grp].size() >= - (size_t)safe_mce_sys().sysctl_reader.get_igmp_max_source_membership()) { + (size_t)safe_mce_sys().sysctl_reader.igmp_max_source_membership) { errno = ENOBUFS; return -1; } } else { // This group is not exist if (m_mc_memberships_map.size() >= - (size_t)safe_mce_sys().sysctl_reader.get_igmp_max_membership()) { + (size_t)safe_mce_sys().sysctl_reader.igmp_max_membership) { errno = ENOBUFS; return -1; } @@ -2917,7 +2917,7 @@ int sockinfo_udp::mc_change_membership_start_helper_ip6(const mc_pending_pram *p case MCAST_JOIN_SOURCE_GROUP: { if (group_exists) { if (m_mc_memberships_map[mc_grp].size() >= - (size_t)safe_mce_sys().sysctl_reader.get_mld_max_source_membership()) { + (size_t)safe_mce_sys().sysctl_reader.mld_max_source_membership) { errno = ENOBUFS; return -1; } @@ -2936,8 +2936,8 @@ int sockinfo_udp::mc_change_membership_start_helper_ip6(const mc_pending_pram *p open MCAST_BLOCK_SOURCE and MCAST_UNBLOCK_SOURCE requires a fix in drop packet logic - see bug #3202713 above size_t max_cap = is_ipv6 ? - (size_t)safe_mce_sys().sysctl_reader.get_mld_max_source_membership() : - (size_t)safe_mce_sys().sysctl_reader.get_igmp_max_source_membership(); if (group_exists + (size_t)safe_mce_sys().sysctl_reader.mld_max_source_membership : + (size_t)safe_mce_sys().sysctl_reader.igmp_max_source_membership; if (group_exists && !src_exists) { if (m_mc_memberships_map[mc_grp].size() >= max_cap) { errno = ENOBUFS; diff --git a/src/core/util/sys_vars.cpp b/src/core/util/sys_vars.cpp index a3881a8f9..d62d7378a 100644 --- a/src/core/util/sys_vars.cpp +++ b/src/core/util/sys_vars.cpp @@ -902,6 +902,33 @@ void mce_sys_var::get_env_params() skip_poll_in_rx = MCE_DEFAULT_SKIP_POLL_IN_RX; multilock = MCE_DEFAULT_MULTILOCK; + // Sysctl configurations + sysctl_reader.tcp_max_syn_backlog = MCE_DEFAULT_TCP_MAX_SYN_BACKLOG; + sysctl_reader.listen_maxconn = MCE_DEFAULT_LISTEN_MAXCONN; //SOMAXCONN CHECK This in socket.h + sysctl_reader.tcp_wmem.min_value = MCE_DEFAULT_TCP_WMEM_MIN; + sysctl_reader.tcp_wmem.default_value = MCE_DEFAULT_TCP_WMEM_DEFAULT; + sysctl_reader.tcp_wmem.max_value = MCE_DEFAULT_TCP_WMEM_MAX; + sysctl_reader.tcp_rmem.min_value = MCE_DEFAULT_TCP_RMEM_MIN; + sysctl_reader.tcp_rmem.default_value = MCE_DEFAULT_TCP_RMEM_DEFAULT; + sysctl_reader.tcp_rmem.max_value = MCE_DEFAULT_TCP_RMEM_MAX; + sysctl_reader.tcp_keepalive_infos.idle_secs = MCE_DEFAULT_TCP_KEEPALIVE_TIME; + sysctl_reader.tcp_keepalive_infos.interval_secs = MCE_DEFAULT_TCP_KEEPALIVE_INTERVAL; + sysctl_reader.tcp_keepalive_infos.num_probes = MCE_DEFAULT_TCP_KEEPALIVE_PROBES; + sysctl_reader.tcp_window_scaling = MCE_DEFAULT_TCP_WINDOW_SCALING; + sysctl_reader.net_core_rmem_max = MCE_DEFAULT_NET_CORE_RMEM_MAX; + sysctl_reader.net_core_wmem_max = MCE_DEFAULT_NET_CORE_WMEM_MAX; + sysctl_reader.net_ipv4_tcp_timestamps = MCE_DEFAULT_NET_IPV4_TCP_TIMESTAMPS; + + // behavior params + // sysctl_reader.net_ipv4_ttl = MCE_DEFAULT_NET_IPV4_TTL; + // sysctl_reader.igmp_max_membership = MCE_DEFAULT_IGMP_MAX_MEMBERSHIP; + // sysctl_reader.igmp_max_source_membership = MCE_DEFAULT_IGMP_MAX_SOURCE_MEMBERSHIP; + // sysctl_reader.mld_max_source_membership = MCE_DEFAULT_MLD_MAX_SOURCE_MEMBERSHIP; + // sysctl_reader.net_ipv6_hop_limit = MCE_DEFAULT_NET_IPV6_HOP_LIMIT; + // sysctl_reader.ipv6_bindv6only = MCE_DEFAULT_IPV6_BINDV6ONLY; + // sysctl_reader.ipv6_conf_all_optimistic_dad = MCE_DEFAULT_IPV6_CONF_ALL_OPTIMISTIC_DAD; + // sysctl_reader.ipv6_conf_all_use_optimistic = MCE_DEFAULT_IPV6_CONF_ALL_USE_OPTIMISTIC; + read_hv(); /* Configure enable_socketxtreme as first because @@ -1823,6 +1850,93 @@ void mce_sys_var::get_env_params() } multilock = (multilock_t)temp; } + + /* Sysctl params*/ + if ((env_ptr = getenv(SYS_VAR_TCP_MAX_SYN_BACKLOG))) { + sysctl_reader.tcp_max_syn_backlog = atoi(env_ptr); + } + + if ((env_ptr = getenv(SYS_VAR_LISTEN_MAXCONN))) { + sysctl_reader.listen_maxconn = atoi(env_ptr); + } + + if ((env_ptr = getenv(SYS_VAR_TCP_WMEM_MIN))) { + sysctl_reader.tcp_wmem.min_value = atoi(env_ptr); + } + if ((env_ptr = getenv(SYS_VAR_TCP_WMEM_DEFAULT))) { + sysctl_reader.tcp_wmem.default_value = atoi(env_ptr); + } + if ((env_ptr = getenv(SYS_VAR_TCP_WMEM_MAX))) { + sysctl_reader.tcp_wmem.max_value = atoi(env_ptr); + } + + if ((env_ptr = getenv(SYS_VAR_TCP_RMEM_MIN))) { + sysctl_reader.tcp_rmem.min_value = atoi(env_ptr); + } + if ((env_ptr = getenv(SYS_VAR_TCP_RMEM_DEFAULT))) { + sysctl_reader.tcp_rmem.default_value = atoi(env_ptr); + } + if ((env_ptr = getenv(SYS_VAR_TCP_RMEM_MAX))) { + sysctl_reader.tcp_rmem.max_value = atoi(env_ptr); + } + + if ((env_ptr = getenv(SYS_VAR_TCP_KEEPALIVE_IDLE))) { + sysctl_reader.tcp_keepalive_infos.idle_secs = atoi(env_ptr); + } + if ((env_ptr = getenv(SYS_VAR_TCP_KEEPALIVE_INTERVAL))) { + sysctl_reader.tcp_keepalive_infos.interval_secs = atoi(env_ptr); + } + if ((env_ptr = getenv(SYS_VAR_TCP_KEEPALIVE_PROBES))) { + sysctl_reader.tcp_keepalive_infos.num_probes = atoi(env_ptr); + } + + if ((env_ptr = getenv(SYS_VAR_TCP_WINDOW_SCALING))) { + sysctl_reader.tcp_window_scaling = atoi(env_ptr); + } + + if ((env_ptr = getenv(SYS_VAR_NET_CORE_RMEM_MAX))) { + sysctl_reader.net_core_rmem_max = atoi(env_ptr); + } + + if ((env_ptr = getenv(SYS_VAR_NET_CORE_WMEM_MAX))) { + sysctl_reader.net_core_wmem_max = atoi(env_ptr); + } + + if ((env_ptr = getenv(SYS_VAR_NET_IPV4_TCP_TIMESTAMPS))) { + sysctl_reader.net_ipv4_tcp_timestamps = atoi(env_ptr); + } + + // if ((env_ptr = getenv(SYS_VAR_NET_IPV4_TTL))) { + // sysctl_reader.net_ipv4_ttl = atoi(env_ptr); + // } + + // if ((env_ptr = getenv(SYS_VAR_IGMP_MAX_MEMBERSHIP))) { + // sysctl_reader.igmp_max_membership = atoi(env_ptr); + // } + + // if ((env_ptr = getenv(SYS_VAR_IGMP_MAX_SOURCE_MEMBERSHIP))) { + // sysctl_reader.igmp_max_source_membership = atoi(env_ptr); + // } + + // if ((env_ptr = getenv(SYS_VAR_MLD_MAX_SOURCE_MEMBERSHIP))) { + // sysctl_reader.mld_max_source_membership = atoi(env_ptr); + // } + + // if ((env_ptr = getenv(SYS_VAR_NET_IPV6_HOP_LIMIT))) { + // sysctl_reader.net_ipv6_hop_limit = atoi(env_ptr); + // } + + // if ((env_ptr = getenv(SYS_VAR_IPV6_BINDV6ONLY))) { + // sysctl_reader.ipv6_bindv6only = atoi(env_ptr); + // } + + // if ((env_ptr = getenv(SYS_VAR_IPV6_CONF_ALL_OPTIMISTIC_DAD))) { + // sysctl_reader.ipv6_conf_all_optimistic_dad = atoi(env_ptr); + // } + + // if ((env_ptr = getenv(SYS_VAR_IPV6_CONF_ALL_USE_OPTIMISTIC))) { + // sysctl_reader.ipv6_conf_all_use_optimistic = atoi(env_ptr); + // } } void set_env_params() diff --git a/src/core/util/sys_vars.h b/src/core/util/sys_vars.h index 036188c16..717c72897 100644 --- a/src/core/util/sys_vars.h +++ b/src/core/util/sys_vars.h @@ -693,6 +693,37 @@ extern mce_sys_var &safe_mce_sys(); #define SYS_VAR_SKIP_POLL_IN_RX "XLIO_SKIP_POLL_IN_RX" #define SYS_VAR_MULTILOCK "XLIO_MULTILOCK" +/* + * This block consists sysctl specific configuration + */ +#define SYS_VAR_TCP_MAX_SYN_BACKLOG "XLIO_TCP_MAX_SYN_BACKLOG" +#define SYS_VAR_LISTEN_MAXCONN "XLIO_LISTEN_MAXCONN" + +#define SYS_VAR_TCP_WMEM_MIN "XLIO_TCP_WMEM_MIN" +#define SYS_VAR_TCP_WMEM_DEFAULT "XLIO_TCP_WMEM_DEFAULT" +#define SYS_VAR_TCP_WMEM_MAX "XLIO_TCP_WMEM_MAX" + +#define SYS_VAR_TCP_RMEM_MIN "XLIO_TCP_RMEM_MIN" +#define SYS_VAR_TCP_RMEM_DEFAULT "XLIO_TCP_RMEM_DEFAULT" +#define SYS_VAR_TCP_RMEM_MAX "XLIO_TCP_RMEM_MAX" + +#define SYS_VAR_TCP_KEEPALIVE_IDLE "XLIO_TCP_KEEPALIVE_IDLE" +#define SYS_VAR_TCP_KEEPALIVE_INTERVAL "XLIO_TCP_KEEPALIVE_INTERVAL" +#define SYS_VAR_TCP_KEEPALIVE_PROBES "XLIO_TCP_KEEPALIVE_PROBES" + +#define SYS_VAR_TCP_WINDOW_SCALING "XLIO_TCP_WINDOW_SCALING" +#define SYS_VAR_NET_CORE_RMEM_MAX "XLIO_NET_CORE_RMEM_MAX" +#define SYS_VAR_NET_CORE_WMEM_MAX "XLIO_NET_CORE_WMEM_MAX" +#define SYS_VAR_NET_IPV4_TCP_TIMESTAMPS "XLIO_NET_IPV4_TCP_TIMESTAMPS" +#define SYS_VAR_NET_IPV4_TTL "XLIO_NET_IPV4_TTL" +#define SYS_VAR_IGMP_MAX_MEMBERSHIP "XLIO_IGMP_MAX_MEMBERSHIP" +#define SYS_VAR_IGMP_MAX_SOURCE_MEMBERSHIP "XLIO_IGMP_MAX_SOURCE_MEMBERSHIP" +#define SYS_VAR_MLD_MAX_SOURCE_MEMBERSHIP "XLIO_MLD_MAX_SOURCE_MEMBERSHIP" +#define SYS_VAR_NET_IPV6_HOP_LIMIT "XLIO_NET_IPV6_HOP_LIMIT" +#define SYS_VAR_IPV6_BINDV6ONLY "XLIO_IPV6_BINDV6ONLY" +#define SYS_VAR_IPV6_CONF_ALL_OPTIMISTIC_DAD "XLIO_IPV6_CONF_ALL_OPTIMISTIC_DAD" +#define SYS_VAR_IPV6_CONF_ALL_USE_OPTIMISTIC "XLIO_IPV6_CONF_ALL_USE_OPTIMISTIC" + /* * This block consists of default values for library specific * configuration variables @@ -904,6 +935,36 @@ extern mce_sys_var &safe_mce_sys(); #define STRQ_MIN_STRIDE_SIZE_BYTES 64 #define STRQ_MAX_STRIDE_SIZE_BYTES 8192 +/* Sysctl configurations*/ +#define MCE_DEFAULT_TCP_MAX_SYN_BACKLOG 50000 +#define MCE_DEFAULT_LISTEN_MAXCONN 4096 + +#define MCE_DEFAULT_TCP_WMEM_MIN 4096 +#define MCE_DEFAULT_TCP_WMEM_DEFAULT 16384 +#define MCE_DEFAULT_TCP_WMEM_MAX 4194304 + +#define MCE_DEFAULT_TCP_RMEM_MIN 4096 +#define MCE_DEFAULT_TCP_RMEM_DEFAULT 16777216 +#define MCE_DEFAULT_TCP_RMEM_MAX 16777216 + +#define MCE_DEFAULT_TCP_KEEPALIVE_TIME 7200 +#define MCE_DEFAULT_TCP_KEEPALIVE_INTERVAL 75 +#define MCE_DEFAULT_TCP_KEEPALIVE_PROBES 9 + +#define MCE_DEFAULT_TCP_WINDOW_SCALING 1 +#define MCE_DEFAULT_NET_CORE_RMEM_MAX 16777216 +#define MCE_DEFAULT_NET_CORE_WMEM_MAX 16777216 +#define MCE_DEFAULT_NET_IPV4_TCP_TIMESTAMPS 1 +//behavior based params +// #define MCE_DEFAULT_NET_IPV4_TTL 64 +// #define MCE_DEFAULT_IGMP_MAX_MEMBERSHIP 1024 +// #define MCE_DEFAULT_IGMP_MAX_SOURCE_MEMBERSHIP 1024 +// #define MCE_DEFAULT_MLD_MAX_SOURCE_MEMBERSHIP 64 +// #define MCE_DEFAULT_NET_IPV6_HOP_LIMIT 64 +// #define MCE_DEFAULT_IPV6_BINDV6ONLY 0 +// #define MCE_DEFAULT_IPV6_CONF_ALL_OPTIMISTIC_DAD 0 +// #define MCE_DEFAULT_IPV6_CONF_ALL_USE_OPTIMISTIC 0 + #define VIRTUALIZATION_FLAG "hypervisor" extern bool g_b_exit; diff --git a/src/core/util/sysctl_reader.h b/src/core/util/sysctl_reader.h index 97072d5e8..b26f4af77 100644 --- a/src/core/util/sysctl_reader.h +++ b/src/core/util/sysctl_reader.h @@ -53,263 +53,65 @@ struct tcp_keepalive_info { class sysctl_reader_t { private: - int sysctl_read(const char *path, int argument_num, const char *format, ...) - { - - FILE *pfile = fopen(path, "r"); - int ans; - - if (!pfile) { - return -1; - } - - va_list arg; - va_start(arg, format); - ans = vfscanf(pfile, format, arg); - va_end(arg); - - fclose(pfile); - - if (ans != argument_num) { - return -1; - } - - return 0; - } - - void init() {} - - sysctl_reader_t() - { - this->init(); - this->update_all(); - } + sysctl_reader_t() { this->update_all(); } public: + int tcp_max_syn_backlog; + int listen_maxconn; + sysctl_tcp_mem tcp_wmem; + sysctl_tcp_mem tcp_rmem; + tcp_keepalive_info tcp_keepalive_infos; + int tcp_window_scaling; + int net_core_rmem_max; + int net_core_wmem_max; + int net_ipv4_tcp_timestamps; + int net_ipv4_ttl; + int igmp_max_membership; + int igmp_max_source_membership; + int mld_max_source_membership; + int net_ipv6_hop_limit; + int ipv6_bindv6only; + int ipv6_conf_all_optimistic_dad; + int ipv6_conf_all_use_optimistic; + static sysctl_reader_t &instance() { static sysctl_reader_t the_instance; return the_instance; } - void update_all() { - get_tcp_max_syn_backlog(true); - get_listen_maxconn(true); - get_tcp_wmem(true); - get_tcp_rmem(true); - get_tcp_window_scaling(true); - get_net_core_rmem_max(true); - get_net_core_wmem_max(true); - get_net_ipv4_tcp_timestamps(true); - get_net_ipv4_ttl(true); - get_igmp_max_membership(true); - get_igmp_max_source_membership(true); - get_mld_max_source_membership(true); - get_net_ipv6_hop_limit(true); - get_ipv6_bindv6only(true); - get_ipv6_conf_all_optimistic_dad(true); - get_ipv6_conf_all_use_optimistic(true); - get_tcp_keepalive_info(true); - } - - int get_tcp_max_syn_backlog(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv4/tcp_max_syn_backlog", 1024); - } - return val; - } - - int get_listen_maxconn(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/core/somaxconn", SOMAXCONN); - } - return val; - } - - const sysctl_tcp_mem *get_tcp_wmem(bool update = false) - { - static sysctl_tcp_mem tcp_mem; - if (update) { - if (sysctl_read("/proc/sys/net/ipv4/tcp_wmem", 3, "%d %d %d", &tcp_mem.min_value, - &tcp_mem.default_value, &tcp_mem.max_value) == -1) { - tcp_mem.min_value = 4096; - tcp_mem.default_value = 16384; - tcp_mem.max_value = 4194304; - vlog_printf(VLOG_WARNING, - "sysctl_reader failed to read net.ipv4.tcp_wmem values - Using " - "defaults : %d %d %d\n", - tcp_mem.min_value, tcp_mem.default_value, tcp_mem.max_value); - } - } - return &tcp_mem; - } - - const sysctl_tcp_mem *get_tcp_rmem(bool update = false) - { - static sysctl_tcp_mem tcp_mem; - if (update) { - if (sysctl_read("/proc/sys/net/ipv4/tcp_rmem", 3, "%d %d %d", &tcp_mem.min_value, - &tcp_mem.default_value, &tcp_mem.max_value) == -1) { - // defaults were taken based on man (7) tcp - tcp_mem.min_value = 4096; - tcp_mem.default_value = 87380; - tcp_mem.max_value = 4194304; - vlog_printf(VLOG_WARNING, - "sysctl_reader failed to read net.ipv4.tcp_rmem values - Using " - "defaults : %d %d %d\n", - tcp_mem.min_value, tcp_mem.default_value, tcp_mem.max_value); - } - } - return &tcp_mem; - } - - const tcp_keepalive_info get_tcp_keepalive_info(bool update = false) - { - static tcp_keepalive_info val = {7200, 75, 9}; - auto read_file_to_positive_int = [](const char *path, int default_value) { - int ret = read_file_to_int(path, default_value); - return ret > 0 ? ret : default_value; - }; - if (update) { - val.idle_secs = - read_file_to_positive_int("/proc/sys/net/ipv4/tcp_keepalive_time", val.idle_secs); - val.interval_secs = read_file_to_positive_int("/proc/sys/net/ipv4/tcp_keepalive_intvl", - val.interval_secs); - val.num_probes = read_file_to_positive_int("/proc/sys/net/ipv4/tcp_keepalive_probes", - val.num_probes); - } - return val; - } - - int get_tcp_window_scaling(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv4/tcp_window_scaling", 0); - } - return val; - } - - int get_net_core_rmem_max(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/core/rmem_max", 229376); - } - return val; - } - - int get_net_core_wmem_max(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/core/wmem_max", 229376); - } - return val; - } - - int get_net_ipv4_tcp_timestamps(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv4/tcp_timestamps", 0); - } - return val; - } - - int get_net_ipv4_ttl(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv4/ip_default_ttl", 64); - } - return val; - } - - int get_net_ipv6_hop_limit(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv6/conf/default/hop_limit", 64); - } - return val; - } - - int get_igmp_max_membership(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv4/igmp_max_memberships", 1024); - if (0 > val) { - vlog_printf(VLOG_WARNING, "failed to read get_igmp_max_membership value\n"); - } - } - return val; - } - - int get_igmp_max_source_membership(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv4/igmp_max_msf", 1024); - if (0 > val) { - vlog_printf(VLOG_WARNING, "failed to read get_igmp_max_source_membership value\n"); - } - } - return val; - } - - int get_mld_max_source_membership(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv6/mld_max_msf", 64); - if (0 > val) { - vlog_printf(VLOG_WARNING, "failed to read get_mld_max_source_membership value\n"); - } - } - return val; - } - - int get_ipv6_bindv6only(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv6/bindv6only", 0); - if (0 > val) { - vlog_printf(VLOG_WARNING, "failed to read bindv6only value\n"); - } - } - return val; - } - - int get_ipv6_conf_all_optimistic_dad(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv6/conf/all/optimistic_dad", 0, VLOG_DEBUG); - if (0 > val) { - vlog_printf(VLOG_DEBUG, "failed to read ipv6/conf/all/optimistic_dad value\n"); - } - } - return val; - } - - int get_ipv6_conf_all_use_optimistic(bool update = false) - { - static int val; - if (update) { - val = read_file_to_int("/proc/sys/net/ipv6/conf/all/use_optimistic", 0, VLOG_DEBUG); - if (0 > val) { - vlog_printf(VLOG_DEBUG, "failed to read ipv6/conf/all/use_optimistic value\n"); - } - } - return val; + // get_tcp_max_syn_backlog(true); + // get_listen_maxconn(true); + // get_tcp_wmem(true); + // get_tcp_rmem(true); + // get_tcp_window_scaling(true); + // get_net_core_rmem_max(true); + // get_net_core_wmem_max(true); + // get_net_ipv4_tcp_timestamps(true); + + //keep behaviour params + // Assign values to variables by reading from the appropriate paths or using default values + net_ipv4_ttl = tmp_read_file_to_int("/proc/sys/net/ipv4/ip_default_ttl", 64); + igmp_max_membership = tmp_read_file_to_int("/proc/sys/net/ipv4/igmp_max_memberships", 1024); + igmp_max_source_membership = tmp_read_file_to_int("/proc/sys/net/ipv4/igmp_max_msf", 1024); + mld_max_source_membership = tmp_read_file_to_int("/proc/sys/net/ipv6/mld_max_msf", 64); + net_ipv6_hop_limit = tmp_read_file_to_int("/proc/sys/net/ipv6/conf/default/hop_limit", 64); + ipv6_bindv6only = tmp_read_file_to_int("/proc/sys/net/ipv6/bindv6only", 0); + ipv6_conf_all_optimistic_dad = + tmp_read_file_to_int("/proc/sys/net/ipv6/conf/all/optimistic_dad", 0); + ipv6_conf_all_use_optimistic = + tmp_read_file_to_int("/proc/sys/net/ipv6/conf/all/use_optimistic", 0); + } + + int tmp_read_file_to_int(const char *path, int default_value) + { + int value = -1; + std::ifstream file_stream(path); + if (!file_stream || !(file_stream >> value)) { + return default_value; + } + return value; } int get_ipv6_if_optimistic_dad(const char *if_name)