Skip to content

Commit 76495fa

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 e2bdf4d commit 76495fa

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,7 @@ PHP_FUNCTION(socket_get_option)
17331733
return;
17341734
}
17351735
#endif
1736+
17361737
}
17371738
}
17381739

@@ -1952,6 +1953,26 @@ PHP_FUNCTION(socket_set_option)
19521953
}
19531954
#endif
19541955

1956+
#ifdef SO_ATTACH_REUSEPORT_CBPF
1957+
case SO_ATTACH_REUSEPORT_CBPF: {
1958+
convert_to_long(arg4);
1959+
if (!Z_LVAL_P(arg4)) {
1960+
RETURN_FALSE;
1961+
}
1962+
static struct sock_filter cbpf[] = {
1963+
BPF_STMT((BPF_LD|BPF_W|BPF_ABS), (uint32_t)(SKF_AD_OFF+SKF_AD_CPU)),
1964+
BPF_STMT((BPF_RET|BPF_A), 0),
1965+
};
1966+
static struct sock_fprog bpfprog = {
1967+
.len = (sizeof(cbpf) / sizeof(cbpf[0])),
1968+
.filter = cbpf,
1969+
};
1970+
optlen = sizeof(bpfprog);
1971+
opt_ptr = &bpfprog;
1972+
break;
1973+
}
1974+
#endif
1975+
19551976
default:
19561977
default_case:
19571978
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, 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)