Skip to content

Commit 112abf2

Browse files
committed
Support for Multipath TCP on Linux.
With this change, Multipath TCP can be enabled on Linux with "listen ... multipath". The listen ... multipath option is supported in the http, stream and mail modules. Note that to enable Multipath TCP on Linux, you must create a socket with the IPPROTO_MPTCP protocol explicitly specified, and it is not possible to modify an existing socket. To allow a transition with minimal impact on client connections, when the multipath option is changed in the configuration, SO_REUSEPORT is set on the old socket and the new socket is opened with IPPROTO_MPTCP. Note that this creates a race window, and connection requests assigned to the old socket will be lost. In particular, this may affect binary upgrades if the WINCH signal is used to preserve the old master process. Requires Linux kernel 5.6 or later. Note, however, that some of the socket options may not be supported with Multipath TCP, or may only be supported in newer kernels. In particular, TCP_NODELAY is only supported with kernel version 5.17 or later. Based on patches by Maxime Dourov and Anthony Doeraene. (grafted from 30e0c97dc26015e9afbc84dff2e451f67835bbf5)
1 parent e12de5b commit 112abf2

14 files changed

Lines changed: 143 additions & 4 deletions

‎auto/unix‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,17 @@ ngx_feature_test="socklen_t optlen = sizeof(struct tcp_info);
536536
. auto/feature
537537

538538

539+
ngx_feature="IPPROTO_MPTCP"
540+
ngx_feature_name="NGX_HAVE_MULTIPATH"
541+
ngx_feature_run=no
542+
ngx_feature_incs="#include <sys/socket.h>
543+
#include <netinet/in.h>"
544+
ngx_feature_path=
545+
ngx_feature_libs=
546+
ngx_feature_test="socket(0, 0, IPPROTO_MPTCP)"
547+
. auto/feature
548+
549+
539550
ngx_feature="accept4()"
540551
ngx_feature_name="NGX_HAVE_ACCEPT4"
541552
ngx_feature_run=no

‎docs/xml/angie/changes.xml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77

88
<changes ver="1.10.0" date="">
99

10+
<change type="feature">
11+
<para lang="ru">
12+
поддержка приема соединений по протоколу MPTCP, включаемая с помощью опции
13+
"multipath" директивы "listen".<br/>
14+
Спасибо Максиму Дунину (freenginx), Максиму Дурову и Энтони Дорену.
15+
</para>
16+
<para lang="en">
17+
support for handling MPTCP connections with the "multipath" parameter of
18+
the "listen" directive.<br/>
19+
Thanks to Maxim Dounin (freenginx), Maxime Dourov, and Anthony Doeraene.
20+
</para>
21+
</change>
22+
1023
</changes>
1124

1225

‎src/core/ngx_connection.c‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
150150
#if (NGX_HAVE_REUSEPORT)
151151
int reuseport;
152152
#endif
153+
#if (NGX_HAVE_MULTIPATH)
154+
int protocol;
155+
#endif
153156

154157
ls = cycle->listening.elts;
155158
for (i = 0; i < cycle->listening.nelts; i++) {
@@ -338,6 +341,25 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
338341

339342
#endif
340343

344+
#if (NGX_HAVE_MULTIPATH)
345+
346+
protocol = 0;
347+
olen = sizeof(int);
348+
349+
if (getsockopt(ls[i].fd, SOL_SOCKET, SO_PROTOCOL,
350+
(void *) &protocol, &olen)
351+
== -1)
352+
{
353+
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
354+
"getsockopt(SO_PROTOCOL) %V failed, ignored",
355+
&ls[i].addr_text);
356+
357+
} else {
358+
ls[i].multipath = (protocol == IPPROTO_MPTCP) ? 1 : 0;
359+
}
360+
361+
#endif
362+
341363
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
342364

