Skip to content

Commit 3cbd94e

Browse files
committed
NATMap: Add per-socket TCP congestion control support.
1 parent 167d29f commit 3cbd94e

6 files changed

Lines changed: 51 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Bind options:
6565
- <port>-<port>: sequential allocation within the range
6666
6767
Forward options:
68+
-C <congestion> TCP congestion control algorithm
6869
-T <timeout> port forwarding timeout in seconds
6970
-t <address> domain name or address of forward target
7071
-p <port> port number of forward target (0: use public port)

src/hev-conf.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static const char *path;
3838
static const char *baddr;
3939
static const char *taddr;
4040
static const char *tport;
41+
static const char *ttcca;
4142
static const char *iface;
4243

4344
const char *
@@ -68,6 +69,7 @@ hev_conf_help (void)
6869
" - <port>-<port>: sequential allocation within the range\n"
6970
"\n"
7071
"Forward options:\n"
72+
" -C <congestion> TCP congestion control algorithm\n"
7173
" -T <timeout> port forwarding timeout in seconds\n"
7274
" -t <address> domain name or address of forward target\n"
7375
" -p <port> port number of forward target (0: use public port)\n";
@@ -82,7 +84,7 @@ hev_conf_init (int argc, char *argv[])
8284
int opt;
8385
char c;
8486

85-
while ((opt = getopt (argc, argv, "46udk:c:s:h:e:f:b:T:t:p:i:")) != -1) {
87+
while ((opt = getopt (argc, argv, "46udk:c:s:h:e:f:b:C:T:t:p:i:")) != -1) {
8688
switch (opt) {
8789
case '4':
8890
type = AF_INET;
@@ -117,6 +119,9 @@ hev_conf_init (int argc, char *argv[])
117119
case 'b':
118120
sscanf (optarg, "%u%c%u", &bport[0], &c, &bport[1]);
119121
break;
122+
case 'C':
123+
ttcca = optarg;
124+
break;
120125
case 'T':
121126
tmsec = strtoul (optarg, NULL, 10) * 1000;
122127
break;
@@ -279,6 +284,12 @@ hev_conf_tmsec (void)
279284
return tmsec;
280285
}
281286

287+
const char *
288+
hev_conf_ttcca (void)
289+
{
290+
return ttcca;
291+
}
292+
282293
const char *
283294
hev_conf_mport (int port)
284295
{

src/hev-conf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ const char *hev_conf_tport (void);
147147
*/
148148
int hev_conf_tmsec (void);
149149

150+
/**
151+
* hev_conf_ttcca:
152+
*
153+
* Get TCP congestion control algorithm for port forwarding.
154+
*
155+
* Returns: returns string.
156+
*/
157+
const char *hev_conf_ttcca (void);
158+
150159
/**
151160
* hev_conf_mport:
152161
* @port: port number

src/hev-misc.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#include <sys/socket.h>
1717

1818
#if defined(__linux__)
19+
#include <netinet/in.h>
1920
#include <sys/syscall.h>
21+
#include <netinet/tcp.h>
2022
#endif
2123

2224
#include "hev-misc.h"
@@ -237,6 +239,16 @@ hev_reuse_port (const char *port)
237239
return result;
238240
}
239241

242+
int
243+
hev_tcp_cca (int fd, const char *algo)
244+
{
245+
#ifdef __linux__
246+
return setsockopt (fd, IPPROTO_TCP, TCP_CONGESTION, algo, strlen (algo));
247+
#else
248+
return 0;
249+
#endif
250+
}
251+
240252
int
241253
hev_run_daemon (void)
242254
{

src/hev-misc.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ int hev_io_yielder (HevTaskYieldType type, void *data);
5757
*/
5858
int hev_reuse_port (const char *port);
5959

60+
/**
61+
* hev_tcp_cca:
62+
* @algo: congestion control algorithm
63+
*
64+
* Set the TCP congestion control algorithm.
65+
*
66+
* Returns: returns zero on successful, otherwise returns -1.
67+
*/
68+
int hev_tcp_cca (int fd, const char *algo);
69+
6070
/**
6171
* hev_run_daemon:
6272
*

src/hev-tfwd.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ client_task_entry (void *data)
3939
HevTask *task = hev_task_self ();
4040
const char *addr;
4141
const char *port;
42+
const char *tcca;
4243
int timeout;
4344
int mode;
4445
int sfd;
@@ -48,6 +49,7 @@ client_task_entry (void *data)
4849
mode = hev_conf_mode ();
4950
addr = hev_conf_taddr ();
5051
port = hev_conf_tport ();
52+
tcca = hev_conf_ttcca ();
5153
timeout = hev_conf_tmsec ();
5254

5355
if (strtoul (port, NULL, 10) == 0)
@@ -60,6 +62,11 @@ client_task_entry (void *data)
6062
return;
6163
}
6264

65+
if (tcca) {
66+
if (hev_tcp_cca (sfd, tcca) < 0)
67+
LOGV (W, "%s", "TCP congeston control failed.");
68+
}
69+
6370
hev_task_add_fd (task, sfd, POLLIN | POLLOUT);
6471
hev_task_io_splice (sfd, sfd, dfd, dfd, 8192, io_yielder, &timeout);
6572

0 commit comments

Comments
 (0)