socket ip priority optimize

This commit is contained in:
Nick Peng
2019-10-16 22:57:04 +08:00
parent 9213f898c3
commit 721c56cbec
3 changed files with 36 additions and 1 deletions

View File

@@ -60,6 +60,8 @@
#define DNS_TCP_CONNECT_TIMEOUT (5)
#define DNS_QUERY_TIMEOUT (500)
#define DNS_QUERY_RETRY (3)
#define SOCKET_PRIORITY (6)
#define SOCKET_IP_TOS (IPTOS_LOWDELAY | IPTOS_RELIABILITY)
#ifndef TCP_FASTOPEN_CONNECT
#define TCP_FASTOPEN_CONNECT 30
@@ -1312,6 +1314,8 @@ static int _dns_client_create_socket_udp(struct dns_server_info *server_info)
struct epoll_event event;
const int on = 1;
const int val = 255;
const int priority = SOCKET_PRIORITY;
const int ip_tos = SOCKET_IP_TOS;
fd = socket(server_info->ai_family, SOCK_DGRAM, 0);
if (fd < 0) {
@@ -1331,6 +1335,8 @@ static int _dns_client_create_socket_udp(struct dns_server_info *server_info)
server_info->status = DNS_SERVER_STATUS_CONNECTIONLESS;
setsockopt(server_info->fd, IPPROTO_IP, IP_RECVTTL, &on, sizeof(on));
setsockopt(server_info->fd, SOL_IP, IP_TTL, &val, sizeof(val));
setsockopt(server_info->fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority));
setsockopt(server_info->fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos));
if (server_info->ai_family == AF_INET6) {
/* for recving ip ttl value */
setsockopt(server_info->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, sizeof(on));
@@ -1352,6 +1358,8 @@ static int _DNS_client_create_socket_tcp(struct dns_server_info *server_info)
int fd = 0;
struct epoll_event event;
int yes = 1;
const int priority = SOCKET_PRIORITY;
const int ip_tos = SOCKET_IP_TOS;
fd = socket(server_info->ai_family, SOCK_STREAM, 0);
if (fd < 0) {
@@ -1370,6 +1378,8 @@ static int _DNS_client_create_socket_tcp(struct dns_server_info *server_info)
}
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority));
setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos));
if (connect(fd, (struct sockaddr *)&server_info->addr, server_info->ai_addrlen) != 0) {
if (errno != EINPROGRESS) {
@@ -1406,6 +1416,8 @@ static int _DNS_client_create_socket_tls(struct dns_server_info *server_info, ch
struct epoll_event event;
SSL *ssl = NULL;
int yes = 1;
const int priority = SOCKET_PRIORITY;
const int ip_tos = SOCKET_IP_TOS;
if (server_info->ssl_ctx == NULL) {
tlog(TLOG_ERROR, "create ssl ctx failed.");
@@ -1435,6 +1447,8 @@ static int _DNS_client_create_socket_tls(struct dns_server_info *server_info, ch
// ? this cause ssl crash ?
// setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority));
setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos));
if (connect(fd, (struct sockaddr *)&server_info->addr, server_info->ai_addrlen) != 0) {
if (errno != EINPROGRESS) {

View File

@@ -38,7 +38,9 @@
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h> /* See NOTES */
#include <sys/types.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#define DNS_MAX_EVENTS 256
#define DNS_SERVER_TMOUT_TTL (5 * 60)
@@ -47,6 +49,8 @@
#define DNS_PING_TIMEOUT (DNS_REQUEST_MAX_TIMEOUT)
#define DNS_TCPPING_START (300)
#define DNS_PING_SECOND_TIMEOUT (DNS_REQUEST_MAX_TIMEOUT - DNS_TCPPING_START)
#define SOCKET_IP_TOS (IPTOS_LOWDELAY | IPTOS_RELIABILITY)
#define SOCKET_PRIORITY (6)
#define RECV_ERROR_AGAIN 1
#define RECV_ERROR_OK 0
@@ -2832,6 +2836,9 @@ static int _dns_create_socket(const char *host_ip, int type)
int port;
char *host = NULL;
int optval = 1;
int yes = 1;
const int priority = SOCKET_PRIORITY;
const int ip_tos = SOCKET_IP_TOS;
if (parse_ip(host_ip, ip, &port) == 0) {
host = ip;
@@ -2859,10 +2866,13 @@ static int _dns_create_socket(const char *host_ip, int type)
tlog(TLOG_ERROR, "set socket opt failed.");
goto errout;
}
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
} else {
setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &optval, sizeof(optval));
setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval, sizeof(optval));
}
setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority));
setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos));
if (bind(fd, gai->ai_addr, gai->ai_addrlen) != 0) {
tlog(TLOG_ERROR, "bind service failed, %s\n", strerror(errno));

View File

@@ -46,6 +46,7 @@
#define ICMP_INPACKET_SIZE 1024
#define IPV4_ADDR_LEN 4
#define IPV6_ADDR_LEN 16
#define SOCKET_PRIORITY (6)
#ifndef ICMP_FILTER
#define ICMP_FILTER 1
@@ -550,6 +551,9 @@ static int _fast_ping_sendping_tcp(struct ping_host_struct *ping_host)
struct epoll_event event;
int flags;
int fd = -1;
int yes = 1;
const int priority = SOCKET_PRIORITY;
const int ip_tos = IP_TOS;
_fast_ping_close_host_sock(ping_host);
@@ -560,6 +564,9 @@ static int _fast_ping_sendping_tcp(struct ping_host_struct *ping_host)
flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority));
setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos));
ping_host->seq++;
if (connect(fd, (struct sockaddr *)&ping_host->addr, ping_host->addr_len) != 0) {
@@ -635,6 +642,7 @@ static int _fast_ping_create_icmp_sock(FAST_PING_TYPE type)
socklen_t optlen = sizeof(buffsize);
const int val = 255;
const int on = 1;
const int ip_tos = (IPTOS_LOWDELAY | IPTOS_RELIABILITY);
switch (type) {
case FAST_PING_ICMP:
@@ -669,6 +677,7 @@ static int _fast_ping_create_icmp_sock(FAST_PING_TYPE type)
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const char *)&buffsize, optlen);
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *)&buffsize, optlen);
setsockopt(fd, SOL_IP, IP_TTL, &val, sizeof(val));
setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos));
memset(&event, 0, sizeof(event));
event.events = EPOLLIN;
@@ -732,6 +741,7 @@ static int _fast_ping_create_udp_sock(FAST_PING_TYPE type)
struct epoll_event event;
const int val = 255;
const int on = 1;
const int ip_tos = (IPTOS_LOWDELAY | IPTOS_RELIABILITY);
switch (type) {
case FAST_PING_UDP:
@@ -763,6 +773,7 @@ static int _fast_ping_create_udp_sock(FAST_PING_TYPE type)
setsockopt(fd, SOL_IP, IP_TTL, &val, sizeof(val));
setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &on, sizeof(on));
setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos));
memset(&event, 0, sizeof(event));
event.events = EPOLLIN;