Userspace: better support for ALLOC_MALLOC

An alternative memory allocation strategy for userspace
This commit is contained in:
Vadim Vetrov 2024-10-26 01:12:52 +03:00
parent f344e525d2
commit 7070ddfc74
No known key found for this signature in database
GPG Key ID: E8A308689D7A73A5
3 changed files with 37 additions and 8 deletions

View File

@ -185,7 +185,7 @@ if ((fake_bitmask) & strategy)
// The Maximum Transmission Unit size for rawsocket
// Larger packets will be fragmented. Applicable for Chrome's kyber.
#define AVAILABLE_MTU 1500
#define AVAILABLE_MTU 1400
#define DEFAULT_QUEUE_NUM 537

View File

@ -85,6 +85,9 @@
#endif /* not a KERNEL_SPACE */
/* An alternative memory allocation strategy for userspace app */
// #define ALLOC_MALLOC
/**
* Use NETBUF_ALLOC and NETBUF_FREE as an abstraction of memory allocation.
* Do not use it within expressions, consider these defines as separate statements.

View File

@ -259,16 +259,27 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {
if (config.verbose)
printf("Split packet!\n");
uint8_t buff1[MNL_SOCKET_BUFFER_SIZE];
NETBUF_ALLOC(buff1, MNL_SOCKET_BUFFER_SIZE);
if (!NETBUF_CHECK(buff1)) {
lgerror("Allocation error", -ENOMEM);
return -ENOMEM;
}
NETBUF_ALLOC(buff2, MNL_SOCKET_BUFFER_SIZE);
if (!NETBUF_CHECK(buff2)) {
lgerror("Allocation error", -ENOMEM);
NETBUF_FREE(buff1);
return -ENOMEM;
}
uint32_t buff1_size = MNL_SOCKET_BUFFER_SIZE;
uint8_t buff2[MNL_SOCKET_BUFFER_SIZE];
uint32_t buff2_size = MNL_SOCKET_BUFFER_SIZE;
if ((ret = tcp_frag(pkt, pktlen, AVAILABLE_MTU-128,
buff1, &buff1_size, buff2, &buff2_size)) < 0) {
errno = -ret;
return ret;
goto free_buffs;
}
int sent = 0;
@ -276,16 +287,23 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {
if (status >= 0) sent += status;
else {
return status;
ret = status;
goto free_buffs;
}
status = send_raw_socket(buff2, buff2_size);
if (status >= 0) sent += status;
else {
return status;
ret = status;
goto free_buffs;
}
return sent;
ret = sent;
free_buffs:
NETBUF_FREE(buff1)
NETBUF_FREE(buff2)
return ret;
}
int ipvx = netproto_version(pkt, pktlen);
@ -452,7 +470,12 @@ int init_queue(int queue_num) {
uint32_t portid = mnl_socket_get_portid(nl);
struct nlmsghdr *nlh;
char buf[BUF_SIZE];
NETBUF_ALLOC(bbuf, BUF_SIZE);
if (!NETBUF_CHECK(bbuf)) {
lgerror("Allocation error", -ENOMEM);
goto die_alloc;
}
char *buf = (char *)bbuf;
/* Support for kernels versions < 3.8 */
// Obsolete and ignored in kernel version 3.8
@ -547,10 +570,13 @@ int init_queue(int queue_num) {
}
NETBUF_FREE(bbuf)
close_socket(&nl);
return 0;
die:
NETBUF_FREE(bbuf)
die_alloc:
close_socket(&nl);
return -1;
}