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

feat: support set TOS in curl #2106

Merged
merged 7 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions core/app_config/AppConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ DEFINE_FLAG_INT32(data_server_port, "", 80);
// DEFINE_FLAG_STRING(alipay_app_zone, "", "ALIPAY_APP_ZONE");
// DEFINE_FLAG_STRING(alipay_zone, "", "ALIPAY_ZONE");
// DEFINE_FLAG_STRING(alipay_zone_env_name, "", "");
DEFINE_FLAG_INT32(curl_ip_dscp, "curl ip dscp, differentiated services codepoint, from 0 to 63", -1);

DECLARE_FLAG_INT32(polling_max_stat_count);
DECLARE_FLAG_INT32(polling_max_stat_count_per_dir);
Expand Down
20 changes: 20 additions & 0 deletions core/common/http/Curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@
#include "common/http/Curl.h"

#include <cstdint>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/socket.h>

#include <map>
#include <string>

#include "curl/curl.h"

#include "Flags.h"
#include "app_config/AppConfig.h"
#include "common/DNSCache.h"
#include "common/StringTools.h"
#include "common/http/HttpResponse.h"
#include "logger/Logger.h"

DECLARE_FLAG_INT32(curl_ip_dscp);

using namespace std;

namespace logtail {
Expand Down Expand Up @@ -99,6 +107,13 @@ static size_t header_write_callback(char* buffer,
return sizes;
}

static size_t socket_write_callback(void* userdata, curl_socket_t fd, curlsocktype purpose) {
// TOS 8 bits: first 6 bits are DSCP (user customized), last 2 bits are ECN (auto set by OS)
int32_t tos = *static_cast<int32_t*>(userdata) << 2;
setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
return 0;
}

CURL* CreateCurlHandler(const string& method,
bool httpsFlag,
const string& host,
Expand Down Expand Up @@ -182,6 +197,11 @@ CURL* CreateCurlHandler(const string& method,
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_IGNORED);
static int sDSCP = INT32_FLAG(curl_ip_dscp); // the lifestyle of sDSCP pointer should be longer than local variable
if (sDSCP >= 0 && sDSCP <= 63) {
curl_easy_setopt(curl, CURLOPT_SOCKOPTDATA, &sDSCP);
curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, socket_write_callback);
}

return curl;
}
Expand Down
Loading