Skip to content

Commit 6ae87a9

Browse files
committed
socket module add SO_ATTACH_REUSEPORT_CPBF for Linux.
to be used in conjunction with SO_REUSPORT, giving a greater control over how we bind a socket instead of the round robin workflow, we do instead attach to the processor id as : - we assign the processor_id to A in the BPF filter. - then returns A. in other words, a more modern version of SO_INCOMING_CPU (ie can have a per worker notion we do not use here).
1 parent 05023a2 commit 6ae87a9

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

ext/sockets/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PHP_ARG_ENABLE([sockets],
55

66
if test "$PHP_SOCKETS" != "no"; then
77
AC_CHECK_FUNCS([hstrerror if_nametoindex if_indextoname])
8-
AC_CHECK_HEADERS([netinet/tcp.h sys/un.h sys/sockio.h])
8+
AC_CHECK_HEADERS([netinet/tcp.h sys/un.h sys/sockio.h linux/filter.h])
99
AC_DEFINE([HAVE_SOCKETS], 1, [ ])
1010

1111
dnl Check for fied ss_family in sockaddr_storage (missing in AIX until 5.3)

ext/sockets/sockets.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
# if HAVE_IF_NAMETOINDEX
5656
# include <net/if.h>
5757
# endif
58+
# if HAVE_LINUX_FILTER_H
59+
# include <linux/filter.h>
60+
# endif
5861
#endif
5962

6063
#include <stddef.h>
@@ -506,6 +509,9 @@ static PHP_MINIT_FUNCTION(sockets)
506509
REGISTER_LONG_CONSTANT("SO_REUSEADDR", SO_REUSEADDR, CONST_CS | CONST_PERSISTENT);
507510
#ifdef SO_REUSEPORT
508511
REGISTER_LONG_CONSTANT("SO_REUSEPORT", SO_REUSEPORT, CONST_CS | CONST_PERSISTENT);
512+
#endif
513+
#ifdef SO_ATTACH_REUSEPORT_CBPF
514+
REGISTER_LONG_CONSTANT("SO_ATTACH_REUSEPORT_CBPF", SO_ATTACH_REUSEPORT_CBPF, CONST_CS | CONST_PERSISTENT);
509515
#endif
510516
REGISTER_LONG_CONSTANT("SO_KEEPALIVE", SO_KEEPALIVE, CONST_CS | CONST_PERSISTENT);
511517
REGISTER_LONG_CONSTANT("SO_DONTROUTE", SO_DONTROUTE, CONST_CS | CONST_PERSISTENT);
@@ -1850,6 +1856,7 @@ PHP_FUNCTION(socket_get_option)
18501856
return;
18511857
}
18521858
#endif
1859+
18531860
}
18541861
}
18551862

@@ -2004,6 +2011,26 @@ PHP_FUNCTION(socket_set_option)
20042011
}
20052012
#endif
20062013

2014+
#ifdef SO_ATTACH_REUSEPORT_CBPF
2015+
case SO_ATTACH_REUSEPORT_CBPF: {
2016+
convert_to_long(arg4);
2017+
if (!Z_LVAL_P(arg4)) {
2018+
RETURN_FALSE;
2019+
}
2020+
static struct sock_filter cbpf[] = {
2021+
BPF_STMT((BPF_LD|BPF_W|BPF_ABS), (uint32_t)(SKF_AD_OFF+SKF_AD_CPU)),
2022+
BPF_STMT((BPF_RET|BPF_A), 0),
2023+
};
2024+
static struct sock_fprog bpfprog = {
2025+
.len = (sizeof(cbpf) / sizeof(cbpf[0])),
2026+
.filter = cbpf,
2027+
};
2028+
optlen = sizeof(bpfprog);
2029+
opt_ptr = &bpfprog;
2030+
break;
2031+
}
2032+
#endif
2033+
20072034
default:
20082035
default_case:
20092036
convert_to_long(arg4);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Test if socket_set_option() works, option:SO_ATTACH_REUSEPORT_CBPF
3+
--EXTENSIONS--
4+
sockets
5+
--SKIPIF--
6+
<?php
7+
8+
if (!defined("SO_ATTACH_REUSEPORT_CBPF")) {
9+
die('SKIP on platforms not supporting SO_ATTACH_REUSEPORT_CBPF');
10+
}
11+
?>
12+
--FILE--
13+
<?php
14+
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
15+
16+
if (!$socket) {
17+
die('Unable to create AF_INET socket [socket]');
18+
}
19+
var_dump(socket_set_option( $socket, SOL_SOCKET, SO_REUSEADDR, true));
20+
var_dump(socket_set_option( $socket, SOL_SOCKET, SO_REUSEPORT, true));
21+
var_dump(socket_set_option( $socket, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, true));
22+
var_dump(socket_bind($socket, '0.0.0.0'));
23+
socket_listen($socket);
24+
socket_close($socket);
25+
?>
26+
--EXPECT--
27+
bool(true)
28+
bool(true)
29+
bool(true)
30+
bool(true)

0 commit comments

Comments
 (0)