Skip to content

Commit 615b800

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). Closes #8062
1 parent e23076f commit 615b800

File tree

7 files changed

+107
-6
lines changed

7 files changed

+107
-6
lines changed

NEWS

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.3.0alpha1
44

5+
- CLI:
6+
. Added pdeathsig to builtin server to terminate workers when the master
7+
process is killed. (ilutov)
8+
59
- Core:
610
. Fixed bug GH-9388 (Improve unset property and __get type incompatibility
711
error message). (ilutov)
@@ -17,9 +21,8 @@ PHP NEWS
1721
- Posix:
1822
. Added posix_sysconf. (David Carlier)
1923

20-
- CLI:
21-
. Added pdeathsig to builtin server to terminate workers when the master
22-
process is killed. (ilutov)
23-
24+
- Sockets:
25+
. Added SO_ATTACH_REUSEPORT_CBPF socket option, to give tighter control
26+
over socket binding for a cpu core. (David Carlier)
2427

2528
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ PHP 8.3 UPGRADE NOTES
6464
. POSIX_SC_NPROCESSORS_CONF
6565
. POSIX_SC_NPROCESSORS_ONLN
6666

67+
- Sockets:
68+
. SO_ATTACH_REUSEPORT_CBPF (Linux only).
69+
6770
========================================
6871
11. Changes to INI File Handling
6972
========================================

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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,7 @@ PHP_FUNCTION(socket_get_option)
17411741
return;
17421742
}
17431743
#endif
1744+
17441745
}
17451746
}
17461747

@@ -1960,6 +1961,40 @@ PHP_FUNCTION(socket_set_option)
19601961
}
19611962
#endif
19621963

1964+
#ifdef SO_ATTACH_REUSEPORT_CBPF
1965+
case SO_ATTACH_REUSEPORT_CBPF: {
1966+
convert_to_long(arg4);
1967+
1968+
if (!Z_LVAL_P(arg4)) {
1969+
ov = 1;
1970+
optlen = sizeof(ov);
1971+
opt_ptr = &ov;
1972+
optname = SO_DETACH_BPF;
1973+
} else {
1974+
uint32_t k = (uint32_t)Z_LVAL_P(arg4);
1975+
static struct sock_filter cbpf[8] = {0};
1976+
static struct sock_fprog bpfprog;
1977+
1978+
switch (k) {
1979+
case SKF_AD_CPU:
1980+
cbpf[0].code = (BPF_LD|BPF_W|BPF_ABS);
1981+
cbpf[0].k = (uint32_t)(SKF_AD_OFF + k);
1982+
cbpf[1].code = (BPF_RET|BPF_A);
1983+
bpfprog.len = 2;
1984+
break;
1985+
default:
1986+
php_error_docref(NULL, E_WARNING, "Unsupported CBPF filter");
1987+
RETURN_FALSE;
1988+
}
1989+
1990+
bpfprog.filter = cbpf;
1991+
optlen = sizeof(bpfprog);
1992+
opt_ptr = &bpfprog;
1993+
}
1994+
break;
1995+
}
1996+
#endif
1997+
19631998
default:
19641999
default_case:
19652000
convert_to_long(arg4);

ext/sockets/sockets.stub.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,27 @@
16781678
*/
16791679
const LOCAL_CREDS = UNKNOWN;
16801680
#endif
1681+
#if defined(SO_ATTACH_REUSEPORT_CBPF)
1682+
/**
1683+
* @var int
1684+
* @cvalue SO_ATTACH_REUSEPORT_CBPF
1685+
*/
1686+
const SO_ATTACH_REUSEPORT_CBPF = UNKNOWN;
1687+
#endif
1688+
#if defined(SO_DETACH_FILTER)
1689+
/**
1690+
* @var int
1691+
* @cvalue SO_DETACH_FILTER
1692+
*/
1693+
const SO_DETACH_FILTER = UNKNOWN;
1694+
#endif
1695+
#if defined(SO_DETACH_BPF)
1696+
/**
1697+
* @var int
1698+
* @cvalue SO_DETACH_BPF
1699+
*/
1700+
const SO_DETACH_BPF = UNKNOWN;
1701+
#endif
16811702

16821703
/**
16831704
* @strict-properties

ext/sockets/sockets_arginfo.h

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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, SKF_AD_CPU));
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)