343365
ngx_memzero(&af, sizeof(struct accept_filter_arg));
@@ -406,7 +428,7 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
406428
ngx_int_t
407429
ngx_open_listening_sockets(ngx_cycle_t *cycle)
408430
{
409-
int reuseaddr;
431+
int reuseaddr, proto;
410432
ngx_uint_t i, tries, failed;
411433
ngx_err_t err;
412434
ngx_log_t *log;
@@ -436,7 +458,7 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
436458

437459
#if (NGX_HAVE_REUSEPORT)
438460

439-
if (ls[i].add_reuseport) {
461+
if (ls[i].add_reuseport || ls[i].reopen) {
440462

441463
/*
442464
* to allow transition from a socket without SO_REUSEPORT
@@ -472,6 +494,18 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
472494

473495
ls[i].add_reuseport = 0;
474496
}
497+
498+
if (ls[i].reopen) {
499+
500+
/*
501+
* to allow transition to Multipath TCP we set SO_REUSEPORT
502+
* on the old socket, and then open a new one
503+
*/
504+
505+
ls[i].fd = (ngx_socket_t) -1;
506+
ls[i].inherited = 0;
507+
ls[i].previous->remain = 0;
508+
}
475509
#endif
476510

477511
if (ls[i].fd != (ngx_socket_t) -1) {
@@ -487,7 +521,15 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
487521
continue;
488522
}
489523

490-
s = ngx_socket(ls[i].sockaddr->sa_family, ls[i].type, 0);
524+
proto = 0;
525+
526+
#if (NGX_HAVE_MULTIPATH)
527+
if (ls[i].multipath) {
528+
proto = IPPROTO_MPTCP;
529+
}
530+
#endif
531+
532+
s = ngx_socket(ls[i].sockaddr->sa_family, ls[i].type, proto);
491533

492534
if (s == (ngx_socket_t) -1) {
493535
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
@@ -517,7 +559,7 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
517559

518560
#if (NGX_HAVE_REUSEPORT)
519561

520-
if (ls[i].reuseport && !ngx_test_config) {
562+
if ((ls[i].reuseport || ls[i].reopen) && !ngx_test_config) {
521563
int reuseport;
522564

523565
reuseport = 1;

‎src/core/ngx_connection.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct ngx_listening_s {
5757
unsigned open:1;
5858
unsigned remain:1;
5959
unsigned ignore:1;
60+
unsigned reopen:1;
6061

6162
unsigned bound:1; /* already bound */
6263
unsigned inherited:1; /* inherited from previous process */
@@ -72,6 +73,7 @@ struct ngx_listening_s {
7273
#endif
7374
unsigned reuseport:1;
7475
unsigned add_reuseport:1;
76+
unsigned multipath:1;
7577
unsigned keepalive:2;
7678
unsigned quic:1;
7779

‎src/core/ngx_cycle.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,12 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
627627
}
628628
#endif
629629

630+
#if (NGX_HAVE_MULTIPATH)
631+
if (ls[i].multipath != nls[n].multipath) {
632+
nls[n].reopen = 1;
633+
}
634+
#endif
635+
630636
break;
631637
}
632638
}

‎src/http/ngx_http.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,10 @@ ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
18761876
ls->reuseport = addr->opt.reuseport;
18771877
#endif
18781878

1879+
#if (NGX_HAVE_MULTIPATH)
1880+
ls->multipath = addr->opt.multipath;
1881+
#endif
1882+
18791883
ls->wildcard = addr->opt.wildcard;
18801884

18811885
#if (NGX_HTTP_V3)

‎src/http/ngx_http_core_module.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,6 +4392,19 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
43924392
continue;
43934393
}
43944394

4395+
if (ngx_strcmp(value[n].data, "multipath") == 0) {
4396+
#if (NGX_HAVE_MULTIPATH)
4397+
lsopt.multipath = 1;
4398+
lsopt.set = 1;
4399+
lsopt.bind = 1;
4400+
#else
4401+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4402+
"multipath is not supported "
4403+
"on this platform, ignored");
4404+
#endif
4405+
continue;
4406+
}
4407+
43954408
if (ngx_strcmp(value[n].data, "ssl") == 0) {
43964409
#if (NGX_HTTP_SSL)
43974410
lsopt.ssl = 1;
@@ -4558,6 +4571,12 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
45584571
}
45594572
#endif
45604573

4574+
#if (NGX_HAVE_MULTIPATH)
4575+
if (lsopt.multipath) {
4576+
return "\"multipath\" parameter is incompatible with \"quic\"";
4577+
}
4578+
#endif
4579+
45614580
#if (NGX_HTTP_SSL)
45624581
if (lsopt.ssl) {
45634582
return "\"ssl\" parameter is incompatible with \"quic\"";

‎src/http/ngx_http_core_module.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ typedef struct {
8989
#endif
9090
unsigned deferred_accept:1;
9191
unsigned reuseport:1;
92+
unsigned multipath:1;
9293
unsigned so_keepalive:2;
9394
unsigned proxy_protocol:1;
9495

‎src/mail/ngx_mail.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ ngx_mail_optimize_servers(ngx_conf_t *cf, ngx_array_t *ports)
347347
ls->ipv6only = addr[i].opt.ipv6only;
348348
#endif
349349

350+
#if (NGX_HAVE_MULTIPATH)
351+
ls->multipath = addr[i].opt.multipath;
352+
#endif
353+
350354
mport = ngx_palloc(cf->pool, sizeof(ngx_mail_port_t));
351355
if (mport == NULL) {
352356
return NGX_CONF_ERROR;

‎src/mail/ngx_mail.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct {
4040
#if (NGX_HAVE_INET6)
4141
unsigned ipv6only:1;
4242
#endif
43+
unsigned multipath:1;
4344
unsigned so_keepalive:2;
4445
unsigned proxy_protocol:1;
4546
#if (NGX_HAVE_KEEPALIVE_TUNABLE)

0 commit comments

Comments
 (0)